Skip to content

Commit b2cd869

Browse files
authored
Properly dealias tuple types when specializing (#18724)
2 parents db39e36 + 22b273f commit b2cd869

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

compiler/src/dotty/tools/dotc/transform/SpecializeTuples.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ class SpecializeTuples extends MiniPhase:
3737
end transformApply
3838

3939
override def transformSelect(tree: Select)(using Context): Tree = tree match
40-
case Select(qual, nme._1) if isAppliedSpecializableTuple(qual.tpe.widen) =>
41-
Select(qual, nme._1.specializedName(qual.tpe.widen.argInfos.slice(0, 1)))
42-
case Select(qual, nme._2) if isAppliedSpecializableTuple(qual.tpe.widen) =>
43-
Select(qual, nme._2.specializedName(qual.tpe.widen.argInfos.slice(1, 2)))
40+
case Select(qual, name @ (nme._1 | nme._2)) =>
41+
qual.tpe.widenDealias match
42+
case AppliedType(tycon, args) if defn.isSpecializableTuple(tycon.classSymbol, args) =>
43+
val argIdx = if name == nme._1 then 0 else 1
44+
Select(qual, name.specializedName(args(argIdx) :: Nil))
45+
case _ =>
46+
tree
4447
case _ => tree
45-
46-
private def isAppliedSpecializableTuple(tp: Type)(using Context) = tp match
47-
case AppliedType(tycon, args) => defn.isSpecializableTuple(tycon.classSymbol, args)
48-
case _ => false
4948
end SpecializeTuples
5049

5150
object SpecializeTuples:

tests/run/i18638.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type U[H, T] = (Unit, Unit)
2+
object O:
3+
opaque type U[H, T] <: (Unit, Unit) = (Unit, Unit)
4+
def u: U[Int, Int] = ((), ())
5+
6+
7+
def test1(u: (Unit, Unit)) = u._1
8+
def test2(u: U[Int, Int]) = u._1
9+
def test3(u: O.U[Int, Int]) = u._1
10+
def test4() =
11+
(((), ()): U[Int, Int]) match
12+
case ((), ()) => println("ok")
13+
14+
@main def Test: Unit =
15+
test1(((), ()))
16+
test2(((), ()))
17+
test3(O.u)
18+
test4()

0 commit comments

Comments
 (0)