From 5069bf2d66625fd220d693e49a923e0b383ced3c Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 16 Jun 2025 20:52:10 -0500 Subject: [PATCH 1/4] Restore behavior when both -Xlint:options and -Xlint:-options are given. --- .../com/sun/tools/javac/util/Options.java | 48 ++++++------------- .../tools/javac/lint/LintOptions.java | 11 +++++ .../tools/javac/lint/LintOptions.out | 4 ++ 3 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 test/langtools/tools/javac/lint/LintOptions.java create mode 100644 test/langtools/tools/javac/lint/LintOptions.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index 63f5b0ca75abb..c882fb4cf8a20 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -172,55 +172,35 @@ public boolean isUnset(Option option, String value) { } /** - * Check whether the given lint category is explicitly enabled or disabled. + * Determine if a specific {@link LintCategory} is explicitly enabled via a custom + * option flag of the form {@code -Flag:key}. * *

- * If the category is neither enabled nor disabled, return the given default value. + * Note: It's possible the category was also explicitly disabled; this method does not check that. * - * @param option the plain (non-custom) option - * @param lc the {@link LintCategory} in question - * @param defaultValue presumed default value - * @return true if {@code lc} would be included - */ - public boolean isSet(Option option, LintCategory lc, boolean defaultValue) { - Option customOption = option.getCustom(); - if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias))) { - return true; - } - if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias))) { - return false; - } - if (isSet(option) || isSet(customOption, Option.LINT_CUSTOM_ALL)) { - return true; - } - if (isSet(customOption, Option.LINT_CUSTOM_NONE)) { - return false; - } - return defaultValue; - } - - /** - * Determine if a specific {@link LintCategory} was explicitly enabled via a custom option flag - * of the form {@code -Flag:all} or {@code -Flag:key}. - * - * @param option the option + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly enabled */ public boolean isExplicitlyEnabled(Option option, LintCategory lc) { - return isSet(option, lc, false); + Option customOption = option.getCustom(); + return lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias)); } /** - * Determine if a specific {@link LintCategory} was explicitly disabled via a custom option flag - * of the form {@code -Flag:none} or {@code -Flag:-key}. + * Determine if a specific {@link LintCategory} is explicitly disabled via a custom + * option flag of the form {@code -Flag:-key}. * - * @param option the option + *

+ * Note: It's possible the category was also explicitly enabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly disabled */ public boolean isExplicitlyDisabled(Option option, LintCategory lc) { - return !isSet(option, lc, true); + Option customOption = option.getCustom(); + return lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias)); } public void put(String name, String value) { diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java new file mode 100644 index 0000000000000..81b0a0dc3250b --- /dev/null +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -0,0 +1,11 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8359596 + * @summary Verify behavior when both "-Xlint:options" and "-Xlint:-options" are given + * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 LintOptions.java + * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:-options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:-options LintOptions.java + */ +class LintOptions { +} diff --git a/test/langtools/tools/javac/lint/LintOptions.out b/test/langtools/tools/javac/lint/LintOptions.out new file mode 100644 index 0000000000000..020c626ee5c20 --- /dev/null +++ b/test/langtools/tools/javac/lint/LintOptions.out @@ -0,0 +1,4 @@ +- compiler.warn.source.no.system.modules.path: 21, (compiler.misc.source.no.system.modules.path.with.target: 21, 21) +- compiler.err.warnings.and.werror +1 error +1 warning From 8dcea7004f9ad1f3c8ee5e6aeb934ed0518a2a37 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 17 Jun 2025 08:59:59 -0500 Subject: [PATCH 2/4] No need for /nodynamiccopyright/ with this test. --- .../tools/javac/lint/LintOptions.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java index 81b0a0dc3250b..b5825fce72841 100644 --- a/test/langtools/tools/javac/lint/LintOptions.java +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -1,5 +1,30 @@ /* - * @test /nodynamiccopyright/ + * Copyright (c) 2025, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +/* + * @test * @bug 8359596 * @summary Verify behavior when both "-Xlint:options" and "-Xlint:-options" are given * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 LintOptions.java From 9e788319c5109a86e1bc1ec3417b3301aa1d0153 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Wed, 18 Jun 2025 16:06:32 -0500 Subject: [PATCH 3/4] Ensure that "-Xlint:none" still works for the affected warnings. The extra checks for "-Xlint:none" are needed now because of JDK-8352612, which changed the behavior of "-Xlint:none" to no longer imply "-nowarn", which allowed the affected warnings to get away with skipping that check. --- .../com/sun/tools/javac/comp/Modules.java | 2 +- .../com/sun/tools/javac/main/Arguments.java | 4 +- .../com/sun/tools/javac/util/Options.java | 41 ++++++++++++++++++- .../tools/javac/lint/LintOptions.java | 2 + 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java index a159793fe327f..0015b73631a99 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java @@ -205,7 +205,7 @@ protected Modules(Context context) { allowAccessIntoSystem = options.isUnset(Option.RELEASE); - lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); + lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); ClassWriter classWriter = ClassWriter.instance(context); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java index 9e3a978c3bc9a..b1dfb0378bde5 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java @@ -503,7 +503,7 @@ public boolean validate() { } } else { // single-module or legacy mode - boolean lintPaths = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.PATH); + boolean lintPaths = !options.isDisabled(Option.XLINT, LintCategory.PATH); if (lintPaths) { Path outDirParent = outDir.getParent(); if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) { @@ -576,7 +576,7 @@ public boolean validate() { reportDiag(Errors.SourcepathModulesourcepathConflict); } - boolean lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); + boolean lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { if (fm instanceof BaseFileManager baseFileManager) { if (source.compareTo(Source.JDK8) <= 0) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index c882fb4cf8a20..cbc69feb421ab 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -171,12 +171,46 @@ public boolean isUnset(Option option, String value) { return !isSet(option, value); } + /** + * Determine if a specific {@link LintCategory} is enabled via a custom + * option flag of the form {@code -Flag}, {@code -Flag:all}, or {@code -Flag:key}. + * + *

+ * Note: It's possible the category was also disabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) + * @param lc the {@link LintCategory} in question + * @return true if {@code lc} has been enabled + */ + public boolean isEnabled(Option option, LintCategory lc) { + Option custom = option.getCustom(); + return isExplicitlyEnabled(option, lc) || isSet(custom) || isSet(custom, Option.LINT_CUSTOM_ALL); + } + + /** + * Determine if a specific {@link LintCategory} is disabled via a custom + * option flag of the form {@code -Flag:none} or {@code -Flag:-key}. + * + *

+ * Note: It's possible the category was also enabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) + * @param lc the {@link LintCategory} in question + * @return true if {@code lc} has been disabled + */ + public boolean isDisabled(Option option, LintCategory lc) { + return isExplicitlyDisabled(option, lc) || isSet(option.getCustom(), Option.LINT_CUSTOM_NONE); + } + /** * Determine if a specific {@link LintCategory} is explicitly enabled via a custom * option flag of the form {@code -Flag:key}. * *

- * Note: It's possible the category was also explicitly disabled; this method does not check that. + * Note: This does not check for option flags of the form {@code -Flag} or {@code -Flag:all}. + * + *

+ * Note: It's possible the category was also disabled; this method does not check that. * * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question @@ -192,7 +226,10 @@ public boolean isExplicitlyEnabled(Option option, LintCategory lc) { * option flag of the form {@code -Flag:-key}. * *

- * Note: It's possible the category was also explicitly enabled; this method does not check that. + * Note: This does not check for an option flag of the form {@code -Flag:none}. + * + *

+ * Note: It's possible the category was also enabled; this method does not check that. * * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java index b5825fce72841..10def72a31a5e 100644 --- a/test/langtools/tools/javac/lint/LintOptions.java +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -31,6 +31,8 @@ * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options LintOptions.java * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:-options LintOptions.java * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:-options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:none LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:none LintOptions.java */ class LintOptions { } From f01f19dcb31187c36b9988f7af588cb836ea99b7 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 24 Jun 2025 09:33:04 -0400 Subject: [PATCH 4/4] Cleanups per review comments. --- .../com/sun/tools/javac/code/Lint.java | 4 +- .../com/sun/tools/javac/comp/Modules.java | 2 +- .../com/sun/tools/javac/main/Arguments.java | 4 +- .../com/sun/tools/javac/util/Options.java | 37 ++++++++----------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java index ede2511f35c48..2ed365bcbfa4c 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java @@ -183,9 +183,9 @@ private void initializeRootIfNeeded() { // Look for specific overrides for (LintCategory lc : LintCategory.values()) { - if (options.isExplicitlyEnabled(Option.XLINT, lc)) { + if (options.isLintExplicitlyEnabled(lc)) { values.add(lc); - } else if (options.isExplicitlyDisabled(Option.XLINT, lc)) { + } else if (options.isLintExplicitlyDisabled(lc)) { values.remove(lc); } } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java index 0015b73631a99..47066b24de993 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java @@ -205,7 +205,7 @@ protected Modules(Context context) { allowAccessIntoSystem = options.isUnset(Option.RELEASE); - lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); + lintOptions = !options.isLintDisabled(LintCategory.OPTIONS); multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); ClassWriter classWriter = ClassWriter.instance(context); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java index b1dfb0378bde5..7aa1cc473b57c 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java @@ -503,7 +503,7 @@ public boolean validate() { } } else { // single-module or legacy mode - boolean lintPaths = !options.isDisabled(Option.XLINT, LintCategory.PATH); + boolean lintPaths = !options.isLintDisabled(LintCategory.PATH); if (lintPaths) { Path outDirParent = outDir.getParent(); if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) { @@ -576,7 +576,7 @@ public boolean validate() { reportDiag(Errors.SourcepathModulesourcepathConflict); } - boolean lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); + boolean lintOptions = !options.isLintDisabled(LintCategory.OPTIONS); if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { if (fm instanceof BaseFileManager baseFileManager) { if (source.compareTo(Source.JDK8) <= 0) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index cbc69feb421ab..32a31028b6800 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -173,71 +173,66 @@ public boolean isUnset(Option option, String value) { /** * Determine if a specific {@link LintCategory} is enabled via a custom - * option flag of the form {@code -Flag}, {@code -Flag:all}, or {@code -Flag:key}. + * option flag of the form {@code -Xlint}, {@code -Xlint:all}, or {@code -Xlint:key}. * *

* Note: It's possible the category was also disabled; this method does not check that. * - * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been enabled */ - public boolean isEnabled(Option option, LintCategory lc) { - Option custom = option.getCustom(); - return isExplicitlyEnabled(option, lc) || isSet(custom) || isSet(custom, Option.LINT_CUSTOM_ALL); + public boolean isLintEnabled(LintCategory lc) { + return isLintExplicitlyEnabled(lc) || + isSet(Option.XLINT_CUSTOM) || + isSet(Option.XLINT_CUSTOM, Option.LINT_CUSTOM_ALL); } /** * Determine if a specific {@link LintCategory} is disabled via a custom - * option flag of the form {@code -Flag:none} or {@code -Flag:-key}. + * option flag of the form {@code -Xlint:none} or {@code -Xlint:-key}. * *

* Note: It's possible the category was also enabled; this method does not check that. * - * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been disabled */ - public boolean isDisabled(Option option, LintCategory lc) { - return isExplicitlyDisabled(option, lc) || isSet(option.getCustom(), Option.LINT_CUSTOM_NONE); + public boolean isLintDisabled(LintCategory lc) { + return isLintExplicitlyDisabled(lc) || isSet(Option.XLINT_CUSTOM, Option.LINT_CUSTOM_NONE); } /** * Determine if a specific {@link LintCategory} is explicitly enabled via a custom - * option flag of the form {@code -Flag:key}. + * option flag of the form {@code -Xlint:key}. * *

- * Note: This does not check for option flags of the form {@code -Flag} or {@code -Flag:all}. + * Note: This does not check for option flags of the form {@code -Xlint} or {@code -Xlint:all}. * *

* Note: It's possible the category was also disabled; this method does not check that. * - * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly enabled */ - public boolean isExplicitlyEnabled(Option option, LintCategory lc) { - Option customOption = option.getCustom(); - return lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias)); + public boolean isLintExplicitlyEnabled(LintCategory lc) { + return lc.optionList.stream().anyMatch(alias -> isSet(Option.XLINT_CUSTOM, alias)); } /** * Determine if a specific {@link LintCategory} is explicitly disabled via a custom - * option flag of the form {@code -Flag:-key}. + * option flag of the form {@code -Xlint:-key}. * *

- * Note: This does not check for an option flag of the form {@code -Flag:none}. + * Note: This does not check for an option flag of the form {@code -Xlint:none}. * *

* Note: It's possible the category was also enabled; this method does not check that. * - * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly disabled */ - public boolean isExplicitlyDisabled(Option option, LintCategory lc) { - Option customOption = option.getCustom(); - return lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias)); + public boolean isLintExplicitlyDisabled(LintCategory lc) { + return lc.optionList.stream().anyMatch(alias -> isSet(Option.XLINT_CUSTOM, "-" + alias)); } public void put(String name, String value) {