8238173: jshell - switch statement with a single default not return cause syntax error

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-04-29 14:54:39 +00:00
parent c76ce28754
commit 8072ea5628
5 changed files with 34 additions and 24 deletions

View File

@ -666,6 +666,10 @@ class Eval {
if (ei == null) {
// We got no type info, check for not a statement by trying
DiagList dl = trialCompile(guts);
if (dl.hasUnreachableError()) {
guts = Wrap.methodUnreachableWrap(compileSource);
dl = trialCompile(guts);
}
if (dl.hasNotStatement()) {
guts = Wrap.methodReturnWrap(compileSource);
dl = trialCompile(guts);

View File

@ -25,14 +25,13 @@
package jdk.jshell;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Source.Feature;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.parser.JavacParser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.parser.Tokens.Token;
import com.sun.tools.javac.resources.CompilerProperties;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import static com.sun.tools.javac.parser.Tokens.TokenKind.CLASS;
import static com.sun.tools.javac.parser.Tokens.TokenKind.COLON;
@ -43,6 +42,7 @@ import static com.sun.tools.javac.parser.Tokens.TokenKind.INTERFACE;
import static com.sun.tools.javac.parser.Tokens.TokenKind.LPAREN;
import static com.sun.tools.javac.parser.Tokens.TokenKind.MONKEYS_AT;
import static com.sun.tools.javac.parser.Tokens.TokenKind.SEMI;
import static com.sun.tools.javac.parser.Tokens.TokenKind.SWITCH;
import static com.sun.tools.javac.parser.Tokens.TokenKind.VOID;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
@ -59,7 +59,6 @@ import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Position;
import static com.sun.tools.javac.parser.Tokens.TokenKind.IDENTIFIER;
/**
* This is a subclass of JavacParser which overrides one method with a modified
* verson of that method designed to allow parsing of one "snippet" of Java
@ -71,8 +70,9 @@ class ReplParser extends JavacParser {
// force starting in expression mode
private final boolean forceExpression;
private final Source source;
public ReplParser(ParserFactory fac,
public ReplParser(ReplParserFactory fac,
com.sun.tools.javac.parser.Lexer S,
boolean keepDocComments,
boolean keepLineMap,
@ -80,6 +80,7 @@ class ReplParser extends JavacParser {
boolean forceExpression) {
super(fac, S, keepDocComments, keepLineMap, keepEndPositions);
this.forceExpression = forceExpression;
this.source = fac.source;
}
/**
@ -176,6 +177,11 @@ class ReplParser extends JavacParser {
return List.<JCTree>of(parseStatement());
}
//fall-through
case SWITCH:
if (token.kind == SWITCH && !Feature.SWITCH_EXPRESSION.allowedInSource(source)) {
return List.<JCTree>of(parseStatement());
}
//fall-through
default:
JCModifiers mods = modifiersOpt(pmods);
if (token.kind == CLASS

View File

@ -25,6 +25,7 @@
package jdk.jshell;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.parser.JavacParser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.ScannerFactory;
@ -49,11 +50,13 @@ class ReplParserFactory extends ParserFactory {
}
private final ScannerFactory scannerFactory;
final Source source;
protected ReplParserFactory(Context context, boolean forceExpression) {
super(context);
this.forceExpression = forceExpression;
this.scannerFactory = ScannerFactory.instance(context);
this.source = Source.instance(context);
}
@Override

View File

@ -85,23 +85,4 @@ public class ToolLocalSimpleTest extends ToolSimpleTest {
);
}
@Test
public void testRawString() {
// can't set --enable-preview for local, ignore
}
@Test
public void testSwitchExpression() {
// can't set --enable-preview for local, ignore
}
@Test
public void testSwitchExpressionCompletion() {
// can't set --enable-preview for local, ignore
}
@Override
public void testRecords() {
// can't set --enable-preview for local, ignore
}
}

View File

@ -23,7 +23,7 @@
/*
* @test
* @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
* @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
* @summary Simple jshell tool tests
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -940,4 +940,20 @@ public class ToolSimpleTest extends ReplToolTesting {
);
}
}
@Test
public void testSwitchStatementExpressionDisambiguation() {
test(false, new String[]{"--no-startup"},
(a) -> assertCommand(a, "switch (0) { default -> 0; }", "$1 ==> 0"),
(a) -> assertCommand(a, "int i;", "i ==> 0"),
(a) -> assertCommand(a, "switch (0) { case 0 -> i = 1; }", ""),
(a) -> assertCommand(a, "i", "i ==> 1"),
(a) -> assertCommandOutputStartsWith(a, "switch (0) { default -> throw new IllegalStateException(); }", "| Exception java.lang.IllegalStateException")
);
test(false, new String[]{"--no-startup", "-C-source", "-C8"},
(a) -> assertCommand(a, "int i;", "i ==> 0"),
(a) -> assertCommand(a, "switch (0) { default: i = 1; }", ""),
(a) -> assertCommand(a, "i", "i ==> 1")
);
}
}