@@ -588,11 +588,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
588588 */
589589 def toNotNullTermRef (tree : Tree , pt : Type )(using Context ): Tree = tree.tpe match
590590 case ref : TermRef
591- if pt != LhsProto && // Ensure it is not the lhs of Assign
592- ctx.notNullInfos.impliesNotNull(ref) &&
593- // If a reference is in the context, it is already trackable at the point we add it.
594- // Hence, we don't use isTracked in the next line, because checking use out of order is enough.
595- ! ref.usedOutOfOrder =>
591+ if ctx.explicitNulls
592+ && pt != LhsProto // Ensure it is not the lhs of Assign
593+ && ctx.notNullInfos.impliesNotNull(ref)
594+ // If a reference is in the context, it is already trackable at the point we add it.
595+ // Hence, we don't use isTracked in the next line, because checking use out of order is enough.
596+ && ! ref.usedOutOfOrder =>
596597 ref match
597598 case OrNull (tpnn) => tree.cast(AndType (ref, tpnn))
598599 case _ => tree
@@ -2228,7 +2229,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22282229 given Context = caseCtx
22292230 val case1 = typedCase(cas, sel, wideSelType, tpe)
22302231 caseCtx = Nullables .afterPatternContext(sel, case1.pat)
2231- if ! alreadyStripped && Nullables .matchesNull(case1) then
2232+ if ctx.explicitNulls && ! alreadyStripped && Nullables .matchesNull(case1) then
22322233 wideSelType = wideSelType.stripNull()
22332234 alreadyStripped = true
22342235 case1
@@ -2261,7 +2262,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22612262 given Context = caseCtx
22622263 val case1 = typedCase(cas, sel, wideSelType, pt)
22632264 caseCtx = Nullables .afterPatternContext(sel, case1.pat)
2264- if ! alreadyStripped && Nullables .matchesNull(case1) then
2265+ if ctx.explicitNulls && ! alreadyStripped && Nullables .matchesNull(case1) then
22652266 wideSelType = wideSelType.stripNull()
22662267 alreadyStripped = true
22672268 case1
0 commit comments