From 189b7dcdeb8620d8d82d65cebf4e63e0a78e6f55 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Thu, 13 Nov 2025 13:46:02 -0500 Subject: [PATCH 1/2] 8368864: Confusing error message (or wrong error) when record component has @deprecated Javadoc tag --- .../sun/tools/javac/parser/JavacParser.java | 5 +- .../javac/records/RecordCompilationTests.java | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index d3539b53541b3..cf26d76c85acf 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -5427,7 +5427,10 @@ private JCExpression insertAnnotationsToMostInner( */ protected JCVariableDecl formalParameter(boolean lambdaParameter, boolean recordComponent) { JCModifiers mods = !recordComponent ? optFinal(Flags.PARAMETER) : modifiersOpt(); - if (recordComponent && mods.flags != 0) { + if (recordComponent && mods.flags != 0 && mods.flags != Flags.DEPRECATED) { + /* for record components we only allow the DEPRECATED flag, which can be set with a + * @deprecated javadoc tag + */ log.error(mods.pos, Errors.RecordCantDeclareFieldModifiers); } if (recordComponent) { diff --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index 1de0ec48f0dbd..f87ec932ad1b4 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -2169,4 +2169,58 @@ record R(@SafeVarargs String... s) { """ ); } + + @Test + void testDeprecatedJavadoc() { + assertOK( + """ + record R( + /** + * @deprecated + */ + @Deprecated + int i + ) {} + """ + ); + assertOK( + """ + record R( + @Deprecated + int i + ) {} + """ + ); + assertOKWithWarning("compiler.warn.missing.deprecated.annotation", + """ + record R( + /** + * @deprecated + */ + int i + ) {} + """ + ); + String[] previousOptions = getCompileOptions(); + try { + setCompileOptions(new String[] {"-Xlint:deprecation"}); + assertOKWithWarning("compiler.warn.has.been.deprecated", + """ + record R( + /** + * @deprecated + */ + @Deprecated + int i + ) {} + class Client { + R r; + int j = r.i(); + } + """ + ); + } finally { + setCompileOptions(previousOptions); + } + } } From b9c04acd1bac0992a0a0b2da040165b789eb1504 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Thu, 13 Nov 2025 18:44:23 -0500 Subject: [PATCH 2/2] test change --- .../javac/records/RecordCompilationTests.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index f87ec932ad1b4..7347abe4f9006 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -2205,19 +2205,19 @@ record R( try { setCompileOptions(new String[] {"-Xlint:deprecation"}); assertOKWithWarning("compiler.warn.has.been.deprecated", - """ - record R( - /** - * @deprecated - */ - @Deprecated - int i - ) {} - class Client { - R r; - int j = r.i(); - } - """ + """ + record R( + /** + * @deprecated + */ + @Deprecated + int i + ) {} + class Client { + R r; + int j = r.i(); + } + """ ); } finally { setCompileOptions(previousOptions);