8254286: Wrong inference in switch expression with "null" arm

Reviewed-by: mcimadamore, vromero
This commit is contained in:
Jan Lahoda 2020-10-23 09:47:29 +00:00
parent 63ce304ea4
commit 0e9205315e
3 changed files with 58 additions and 3 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/switchexpr

@ -2026,7 +2026,10 @@ public class Attr extends JCTree.Visitor {
// both are known to be reference types. The result is
// lub(thentype,elsetype). This cannot fail, as it will
// always be possible to infer "Object" if nothing better.
return types.lub(condTypes.stream().map(t -> t.baseType()).collect(List.collector()));
return types.lub(condTypes.stream()
.map(t -> t.baseType())
.filter(t -> !t.hasTag(BOT))
.collect(List.collector()));
}
final static TypeTag[] primitiveTags = new TypeTag[]{

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8206986
* @bug 8206986 8254286
* @summary Check types inferred for switch expressions.
* @compile/fail/ref=ExpressionSwitchInfer.out -XDrawDiagnostics ExpressionSwitchInfer.java
*/
@ -34,4 +34,53 @@ public class ExpressionSwitchInfer {
return null;
}
void bug8254286(I1 i1, I2 i2, int s) {
var t1 = switch (s) {
case 1 -> i1;
case 2 -> null;
default -> i2;
};
t1.m();
var t2 = switch (s) {
case 2 -> null;
case 1 -> i1;
default -> i2;
};
t2.m();
var t3 = switch (s) {
case 1 -> i1;
default -> i2;
case 2 -> null;
};
t3.m();
var t4 = switch (s) {
case 1 -> i1;
default -> null;
};
t4.m();
var t5 = switch (s) {
default -> null;
case 1 -> i1;
};
t5.m();
var t6 = switch (s) {
default -> null;
};
var t7 = switch (s) {
case 1 -> null;
default -> null;
};
var t8 = switch (s) {
case 1 -> null;
case 2 -> null;
default -> null;
};
}
interface I {
void m();
}
interface I1 extends I {}
interface I2 extends I {}
}

@ -1,4 +1,7 @@
ExpressionSwitchInfer.java:17:95: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
ExpressionSwitchInfer.java:26:38: compiler.err.cant.resolve.location.args: kindname.method, substring, , int, (compiler.misc.location: kindname.interface, java.lang.CharSequence, null)
ExpressionSwitchInfer.java:30:23: compiler.err.prob.found.req: (compiler.misc.incompatible.type.in.switch.expression: (compiler.misc.inconvertible.types: int, java.lang.String))
3 errors
ExpressionSwitchInfer.java:66:13: compiler.err.cant.infer.local.var.type: t6, (compiler.misc.local.cant.infer.null)
ExpressionSwitchInfer.java:69:13: compiler.err.cant.infer.local.var.type: t7, (compiler.misc.local.cant.infer.null)
ExpressionSwitchInfer.java:73:13: compiler.err.cant.infer.local.var.type: t8, (compiler.misc.local.cant.infer.null)
6 errors