Skip to content

Commit 11e03a0

Browse files
committed
Allow to reduce type member extractors when the member is a class.
1 parent b14ae99 commit 11e03a0

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,16 +3365,26 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
33653365
val info = denot.info match
33663366
case TypeAlias(alias) => alias
33673367
case info => info // Notably, RealTypeBounds, which will eventually give a MatchResult.NoInstances
3368-
if info.isInstanceOf[ClassInfo] then
3369-
/* The member is not an alias (we'll get Stuck instead of NoInstances,
3370-
* which is not ideal, but we cannot make a RealTypeBounds of ClassInfo).
3368+
val infoRefersToSkolem = stableScrut.isInstanceOf[SkolemType] && stableScrut.occursIn(info)
3369+
if infoRefersToSkolem && info.isInstanceOf[ClassInfo] then
3370+
/* We would like to create a `RealTypeBounds(info, info)` to get a `MatchResult.NoInstances`
3371+
* but that is not allowed for `ClassInfo`. So instead we return `false`, which will result
3372+
* in a `MatchResult.Stuck` instead.
33713373
*/
33723374
false
33733375
else
3374-
val infoRefersToSkolem = stableScrut.isInstanceOf[SkolemType] && stableScrut.occursIn(info)
3375-
val info1 =
3376-
if infoRefersToSkolem && !info.isInstanceOf[TypeBounds] then RealTypeBounds(info, info) // to trigger a MatchResult.NoInstances
3377-
else info
3376+
val info1 = info match
3377+
case ClassInfo(prefix, cls, _, _, _) =>
3378+
// Re-select the class from the prefix
3379+
prefix.select(cls)
3380+
case info: TypeBounds =>
3381+
// Will already trigger a MatchResult.NoInstances
3382+
info
3383+
case _ if infoRefersToSkolem =>
3384+
// Explicitly trigger a MatchResult.NoInstances
3385+
RealTypeBounds(info, info)
3386+
case _ =>
3387+
info
33783388
rec(capture, info1, variance = 0, scrutIsWidenedAbstract)
33793389
case _ =>
33803390
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)