8308399: Recommend --release when -source and -target are misused

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2023-11-29 12:27:54 +00:00
parent 335f5dbf62
commit 65dfcae6d6
23 changed files with 119 additions and 55 deletions

View File

@ -62,10 +62,12 @@ import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
import com.sun.tools.javac.platform.PlatformDescription;
import com.sun.tools.javac.platform.PlatformUtils;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticInfo;
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
@ -521,9 +523,9 @@ public class Arguments {
if (target.compareTo(source.requiredTarget()) < 0) {
if (targetString != null) {
if (sourceString == null) {
reportDiag(Warnings.TargetDefaultSourceConflict(targetString, source.requiredTarget()));
reportDiag(Errors.TargetDefaultSourceConflict(source.name, targetString));
} else {
reportDiag(Warnings.SourceTargetConflict(sourceString, source.requiredTarget()));
reportDiag(Errors.SourceTargetConflict(sourceString, targetString));
}
return false;
} else {
@ -569,10 +571,10 @@ public class Arguments {
if (fm instanceof BaseFileManager baseFileManager) {
if (source.compareTo(Source.JDK8) <= 0) {
if (baseFileManager.isDefaultBootClassPath())
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name));
log.warning(LintCategory.OPTIONS, Warnings.SourceNoBootclasspath(source.name, releaseNote(source, targetString)));
} else {
if (baseFileManager.isDefaultSystemModulesPath())
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name));
log.warning(LintCategory.OPTIONS, Warnings.SourceNoSystemModulesPath(source.name, releaseNote(source, targetString)));
}
}
}
@ -640,6 +642,22 @@ public class Arguments {
return !errors && (log.nerrors == 0);
}
private Fragment releaseNote(Source source, String targetString) {
if (source.compareTo(Source.JDK8) <= 0) {
if (targetString != null) {
return Fragments.SourceNoBootclasspathWithTarget(source.name, targetString);
} else {
return Fragments.SourceNoBootclasspath(source.name);
}
} else {
if (targetString != null) {
return Fragments.SourceNoSystemModulesPathWithTarget(source.name, targetString);
} else {
return Fragments.SourceNoSystemModulesPath(source.name);
}
}
}
private void validateAddExports(SourceVersion sv) {
String addExports = options.get(Option.ADD_EXPORTS);
if (addExports != null) {

View File

@ -2119,13 +2119,33 @@ compiler.warn.static.not.qualified.by.type=\
compiler.warn.static.not.qualified.by.type2=\
static {0} should not be used as a member of an anonymous class
# 0: string
# 0: string, 1: fragment
compiler.warn.source.no.bootclasspath=\
bootstrap class path not set in conjunction with -source {0}
bootstrap class path is not set in conjunction with -source {0}\n{1}
# 0: string, 1: fragment
compiler.warn.source.no.system.modules.path=\
location of system modules is not set in conjunction with -source {0}\n{1}
# 0: string
compiler.warn.source.no.system.modules.path=\
system modules path not set in conjunction with -source {0}
compiler.misc.source.no.bootclasspath=\
not setting the bootstrap class path may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} because it sets the bootstrap class path automatically
# 0: string
compiler.misc.source.no.system.modules.path=\
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} because it sets the location of system modules automatically
# 0: string, 1: string
compiler.misc.source.no.bootclasspath.with.target=\
not setting the bootstrap class path may lead to class files that cannot run on JDK 8\n\
--release {0} is recommended instead of -source {0} -target {1} because it sets the bootstrap class path automatically
# 0: string, 1: string
compiler.misc.source.no.system.modules.path.with.target=\
not setting the location of system modules may lead to class files that cannot run on JDK {0}\n\
--release {0} is recommended instead of -source {0} -target {1} because it sets the location of system modules automatically
# 0: string
compiler.warn.option.obsolete.source=\
@ -3972,13 +3992,15 @@ compiler.err.error.writing.file=\
compiler.err.sourcepath.modulesourcepath.conflict=\
cannot specify both --source-path and --module-source-path
# 0: string, 1: target
compiler.warn.source.target.conflict=\
source release {0} requires target release {1}
# 0: string, 1: string
compiler.err.source.target.conflict=\
specified target release {1} is too old for the specified source release {0}\n\
--release {1} is recommended when compiling code to run on JDK {1}
# 0: string, 1: target
compiler.warn.target.default.source.conflict=\
target release {0} conflicts with default source release {1}
# 0: string, 1: string
compiler.err.target.default.source.conflict=\
specified target release {1} is too old for the default source release {0}\n\
--release {1} is recommended when compiling code to run on JDK {1}
# 0: profile, 1: target
compiler.warn.profile.target.conflict=\

View File

@ -1,4 +1,4 @@
- compiler.warn.source.no.system.modules.path: 15
- compiler.warn.source.no.system.modules.path: 15, (compiler.misc.source.no.system.modules.path: 15)
MinContextOpTest.java:16:25: compiler.err.mod.not.allowed.here: static
MinContextOpTest.java:22:25: compiler.err.mod.not.allowed.here: static
MinContextOpTest.java:28:34: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T,K,V,E, (compiler.misc.inconvertible.types: java.util.function.Function<MinContextOpTest.A.T,MinContextOpTest.A.T>, java.util.function.Function<? super MinContextOpTest.A.T,? extends MinContextOpTest.A.T<?>>))

View File

@ -206,8 +206,8 @@ compiler.err.two.class.loaders.2
compiler.err.unmatched.quote
compiler.err.unsupported.release.version
compiler.warn.profile.target.conflict
compiler.warn.source.target.conflict
compiler.warn.target.default.source.conflict
compiler.err.source.target.conflict
compiler.err.target.default.source.conflict
compiler.err.preview.not.latest
compiler.err.preview.without.source.or.release
compiler.misc.illegal.signature # the compiler can now detect more non-denotable types before class writing

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,7 @@
*/
// key: compiler.err.static.declaration.not.allowed.in.inner.classes
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
class EnumsMustBeStatic {
class Nested {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,7 +22,6 @@
*/
// key: compiler.err.expected3
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
int Expected3;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,7 @@
*/
// key: compiler.err.icls.cant.have.static.decl
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
class InnerClassCantHaveStatic {
class Inner {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,7 @@
*/
// key: compiler.err.intf.not.allowed.here
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
class InterfaceNotAllowed {
void m() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,8 +22,7 @@
*/
// key: compiler.err.local.enum
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
class LocalEnum {
void m() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,6 +25,7 @@
// key: compiler.warn.option.obsolete.target
// key: compiler.warn.option.obsolete.suppression
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath.with.target
// options: -source 1.8 -target 1.8
class ObsoleteSourceAndTarget {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,6 +23,7 @@
// key: compiler.err.option.removed.source
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath
// options: -source 1.5
class RemovedSourceAndTarget {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -24,6 +24,7 @@
// key: compiler.err.option.removed.source
// key: compiler.err.option.removed.target
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath.with.target
// options: -source 1.5 -target 1.5
class RemovedSourceAndTarget {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,6 @@
// key: compiler.misc.feature.records
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.warn.source.no.system.modules.path
// options: -source 15
// options: --release 15
record R() {}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,7 @@
// key: compiler.misc.feature.sealed.classes
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.warn.source.no.system.modules.path
// options: -source 16
// options: --release 16
sealed class Sealed {}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
*/
// key: compiler.warn.source.no.bootclasspath
// key: compiler.misc.source.no.bootclasspath
// key: compiler.warn.option.obsolete.source
// key: compiler.warn.option.obsolete.suppression
// options: -source 8

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
*/
// key: compiler.warn.source.no.system.modules.path
// key: compiler.misc.source.no.system.modules.path
// options: -source 9
class SourceNoSystemModulesPath { }

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.warn.source.no.system.modules.path
// key: compiler.misc.source.no.system.modules.path.with.target
// options: -source 9 -target 9
class SourceNoSystemModulesPath { }

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,7 @@
// key: compiler.misc.feature.text.blocks
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.warn.source.no.system.modules.path
// options: -source 14
// options: --release 14
class TextBlockSource {
String m() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,8 +23,7 @@
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.misc.feature.unnamed.variables
// key: compiler.warn.source.no.system.modules.path
// options: -source 21
// options: --release 21
public class UnderscoreInLambdaExpression {
java.util.function.Function<String,String> f = _ -> "x";

View File

@ -78,7 +78,7 @@ public class BCPOrSystemNotSpecified extends TestRunner {
List<String> log;
List<String> expected = Arrays.asList(
"- compiler.warn.source.no.bootclasspath: 8",
"- compiler.warn.source.no.bootclasspath: 8, (compiler.misc.source.no.bootclasspath: 8)",
"- compiler.warn.option.obsolete.source: 8",
"- compiler.warn.option.obsolete.suppression",
"3 warnings"
@ -125,7 +125,7 @@ public class BCPOrSystemNotSpecified extends TestRunner {
List<String> log;
List<String> expected = Arrays.asList(
"- compiler.warn.source.no.system.modules.path: 9",
"- compiler.warn.source.no.system.modules.path: 9, (compiler.misc.source.no.system.modules.path: 9)",
"1 warning"
);

View File

@ -1,4 +1,4 @@
- compiler.warn.source.no.bootclasspath: 8
- compiler.warn.source.no.bootclasspath: 8, (compiler.misc.source.no.bootclasspath: 8)
- compiler.warn.option.obsolete.source: 8
- compiler.warn.option.obsolete.suppression
- compiler.err.warnings.and.werror

View File

@ -139,13 +139,13 @@ public class OptionSmokeTest extends TestRunner {
@Test
public void sourceAndTargetMismatch(Path base) throws Exception {
doTest(base, String.format("warning: source release %s requires target release %s", Source.DEFAULT.name, Source.DEFAULT.name),
doTest(base, String.format("error: specified target release %s is too old for the specified source release %s", Source.MIN.name, Source.DEFAULT.name),
String.format("-source %s -target %s", Source.DEFAULT.name, Source.MIN.name));
}
@Test
public void targetConflictsWithDefaultSource(Path base) throws Exception {
doTest(base, String.format("warning: target release %s conflicts with default source release %s", Source.MIN.name, Source.DEFAULT.name),
doTest(base, String.format("error: specified target release %s is too old for the default source release %s", Source.MIN.name, Source.DEFAULT.name),
String.format("-target %s", Source.MIN.name));
}

View File

@ -1,4 +1,4 @@
- compiler.warn.source.no.system.modules.path: 10
- compiler.warn.source.no.system.modules.path: 10, (compiler.misc.source.no.system.modules.path: 10)
VarInImplicitLambdaNegTest01.java:12:36: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.var.syntax.in.implicit.lambda), 10, 11
VarInImplicitLambdaNegTest01.java:15:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.implicit.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:17:52: compiler.err.restricted.type.not.allowed.here: var