Skip to content

Commit

Permalink
Additional handeling for null and nothing types within Or terms
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Martin committed Mar 25, 2024
1 parent 9e6b993 commit 4cf2c86
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3442,6 +3442,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
private def flattenOr(tp: Type)(using Context): Type =
var options: List[Type] = Nil
var doUpdate: Boolean = false
// Null and Nothing are sub types of everything, and are puled out post-typing via TypeOps, nevertheless,
// we reduce to at most one of each at this stage
var nullRef: Option[Type] = None
var nothingRef: Option[Type] = None

def offer(next: Type): Unit =
// By checking at insert time, we will never add an element to the internal state if it is invalidated by
Expand All @@ -3450,6 +3454,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case OrType(o1, o2) =>
offer(o1)
offer(o2)
case nothing if nothing.isNothingType =>
if (nothingRef.isDefined)
doUpdate = true
else
nothingRef = Some(nothing)
case nul if nul.isNullType =>
if (nullRef.isDefined)
doUpdate = true
else
nullRef = Some(nul)
case _ =>
if (!options.exists(prior => next <:< prior))
options = next :: options
Expand All @@ -3458,7 +3472,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer

offer(tp)
if (doUpdate)
val typesToAdd = options.reverse.tails.flatMap {
val distinctTypes = options.reverse.tails.map {
case curr :: allLaterAdditions
if !allLaterAdditions.exists(later => curr <:< later) =>
Some(curr)
Expand All @@ -3467,6 +3481,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
None
}

val typesToAdd = (Iterator(nothingRef, nullRef) ++ distinctTypes).flatten

def addHelper(add: Type, orTree: List[Option[Type]], iter: Int = 0): List[Option[Type]] =
orTree match
case None :: more =>
Expand Down

0 comments on commit 4cf2c86

Please sign in to comment.