Skip to content

Commit 80f64e5

Browse files
committed
Allow to reduce type member extractors when the member is a class.
1 parent 03f5b74 commit 80f64e5

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,23 +3361,33 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
33613361
val info = denot.info match
33623362
case TypeAlias(alias) => alias
33633363
case info => info // Notably, RealTypeBounds, which will eventually give a MatchResult.NoInstances
3364-
if info.isInstanceOf[ClassInfo] then
3365-
/* The member is not an alias (we'll get Stuck instead of NoInstances,
3366-
* which is not ideal, but we cannot make a RealTypeBounds of ClassInfo).
3364+
val infoRefersToSkolem = stableScrut match
3365+
case stableScrut: SkolemType =>
3366+
new TypeAccumulator[Boolean] {
3367+
def apply(prev: Boolean, tp: Type): Boolean =
3368+
prev || (tp eq stableScrut) || foldOver(prev, tp)
3369+
}.apply(false, info)
3370+
case _ =>
3371+
false
3372+
if infoRefersToSkolem && info.isInstanceOf[ClassInfo] then
3373+
/* We would like to create a `RealTypeBounds(info, info)` to get a `MatchResult.NoInstances`
3374+
* but that is not allowed for `ClassInfo`. So instead we return `false`, which will result
3375+
* in a `MatchResult.Stuck` instead.
33673376
*/
33683377
false
33693378
else
3370-
val infoRefersToSkolem = stableScrut match
3371-
case stableScrut: SkolemType =>
3372-
new TypeAccumulator[Boolean] {
3373-
def apply(prev: Boolean, tp: Type): Boolean =
3374-
prev || (tp eq stableScrut) || foldOver(prev, tp)
3375-
}.apply(false, info)
3379+
val info1 = info match
3380+
case ClassInfo(prefix, cls, _, _, _) =>
3381+
// Re-select the class from the prefix
3382+
prefix.select(cls)
3383+
case info: TypeBounds =>
3384+
// Will already trigger a MatchResult.NoInstances
3385+
info
3386+
case _ if infoRefersToSkolem =>
3387+
// Explicitly trigger a MatchResult.NoInstances
3388+
RealTypeBounds(info, info)
33763389
case _ =>
3377-
false
3378-
val info1 =
3379-
if infoRefersToSkolem && !info.isInstanceOf[TypeBounds] then RealTypeBounds(info, info) // to trigger a MatchResult.NoInstances
3380-
else info
3390+
info
33813391
rec(capture, info1, variance = 0, scrutIsWidenedAbstract)
33823392
case _ =>
33833393
false
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E172] Type Error: tests/neg/match-type-enumeration-value-hack.scala:11:40 ------------------------------------------
2+
11 | summon[Suit#Value =:= EnumValue[Suit]] // error
3+
| ^
4+
| Cannot prove that Suit#Value =:= EnumValue[Suit].
5+
|
6+
| Note: a match type could not be fully reduced:
7+
|
8+
| trying to reduce EnumValue[Suit]
9+
| failed since selector Suit
10+
| does not match case EnumValueAux[t] => t
11+
| and cannot be shown to be disjoint from it either.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type EnumValueAux[A] = ({ type Value }) { type Value = A }
2+
3+
type EnumValue[E <: Enumeration] = E match
4+
case EnumValueAux[t] => t
5+
6+
// A class extending Enumeration does not yet define a concrete enumeration
7+
class Suit extends Enumeration:
8+
val Hearts, Diamonds, Clubs, Spades = Val()
9+
10+
object Test:
11+
summon[Suit#Value =:= EnumValue[Suit]] // error
12+
end Test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type EnumValueAux[A] = ({ type Value }) { type Value = A }
2+
3+
type EnumValue[E <: Enumeration] = E match
4+
case EnumValueAux[t] => t
5+
6+
object Suit extends Enumeration:
7+
val Hearts, Diamonds, Clubs, Spades = Val()
8+
9+
object Test:
10+
summon[Suit.Value =:= EnumValue[Suit.type]]
11+
end Test

0 commit comments

Comments
 (0)