Skip to content
Permalink
Browse files
8275097: Wrong span of the 'default' tag
Reviewed-by: vromero
  • Loading branch information
Jan Lahoda committed Nov 8, 2021
1 parent fa754b8 commit 0c2d00bff7b96cca53820aadfdaf09c840a2a33a
Showing 3 changed files with 93 additions and 7 deletions.
@@ -3031,9 +3031,9 @@ protected List<JCCase> switchBlockStatementGroup() {
}
case DEFAULT: {
nextToken();
JCCaseLabel defaultPattern = toP(F.at(pos).DefaultCaseLabel());
CaseTree.CaseKind caseKind;
JCTree body = null;
int patternPos = token.pos;
if (token.kind == ARROW) {
checkSourceLevel(Feature.SWITCH_RULE);
accept(ARROW);
@@ -3049,7 +3049,6 @@ protected List<JCCase> switchBlockStatementGroup() {
caseKind = JCCase.STATEMENT;
stats = blockStatements();
}
JCCaseLabel defaultPattern = toP(F.at(patternPos).DefaultCaseLabel());
c = F.at(pos).Case(caseKind, List.of(defaultPattern), stats, body);
if (stats.isEmpty())
storeEnd(c, S.prevToken().endPos);
@@ -23,7 +23,7 @@

/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -52,7 +52,6 @@
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTaskImpl;
@@ -83,6 +82,7 @@
import javax.tools.ToolProvider;

import com.sun.source.tree.CaseTree;
import com.sun.source.tree.DefaultCaseLabelTree;
import com.sun.source.util.TreePathScanner;
import java.util.Objects;

@@ -1820,6 +1820,93 @@ public Void visitErroneous(ErroneousTree tree, Void p) {
}.scan(cut, null);
}

@Test //JDK-8275097
void testDefaultTagPosition() throws IOException {
String code = """
package t;
class Test {
private void test1(int i) {
switch (i) {
default:
}
}
private void test2(int i) {
switch (i) {
case default:
}
}
private int test3(int i) {
return switch (i) {
default: yield 0;
}
}
private int test4(int i) {
return switch (i) {
case default: yield 0;
}
}
private void test5(int i) {
switch (i) {
default -> {}
}
}
private void test6(int i) {
switch (i) {
case default -> {}
}
}
private int test5(int i) {
return switch (i) {
default -> { yield 0; }
}
}
private int test6(int i) {
return switch (i) {
case default -> { yield 0; }
}
}
private int test7(int i) {
return switch (i) {
default -> 0;
}
}
private int test8(int i) {
return switch (i) {
case default -> 0;
}
}
}
""";

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>() {
@Override
public Void visitDefaultCaseLabel(DefaultCaseLabelTree tree, Void p) {
int start = (int) sp.getStartPosition(cut, tree);
int end = (int) sp.getEndPosition(cut, tree);
String defaultName = code.substring(start, end);
if (!"default".equals(defaultName)) {
throw new AssertionError("Incorrect span: " + defaultName);
}
return super.visitDefaultCaseLabel(tree, p);
}

@Override
public Void visitCase(CaseTree node, Void p) {
scan(node.getLabels(), p);
if (node.getCaseKind() == CaseTree.CaseKind.RULE)
scan(node.getBody(), p);
else
scan(node.getStatements(), p);
return null;
}
}.scan(cut, null);
}

void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)
@@ -5,13 +5,13 @@ SwitchErrors.java:23:18: compiler.err.prob.found.req: (compiler.misc.inconvertib
SwitchErrors.java:28:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.type.null, int)
SwitchErrors.java:29:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
SwitchErrors.java:30:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.CharSequence)
SwitchErrors.java:36:20: compiler.err.total.pattern.and.default
SwitchErrors.java:36:13: compiler.err.total.pattern.and.default
SwitchErrors.java:42:18: compiler.err.pattern.dominated
SwitchErrors.java:42:24: compiler.err.total.pattern.and.default
SwitchErrors.java:48:18: compiler.err.total.pattern.and.default
SwitchErrors.java:54:18: compiler.err.duplicate.total.pattern
SwitchErrors.java:60:20: compiler.err.duplicate.default.label
SwitchErrors.java:66:20: compiler.err.duplicate.default.label
SwitchErrors.java:60:13: compiler.err.duplicate.default.label
SwitchErrors.java:66:13: compiler.err.duplicate.default.label
SwitchErrors.java:71:27: compiler.err.duplicate.default.label
SwitchErrors.java:77:18: compiler.err.duplicate.case.label
SwitchErrors.java:82:24: compiler.err.duplicate.case.label

1 comment on commit 0c2d00b

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 0c2d00b Nov 8, 2021

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.