Skip to content

Commit 6c730b4

Browse files
authored
Merge pull request #190 from scala/backport-lts-3.3-22369
Backport "Preserve hard unions in widenSingletons" to 3.3 LTS
2 parents 6a2dab9 + c83d67c commit 6c730b4

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,9 @@ object Types extends TypeUtils {
13611361
case tp =>
13621362
tp
13631363

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

35453545
override def widenSingletons(skipSoftUnions: Boolean)(using Context): Type =
3546-
if isSoft && skipSoftUnions then this
3546+
if !isSoft || skipSoftUnions then this
35473547
else
35483548
if widenedRunId != ctx.runId then
35493549
myWidened = computeWidenSingletons()

tests/pos/22219.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type MonthNumber = 1 | 2
2+
3+
def main =
4+
val x = 1: MonthNumber
5+
val y: MonthNumber = x

tests/pos/22219b.scala

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type MonthNumber =
2+
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
3+
4+
def main =
5+
List[(String, MonthNumber)](
6+
"January" -> 1,
7+
"February" -> 2,
8+
"March" -> 3,
9+
"April" -> 4,
10+
"May" -> 5,
11+
"June"-> 6,
12+
"July" -> 7,
13+
"August" -> 8,
14+
"September" -> 9,
15+
"October" -> 10,
16+
"November" -> 11,
17+
"December" -> 12
18+
).foreach { (name, number) =>
19+
summon[number.type <:< MonthNumber]
20+
}

0 commit comments

Comments
 (0)