Skip to content

Commit 676710c

Browse files
committed
Add ctx.explicitNulls to guard some calls to stripNull and OrNull.unapply
1 parent eb92601 commit 676710c

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ object Types extends TypeUtils {
14511451
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
14521452
*/
14531453
def widenUnion(using Context): Type = widen match
1454-
case tp: OrType =>
1454+
case tp: OrType if ctx.explicitNulls =>
14551455
val tp1 = tp.stripNull(stripFlexibleTypes = false)
14561456
if tp1 ne tp then
14571457
val tp1Widen = tp1.widenUnionWithoutNull

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ trait TypeAssigner {
176176
val qualType = qual.tpe.widenIfUnstable
177177
def kind = if tree.isType then "type" else "value"
178178
val foundWithoutNull = qualType match
179-
case OrNull(qualType1) if qualType1 <:< defn.ObjectType =>
179+
case OrNull(qualType1) if ctx.explicitNulls && qualType1 <:< defn.ObjectType =>
180180
val name = tree.name
181181
val pre = maybeSkolemizePrefix(qualType1, name)
182182
reallyExists(qualType1.findMember(name, pre))

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)