From 0791ff7489f9523c925a9d04d15fd2c18d905e8b Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 8 Apr 2025 20:18:31 -0500 Subject: [PATCH 1/2] Refactor handling of DEPRECATION_ON_IMPORT via new flag Check.importSuppression. --- .../com/sun/tools/javac/comp/Check.java | 33 ++++++++++--------- .../com/sun/tools/javac/comp/TypeEnter.java | 5 ++- .../javac/preview/PreviewAutoSuppress.java | 4 +-- 3 files changed, 22 insertions(+), 20 deletions(-) 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 dbf66c88f0a9f..a49751e91e9ff 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 @@ 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 **************************************************************************/ @@ -228,6 +234,12 @@ Lint setLint(Lint newLint) { 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 @@ 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. @@ -3773,8 +3776,8 @@ void checkDeprecated(final DiagnosticPosition pos, final Symbol other, final Sym } 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)); @@ -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))); + warnPreviewAPI(pos, LintWarnings.IsPreview(s)); } } else { - deferredLintHandler.report(_l -> warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s))); + warnPreviewAPI(pos, LintWarnings.IsPreviewReflective(s)); } } if (preview.declaredUsingPreviewFeature(s)) { @@ -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)); } } } 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 a15ae9943d720..d0b85b3d1cb99 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 @@ private void doModuleImport(JCModuleImport tree) { 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 @@ Type attribImportType(JCTree tree, Env env) { 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 c7cffda1f6972..501eea58331a5 100644 --- a/test/langtools/tools/javac/preview/PreviewAutoSuppress.java +++ b/test/langtools/tools/javac/preview/PreviewAutoSuppress.java @@ -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)) From c49164395469e75b54667a554b4043f45acf3ee9 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 8 Apr 2025 20:21:36 -0500 Subject: [PATCH 2/2] Bump copyright. --- test/langtools/tools/javac/preview/PreviewAutoSuppress.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/langtools/tools/javac/preview/PreviewAutoSuppress.java b/test/langtools/tools/javac/preview/PreviewAutoSuppress.java index 501eea58331a5..058ccdf0a2a9d 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