Skip to content

Commit

Permalink
Preserve hard unions in widenSingletons
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Jan 14, 2025
1 parent af655c9 commit 2472a58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1403,9 +1403,9 @@ object Types extends TypeUtils {
case tp =>
tp

/** Widen all top-level singletons reachable by dealiasing
* and going to the operands of & and |.
* Overridden and cached in OrType.
/** Widen all top-level singletons reachable by dealiasing and going to the
* operands of intersections and soft unions (only when `skipSoftUnions` is
* `false`). Overridden and cached in [[OrType]].
*/
def widenSingletons(skipSoftUnions: Boolean = false)(using Context): Type = dealias match {
case tp: SingletonType =>
Expand Down Expand Up @@ -3630,7 +3630,7 @@ object Types extends TypeUtils {
myAtoms

override def widenSingletons(skipSoftUnions: Boolean)(using Context): Type =
if isSoft && skipSoftUnions then this
if !isSoft || skipSoftUnions then this
else
if widenedRunId != ctx.runId then
myWidened = computeWidenSingletons()
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/22219.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type MonthNumber = 1 | 2

def main =
val x = 1: MonthNumber
val y: MonthNumber = x
20 changes: 20 additions & 0 deletions tests/pos/22219b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type MonthNumber =
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12

def main =
List[(String, MonthNumber)](
"January" -> 1,
"February" -> 2,
"March" -> 3,
"April" -> 4,
"May" -> 5,
"June"-> 6,
"July" -> 7,
"August" -> 8,
"September" -> 9,
"October" -> 10,
"November" -> 11,
"December" -> 12
).foreach { (name, number) =>
summon[number.type <:< MonthNumber]
}

0 comments on commit 2472a58

Please sign in to comment.