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

+ report error rather than crashing

Fixes eclipse-jdt#3369
  • Loading branch information
stephan-herrmann committed Feb 15, 2025
1 parent 9db6128 commit 709305d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 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 All @@ -8,6 +8,10 @@
*
* SPDX-License-Identifier: EPL-2.0
*
* This is an implementation of an early-draft specification developed under the Java
* Community Process (JCP) and is made available for testing and evaluation purposes
* only. The code is not compatible with any specification of the JCP.
*
* Contributors:
* IBM Corporation - initial API and implementation
* Stephan Herrmann - Contributions for
Expand Down Expand Up @@ -552,6 +556,10 @@ public void resolve(BlockScope upperScope) {
this.expression.computeConversion(upperScope, expressionType, expressionType);
checkType: {
if (expressionType.isBaseType()) {
if (expressionType.id == TypeIds.T_void) {
upperScope.problemReporter().illegalVoidExpression(this.expression);
break checkType;
}
if (JavaFeature.PRIMITIVES_IN_PATTERNS.isSupported(compilerOptions)) {
upperScope.referenceContext().compilationResult().usesPreview = true;
this.isPrimitiveSwitch = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2701,4 +2701,45 @@ public static void main(String[] args) {
},
"Null");
}
public void testGH3369_statement() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
public static void main(String[] args) {
switch (main(null)) {
}
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 3)\n" +
" switch (main(null)) {\n" +
" ^^^^^^^^^^\n" +
"Expression must return a value\n" +
"----------\n");
}
public void testGH3369_expression() {
runNegativeTest(new String[] {
"X.java",
"""
public class X {
int foo() {
return switch (bar()) {
default -> 1;
};
}
void bar() {}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 3)\n" +
" return switch (bar()) {\n" +
" ^^^^^\n" +
"Expression must return a value\n" +
"----------\n");
}
}

0 comments on commit 709305d

Please sign in to comment.