Skip to content

Commit 0e92053

Browse files
committed
8254286: Wrong inference in switch expression with "null" arm
Reviewed-by: mcimadamore, vromero
1 parent 63ce304 commit 0e92053

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,10 @@ Type condType(List<DiagnosticPosition> positions, List<Type> condTypes) {
20262026
// both are known to be reference types. The result is
20272027
// lub(thentype,elsetype). This cannot fail, as it will
20282028
// always be possible to infer "Object" if nothing better.
2029-
return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
2029+
return types.lub(condTypes.stream()
2030+
.map(t -> t.baseType())
2031+
.filter(t -> !t.hasTag(BOT))
2032+
.collect(List.collector()));
20302033
}
20312034

20322035
final static TypeTag[] primitiveTags = new TypeTag[]{

test/langtools/tools/javac/switchexpr/ExpressionSwitchInfer.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8206986
3+
* @bug 8206986 8254286
44
* @summary Check types inferred for switch expressions.
55
* @compile/fail/ref=ExpressionSwitchInfer.out -XDrawDiagnostics ExpressionSwitchInfer.java
66
*/
@@ -34,4 +34,53 @@ private <T> T test(List<T> l, Class<T> c, String param) {
3434
return null;
3535
}
3636

37+
void bug8254286(I1 i1, I2 i2, int s) {
38+
var t1 = switch (s) {
39+
case 1 -> i1;
40+
case 2 -> null;
41+
default -> i2;
42+
};
43+
t1.m();
44+
var t2 = switch (s) {
45+
case 2 -> null;
46+
case 1 -> i1;
47+
default -> i2;
48+
};
49+
t2.m();
50+
var t3 = switch (s) {
51+
case 1 -> i1;
52+
default -> i2;
53+
case 2 -> null;
54+
};
55+
t3.m();
56+
var t4 = switch (s) {
57+
case 1 -> i1;
58+
default -> null;
59+
};
60+
t4.m();
61+
var t5 = switch (s) {
62+
default -> null;
63+
case 1 -> i1;
64+
};
65+
t5.m();
66+
var t6 = switch (s) {
67+
default -> null;
68+
};
69+
var t7 = switch (s) {
70+
case 1 -> null;
71+
default -> null;
72+
};
73+
var t8 = switch (s) {
74+
case 1 -> null;
75+
case 2 -> null;
76+
default -> null;
77+
};
78+
}
79+
80+
interface I {
81+
void m();
82+
}
83+
interface I1 extends I {}
84+
interface I2 extends I {}
85+
3786
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
ExpressionSwitchInfer.java:17:95: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
22
ExpressionSwitchInfer.java:26:38: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
33
ExpressionSwitchInfer.java:30:23: compiler.err.prob.found.req: (compiler.misc.incompatible.type.in.switch.expression: (compiler.misc.inconvertible.types: int, java.lang.String))
4-
3 errors
4+
ExpressionSwitchInfer.java:66:13: compiler.err.cant.infer.local.var.type: t6, (compiler.misc.local.cant.infer.null)
5+
ExpressionSwitchInfer.java:69:13: compiler.err.cant.infer.local.var.type: t7, (compiler.misc.local.cant.infer.null)
6+
ExpressionSwitchInfer.java:73:13: compiler.err.cant.infer.local.var.type: t8, (compiler.misc.local.cant.infer.null)
7+
6 errors

0 commit comments

Comments
 (0)