8354090: Refactor import warning suppression in Check.java

Reviewed-by: mcimadamore
This commit is contained in:
Archie Cobbs 2025-04-10 14:46:36 +00:00
parent c4c3edfa96
commit e50af6db09
3 changed files with 23 additions and 21 deletions

View File

@ -218,6 +218,12 @@ public class Check {
*/
private final boolean allowSealed;
/** Whether to force suppression of deprecation and preview warnings.
* This happens when attributing import statements for JDK 9+.
* @see Feature#DEPRECATION_ON_IMPORT
*/
private boolean importSuppression;
/* *************************************************************************
* Errors and Warnings
**************************************************************************/
@ -228,6 +234,12 @@ public class Check {
return prev;
}
boolean setImportSuppression(boolean newImportSuppression) {
boolean prev = importSuppression;
importSuppression = newImportSuppression;
return prev;
}
MethodSymbol setMethod(MethodSymbol newMethod) {
MethodSymbol prev = method;
method = newMethod;
@ -261,19 +273,10 @@ public class Check {
* @param msg A Warning describing the problem.
*/
public void warnPreviewAPI(DiagnosticPosition pos, LintWarning warnKey) {
if (!lint.isSuppressed(LintCategory.PREVIEW))
if (!importSuppression && !lint.isSuppressed(LintCategory.PREVIEW))
preview.reportPreviewWarning(pos, warnKey);
}
/** Log a preview warning.
* @param pos Position to be used for error reporting.
* @param msg A Warning describing the problem.
*/
public void warnDeclaredUsingPreview(DiagnosticPosition pos, Symbol sym) {
if (!lint.isSuppressed(LintCategory.PREVIEW))
preview.reportPreviewWarning(pos, LintWarnings.DeclaredUsingPreview(kindName(sym), sym));
}
/** Log a preview warning.
* @param pos Position to be used for error reporting.
* @param msg A Warning describing the problem.
@ -3780,8 +3783,8 @@ public class Check {
}
void checkDeprecated(Supplier<DiagnosticPosition> pos, final Symbol other, final Symbol s) {
if ( (s.isDeprecatedForRemoval()
|| s.isDeprecated() && !other.isDeprecated())
if (!importSuppression
&& (s.isDeprecatedForRemoval() || s.isDeprecated() && !other.isDeprecated())
&& (s.outermostClass() != other.outermostClass() || s.outermostClass() == null)
&& s.kind != Kind.PCK) {
deferredLintHandler.report(_l -> warnDeprecated(pos.get(), s));
@ -3830,10 +3833,10 @@ public class Check {
log.error(pos, Errors.IsPreview(s));
} else {
preview.markUsesPreview(pos);
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreview(s)));
warnPreviewAPI(pos, LintWarnings.IsPreview(s));
}
} else {
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s)));
warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s));
}
}
if (preview.declaredUsingPreviewFeature(s)) {
@ -3842,7 +3845,7 @@ public class Check {
//If "s" is compiled from source, then there was an error for it already;
//if "s" is from classfile, there already was an error for the classfile.
preview.markUsesPreview(pos);
deferredLintHandler.report(_l -> warnDeclaredUsingPreview(pos, s));
warnPreviewAPI(pos, LintWarnings.DeclaredUsingPreview(kindName(s), s));
}
}
}

View File

@ -527,8 +527,7 @@ public class TypeEnter implements Completer {
Type attribImportType(JCTree tree, Env<AttrContext> env) {
Assert.check(completionEnabled);
Lint prevLint = chk.setLint(allowDeprecationOnImport ?
lint : lint.suppress(LintCategory.DEPRECATION, LintCategory.REMOVAL, LintCategory.PREVIEW));
boolean prevImportSuppression = chk.setImportSuppression(!allowDeprecationOnImport);
try {
// To prevent deep recursion, suppress completion of some
// types.
@ -536,7 +535,7 @@ public class TypeEnter implements Completer {
return attr.attribType(tree, env);
} finally {
completionEnabled = true;
chk.setLint(prevLint);
chk.setImportSuppression(prevImportSuppression);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -190,8 +190,8 @@ public class PreviewAutoSuppress extends TestRunner {
.getOutputLines(Task.OutputKind.DIRECT);
expected =
List.of("Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
"Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
List.of("Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
"Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
"2 warnings");
if (!log.equals(expected))