8357361: Exception when compiling switch expression with inferred type

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2025-05-22 05:56:17 +00:00
parent 68c1d305e7
commit ec6706520b
2 changed files with 24 additions and 9 deletions

View File

@ -810,9 +810,6 @@ public class TransTypes extends TreeTranslator {
}
public void visitSwitch(JCSwitch tree) {
Type selsuper = types.supertype(tree.selector.type);
boolean enumSwitch = selsuper != null &&
selsuper.tsym == syms.enumSym;
tree.selector = translate(tree.selector, erasure(tree.selector.type));
tree.cases = translateCases(tree.cases);
result = tree;
@ -848,11 +845,8 @@ public class TransTypes extends TreeTranslator {
}
public void visitSwitchExpression(JCSwitchExpression tree) {
Type selsuper = types.supertype(tree.selector.type);
boolean enumSwitch = selsuper != null &&
selsuper.tsym == syms.enumSym;
tree.selector = translate(tree.selector, erasure(tree.selector.type));
tree.cases = translate(tree.cases, tree.type);
tree.cases = translate(tree.cases, erasure(tree.type));
tree.type = erasure(tree.type);
result = retype(tree, tree.type, pt);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, 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
@ -23,12 +23,14 @@
/*
* @test
* @bug 8214031
* @bug 8214031 8357361
* @summary Verify various corner cases with nested switch expressions.
* @compile ExpressionSwitchBugsInGen.java
* @run main ExpressionSwitchBugsInGen
*/
import java.util.Objects;
public class ExpressionSwitchBugsInGen {
public static void main(String... args) {
new ExpressionSwitchBugsInGen().test(0, 0, 0, false);
@ -43,6 +45,8 @@ public class ExpressionSwitchBugsInGen {
new ExpressionSwitchBugsInGen().testSwitchExpressionInConditional(1, 1, 1);
new ExpressionSwitchBugsInGen().testIntBoxing(0, 10, 10);
new ExpressionSwitchBugsInGen().testIntBoxing(1, 10, -1);
new ExpressionSwitchBugsInGen().testSwitchExpressionTypeErased(0);
new ExpressionSwitchBugsInGen().testSwitchExpressionTypeErased(1);
}
private void test(int a, int b, int c, boolean expected) {
@ -91,4 +95,21 @@ public class ExpressionSwitchBugsInGen {
}
}
//JDK-8357361:
private void testSwitchExpressionTypeErased(int i) {
interface Readable<R extends String> {
R getReader();
}
Readable<?> readable = () -> "";
var v = switch (i) {
case 0 -> readable.getReader();
default -> null;
};
var expected = i == 0 ? "" : null;
if (!Objects.equals(v, expected)) {
throw new IllegalStateException("Expected: " + expected +
", got: " + v);
}
}
}