Skip to content

Commit 8d37e99

Browse files
committed
fix code to pass explicit null check
1 parent b5fa369 commit 8d37e99

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ sealed abstract class GadtConstraint extends Showable {
7171
def resetScrutineePath(): Unit
7272

7373
/** Set the scrutinee path. */
74-
def withScrutineePath[T](path: TermRef)(op: => T): T
74+
def withScrutineePath[T](path: TermRef | Null)(op: => T): T
7575

7676
/** Is the symbol registered in the constraint?
7777
*
@@ -111,7 +111,7 @@ final class ProperGadtConstraint private(
111111
private var pathDepMapping: SimpleIdentityMap[PathType, SimpleIdentityMap[Symbol, TypeVar]],
112112
private var pathDepReverseMapping: SimpleIdentityMap[TypeParamRef, TypeRef],
113113
private var wasConstrained: Boolean,
114-
private var myScrutineePath: TermRef,
114+
private var myScrutineePath: TermRef | Null,
115115
private var myUnionFind: SimpleIdentityMap[PathType, PathType]
116116
) extends GadtConstraint with ConstraintHandling {
117117
import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr}
@@ -181,7 +181,7 @@ final class ProperGadtConstraint private(
181181
val tp1 = left.externalize(p1)
182182
right.tvarOf(tp1) match {
183183
case null =>
184-
case tvar2 =>
184+
case tvar2: TypeVar =>
185185
mapping1 = mapping1.updated(p1, tvar2.origin)
186186
mapping2 = mapping2.updated(tvar2.origin, p1)
187187
}
@@ -192,7 +192,7 @@ final class ProperGadtConstraint private(
192192
def mapTypeParam(m: SimpleIdentityMap[TypeParamRef, TypeParamRef])(tpr: TypeParamRef) =
193193
m(tpr) match
194194
case null => tpr
195-
case tpr1 => tpr1
195+
case tpr1: TypeParamRef => tpr1
196196

197197
(mapTypeParam(mapping1), mapTypeParam(mapping2))
198198
}
@@ -295,7 +295,7 @@ final class ProperGadtConstraint private(
295295
private def tvarOf(path: PathType, sym: Symbol)(using Context): TypeVar | Null =
296296
pathDepMapping(path) match
297297
case null => null
298-
case innerMapping => innerMapping(sym)
298+
case innerMapping => innerMapping.nn(sym)
299299

300300
/** Try to retrieve type variable for some TypeRef.
301301
* Both type parameters and path-dependent types are considered.
@@ -389,7 +389,7 @@ final class ProperGadtConstraint private(
389389
pathDepMapping = pathDepMapping.updated(path, {
390390
val old: SimpleIdentityMap[Symbol, TypeVar] = pathDepMapping(path) match
391391
case null => SimpleIdentityMap.empty
392-
case m => m
392+
case m => m.nn
393393

394394
old.updated(sym, tv)
395395
})
@@ -550,28 +550,30 @@ final class ProperGadtConstraint private(
550550
private def lookupPath(p: PathType): PathType | Null =
551551
def recur(p: PathType, steps: Int = 0): PathType | Null = myUnionFind(p) match
552552
case null => null
553-
case q if p eq q => q
554-
case q =>
553+
case q: PathType if q eq p => q
554+
case q: PathType =>
555555
if steps <= 1024 then
556556
recur(q, steps + 1)
557557
else
558558
assert(false, "lookup step exceeding the threshold, possibly because of a loop in the union find")
559559
recur(p)
560560

561561
override def addEquality(p: PathType, q: PathType): Unit =
562-
val newRep = lookupPath(p) match
562+
val newRep: PathType = lookupPath(p) match
563563
case null => lookupPath(q) match
564564
case null => p
565-
case r => r
566-
case r => r
565+
case r: PathType => r
566+
case r: PathType => r
567567

568568
myUnionFind = myUnionFind.updated(p, newRep)
569569
myUnionFind = myUnionFind.updated(q, newRep)
570570

571571
override def isEquivalent(p: PathType, q: PathType): Boolean =
572572
lookupPath(p) match
573573
case null => false
574-
case p0 => p0 eq lookupPath(q)
574+
case p0: PathType => lookupPath(q) match
575+
case null => false
576+
case q0: PathType => p0 eq q0
575577

576578
override def isLess(sym1: Symbol, sym2: Symbol)(using Context): Boolean =
577579
constraint.isLess(tvarOrError(sym1).origin, tvarOrError(sym2).origin)
@@ -591,7 +593,7 @@ final class ProperGadtConstraint private(
591593
override def fullBounds(p: PathType, sym: Symbol)(using Context): TypeBounds | Null =
592594
tvarOf(p, sym) match {
593595
case null => null
594-
case tv => fullBounds(tv.origin)
596+
case tv => fullBounds(tv.nn.origin)
595597
}
596598

597599
override def fullBounds(tp: TypeRef)(using Context): TypeBounds | Null =
@@ -625,7 +627,7 @@ final class ProperGadtConstraint private(
625627
case TypeAlias(tpr: TypeParamRef) if reverseMapping.contains(tpr) =>
626628
TypeAlias(reverseMapping(tpr).nn.typeRef)
627629
case TypeAlias(tpr: TypeParamRef) if pathDepReverseMapping.contains(tpr) =>
628-
TypeAlias(pathDepReverseMapping(tpr))
630+
TypeAlias(pathDepReverseMapping(tpr).nn)
629631
case tb => tb
630632
}
631633
retrieveBounds
@@ -643,7 +645,7 @@ final class ProperGadtConstraint private(
643645

644646
override def contains(path: PathType, sym: Symbol)(using Context): Boolean = pathDepMapping(path) match
645647
case null => false
646-
case innerMapping => innerMapping(sym) != null
648+
case innerMapping => innerMapping.nn(sym) != null
647649

648650
override def registeredTypeMembers(path: PathType): List[Symbol] = pathDepMapping(path).nn.keys
649651

@@ -691,7 +693,7 @@ final class ProperGadtConstraint private(
691693

692694
override def resetScrutineePath(): Unit = myScrutineePath = null
693695

694-
override def withScrutineePath[T](path: TermRef)(op: => T): T =
696+
override def withScrutineePath[T](path: TermRef | Null)(op: => T): T =
695697
val saved = this.myScrutineePath
696698
this.myScrutineePath = path
697699
val result = op
@@ -826,7 +828,7 @@ final class ProperGadtConstraint private(
826828

827829
override def resetScrutineePath(): Unit = ()
828830

829-
override def withScrutineePath[T](path: TermRef)(op: => T): T = op
831+
override def withScrutineePath[T](path: TermRef | Null)(op: => T): T = op
830832

831833
override def fresh = new ProperGadtConstraint
832834
override def restore(other: GadtConstraint): Unit =

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,9 @@ trait PatternTypeConstrainer { self: TypeComparer =>
255255

256256
/** Show the scrutinee. Will show the path if available. */
257257
private def scrutRepr(scrut: Type): String =
258-
if ctx.gadt.scrutineePath != null then
259-
ctx.gadt.scrutineePath.show
260-
else
261-
scrut.show
258+
ctx.gadt.scrutineePath match
259+
case null => scrut.show
260+
case p: PathType => p.show
262261

263262
/** Constrain "simple" patterns (see `constrainPatternType`).
264263
*

0 commit comments

Comments
 (0)