Skip to content

Normalize tuple types in var args seq literals and classOf instances #23465

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
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ class TypeUtils:
def namedTupleElementTypes(derived: Boolean)(using Context): List[(TermName, Type)] =
namedTupleElementTypesUpTo(Int.MaxValue, derived)

/** If this is a generic tuple type with arity <= MaxTupleArity, return the
* corresponding TupleN type, otherwise return this.
*/
def normalizedTupleType(using Context): Type =
if self.isGenericTuple then
self.tupleElementTypes match
case Some(elems) if elems.size <= Definitions.MaxTupleArity => defn.tupleType(elems)
case _ => self
else
self

def isNamedTupleType(using Context): Boolean = self match
case defn.NamedTuple(_, _) => true
case _ => false
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ trait Applications extends Compatibility {
def makeVarArg(n: Int, elemFormal: Type): Unit = {
val args = typedArgBuf.takeRight(n).toList
typedArgBuf.dropRightInPlace(n)
val elemtpt = TypeTree(elemFormal, inferred = true)
val elemtpt = TypeTree(elemFormal.normalizedTupleType, inferred = true)
typedArgBuf += seqToRepeated(SeqLiteral(args, elemtpt))
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
if defn.SpecialClassTagClasses.contains(sym) then
classTagModul.select(sym.name.toTermName).withSpan(span)
else
val ctype = escapeJavaArray(erasure(tp))
val ctype = escapeJavaArray(erasure(tp.normalizedTupleType))
if ctype.exists then
classTagModul.select(nme.apply)
.appliedToType(tp)
Expand Down
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -847,10 +847,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// Otherwise, map combinations of A *: B *: .... EmptyTuple with nesting levels <= 22
// to the Tuple class of the right arity and select from that one
def trySmallGenericTuple(qual: Tree, withCast: Boolean) =
if qual.tpe.isSmallGenericTuple then
val tp = qual.tpe.widenTermRefExpr
val tpNormalized = tp.normalizedTupleType
if tp ne tpNormalized then
if withCast then
val elems = qual.tpe.widenTermRefExpr.tupleElementTypes.getOrElse(Nil)
typedSelectWithAdapt(tree, pt, qual.cast(defn.tupleType(elems)))
typedSelectWithAdapt(tree, pt, qual.cast(tpNormalized))
else
typedSelectWithAdapt(tree, pt, qual)
else EmptyTree
Expand Down
2 changes: 2 additions & 0 deletions tests/run/i22345.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def Test: Unit =
val a: Array[(Int, String)] = Array[Int *: String *: EmptyTuple]()
2 changes: 2 additions & 0 deletions tests/run/i22345b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def Test: Unit =
val a: Array[(Int, String)] = Array[Int *: String *: EmptyTuple]((1, "hello"))
4 changes: 4 additions & 0 deletions tests/run/i22345c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def makeSeq[T](args: T*): Seq[T] = args

@main def Test: Unit =
val a: Array[(Int, String)] = makeSeq[Int *: String *: EmptyTuple]().toArray
Loading