Skip to content

Commit 50e3a77

Browse files
liufengyundwijnand
authored andcommitted
Fix bootstrapping
1 parent 260183f commit 50e3a77

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ object Types {
12871287
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
12881288
*/
12891289
def widenUnion(using Context): Type = widen match
1290-
case tp @ OrNull(tp1): OrType =>
1290+
case tp @ OrNull(tp1) =>
12911291
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
12921292
val tp1Widen = tp1.widenUnionWithoutNull
12931293
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
@@ -3497,10 +3497,31 @@ object Types {
34973497
*/
34983498
object OrNull {
34993499
def apply(tp: Type)(using Context) =
3500-
if tp.isNullType then tp else OrType(tp, defn.NullType, soft = false)
3500+
OrType(tp, defn.NullType, soft = false)
3501+
def unapply(tp: OrType)(using Context): Option[Type] =
3502+
if (ctx.explicitNulls) {
3503+
val tp1 = tp.stripNull()
3504+
if tp1 ne tp then Some(tp1) else None
3505+
}
3506+
else None
3507+
}
3508+
3509+
/** An extractor object to pattern match against a Java-nullable union.
3510+
* e.g.
3511+
*
3512+
* (tp: Type) match
3513+
* case OrUncheckedNull(tp1) => // tp had the form `tp1 | UncheckedNull`
3514+
* case _ => // tp was not a Java-nullable union
3515+
*/
3516+
object OrUncheckedNull {
3517+
def apply(tp: Type)(using Context) =
3518+
OrType(tp, defn.UncheckedNullAliasType, soft = false)
35013519
def unapply(tp: Type)(using Context): Option[Type] =
3502-
val tp1 = tp.stripNull
3503-
if tp1 ne tp then Some(tp1) else None
3520+
if (ctx.explicitNulls) {
3521+
val tp1 = tp.stripUncheckedNull
3522+
if tp1 ne tp then Some(tp1) else None
3523+
}
3524+
else None
35043525
}
35053526

35063527
// ----- ExprType and LambdaTypes -----------------------------------

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
476476
* (x: T | Null) => x.$asInstanceOf$[x.type & T]
477477
*/
478478
def toNotNullTermRef(tree: Tree, pt: Type)(using Context): Tree = tree.tpe match
479-
case ref @ OrNull(tpnn) : TermRef
479+
case ref: TermRef
480480
if pt != AssignProto && // Ensure it is not the lhs of Assign
481481
ctx.notNullInfos.impliesNotNull(ref) &&
482482
// If a reference is in the context, it is already trackable at the point we add it.
483483
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
484484
!ref.usedOutOfOrder =>
485-
tree.cast(AndType(ref, tpnn))
485+
ref.widenDealias match
486+
case OrNull(tpnn) =>
487+
tree.cast(AndType(ref, tpnn))
488+
case _ =>
489+
tree
486490
case _ =>
487491
tree
488492

0 commit comments

Comments
 (0)