Skip to content

Fix tuple member selection so it works with GADT healing #16766

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

Merged
merged 1 commit into from
Jan 26, 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
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1675,6 +1675,15 @@ class Definitions {
rec(tp.stripTypeVar, Nil, bound)
}

def isSmallGenericTuple(tp: Type)(using Context): Boolean =
if tp.derivesFrom(defn.PairClass) && !defn.isTupleNType(tp.widenDealias) then
// If this is a generic tuple we need to cast it to make the TupleN/ members accessible.
// This works only for generic tuples of known size up to 22.
defn.tupleTypes(tp.widenTermRefExpr) match
case Some(elems) if elems.length <= Definitions.MaxTupleArity => true
case _ => false
else false

def isProductSubType(tp: Type)(using Context): Boolean = tp.derivesFrom(ProductClass)

/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ContextFunctionN
Expand Down
17 changes: 8 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// There's a second trial where we try to instantiate all type variables in `qual.tpe.widen`,
// but that is done only after we search for extension methods or conversions.
typedSelect(tree, pt, qual)
else if defn.isSmallGenericTuple(qual.tpe) then
val elems = defn.tupleTypes(qual.tpe.widenTermRefExpr).getOrElse(Nil)
typedSelect(tree, pt, qual.cast(defn.tupleType(elems)))
else
val tree1 = tryExtensionOrConversion(
tree, pt, IgnoredProto(pt), qual, ctx.typerState.ownedVars, this, inSelect = true)
Expand All @@ -654,6 +657,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if checkedType1.exists then
gadts.println(i"Member selection healed by GADT approximation")
finish(tree1, qual1, checkedType1)
else if defn.isSmallGenericTuple(qual1.tpe) then
gadts.println(i"Tuple member selection healed by GADT approximation")
typedSelect(tree, pt, qual1)
else
tryExtensionOrConversion(tree1, pt, IgnoredProto(pt), qual1, ctx.typerState.ownedVars, this, inSelect = true)
else EmptyTree
Expand Down Expand Up @@ -3998,15 +4004,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
else err.typeMismatch(tree, pt, failure)

pt match
case pt: SelectionProto =>
if tree.tpe.derivesFrom(defn.PairClass) && !defn.isTupleNType(tree.tpe.widenDealias) then
// If this is a generic tuple we need to cast it to make the TupleN/ members accessible.
// This works only for generic tuples of known size up to 22.
defn.tupleTypes(tree.tpe.widenTermRefExpr) match
case Some(elems) if elems.length <= Definitions.MaxTupleArity =>
tree.cast(defn.tupleType(elems))
case _ => tree
else tree // other adaptations for selections are handled in typedSelect
case _: SelectionProto =>
tree // adaptations for selections are handled in typedSelect
case _ if ctx.mode.is(Mode.ImplicitsEnabled) && tree.tpe.isValueType =>
if pt.isRef(defn.AnyValClass, skipRefined = false)
|| pt.isRef(defn.ObjectClass, skipRefined = false)
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/i16590.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum Tag[A]:
case MyTuple extends Tag[(String, String)]

def printIt[A](t: Tag[A], a: A): Unit =
t match
case Tag.MyTuple => println(a._1)

enum Tag2[A]:
case MyTuple extends Tag2[String *: String *: EmptyTuple]

def printIt2[A](t: Tag2[A], a: A): Unit =
t match
case Tag2.MyTuple => println(a._1)