Skip to content

8361445: javac crashes on unresolvable constant in @SuppressWarnings #26142

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ private EnumSet<LintCategory> suppressionsFrom(Attribute.Compound suppressWarnin
EnumSet<LintCategory> result = LintCategory.newEmptySet();
Attribute.Array values = (Attribute.Array)suppressWarnings.member(names.value);
for (Attribute value : values.values) {
Optional.of((String)((Attribute.Constant)value).value)
Optional.of(value)
.filter(val -> val instanceof Attribute.Constant)
.map(val -> (String) ((Attribute.Constant) val).value)
.flatMap(LintCategory::get)
.filter(lc -> lc.annotationSuppression)
.ifPresent(result::add);
Comment on lines +533 to 538
Copy link

Choose a reason for hiding this comment

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

It might be better to do the following, which also fixes the non‑String case:

Suggested change
Optional.of(value)
.filter(val -> val instanceof Attribute.Constant)
.map(val -> (String) ((Attribute.Constant) val).value)
.flatMap(LintCategory::get)
.filter(lc -> lc.annotationSuppression)
.ifPresent(result::add);
if (value instanceof Attribute.Constant c && c.value instanceof String s) {
LintCategory.get(s)
.filter(lc -> lc.annotationSuppression)
.ifPresent(result::add);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason to believe the non-String case is not covered by the current code? In the test, there is testSuppressWarningsErroneousAttribute2 which shows the current code works for non-String constant of type int, is there a case we can add to the tests showing the current code not working?

Expand Down
55 changes: 53 additions & 2 deletions test/langtools/tools/javac/recovery/AnnotationRecovery.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 All @@ -23,7 +23,7 @@

/*
* @test
* @bug 8270139
* @bug 8270139 8361445
* @summary Verify error recovery w.r.t. annotations
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -110,4 +110,55 @@ public void testRepeatableAnnotationWrongAttribute() throws Exception {
}
}

@Test //JDK-8361445
public void testSuppressWarningsErroneousAttribute1() throws Exception {
String code = """
@SuppressWarnings(CONST)
public class Test {
public static final String CONST = "";
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics", "-XDdev")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.getOutputLines(OutputKind.DIRECT);

List<String> expected = List.of(
"Test.java:1:19: compiler.err.cant.resolve: kindname.variable, CONST, , ",
"1 error"
);

if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}

@Test //JDK-8361445
public void testSuppressWarningsErroneousAttribute2() throws Exception {
String code = """
@SuppressWarnings(0)
public class Test {
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics", "-XDdev")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.getOutputLines(OutputKind.DIRECT);

List<String> expected = List.of(
"Test.java:1:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)",
"1 error"
);

if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}

}