Skip to content

Commit 8072ea5

Browse files
committed
8238173: jshell - switch statement with a single default not return cause syntax error
Reviewed-by: vromero
1 parent c76ce28 commit 8072ea5

5 files changed

Lines changed: 34 additions & 24 deletions

File tree

src/jdk.jshell/share/classes/jdk/jshell/Eval.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ private List<Snippet> processExpression(String userSource, Tree tree, String com
666666
if (ei == null) {
667667
// We got no type info, check for not a statement by trying
668668
DiagList dl = trialCompile(guts);
669+
if (dl.hasUnreachableError()) {
670+
guts = Wrap.methodUnreachableWrap(compileSource);
671+
dl = trialCompile(guts);
672+
}
669673
if (dl.hasNotStatement()) {
670674
guts = Wrap.methodReturnWrap(compileSource);
671675
dl = trialCompile(guts);

src/jdk.jshell/share/classes/jdk/jshell/ReplParser.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525

2626
package jdk.jshell;
2727

28+
import com.sun.tools.javac.code.Source;
2829
import com.sun.tools.javac.code.Source.Feature;
2930
import com.sun.tools.javac.code.TypeTag;
3031
import com.sun.tools.javac.parser.JavacParser;
31-
import com.sun.tools.javac.parser.ParserFactory;
3232
import com.sun.tools.javac.parser.Tokens.Comment;
3333
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
3434
import com.sun.tools.javac.parser.Tokens.Token;
35-
import com.sun.tools.javac.resources.CompilerProperties;
3635
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3736
import static com.sun.tools.javac.parser.Tokens.TokenKind.CLASS;
3837
import static com.sun.tools.javac.parser.Tokens.TokenKind.COLON;
@@ -43,6 +42,7 @@
4342
import static com.sun.tools.javac.parser.Tokens.TokenKind.LPAREN;
4443
import static com.sun.tools.javac.parser.Tokens.TokenKind.MONKEYS_AT;
4544
import static com.sun.tools.javac.parser.Tokens.TokenKind.SEMI;
45+
import static com.sun.tools.javac.parser.Tokens.TokenKind.SWITCH;
4646
import static com.sun.tools.javac.parser.Tokens.TokenKind.VOID;
4747
import com.sun.tools.javac.tree.JCTree;
4848
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
@@ -59,7 +59,6 @@
5959
import com.sun.tools.javac.util.Name;
6060
import com.sun.tools.javac.util.Position;
6161

62-
import static com.sun.tools.javac.parser.Tokens.TokenKind.IDENTIFIER;
6362
/**
6463
* This is a subclass of JavacParser which overrides one method with a modified
6564
* verson of that method designed to allow parsing of one "snippet" of Java
@@ -71,15 +70,17 @@ class ReplParser extends JavacParser {
7170

7271
// force starting in expression mode
7372
private final boolean forceExpression;
73+
private final Source source;
7474

75-
public ReplParser(ParserFactory fac,
75+
public ReplParser(ReplParserFactory fac,
7676
com.sun.tools.javac.parser.Lexer S,
7777
boolean keepDocComments,
7878
boolean keepLineMap,
7979
boolean keepEndPositions,
8080
boolean forceExpression) {
8181
super(fac, S, keepDocComments, keepLineMap, keepEndPositions);
8282
this.forceExpression = forceExpression;
83+
this.source = fac.source;
8384
}
8485

8586
/**
@@ -176,6 +177,11 @@ List<? extends JCTree> replUnit(JCModifiers pmods, Comment dc) {
176177
return List.<JCTree>of(parseStatement());
177178
}
178179
//fall-through
180+
case SWITCH:
181+
if (token.kind == SWITCH && !Feature.SWITCH_EXPRESSION.allowedInSource(source)) {
182+
return List.<JCTree>of(parseStatement());
183+
}
184+
//fall-through
179185
default:
180186
JCModifiers mods = modifiersOpt(pmods);
181187
if (token.kind == CLASS

src/jdk.jshell/share/classes/jdk/jshell/ReplParserFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package jdk.jshell;
2727

28+
import com.sun.tools.javac.code.Source;
2829
import com.sun.tools.javac.parser.JavacParser;
2930
import com.sun.tools.javac.parser.ParserFactory;
3031
import com.sun.tools.javac.parser.ScannerFactory;
@@ -49,11 +50,13 @@ class Mark {}
4950
}
5051

5152
private final ScannerFactory scannerFactory;
53+
final Source source;
5254

5355
protected ReplParserFactory(Context context, boolean forceExpression) {
5456
super(context);
5557
this.forceExpression = forceExpression;
5658
this.scannerFactory = ScannerFactory.instance(context);
59+
this.source = Source.instance(context);
5760
}
5861

5962
@Override

test/langtools/jdk/jshell/ToolLocalSimpleTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,4 @@ public void testOptionBadR() {
8585
);
8686
}
8787

88-
@Test
89-
public void testRawString() {
90-
// can't set --enable-preview for local, ignore
91-
}
92-
93-
@Test
94-
public void testSwitchExpression() {
95-
// can't set --enable-preview for local, ignore
96-
}
97-
98-
@Test
99-
public void testSwitchExpressionCompletion() {
100-
// can't set --enable-preview for local, ignore
101-
}
102-
103-
@Override
104-
public void testRecords() {
105-
// can't set --enable-preview for local, ignore
106-
}
10788
}

test/langtools/jdk/jshell/ToolSimpleTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715 8239536 8247456 8246774
26+
* @bug 8153716 8143955 8151754 8150382 8153920 8156910 8131024 8160089 8153897 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715 8239536 8247456 8246774 8238173
2727
* @summary Simple jshell tool tests
2828
* @modules jdk.compiler/com.sun.tools.javac.api
2929
* jdk.compiler/com.sun.tools.javac.main
@@ -940,4 +940,20 @@ public void testImportChange() {
940940
);
941941
}
942942
}
943+
944+
@Test
945+
public void testSwitchStatementExpressionDisambiguation() {
946+
test(false, new String[]{"--no-startup"},
947+
(a) -> assertCommand(a, "switch (0) { default -> 0; }", "$1 ==> 0"),
948+
(a) -> assertCommand(a, "int i;", "i ==> 0"),
949+
(a) -> assertCommand(a, "switch (0) { case 0 -> i = 1; }", ""),
950+
(a) -> assertCommand(a, "i", "i ==> 1"),
951+
(a) -> assertCommandOutputStartsWith(a, "switch (0) { default -> throw new IllegalStateException(); }", "| Exception java.lang.IllegalStateException")
952+
);
953+
test(false, new String[]{"--no-startup", "-C-source", "-C8"},
954+
(a) -> assertCommand(a, "int i;", "i ==> 0"),
955+
(a) -> assertCommand(a, "switch (0) { default: i = 1; }", ""),
956+
(a) -> assertCommand(a, "i", "i ==> 1")
957+
);
958+
}
943959
}

0 commit comments

Comments
 (0)