diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java index a27bf235e7e..d79618adad7 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java @@ -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 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)); } } } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java index a15ae9943d7..d0b85b3d1cb 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TypeEnter.java @@ -527,8 +527,7 @@ public class TypeEnter implements Completer { Type attribImportType(JCTree tree, Env 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); } } diff --git a/test/langtools/tools/javac/preview/PreviewAutoSuppress.java b/test/langtools/tools/javac/preview/PreviewAutoSuppress.java index c7cffda1f69..058ccdf0a2a 100644 --- a/test/langtools/tools/javac/preview/PreviewAutoSuppress.java +++ b/test/langtools/tools/javac/preview/PreviewAutoSuppress.java @@ -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))