mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-21 11:34:38 +02:00
8226510: No compilation error when switch expression has no result expressions
Ensure a compile-time error is produced when there are no result expressions in an switch expression. Reviewed-by: vromero
This commit is contained in:
parent
df6fbdb2af
commit
e81d3fa8c3
15 changed files with 90 additions and 18 deletions
|
@ -1447,6 +1447,9 @@ public class Attr extends JCTree.Visitor {
|
||||||
if (tree.cases.isEmpty()) {
|
if (tree.cases.isEmpty()) {
|
||||||
log.error(tree.pos(),
|
log.error(tree.pos(),
|
||||||
Errors.SwitchExpressionEmpty);
|
Errors.SwitchExpressionEmpty);
|
||||||
|
} else if (caseTypes.isEmpty()) {
|
||||||
|
log.error(tree.pos(),
|
||||||
|
Errors.SwitchExpressionNoResultExpressions);
|
||||||
}
|
}
|
||||||
|
|
||||||
Type owntype = (tree.polyKind == PolyKind.STANDALONE) ? condType(caseTypePositions.toList(), caseTypes.toList()) : pt();
|
Type owntype = (tree.polyKind == PolyKind.STANDALONE) ? condType(caseTypePositions.toList(), caseTypes.toList()) : pt();
|
||||||
|
|
|
@ -223,6 +223,9 @@ compiler.warn.invalid.yield=\
|
||||||
compiler.err.switch.expression.empty=\
|
compiler.err.switch.expression.empty=\
|
||||||
switch expression does not have any case clauses
|
switch expression does not have any case clauses
|
||||||
|
|
||||||
|
compiler.err.switch.expression.no.result.expressions=\
|
||||||
|
switch expression does not have any result expressions
|
||||||
|
|
||||||
# 0: name
|
# 0: name
|
||||||
compiler.err.call.must.be.first.stmt.in.ctor=\
|
compiler.err.call.must.be.first.stmt.in.ctor=\
|
||||||
call to {0} must be first statement in constructor
|
call to {0} must be first statement in constructor
|
||||||
|
|
|
@ -30,7 +30,8 @@ class BreakOutsideSwitchExpression {
|
||||||
int t(int i) {
|
int t(int i) {
|
||||||
OUT: while (true) {
|
OUT: while (true) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
default: break OUT;
|
case 0: break OUT;
|
||||||
|
default: yield 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -30,7 +30,8 @@ class ContinueOutsideSwitchExpression {
|
||||||
int t(int i) {
|
int t(int i) {
|
||||||
OUT: while (true) {
|
OUT: while (true) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
default: continue OUT;
|
case 0: continue OUT;
|
||||||
|
default: yield 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,8 @@
|
||||||
class ReturnOutsideSwitchExpression {
|
class ReturnOutsideSwitchExpression {
|
||||||
int t(int i) {
|
int t(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
default: return -1;
|
case 0: return -1;
|
||||||
|
default: yield 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,8 @@
|
||||||
class RuleCompletesNormally {
|
class RuleCompletesNormally {
|
||||||
public String convert(int i) {
|
public String convert(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
default -> {}
|
case 0 -> {}
|
||||||
|
default -> "";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
class SwitchExpressionCompletesNormally {
|
class SwitchExpressionCompletesNormally {
|
||||||
public String convert(int i) {
|
public String convert(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
|
case 0: yield "";
|
||||||
default:
|
default:
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2019, 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
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// key: compiler.err.switch.expression.no.result.expressions
|
||||||
|
// key: compiler.note.preview.filename
|
||||||
|
// key: compiler.note.preview.recompile
|
||||||
|
// options: --enable-preview -source ${jdk.version}
|
||||||
|
|
||||||
|
class SwitchExpressionCompletesNormally {
|
||||||
|
public String convert(int i) {
|
||||||
|
return switch (i) {
|
||||||
|
default -> throw new AssertionError();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,15 +23,29 @@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @test
|
* @test
|
||||||
* @bug 8206986
|
* @bug 8206986 8226510
|
||||||
* @summary Verify than an empty switch expression is rejected.
|
* @summary Verify than a switch that does not yield a value is rejected.
|
||||||
* @compile/fail/ref=EmptySwitch.out --enable-preview -source ${jdk.version} -XDrawDiagnostics EmptySwitch.java
|
* @compile/fail/ref=EmptySwitch.out --enable-preview -source ${jdk.version} -XDrawDiagnostics -XDshould-stop.at=FLOW EmptySwitch.java
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class EmptySwitch {
|
public class EmptySwitch {
|
||||||
private void print(EmptySwitchEnum t) {
|
private void print(EmptySwitchEnum t) {
|
||||||
(switch (t) {
|
(switch (t) {
|
||||||
}).toString();
|
}).toString();
|
||||||
|
(switch (t) {
|
||||||
|
default -> throw new IllegalStateException();
|
||||||
|
}).toString();
|
||||||
|
(switch (t) {
|
||||||
|
default: throw new IllegalStateException();
|
||||||
|
}).toString();
|
||||||
|
(switch (0) {
|
||||||
|
case 0: yield "";
|
||||||
|
default:
|
||||||
|
}).toString();
|
||||||
|
(switch (0) {
|
||||||
|
case 0 -> { yield ""; }
|
||||||
|
default -> { }
|
||||||
|
}).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
enum EmptySwitchEnum {
|
enum EmptySwitchEnum {
|
||||||
|
|
|
@ -1,4 +1,8 @@
|
||||||
EmptySwitch.java:33:10: compiler.err.switch.expression.empty
|
EmptySwitch.java:33:10: compiler.err.switch.expression.empty
|
||||||
|
EmptySwitch.java:35:10: compiler.err.switch.expression.no.result.expressions
|
||||||
|
EmptySwitch.java:38:10: compiler.err.switch.expression.no.result.expressions
|
||||||
|
EmptySwitch.java:44:9: compiler.err.switch.expression.completes.normally
|
||||||
|
EmptySwitch.java:47:26: compiler.err.rule.completes.normally
|
||||||
- compiler.note.preview.filename: EmptySwitch.java
|
- compiler.note.preview.filename: EmptySwitch.java
|
||||||
- compiler.note.preview.recompile
|
- compiler.note.preview.recompile
|
||||||
1 error
|
5 errors
|
||||||
|
|
|
@ -41,9 +41,11 @@ public class ExpressionSwitchBreaks2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
j: print(switch (i) {
|
j: print(switch (i) {
|
||||||
|
case 0: yield 0;
|
||||||
default: break j;
|
default: break j;
|
||||||
}, 0);
|
}, 0);
|
||||||
j2: print(switch (i) {
|
j2: print(switch (i) {
|
||||||
|
case 0: yield 0;
|
||||||
default: break j2;
|
default: break j2;
|
||||||
}, 0);
|
}, 0);
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -5,8 +5,8 @@ ExpressionSwitchBreaks2.java:29:29: compiler.err.continue.outside.switch.express
|
||||||
ExpressionSwitchBreaks2.java:30:29: compiler.err.undef.label: UNKNOWN
|
ExpressionSwitchBreaks2.java:30:29: compiler.err.undef.label: UNKNOWN
|
||||||
ExpressionSwitchBreaks2.java:40:17: compiler.err.no.switch.expression
|
ExpressionSwitchBreaks2.java:40:17: compiler.err.no.switch.expression
|
||||||
ExpressionSwitchBreaks2.java:40:29: compiler.err.cant.resolve.location: kindname.variable, undef, , , (compiler.misc.location: kindname.class, ExpressionSwitchBreaks2, null)
|
ExpressionSwitchBreaks2.java:40:29: compiler.err.cant.resolve.location: kindname.variable, undef, , , (compiler.misc.location: kindname.class, ExpressionSwitchBreaks2, null)
|
||||||
ExpressionSwitchBreaks2.java:44:22: compiler.err.break.outside.switch.expression
|
ExpressionSwitchBreaks2.java:45:22: compiler.err.break.outside.switch.expression
|
||||||
ExpressionSwitchBreaks2.java:47:22: compiler.err.break.outside.switch.expression
|
ExpressionSwitchBreaks2.java:49:22: compiler.err.break.outside.switch.expression
|
||||||
- compiler.note.preview.filename: ExpressionSwitchBreaks2.java
|
- compiler.note.preview.filename: ExpressionSwitchBreaks2.java
|
||||||
- compiler.note.preview.recompile
|
- compiler.note.preview.recompile
|
||||||
9 errors
|
9 errors
|
|
@ -22,6 +22,7 @@ public class ExpressionSwitchFlow {
|
||||||
private String test3(int i) {
|
private String test3(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
case 0 -> {}
|
case 0 -> {}
|
||||||
|
case 1 -> "";
|
||||||
default -> throw new IllegalStateException();
|
default -> throw new IllegalStateException();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -40,17 +41,20 @@ public class ExpressionSwitchFlow {
|
||||||
private String test6(int i) {
|
private String test6(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
case 0 -> throw new IllegalStateException();
|
case 0 -> throw new IllegalStateException();
|
||||||
|
case 1 -> "";
|
||||||
default -> {}
|
default -> {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private String test7(int i) {
|
private String test7(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
case 0: throw new IllegalStateException();
|
case 0: throw new IllegalStateException();
|
||||||
|
case 1: yield "";
|
||||||
default:
|
default:
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private String test8(int i) {
|
private String test8(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
|
case 1: yield "";
|
||||||
case 0: i++;
|
case 0: i++;
|
||||||
default: {
|
default: {
|
||||||
}
|
}
|
||||||
|
@ -58,6 +62,7 @@ public class ExpressionSwitchFlow {
|
||||||
}
|
}
|
||||||
private String test9(int i) {
|
private String test9(int i) {
|
||||||
return switch (i) {
|
return switch (i) {
|
||||||
|
case 1: yield "";
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
System.err.println();
|
System.err.println();
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
ExpressionSwitchFlow.java:11:24: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:11:24: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:18:13: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:18:13: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:24:24: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:24:24: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:31:25: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:32:25: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:37:25: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:38:25: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:43:25: compiler.err.rule.completes.normally
|
ExpressionSwitchFlow.java:45:25: compiler.err.rule.completes.normally
|
||||||
ExpressionSwitchFlow.java:50:9: compiler.err.switch.expression.completes.normally
|
ExpressionSwitchFlow.java:53:9: compiler.err.switch.expression.completes.normally
|
||||||
ExpressionSwitchFlow.java:57:9: compiler.err.switch.expression.completes.normally
|
ExpressionSwitchFlow.java:61:9: compiler.err.switch.expression.completes.normally
|
||||||
ExpressionSwitchFlow.java:64:9: compiler.err.switch.expression.completes.normally
|
ExpressionSwitchFlow.java:69:9: compiler.err.switch.expression.completes.normally
|
||||||
- compiler.note.preview.filename: ExpressionSwitchFlow.java
|
- compiler.note.preview.filename: ExpressionSwitchFlow.java
|
||||||
- compiler.note.preview.recompile
|
- compiler.note.preview.recompile
|
||||||
9 errors
|
9 errors
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
WrongBreakTest.java:36:41: compiler.err.illegal.start.of.expr
|
WrongBreakTest.java:36:41: compiler.err.illegal.start.of.expr
|
||||||
WrongBreakTest.java:35:39: compiler.err.break.outside.switch.expression
|
WrongBreakTest.java:35:39: compiler.err.break.outside.switch.expression
|
||||||
|
WrongBreakTest.java:35:17: compiler.err.switch.expression.no.result.expressions
|
||||||
WrongBreakTest.java:36:9: compiler.err.ref.ambiguous: test, kindname.method, test(int), WrongBreakTest, kindname.method, test(java.lang.Object), WrongBreakTest
|
WrongBreakTest.java:36:9: compiler.err.ref.ambiguous: test, kindname.method, test(int), WrongBreakTest, kindname.method, test(java.lang.Object), WrongBreakTest
|
||||||
WrongBreakTest.java:38:13: compiler.err.no.switch.expression
|
WrongBreakTest.java:38:13: compiler.err.no.switch.expression
|
||||||
WrongBreakTest.java:41:13: compiler.err.no.switch.expression
|
WrongBreakTest.java:41:13: compiler.err.no.switch.expression
|
||||||
- compiler.note.preview.filename: WrongBreakTest.java
|
- compiler.note.preview.filename: WrongBreakTest.java
|
||||||
- compiler.note.preview.recompile
|
- compiler.note.preview.recompile
|
||||||
5 errors
|
6 errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue