Skip to content

Commit 1a9c9eb

Browse files
committed
Rename underlyingIfRepeated(isJava) to translateFromRepeated(toArray)
The previous name was slightly misleading: we can pass Array sequence arguments to Scala methods and Seq sequence argument to Java methods, so we may want to translate a repeated parameter type to either an Array or a Seq irrespective of what it erases to (we don't right now but we will in the next commit).
1 parent f26808e commit 1a9c9eb

File tree

8 files changed

+20
-20
lines changed

8 files changed

+20
-20
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,16 @@ class TypeApplications(val self: Type) extends AnyVal {
388388
else self
389389
}
390390

391-
/** If this is repeated parameter type, its underlying Seq type,
392-
* or, if isJava is true, Array type, else the type itself.
391+
/** If this is a repeated parameter `*T`, translate it to either `Seq[T]` or
392+
* `Array[? <: T]` depending on the value of `toArray`, keep other types as
393+
* they are.
393394
*/
394-
def underlyingIfRepeated(isJava: Boolean)(implicit ctx: Context): Type =
395-
if (self.isRepeatedParam) {
396-
val seqClass = if (isJava) defn.ArrayClass else defn.SeqClass
397-
// If `isJava` is set, then we want to turn `RepeatedParam[T]` into `Array[? <: T]`,
398-
// since arrays aren't covariant until after erasure. See `tests/pos/i5140`.
399-
translateParameterized(defn.RepeatedParamClass, seqClass, wildcardArg = isJava)
400-
}
395+
def translateFromRepeated(toArray: Boolean)(using Context): Type =
396+
if self.isRepeatedParam then
397+
val seqClass = if (toArray) defn.ArrayClass else defn.SeqClass
398+
// We want `Array[? <: T]` because arrays aren't covariant until after
399+
// erasure. See `tests/pos/i5140`.
400+
translateParameterized(defn.RepeatedParamClass, seqClass, wildcardArg = toArray)
401401
else self
402402

403403
/** If this is an encoding of a (partially) applied type, return its arguments,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object TypeErasure {
155155
case etp => etp
156156

157157
def sigName(tp: Type, isJava: Boolean)(implicit ctx: Context): TypeName = {
158-
val normTp = tp.underlyingIfRepeated(isJava)
158+
val normTp = tp.translateFromRepeated(toArray = isJava)
159159
val erase = erasureFn(isJava, semiEraseVCs = false, isConstructor = false, wildcardOK = true)
160160
erase.sigName(normTp)(preErasureCtx)
161161
}
@@ -448,7 +448,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
448448
val tycon = tp.tycon
449449
if (tycon.isRef(defn.ArrayClass)) eraseArray(tp)
450450
else if (tycon.isRef(defn.PairClass)) erasePair(tp)
451-
else if (tp.isRepeatedParam) apply(tp.underlyingIfRepeated(isJava))
451+
else if (tp.isRepeatedParam) apply(tp.translateFromRepeated(toArray = isJava))
452452
else apply(tp.translucentSuperType)
453453
case _: TermRef | _: ThisType =>
454454
this(tp.widen)
@@ -540,7 +540,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
540540
// See doc comment for ElimByName for speculation how we could improve this.
541541
else
542542
MethodType(Nil, Nil,
543-
eraseResult(sym.info.finalResultType.underlyingIfRepeated(isJava)))
543+
eraseResult(sym.info.finalResultType.translateFromRepeated(toArray = isJava)))
544544
case tp1: PolyType =>
545545
eraseResult(tp1.resultType) match
546546
case rt: MethodType => rt

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,7 @@ object Types {
16071607
case res => res
16081608
}
16091609
val funType = defn.FunctionOf(
1610-
formals1 mapConserve (_.underlyingIfRepeated(mt.isJavaMethod)),
1610+
formals1 mapConserve (_.translateFromRepeated(toArray = mt.isJavaMethod)),
16111611
result1, isContextual, isErased)
16121612
if (mt.isResultDependent) RefinedType(funType, nme.apply, mt)
16131613
else funType

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
593593
if (tag == ALIASsym) TypeAlias(tp1)
594594
else if (denot.isType) checkNonCyclic(denot.symbol, tp1, reportErrors = false)
595595
// we need the checkNonCyclic call to insert LazyRefs for F-bounded cycles
596-
else if (!denot.is(Param)) tp1.underlyingIfRepeated(isJava = false)
596+
else if (!denot.is(Param)) tp1.translateFromRepeated(toArray = false)
597597
else tp1
598598
if (denot.isConstructor) addConstructorTypeParams(denot)
599599
if (atEnd)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
5252
val resultType1 = elimRepeated(resultType)
5353
val paramTypes1 =
5454
if (paramTypes.nonEmpty && paramTypes.last.isRepeatedParam) {
55-
val last = paramTypes.last.underlyingIfRepeated(tp.isJavaMethod)
55+
val last = paramTypes.last.translateFromRepeated(toArray = tp.isJavaMethod)
5656
paramTypes.init :+ last
5757
}
5858
else paramTypes
@@ -159,7 +159,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
159159
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(tp.resultType))
160160
case tp: MethodType =>
161161
val inits :+ last = tp.paramInfos
162-
val last1 = last.underlyingIfRepeated(isJava = true)
162+
val last1 = last.translateFromRepeated(toArray = true)
163163
tp.derivedLambdaType(tp.paramNames, inits :+ last1, tp.resultType)
164164
}
165165
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class SyntheticMembers(thisPhase: DenotTransformer) {
420420
for ((formal, idx) <- methTpe.paramInfos.zipWithIndex) yield {
421421
val elem =
422422
param.select(defn.Product_productElement).appliedTo(Literal(Constant(idx)))
423-
.ensureConforms(formal.underlyingIfRepeated(isJava = false))
423+
.ensureConforms(formal.translateFromRepeated(toArray = false))
424424
if (formal.isRepeatedParam) ctx.typer.seqToRepeated(elem) else elem
425425
}
426426
New(classRef, elems)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ trait QuotesAndSplices {
207207
try ref(defn.InternalQuoted_patternHole.termRef).appliedToType(tree.tpe).withSpan(tree.span)
208208
finally {
209209
val patType = pat.tpe.widen
210-
val patType1 = patType.underlyingIfRepeated(isJava = false)
210+
val patType1 = patType.translateFromRepeated(toArray = false)
211211
val pat1 = if (patType eq patType1) pat else pat.withType(patType1)
212212
patBuf += pat1
213213
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ class Typer extends Namer
729729
if (untpd.isWildcardStarArg(tree)) {
730730
def typedWildcardStarArgExpr = {
731731
val ptArg =
732-
if (ctx.mode.is(Mode.QuotedPattern)) pt.underlyingIfRepeated(isJava = false)
732+
if (ctx.mode.is(Mode.QuotedPattern)) pt.translateFromRepeated(toArray = false)
733733
else WildcardType
734734
val tpdExpr = typedExpr(tree.expr, ptArg)
735735
tpdExpr.tpe.widenDealias match {
@@ -1158,7 +1158,7 @@ class Typer extends Namer
11581158
if (!param.tpt.isEmpty) param
11591159
else cpy.ValDef(param)(
11601160
tpt = untpd.TypeTree(
1161-
inferredParamType(param, protoFormal(i)).underlyingIfRepeated(isJava = false)))
1161+
inferredParamType(param, protoFormal(i)).translateFromRepeated(toArray = false)))
11621162
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual)
11631163
}
11641164
typed(desugared, pt)

0 commit comments

Comments
 (0)