Skip to content

Commit cf184a0

Browse files
committed
Correctly specify argument type when in Java varargs bridge
1 parent 0aa702f commit cf184a0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
125125
* @return a thicket consisting of `ddef` and a varargs bridge method
126126
* which overrides the Java varargs method JM from this phase on
127127
* and forwards to `ddef`.
128+
*
129+
* A bridge is necessary because the following hold
130+
* - the varargs in `ddef` will change from `RepeatedParam[T]` to `Seq[T]` after this phase
131+
* - _but_ the callers of `ddef` expect its varargs to be changed to `Array[_ <: T]`, since it overrides
132+
* a Java varargs
133+
* The solution is to add a "bridge" method that converts its argument from `Array[_ <: T]` to `Seq[T]` and
134+
* forwards it to `ddef`.
128135
*/
129136
private def addVarArgsBridge(ddef: DefDef)(implicit ctx: Context): Tree = {
130137
val original = ddef.symbol.asTerm
@@ -133,7 +140,9 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
133140
info = toJavaVarArgs(ddef.symbol.info)).enteredAfter(thisPhase).asTerm
134141
val bridgeDef = polyDefDef(bridge, trefs => vrefss => {
135142
val (vrefs :+ varArgRef) :: vrefss1 = vrefss
136-
val elemtp = varArgRef.tpe.widen.argTypes.head
143+
// Can't call `.argTypes` here because the underlying array type is of the
144+
// form `Array[_ <: SomeType]`, so we need `.argInfos` to get the `TypeBounds`.
145+
val elemtp = varArgRef.tpe.widen.argInfos.head
137146
ref(original.termRef)
138147
.appliedToTypes(trefs)
139148
.appliedToArgs(vrefs :+ tpd.wrapArray(varArgRef, elemtp))

0 commit comments

Comments
 (0)