Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8275097: Wrong span of the 'default' tag
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Nov 8, 2021
1 parent fa754b8 commit 0c2d00b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
91 changes: 89 additions & 2 deletions 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
* @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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/langtools/tools/javac/patterns/SwitchErrors.out
Expand Up @@ -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
Expand Down

1 comment on commit 0c2d00b

@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.