Skip to content

Support Mirror for generic tuples arity > 22 #23363

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/StdNames.scala
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ object StdNames {
val This: N = "This"
val ThisType: N = "ThisType"
val Tuple2: N = "Tuple2"
val Tuple: N = "Tuple"
val TYPE_ : N = "TYPE"
val TypeApply: N = "TypeApply"
val TypeRef: N = "TypeRef"
Expand Down
22 changes: 13 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,24 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
makeProductMirror(typeElems, elemLabels, tpnme.NamedTuple, mirrorRef)
end makeNamedTupleProductMirror

def makeTupleProductMirror(tps: List[Type]): TreeWithErrors =
val arity = tps.size
if arity <= Definitions.MaxTupleArity then
val tupleCls = defn.TupleType(arity).nn.classSymbol
makeClassProductMirror(tupleCls.owner.reachableThisType, tupleCls, Some(tps))
else
val elemLabels = (for i <- 1 to arity yield ConstantType(Constant(s"_$i"))).toList
val mirrorRef: Type => Tree = _ => newTupleMirror(arity)
makeProductMirror(tps, elemLabels, tpnme.Tuple, mirrorRef)
end makeTupleProductMirror

def makeClassProductMirror(pre: Type, cls: Symbol, tps: Option[List[Type]]) =
val accessors = cls.caseAccessors
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
val typeElems = tps.getOrElse(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
val mirrorRef = (monoType: Type) =>
if cls.useCompanionAsProductMirror then companionPath(pre, cls, span)
else if defn.isTupleClass(cls) then newTupleMirror(typeElems.size) // TODO: cls == defn.PairClass when > 22
else if defn.isTupleClass(cls) then newTupleMirror(typeElems.size)
else anonymousMirror(monoType, MirrorImpl.OfProduct(pre), span)
makeProductMirror(typeElems, elemLabels, cls.name, mirrorRef)
end makeClassProductMirror
Expand Down Expand Up @@ -478,14 +489,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
}
withNoErrors(singletonPath.cast(mirrorType).withSpan(span))
case MirrorSource.GenericTuple(tps) =>
val maxArity = Definitions.MaxTupleArity
val arity = tps.size
if tps.size <= maxArity then
val tupleCls = defn.TupleType(arity).nn.classSymbol
makeClassProductMirror(tupleCls.owner.reachableThisType, tupleCls, Some(tps))
else
val reason = s"it reduces to a tuple with arity $arity, expected arity <= $maxArity"
withErrors(i"${defn.PairClass} is not a generic product because $reason")
makeTupleProductMirror(tps)
case MirrorSource.NamedTuple(nameTypePairs) =>
makeNamedTupleProductMirror(nameTypePairs)
case MirrorSource.ClassSymbol(pre, cls) =>
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/pos/i15398.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object i15398 {
type Tuple23 = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)

summon[Tuple.Size[Tuple23] =:= 23]
val m = summon[scala.deriving.Mirror.Of[Tuple23]]
summon[m.MirroredLabel =:= "Tuple"]
summon[m.MirroredElemTypes =:= Tuple23]
summon[m.MirroredElemLabels =:= ("_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_9", "_10", "_11", "_12", "_13", "_14", "_15", "_16", "_17", "_18", "_19", "_20", "_21", "_22", "_23")]
}
Loading