Skip to content

Commit 48f97e7

Browse files
committed
Refine atoms computation some more.
If we have `x: 0 | 1` then `x.type` should have atoms {`0`, `1`} only if widening is allowed. On the other hand, if `x: 0` then `x` has atoms `{0}`irrespective of widening.
1 parent 09c989a commit 48f97e7

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ object Types {
11571157
* these types as a set, otherwise the empty set.
11581158
* Overridden and cached in OrType.
11591159
* @param widenOK If type proxies that are upperbounded by types with atoms
1160-
* have the same atoms.
1160+
* have the same atoms.
11611161
*/
11621162
def atoms(widenOK: Boolean = false)(implicit ctx: Context): Set[Type] = dealias match {
11631163
case tp: SingletonType =>
@@ -1174,8 +1174,9 @@ object Types {
11741174
case _ => tp
11751175
}
11761176
val underlyingAtoms = tp.underlying.atoms(widenOK)
1177-
if (underlyingAtoms.isEmpty && tp.isStable) Set.empty + normalize(tp)
1178-
else underlyingAtoms
1177+
if underlyingAtoms.isEmpty && tp.isStable then Set.empty + normalize(tp)
1178+
else if underlyingAtoms.size == 1 || widenOK then underlyingAtoms
1179+
else Set.empty
11791180
case tp: ExprType => tp.resType.atoms(widenOK)
11801181
case tp: OrType => tp.atoms(widenOK) // `atoms` overridden in OrType
11811182
case tp: AndType => tp.tp1.atoms(widenOK) & tp.tp2.atoms(widenOK)

tests/pos/i8128.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
object Test {
2-
def id: (x: 1 | 0) => x.type = x => x
3-
id(0): 0 // fails
2+
def id: (x: 1 | 0) => x.type = ???
3+
id(0): 0 // ok
44

55
def id2: Function1[1 | 0, 1 | 0] {
66
def apply(x: 1 | 0): x.type
77
} = ???
8-
id2(0): 0 // fails
8+
id2(0): 0 // ok
99

1010
def id3: Function1[1 | 0, Int] {
1111
def apply(x: 1 | 0): x.type

0 commit comments

Comments
 (0)