Skip to content

Commit

Permalink
[JEP 488][Preview][Primitive Patterns] ECJ crashes compiling switch over
Browse files Browse the repository at this point in the history
void typed selector expression

+ rephrase error message
+ add tests for pre-existing cases of that error

Fixes eclipse-jdt#3369
  • Loading branch information
stephan-herrmann committed Feb 14, 2025
1 parent 252566c commit c87d1e2
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
161 = Unreachable code
162 = Cannot return from within an initializer
163 = Initializer does not complete normally
164 = Expression must return a value
164 = This expression yields no value
165 = Unreachable catch block for {0}. Only more specific exceptions are thrown and they are handled by previous catch block(s).
166 = The default case is already defined
167 = Unreachable catch block for {0}. This exception is never thrown from the try statement body
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2014 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -36,6 +36,18 @@ public static Test suite() {
public static Class testClass() {
return AssertionTest.class;
}
// ========= OPT-IN to run.javac mode: ===========
@Override
protected void setUp() throws Exception {
this.runJavacOptIn = true;
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
this.runJavacOptIn = false; // do it last, so super can still clean up
}
// =================================================

public void test001() {
this.runNegativeTest(
Expand Down Expand Up @@ -639,4 +651,84 @@ public void test023() {
" }\n" +
"}\n"}, "Hello");
}
public void testVoidAssertion() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
void v() {}
void m() {
assert v();
}
void n(boolean f) {
assert f : v();
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 4)
assert v();
^^^
Type mismatch: cannot convert from void to boolean
----------
2. ERROR in X.java (at line 7)
assert f : v();
^^^
This expression yields no value
----------
""");
}

public void testAssertionWithCustomException() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
public class X {
void m(boolean f) {
assert f : new IllegalArgumentException("f should be true");
}
public static void main(String... args) throws Throwable {
try {
new X().m(false);
} catch (AssertionError ae) {
try {
throw ae.getCause();
} catch (IllegalArgumentException iae) {
System.out.print(iae.getMessage());
}
}
}
}
"""};
runner.expectedOutputString = "f should be true";
runner.vmArguments = new String[] {"-ea"};
runner.runConformTest();
}

public void testVoidSynchronized() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
void v() {}
void m() {
synchronized(v()) {
System.out.print(1);
}
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 4)
synchronized(v()) {
^^^
This expression yields no value
----------
""");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2699,7 +2699,7 @@ public static void main(String[] args) {
"1. ERROR in X.java (at line 3)\n" +
" switch (main(null)) {\n" +
" ^^^^^^^^^^\n" +
"Expression must return a value\n" +
"This expression yields no value\n" +
"----------\n");
}
public void testGH3369_expression() {
Expand All @@ -2720,7 +2720,7 @@ void bar() {}
"1. ERROR in X.java (at line 3)\n" +
" return switch (bar()) {\n" +
" ^^^^^\n" +
"Expression must return a value\n" +
"This expression yields no value\n" +
"----------\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7674,19 +7674,12 @@ public static void main(String[] args) {
}
"""
},
this.complianceLevel < ClassFileConstants.JDK21 ?
"----------\n"
+ "1. ERROR in X.java (at line 7)\n"
+ " switch (foo()) {\n"
+ " ^^^^^\n"
+ "Expression must return a value\n"
+ "----------\n" :
"----------\n" +
"1. ERROR in X.java (at line 7)\n" +
" switch (foo()) {\n" +
" ^^^^^\n" +
"Expression must return a value\n" +
"----------\n");
"----------\n" +
"1. ERROR in X.java (at line 7)\n" +
" switch (foo()) {\n" +
" ^^^^^\n" +
"This expression yields no value\n" +
"----------\n");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2382
// VerifyError in switch expression on double
Expand Down Expand Up @@ -7793,7 +7786,7 @@ public static void main(String[] args) {
+ "1. ERROR in X.java (at line 7)\n"
+ " switch (foo()) {\n"
+ " ^^^^^\n"
+ "Expression must return a value\n"
+ "This expression yields no value\n"
+ "----------\n"
+ "2. ERROR in X.java (at line 8)\n"
+ " case null -> System.out.println(d);\n"
Expand All @@ -7804,7 +7797,7 @@ public static void main(String[] args) {
"1. ERROR in X.java (at line 7)\n" +
" switch (foo()) {\n" +
" ^^^^^\n" +
"Expression must return a value\n" +
"This expression yields no value\n" +
"----------\n" +
"2. ERROR in X.java (at line 7)\n" +
" switch (foo()) {\n" +
Expand Down

0 comments on commit c87d1e2

Please sign in to comment.