Skip to content

Commit

Permalink
8293897: Synthetic final modifier is part of the AST for a try-with-r…
Browse files Browse the repository at this point in the history
…esource resource

Reviewed-by: sundar
  • Loading branch information
lahodaj committed Sep 21, 2022
1 parent d14e96d commit 95ec2ea
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
Expand Up @@ -1900,6 +1900,8 @@ public void report(DiagnosticPosition pos, JCDiagnostic details) {
checkAutoCloseable(resource.pos(), localEnv, resource.type);

VarSymbol var = ((JCVariableDecl) resource).sym;

var.flags_field |= Flags.FINAL;
var.setData(ElementKind.RESOURCE_VARIABLE);
} else {
attribTree(resource, tryEnv, twrResult);
Expand Down
Expand Up @@ -3662,15 +3662,14 @@ List<JCTree> resources() {
* | Expression
*/
protected JCTree resource() {
int startPos = token.pos;
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
JCModifiers mods = optFinal(Flags.FINAL);
JCModifiers mods = optFinal(0);
JCExpression t = parseType(true);
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
}
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL));
JCModifiers mods = F.Modifiers(0);
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
} else {
checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
Expand Down
84 changes: 83 additions & 1 deletion test/langtools/tools/javac/parser/JavacParserTest.java
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -71,6 +71,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeKind;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
Expand Down Expand Up @@ -1907,6 +1908,87 @@ public Void visitCase(CaseTree node, Void p) {
}.scan(cut, null);
}

@Test //JDK-8293897
void testImplicitFinalInTryWithResources() throws IOException {
String code = """
package t;
class Test {
void test1() {
try (AutoCloseable ac = null) {}
}
void test2() {
try (@Ann AutoCloseable withAnnotation = null) {}
}
void test3() {
try (final AutoCloseable withFinal = null) {}
}
void test4() {
try (final @Ann AutoCloseable withAnnotationFinal = null) {}
}
@interface Ann {}
}
""";

JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
Trees t = Trees.instance(ct);
SourcePositions sp = t.getSourcePositions();
new TreeScanner<Void, Void>() {
boolean modifiersHaveFinal;
boolean modifiersHaveSpan;

@Override
public Void visitVariable(VariableTree node, Void p) {
boolean prevModifiersHaveFinal = modifiersHaveFinal;
boolean prevModifiersHaveSpan = modifiersHaveSpan;
try {
modifiersHaveFinal = node.getName().toString().contains("Final");
modifiersHaveSpan = modifiersHaveFinal ||
node.getName().toString().contains("Annotation");
return super.visitVariable(node, p);
} finally {
modifiersHaveFinal = prevModifiersHaveFinal;
modifiersHaveSpan = prevModifiersHaveSpan;
}
}
@Override
public Void visitClass(ClassTree node, Void p) {
boolean prevModifiersHaveSpan = modifiersHaveSpan;
try {
modifiersHaveSpan = node.getKind() == Kind.ANNOTATION_TYPE;
return super.visitClass(node, p);
} finally {
modifiersHaveSpan = prevModifiersHaveSpan;
}
}
@Override
public Void visitModifiers(ModifiersTree node, Void p) {
if (modifiersHaveFinal) {
if (!node.getFlags().contains(Modifier.FINAL)) {
throw new AssertionError("Expected final missing.");
}
} else {
if (node.getFlags().contains(Modifier.FINAL)) {
throw new AssertionError("Unexpected final modified.");
}
}
long start = sp.getStartPosition(cut, node);
long end = sp.getEndPosition(cut, node);
if (modifiersHaveSpan) {
if (start == (-1) || end == (-1)) {
throw new AssertionError("Incorrect modifier span: " + start + "-" + end);
}
} else {
if (start != (-1) || end != (-1)) {
throw new AssertionError("Incorrect modifier span: " + start + "-" + end);
}
}
return super.visitModifiers(node, p);
}
}.scan(cut, null);
}

void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)
Expand Down

1 comment on commit 95ec2ea

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.