Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly dealias tuple types when specializing #18724

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/SpecializeTuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ class SpecializeTuples extends MiniPhase:
end transformApply

override def transformSelect(tree: Select)(using Context): Tree = tree match
case Select(qual, nme._1) if isAppliedSpecializableTuple(qual.tpe.widen) =>
Select(qual, nme._1.specializedName(qual.tpe.widen.argInfos.slice(0, 1)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was that qual.tpe.widen returned U[Int, Int], isAppliedSpecializableTuple detected the tuple type through internal aliasing, but qual.tpe.widen.argInfos used the args of U[Int, Int] instead of the args of its dealiased type Tuple2[Unit, Unit]

case Select(qual, nme._2) if isAppliedSpecializableTuple(qual.tpe.widen) =>
Select(qual, nme._2.specializedName(qual.tpe.widen.argInfos.slice(1, 2)))
case Select(qual, name @ (nme._1 | nme._2)) =>
qual.tpe.widenDealias match
case AppliedType(tycon, args) if defn.isSpecializableTuple(tycon.classSymbol, args) =>
val argIdx = if name == nme._1 then 0 else 1
Select(qual, name.specializedName(args(argIdx) :: Nil))
case _ =>
tree
case _ => tree

private def isAppliedSpecializableTuple(tp: Type)(using Context) = tp match
case AppliedType(tycon, args) => defn.isSpecializableTuple(tycon.classSymbol, args)
case _ => false
end SpecializeTuples

object SpecializeTuples:
Expand Down
18 changes: 18 additions & 0 deletions tests/run/i18638.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type U[H, T] = (Unit, Unit)
object O:
opaque type U[H, T] <: (Unit, Unit) = (Unit, Unit)
def u: U[Int, Int] = ((), ())


def test1(u: (Unit, Unit)) = u._1
def test2(u: U[Int, Int]) = u._1
def test3(u: O.U[Int, Int]) = u._1
def test4() =
(((), ()): U[Int, Int]) match
case ((), ()) => println("ok")

@main def Test: Unit =
test1(((), ()))
test2(((), ()))
test3(O.u)
test4()
Loading