8276149: jshell throws EOF error when throwing exception inside switch expression

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-11-08 13:20:44 +00:00
parent 4c14eddf41
commit fa754b8ffd
2 changed files with 13 additions and 5 deletions
src/jdk.jshell/share/classes/jdk/jshell
test/langtools/jdk/jshell

@ -557,9 +557,11 @@ class CompletenessAnalyzer {
break;
}
// Detect an error if we are at starting position and the last
// token wasn't a terminating one. Special case: within braces,
// comma can proceed semicolon, e.g. the values list in enum
if (ct.kind.isStart() && !prevTK.isOkToTerminate() && prevTK != COMMA) {
// token wasn't a terminating one. Special cases:
// -within braces, comma can procede semicolon, e.g. the values list in enum
// -arrow can be followed by a throw, e.g. in a switch/switch expression
if (ct.kind.isStart() && !prevTK.isOkToTerminate() && prevTK != COMMA &&
!(prevTK == ARROW && ct.kind == THROW)) {
return new CT(ERROR, current, "No '" + prevTK + "' before '" + ct.kind + "'");
}
if (stack.isEmpty() || ct.kind.isError()) {

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8149524 8131024 8165211 8080071 8130454 8167343 8129559 8114842 8182268 8223782 8235474 8246774
* @bug 8149524 8131024 8165211 8080071 8130454 8167343 8129559 8114842 8182268 8223782 8235474 8246774 8276149
* @summary Test SourceCodeAnalysis
* @build KullaTesting TestingInputStream
* @run testng CompletenessTest
@ -223,6 +223,12 @@ public class CompletenessTest extends KullaTesting {
"static record D(String i, String j)",
"static record D(String i) {",
"static record D(String i, String j) {",
//JDK-8276149:
"void t(int i) { int v = switch (i) { case 0 -> ",
"void t(int i) { int v = switch (i) { case 0 -> {",
"void t(int i) { int v = switch (i) { case 0 -> a = b;",
"void t(int i) { int v = switch (i) { case 0 -> System.err.println(1);",
"void t(int i) { int v = switch (i) { case 0 -> throw new IllegalStateException();",
};
static final String[] unknown = new String[] {