Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve hard unions in widenSingletons #22369

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}
Loading