8226522: No compilation error reported when yield is used in incorrect context
Reviewed-by: mcimadamore
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Fri May 24 04:18:18 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Jul 02 09:14:51 2019 +0200
@@ -775,9 +775,9 @@
public JCExpression unannotatedType(boolean allowVar) {
JCExpression result = term(TYPE);
- Name restrictedTypeName;
-
- if (!allowVar && (restrictedTypeName = restrictedTypeName(result, true)) != null) {
+ Name restrictedTypeName = restrictedTypeName(result, !allowVar);
+
+ if (restrictedTypeName != null && (!allowVar || restrictedTypeName != names.var)) {
syntaxError(result.pos, Errors.RestrictedTypeNotAllowedHere(restrictedTypeName));
}
--- a/test/langtools/tools/javac/switchexpr/WarnWrongYieldTest.java Fri May 24 04:18:18 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/WarnWrongYieldTest.java Tue Jul 02 09:14:51 2019 +0200
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 8223305
+ * @bug 8223305 8226522
* @summary Verify correct warnings w.r.t. yield
* @compile/ref=WarnWrongYieldTest.out -source 13 -XDrawDiagnostics -XDshould-stop.at=ATTR WarnWrongYieldTest.java
*/
@@ -159,4 +159,12 @@
//OK - yield is a variable:
yield[0] = 5;
}
+
+ private void lambda() {
+ SAM s = (yield y) -> {};
+ }
+
+ interface SAM {
+ public void m(yield o);
+ }
}
--- a/test/langtools/tools/javac/switchexpr/WarnWrongYieldTest.out Fri May 24 04:18:18 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/WarnWrongYieldTest.out Tue Jul 02 09:14:51 2019 +0200
@@ -11,8 +11,11 @@
WarnWrongYieldTest.java:118:9: compiler.warn.invalid.yield
WarnWrongYieldTest.java:123:22: compiler.warn.invalid.yield
WarnWrongYieldTest.java:152:24: compiler.warn.invalid.yield
+WarnWrongYieldTest.java:164:18: compiler.warn.restricted.type.not.allowed.preview: yield, 13
+WarnWrongYieldTest.java:168:23: compiler.warn.restricted.type.not.allowed.preview: yield, 13
WarnWrongYieldTest.java:34:28: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:45:5: compiler.warn.illegal.ref.to.restricted.type: yield
+WarnWrongYieldTest.java:168:23: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:72:9: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:75:9: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:75:24: compiler.warn.illegal.ref.to.restricted.type: yield
@@ -22,4 +25,5 @@
WarnWrongYieldTest.java:84:27: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:84:43: compiler.warn.illegal.ref.to.restricted.type: yield
WarnWrongYieldTest.java:153:24: compiler.warn.illegal.ref.to.restricted.type: yield
-24 warnings
+WarnWrongYieldTest.java:164:18: compiler.warn.illegal.ref.to.restricted.type: yield
+28 warnings
\ No newline at end of file
--- a/test/langtools/tools/javac/switchexpr/WrongYieldTest.java Fri May 24 04:18:18 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/WrongYieldTest.java Tue Jul 02 09:14:51 2019 +0200
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 8223305
+ * @bug 8223305 8226522
* @summary Ensure proper errors are returned for yields.
* @compile/fail/ref=WrongYieldTest.out --enable-preview -source ${jdk.version} -XDrawDiagnostics -XDshould-stop.at=ATTR WrongYieldTest.java
*/
@@ -222,4 +222,12 @@
//OK - yield is a variable:
yield[0] = 5;
}
+
+ private void lambda() {
+ SAM s = (yield y) -> {};
+ }
+
+ interface SAM {
+ public void m(Object o);
+ }
}
--- a/test/langtools/tools/javac/switchexpr/WrongYieldTest.out Fri May 24 04:18:18 2019 -0700
+++ b/test/langtools/tools/javac/switchexpr/WrongYieldTest.out Tue Jul 02 09:14:51 2019 +0200
@@ -1,11 +1,13 @@
WrongYieldTest.java:39:11: compiler.err.restricted.type.not.allowed: yield, 13
WrongYieldTest.java:45:5: compiler.err.restricted.type.not.allowed.here: yield
+WrongYieldTest.java:123:15: compiler.err.restricted.type.not.allowed.here: yield
WrongYieldTest.java:136:9: compiler.err.invalid.yield
WrongYieldTest.java:146:9: compiler.err.invalid.yield
WrongYieldTest.java:151:9: compiler.err.invalid.yield
WrongYieldTest.java:161:9: compiler.err.invalid.yield
WrongYieldTest.java:166:22: compiler.err.invalid.yield
WrongYieldTest.java:215:24: compiler.err.invalid.yield
+WrongYieldTest.java:227:18: compiler.err.restricted.type.not.allowed.here: yield
WrongYieldTest.java:34:24: compiler.err.illegal.ref.to.restricted.type: yield
WrongYieldTest.java:95:9: compiler.err.no.switch.expression
WrongYieldTest.java:95:15: compiler.err.cant.resolve.location: kindname.variable, y1, , , (compiler.misc.location: kindname.class, t.WrongYieldTest, null)
@@ -26,4 +28,4 @@
WrongYieldTest.java:216:24: compiler.err.illegal.ref.to.restricted.type: yield
- compiler.note.preview.filename: WrongYieldTest.java
- compiler.note.preview.recompile
-26 errors
+28 errors