Skip to content

Commit 62c8165

Browse files
committed
fix code to pass explicit null check
1 parent 4cc9f64 commit 62c8165

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
*
@@ -113,7 +113,7 @@ final class ProperGadtConstraint private(
113113
private var pathDepMapping: SimpleIdentityMap[PathType, SimpleIdentityMap[Symbol, TypeVar]],
114114
private var pathDepReverseMapping: SimpleIdentityMap[TypeParamRef, TypeRef],
115115
private var wasConstrained: Boolean,
116-
private var myScrutineePath: TermRef,
116+
private var myScrutineePath: TermRef | Null,
117117
private var myUnionFind: SimpleIdentityMap[PathType, PathType]
118118
) extends GadtConstraint with ConstraintHandling {
119119
import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr}
@@ -183,7 +183,7 @@ final class ProperGadtConstraint private(
183183
val tp1 = left.externalize(p1)
184184
right.tvarOf(tp1) match {
185185
case null =>
186-
case tvar2 =>
186+
case tvar2: TypeVar =>
187187
mapping1 = mapping1.updated(p1, tvar2.origin)
188188
mapping2 = mapping2.updated(tvar2.origin, p1)
189189
}
@@ -194,7 +194,7 @@ final class ProperGadtConstraint private(
194194
def mapTypeParam(m: SimpleIdentityMap[TypeParamRef, TypeParamRef])(tpr: TypeParamRef) =
195195
m(tpr) match
196196
case null => tpr
197-
case tpr1 => tpr1
197+
case tpr1: TypeParamRef => tpr1
198198

199199
(mapTypeParam(mapping1), mapTypeParam(mapping2))
200200
}
@@ -297,7 +297,7 @@ final class ProperGadtConstraint private(
297297
private def tvarOf(path: PathType, sym: Symbol)(using Context): TypeVar | Null =
298298
pathDepMapping(path) match
299299
case null => null
300-
case innerMapping => innerMapping(sym)
300+
case innerMapping => innerMapping.nn(sym)
301301

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

396396
old.updated(sym, tv)
397397
})
@@ -552,28 +552,30 @@ final class ProperGadtConstraint private(
552552
private def lookupPath(p: PathType): PathType | Null =
553553
def recur(p: PathType, steps: Int = 0): PathType | Null = myUnionFind(p) match
554554
case null => null
555-
case q if p eq q => q
556-
case q =>
555+
case q: PathType if q eq p => q
556+
case q: PathType =>
557557
if steps <= 1024 then
558558
recur(q, steps + 1)
559559
else
560560
assert(false, "lookup step exceeding the threshold, possibly because of a loop in the union find")
561561
recur(p)
562562

563563
override def addEquality(p: PathType, q: PathType): Unit =
564-
val newRep = lookupPath(p) match
564+
val newRep: PathType = lookupPath(p) match
565565
case null => lookupPath(q) match
566566
case null => p
567-
case r => r
568-
case r => r
567+
case r: PathType => r
568+
case r: PathType => r
569569

570570
myUnionFind = myUnionFind.updated(p, newRep)
571571
myUnionFind = myUnionFind.updated(q, newRep)
572572

573573
override def isEquivalent(p: PathType, q: PathType): Boolean =
574574
lookupPath(p) match
575575
case null => false
576-
case p0 => p0 eq lookupPath(q)
576+
case p0: PathType => lookupPath(q) match
577+
case null => false
578+
case q0: PathType => p0 eq q0
577579

578580
override def isLess(sym1: Symbol, sym2: Symbol)(using Context): Boolean =
579581
constraint.isLess(tvarOrError(sym1).origin, tvarOrError(sym2).origin)
@@ -593,7 +595,7 @@ final class ProperGadtConstraint private(
593595
override def fullBounds(p: PathType, sym: Symbol)(using Context): TypeBounds | Null =
594596
tvarOf(p, sym) match {
595597
case null => null
596-
case tv => fullBounds(tv.origin)
598+
case tv => fullBounds(tv.nn.origin)
597599
}
598600

599601
override def fullBounds(tp: TypeRef)(using Context): TypeBounds | Null =
@@ -622,7 +624,7 @@ final class ProperGadtConstraint private(
622624
case TypeAlias(tpr: TypeParamRef) if reverseMapping.contains(tpr) =>
623625
TypeAlias(reverseMapping(tpr).nn.typeRef)
624626
case TypeAlias(tpr: TypeParamRef) if pathDepReverseMapping.contains(tpr) =>
625-
TypeAlias(pathDepReverseMapping(tpr))
627+
TypeAlias(pathDepReverseMapping(tpr).nn)
626628
case tb => tb
627629
}
628630
retrieveBounds
@@ -640,7 +642,7 @@ final class ProperGadtConstraint private(
640642

641643
override def contains(path: PathType, sym: Symbol)(using Context): Boolean = pathDepMapping(path) match
642644
case null => false
643-
case innerMapping => innerMapping(sym) != null
645+
case innerMapping => innerMapping.nn(sym) != null
644646

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

@@ -690,7 +692,7 @@ final class ProperGadtConstraint private(
690692

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

693-
override def withScrutineePath[T](path: TermRef)(op: => T): T =
695+
override def withScrutineePath[T](path: TermRef | Null)(op: => T): T =
694696
val saved = this.myScrutineePath
695697
this.myScrutineePath = path
696698
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)