Skip to content

Commit eb01981

Browse files
committed
Fix varargs
1 parent 45f56cb commit eb01981

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,22 @@ object JavaNullInterop {
100100

101101
/** Should we nullify `tp` at the outermost level? */
102102
def needsNull(tp: Type): Boolean =
103-
!outermostLevelAlreadyNullable && (tp match {
103+
!(outermostLevelAlreadyNullable || (tp match {
104104
case tp: TypeRef =>
105105
// We don't modify value types because they're non-nullable even in Java.
106-
!tp.symbol.isValueClass &&
106+
tp.symbol.isValueClass
107+
// We don't modify unit types.
108+
|| tp.isRef(defn.UnitClass)
107109
// We don't modify `Any` because it's already nullable.
108-
!tp.isRef(defn.AnyClass) &&
110+
|| tp.isRef(defn.AnyClass)
109111
// We don't nullify Java varargs at the top level.
110112
// Example: if `setNames` is a Java method with signature `void setNames(String... names)`,
111113
// then its Scala signature will be `def setNames(names: (String|Null)*): Unit`.
112114
// This is because `setNames(null)` passes as argument a single-element array containing the value `null`,
113115
// and not a `null` array.
114-
!tp.isRef(defn.RepeatedParamClass)
116+
|| !ctx.flexibleTypes && tp.isRef(defn.RepeatedParamClass)
115117
case _ => true
116-
})
118+
}))
117119

118120
override def apply(tp: Type): Type = tp match {
119121
case tp: TypeRef if needsNull(tp) => nullify(tp)

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -970,13 +970,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
970970
// A sequence argument `xs: _*` can be either a `Seq[T]` or an `Array[_ <: T]`,
971971
// irrespective of whether the method we're calling is a Java or Scala method,
972972
// so the expected type is the union `Seq[T] | Array[_ <: T]`.
973-
val ptArg =
973+
val pt1 = pt.stripFlexible
974+
val ptArg0 =
974975
// FIXME(#8680): Quoted patterns do not support Array repeated arguments
975976
if ctx.mode.isQuotedPattern then
976-
pt.translateFromRepeated(toArray = false, translateWildcard = true)
977+
pt1.translateFromRepeated(toArray = false, translateWildcard = true)
977978
else
978-
pt.translateFromRepeated(toArray = false, translateWildcard = true)
979-
| pt.translateFromRepeated(toArray = true, translateWildcard = true)
979+
pt1.translateFromRepeated(toArray = false, translateWildcard = true)
980+
| pt1.translateFromRepeated(toArray = true, translateWildcard = true)
981+
val ptArg = if pt1 eq pt then ptArg0 else FlexibleType(ptArg0)
980982
val expr0 = typedExpr(tree.expr, ptArg)
981983
val expr1 = if ctx.explicitNulls && (!ctx.mode.is(Mode.Pattern)) then
982984
if expr0.tpe.isNullType then

0 commit comments

Comments
 (0)