Skip to content

Commit dc60abc

Browse files
committed
Fix bug in handling of nullable unions during widening
We need to call normalize on the widened type, as opposed to the receiver of `widenUnion`, which need not be a union (could be a TermRef).
1 parent 0e50082 commit dc60abc

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,10 @@ object Types {
11941194
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
11951195
*/
11961196
def widenUnion(implicit ctx: Context): Type = widen match {
1197-
case OrType(tp1, tp2) =>
1198-
if (ctx.explicitNulls && isNullableUnion) {
1197+
case tp @ OrType(tp1, tp2) =>
1198+
if (ctx.explicitNulls && tp.isNullableUnion) {
11991199
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
1200-
val OrType(leftTpe, nullTpe) = normNullableUnion
1200+
val OrType(leftTpe, nullTpe) = tp.normNullableUnion
12011201
OrType(leftTpe.widenUnion, nullTpe)
12021202
} else {
12031203
ctx.typeComparer.lub(tp1.widenUnion, tp2.widenUnion, canConstrain = true) match {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
// Test that we correctly handle nullable unions when widening
3+
// (we don't widen them).
4+
class Test {
5+
def foo(): Unit = {
6+
val x: String|Null = ???
7+
val y = x // this used to crash the compiler
8+
}
9+
}

0 commit comments

Comments
 (0)