Skip to content

Commit abd056f

Browse files
srikanth-sankaraniloveeclipse
authored andcommitted
Java 24 switch expression and type inference generates wrong/unsecure
bytecode * Fixes eclipse-jdt#3920
1 parent 49edd64 commit abd056f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private TypeBinding resultType() {
7373
TypeBinding uniformType = null;
7474
for (Expression rExpression : this.rExpressions)
7575
uniformType = uniformType == null ? rExpression.resolvedType : NullAnnotationMatching.moreDangerousType(uniformType, rExpression.resolvedType);
76-
return uniformType;
76+
return resolveAsType(uniformType);
7777
}
7878

7979
if (this.allBoolean)

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchExpressionsYieldTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8432,4 +8432,30 @@ public static <S extends Index> AbstractLine<S> create(int id, S index, int owne
84328432
"The method state() is undefined for the type X\n" +
84338433
"----------\n");
84348434
}
8435+
8436+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3920
8437+
// Java 24 switch expression and type inference generates wrong/unsecure bytecode
8438+
public void testIssue3920() {
8439+
this.runConformTest(
8440+
new String[] {
8441+
"X.java",
8442+
"""
8443+
import java.util.List;
8444+
import java.util.stream.Collectors;
8445+
8446+
public class X {
8447+
public static void main(String[] args) {
8448+
var l = List.of("A","B");
8449+
int i = 12;
8450+
var t = switch(i) {
8451+
case 1 -> l.stream().collect(Collectors.joining(" "));
8452+
default -> "Fixed!";
8453+
};
8454+
System.out.println(t);
8455+
}
8456+
}
8457+
"""
8458+
},
8459+
"Fixed!");
8460+
}
84358461
}

0 commit comments

Comments
 (0)