Skip to content

8354090: Refactor import warning suppression in Check.java #24532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ protected Check(Context context) {
*/
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
**************************************************************************/
Expand All @@ -228,6 +234,12 @@ Lint setLint(Lint newLint) {
return prev;
}

boolean setImportSuppression(boolean newImportSuppression) {
boolean prev = importSuppression;
importSuppression = newImportSuppression;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine. Another alternative that I'm mentioning for completeness would have been for attribImportType to set a flag on AttrContext (and then augment some of the methods in Check to accept an extra env parameter).

return prev;
}

MethodSymbol setMethod(MethodSymbol newMethod) {
MethodSymbol prev = method;
method = newMethod;
Expand Down Expand Up @@ -261,19 +273,10 @@ void warnDeprecated(DiagnosticPosition pos, Symbol sym) {
* @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.
Expand Down Expand Up @@ -3773,8 +3776,8 @@ void checkDeprecated(final DiagnosticPosition pos, final Symbol other, final Sym
}

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));
Expand Down Expand Up @@ -3823,10 +3826,10 @@ void checkPreview(DiagnosticPosition pos, Symbol other, Type site, Symbol s) {
log.error(pos, Errors.IsPreview(s));
} else {
preview.markUsesPreview(pos);
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreview(s)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't defer here because we want warnPreviewAPI() to use the current value of importSuppression, not some later value. But these warnings didn't really need to be deferred in the first place - because we can just use the current Lint instance, and in fact, that's what it was already doing. But a side effect of these changes is that the warnings checked in the PreviewAutoSuppress.java regression test get reordered.

warnPreviewAPI(pos, LintWarnings.IsPreview(s));
}
} else {
deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s)));
warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s));
}
}
if (preview.declaredUsingPreviewFeature(s)) {
Expand All @@ -3835,7 +3838,7 @@ void checkPreview(DiagnosticPosition pos, Symbol other, Type site, Symbol s) {
//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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,15 @@ private void doModuleImport(JCModuleImport tree) {

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.
completionEnabled = false;
return attr.attribType(tree, env);
} finally {
completionEnabled = true;
chk.setLint(prevLint);
chk.setImportSuppression(prevImportSuppression);
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/langtools/tools/javac/preview/PreviewAutoSuppress.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -190,8 +190,8 @@ public static class C extends Outer {}
.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))
Expand Down