Skip to content

Commit bff2de6

Browse files
committed
Allow to reduce type member extractors when the member is a class.
1 parent ff710e4 commit bff2de6

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
@@ -3357,23 +3357,33 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
33573357
val info = denot.info match
33583358
case TypeAlias(alias) => alias
33593359
case info => info // Notably, RealTypeBounds, which will eventually give a MatchResult.NoInstances
3360-
if info.isInstanceOf[ClassInfo] then
3361-
/* The member is not an alias (we'll get Stuck instead of NoInstances,
3362-
* which is not ideal, but we cannot make a RealTypeBounds of ClassInfo).
3360+
val infoRefersToSkolem = stableScrut match
3361+
case stableScrut: SkolemType =>
3362+
new TypeAccumulator[Boolean] {
3363+
def apply(prev: Boolean, tp: Type): Boolean =
3364+
prev || (tp eq stableScrut) || foldOver(prev, tp)
3365+
}.apply(false, info)
3366+
case _ =>
3367+
false
3368+
if infoRefersToSkolem && info.isInstanceOf[ClassInfo] then
3369+
/* We would like to create a `RealTypeBounds(info, info)` to get a `MatchResult.NoInstances`
3370+
* but that is not allowed for `ClassInfo`. So instead we return `false`, which will result
3371+
* in a `MatchResult.Stuck` instead.
33633372
*/
33643373
false
33653374
else
3366-
val infoRefersToSkolem = stableScrut match
3367-
case stableScrut: SkolemType =>
3368-
new TypeAccumulator[Boolean] {
3369-
def apply(prev: Boolean, tp: Type): Boolean =
3370-
prev || (tp eq stableScrut) || foldOver(prev, tp)
3371-
}.apply(false, info)
3375+
val info1 = info match
3376+
case ClassInfo(prefix, cls, _, _, _) =>
3377+
// Re-select the class from the prefix
3378+
prefix.select(cls)
3379+
case info: TypeBounds =>
3380+
// Will already trigger a MatchResult.NoInstances
3381+
info
3382+
case _ if infoRefersToSkolem =>
3383+
// Explicitly trigger a MatchResult.NoInstances
3384+
RealTypeBounds(info, info)
33723385
case _ =>
3373-
false
3374-
val info1 =
3375-
if infoRefersToSkolem && !info.isInstanceOf[TypeBounds] then RealTypeBounds(info, info) // to trigger a MatchResult.NoInstances
3376-
else info
3386+
info
33773387
rec(capture, info1, variance = 0, scrutIsWidenedAbstract)
33783388
case _ =>
33793389
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)