Skip to content

Commit 3ae6cc4

Browse files
Fix generic signature of java-compatible varargs
1 parent b527a75 commit 3ae6cc4

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,14 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
227227
/** Translate a repeated type T* to an `Array[? <: Upper]`
228228
* such that it is compatible with java varargs.
229229
*
230-
* If T is not a primitive type, we set `Upper = T & AnyRef`
230+
* When necessary we set `Upper = T & AnyRef`
231231
* to prevent the erasure of `Array[? <: Upper]` to Object,
232232
* which would break the varargs from Java.
233233
*/
234234
private def varargArrayType(tp: Type)(using Context): Type =
235-
val array = tp.translateFromRepeated(toArray = true)
236-
val element = array.elemType.typeSymbol
237-
238-
if element.isPrimitiveValueClass then array
239-
else defn.ArrayOf(TypeBounds.upper(AndType(element.typeRef, defn.AnyRefType)))
235+
val array = tp.translateFromRepeated(toArray = true) // Array[? <: T]
236+
val element = array.elemType.hiBound // T
240237

238+
if element <:< defn.AnyRefType || element.typeSymbol.isPrimitiveValueClass then array
239+
else defn.ArrayOf(TypeBounds.upper(AndType(element, defn.AnyRefType))) // Array[? <: T & AnyRef]
241240
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ object GenericSignatures {
186186
case defn.ArrayOf(elemtp) =>
187187
if (isUnboundedGeneric(elemtp))
188188
jsig(defn.ObjectType)
189-
else {
189+
else
190190
builder.append(ClassfileConstants.ARRAY_TAG)
191-
jsig(elemtp)
192-
}
191+
elemtp match
192+
case TypeBounds(lo, hi) => jsig(hi.widenDealias)
193+
case _ => jsig(elemtp)
193194

194195
case RefOrAppliedType(sym, pre, args) =>
195196
if (sym == defn.PairClass && tp.tupleArity > Definitions.MaxTupleArity)

0 commit comments

Comments
 (0)