Skip to content

Commit 82fb3d2

Browse files
committed
Refactor methods for converting to repeated
- For trees, get rid of arrayToRepeated/seqToRepeated in favor of a toRepeated. - For types, add a translateToRepeated. - In typedTyped, create the Typed node manually to preserve the span of the original type ascription as well as potential tree attachments.
1 parent b5c3920 commit 82fb3d2

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
13521352
* Used to make arguments for methods that accept varargs.
13531353
*/
13541354
def repeated(trees: List[Tree], tpt: Tree)(using Context): Tree =
1355-
ctx.typeAssigner.arrayToRepeated(JavaSeqLiteral(trees, tpt))
1355+
ctx.typeAssigner.toRepeated(JavaSeqLiteral(trees, tpt))
13561356

13571357
/** Create a tree representing a list containing all
13581358
* the elements of the argument list. A "list of tree to

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,14 @@ class TypeApplications(val self: Type) extends AnyVal {
402402
translateParameterized(defn.RepeatedParamClass, seqClass, wildcardArg = toArray)
403403
else self
404404

405+
/** Translate a `Seq[T]` or `Array[T]` into a `*T`, keep other types as is. */
406+
def translateToRepeated(using Context): Type =
407+
val from = self.widenDealias.classSymbol
408+
if (from eq defn.SeqClass) || (from eq defn.ArrayClass)
409+
translateParameterized(from.asClass, defn.RepeatedParamClass)
410+
else
411+
self
412+
405413
/** If this is an encoding of a (partially) applied type, return its arguments,
406414
* otherwise return Nil.
407415
* Existential types in arguments are returned as TypeBounds instances.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
421421
val elem =
422422
param.select(defn.Product_productElement).appliedTo(Literal(Constant(idx)))
423423
.ensureConforms(formal.translateFromRepeated(toArray = false))
424-
if (formal.isRepeatedParam) ctx.typer.seqToRepeated(elem) else elem
424+
if (formal.isRepeatedParam) ctx.typeAssigner.toRepeated(elem) else elem
425425
}
426426
New(classRef, elems)
427427
}

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ trait Applications extends Compatibility {
697697
val args = typedArgBuf.takeRight(n).toList
698698
typedArgBuf.trimEnd(n)
699699
val elemtpt = TypeTree(elemFormal)
700-
typedArgBuf += seqToRepeated(SeqLiteral(args, elemtpt))
700+
typedArgBuf += ctx.typeAssigner.toRepeated(SeqLiteral(args, elemtpt))
701701
}
702702

703703
def harmonizeArgs(args: List[TypedArg]): List[Tree] = harmonize(args)

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ trait TypeAssigner {
165165
then checkNoPrivateLeaks(sym)
166166
else sym.info
167167

168-
private def toRepeated(tree: Tree, from: ClassSymbol)(using Context): Tree =
169-
Typed(tree, TypeTree(tree.tpe.widen.translateParameterized(from, defn.RepeatedParamClass)))
170-
171-
def seqToRepeated(tree: Tree)(using Context): Tree = toRepeated(tree, defn.SeqClass)
172-
173-
def arrayToRepeated(tree: Tree)(using Context): Tree = toRepeated(tree, defn.ArrayClass)
168+
def toRepeated(tree: Tree)(using Context): Tree =
169+
Typed(tree, TypeTree(tree.tpe.translateToRepeated))
174170

175171
/** A denotation exists really if it exists and does not point to a stale symbol. */
176172
final def reallyExists(denot: Denotation)(using Context): Boolean = try

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,9 @@ class Typer extends Namer
733733
// FIXME(#8680): Quoted patterns do not support Array repeated arguments
734734
if (ctx.mode.is(Mode.QuotedPattern)) pt.translateFromRepeated(toArray = false)
735735
else pt.translateFromRepeated(toArray = false) | pt.translateFromRepeated(toArray = true)
736-
val tpdExpr = typedExpr(tree.expr, ptArg)
737-
tpdExpr.tpe.widenDealias match {
738-
case defn.ArrayOf(_) =>
739-
arrayToRepeated(tpdExpr)
740-
case _ =>
741-
seqToRepeated(tpdExpr)
742-
}
736+
val expr1 = typedExpr(tree.expr, ptArg)
737+
val tpt1 = TypeTree(expr1.tpe.translateToRepeated).withSpan(tree.tpt.span)
738+
assignType(cpy.Typed(tree)(expr1, tpt1), tpt1)
743739
}
744740
cases(
745741
ifPat = ascription(TypeTree(defn.RepeatedParamType.appliedTo(pt)), isWildcard = true),

0 commit comments

Comments
 (0)