8380971: New -Werror syntax does not exclude categories

Reviewed-by: liach
This commit is contained in:
Archie Cobbs 2026-04-20 18:50:04 +00:00
parent ff775385b0
commit be85091514
7 changed files with 45 additions and 6 deletions

View File

@ -440,7 +440,7 @@ public class JavaCompiler {
options.isSet(G_CUSTOM, "lines");
devVerbose = options.isSet("dev");
processPcks = options.isSet("process.packages");
werrorAny = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL);
werrorNonLint = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL);
werrorLint = options.getLintCategoriesOf(WERROR, LintCategory::newEmptySet);
verboseCompilePolicy = options.isSet("verboseCompilePolicy");
@ -510,9 +510,9 @@ public class JavaCompiler {
*/
protected boolean processPcks;
/** Switch: treat any kind of warning (lint or non-lint) as an error.
/** Switch: treat non-lint warnings as errors. Set by either "-Werror" or "-Werror:all".
*/
protected boolean werrorAny;
protected boolean werrorNonLint;
/** Switch: treat lint warnings in the specified {@link LintCategory}s as errors.
*/
@ -583,7 +583,7 @@ public class JavaCompiler {
public int errorCount() {
log.reportOutstandingWarnings();
if (log.nerrors == 0 && log.nwarnings > 0 &&
(werrorAny || werrorLint.clone().removeAll(log.lintWarnings))) {
((werrorNonLint && log.nonLintWarnings > 0) || werrorLint.clone().removeAll(log.lintWarnings))) {
log.error(Errors.WarningsAndWerror);
}
return log.nerrors;
@ -593,7 +593,7 @@ public class JavaCompiler {
* Should warnings in the given lint category be treated as errors due to a {@code -Werror} flag?
*/
public boolean isWerror(LintCategory lc) {
return werrorAny || werrorLint.contains(lc);
return werrorLint.contains(lc);
}
protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) {

View File

@ -558,6 +558,10 @@ public class Log extends AbstractLog {
*/
public int nwarnings = 0;
/** The total number of non-lint warnings encountered so far.
*/
public int nonLintWarnings = 0;
/** Tracks whether any warnings have been encountered in each {@link LintCategory}.
*/
public final EnumSet<LintCategory> lintWarnings = LintCategory.newEmptySet();
@ -888,6 +892,7 @@ public class Log extends AbstractLog {
lintWarnings.clear();
nerrors = 0;
nwarnings = 0;
nonLintWarnings = 0;
nsuppressederrors = 0;
nsuppressedwarns = 0;
while (diagnosticHandler.prev != null)
@ -981,7 +986,7 @@ public class Log extends AbstractLog {
nwarnings++;
Optional.of(diag)
.map(JCDiagnostic::getLintCategory)
.ifPresent(lintWarnings::add);
.ifPresentOrElse(lintWarnings::add, () -> nonLintWarnings++);
break;
case ERROR:
nerrors++;
@ -1121,6 +1126,7 @@ public class Log extends AbstractLog {
}
prompt();
nwarnings++;
nonLintWarnings++;
warnWriter.flush();
}

View File

@ -0,0 +1,4 @@
WerrorLint2.java:16:30: compiler.warn.empty.if
- compiler.err.warnings.and.werror
1 error
1 warning

View File

@ -0,0 +1,5 @@
WerrorLint2.java:16:30: compiler.warn.empty.if
WerrorLint2.java:17:28: compiler.warn.diamond.redundant.args
- compiler.err.warnings.and.werror
1 error
2 warnings

View File

@ -0,0 +1,19 @@
/*
* @test /nodynamiccopyright/
* @bug 8380971
*
* @compile/fail/ref=WerrorLint2.fail1.out -XDrawDiagnostics -Xlint:all -Werror:all WerrorLint2.java
* @compile/ref=WerrorLint2.warn1.out -XDrawDiagnostics -Xlint:all -Werror:all,-empty WerrorLint2.java
* @compile/ref=WerrorLint2.warn1.out -XDrawDiagnostics -Xlint:all -Werror:-empty WerrorLint2.java
* @compile/fail/ref=WerrorLint2.fail1.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:all WerrorLint2.java
* @compile/fail/ref=WerrorLint2.fail2.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:all,-empty WerrorLint2.java
* @compile/ref=WerrorLint2.warn2.out -XDrawDiagnostics -Xlint:all -XDfind=diamond -Werror:-empty WerrorLint2.java
*/
class WerrorLint2 {
ThreadLocal<Void> t;
void m() {
if (hashCode() == 1) ; // warning: [empty] empty statement after if
t = new ThreadLocal<Void>(); // warning: Redundant type arguments in new expression (use diamond operator instead).
}
}

View File

@ -0,0 +1,2 @@
WerrorLint2.java:16:30: compiler.warn.empty.if
1 warning

View File

@ -0,0 +1,3 @@
WerrorLint2.java:16:30: compiler.warn.empty.if
WerrorLint2.java:17:28: compiler.warn.diamond.redundant.args
2 warnings