Skip to content

Commit cced037

Browse files
Improve @varargs errors and fix some doc
1 parent 3ae6cc4 commit cced037

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
128128
| - there must be a single repeated parameter
129129
| - it must be the last argument in the last parameter list
130130
|""".stripMargin,
131-
tree.sourcePos)
131+
sym.sourcePos)
132132
tree
133133
else
134134
addVarArgsBridge(tree, isOverride)
135135
else
136136
tree
137137
else
138138
if hasAnnotation then
139-
ctx.error("A method without repeated parameters cannot be annotated with @varargs", tree.sourcePos)
139+
ctx.error("A method without repeated parameters cannot be annotated with @varargs", sym.sourcePos)
140140
tree
141141
}
142142

@@ -156,7 +156,8 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
156156
lastp.last.isRepeatedParam
157157
case pt: PolyType =>
158158
isValidJavaVarArgs(pt.resultType)
159-
case _ => false
159+
case _ =>
160+
throw new Exception("Match error in @varargs bridge logic. This should not happen, please open an issue " + tp)
160161

161162

162163
/** Add a Java varargs bridge
@@ -179,22 +180,23 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
179180
// although it's not strictly necessary for overrides
180181
// (but it is for non-overrides)
181182
val flags = ddef.symbol.flags | JavaVarargs
183+
184+
// The java-compatible bridge symbol
182185
val bridge = original.copy(
183186
// non-overrides cannot be synthetic otherwise javac refuses to call them
184187
flags = if javaOverride then flags | Artifact else flags,
185188
info = toJavaVarArgs(ddef.symbol.info)
186-
).enteredAfter(thisPhase).asTerm
189+
).asTerm
187190

188-
val bridgeDenot = bridge.denot
189191
currentClass.info.member(bridge.name).alternatives.find { s =>
190-
s.matches(bridgeDenot) &&
191-
!(s.asSymDenotation.is(JavaDefined) && javaOverride)
192+
s.matches(bridge) &&
193+
!(javaOverride && s.asSymDenotation.is(JavaDefined))
192194
} match
193195
case Some(conflict) =>
194-
ctx.error(s"@varargs produces a forwarder method that conflicts with ${conflict.showDcl}", ddef.sourcePos)
195-
EmptyTree
196+
ctx.error(s"@varargs produces a forwarder method that conflicts with ${conflict.showDcl}", original.sourcePos)
197+
ddef
196198
case None =>
197-
val bridgeDef = polyDefDef(bridge, trefs => vrefss => {
199+
val bridgeDef = polyDefDef(bridge.enteredAfter(thisPhase), trefs => vrefss => {
198200
val init :+ (last :+ vararg) = vrefss
199201
// Can't call `.argTypes` here because the underlying array type is of the
200202
// form `Array[? <: SomeType]`, so we need `.argInfos` to get the `TypeBounds`.
@@ -213,16 +215,16 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
213215

214216
/** Convert type from Scala to Java varargs method */
215217
private def toJavaVarArgs(tp: Type)(using Context): Type = tp match
216-
case tp: PolyType =>
217-
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(tp.resultType))
218-
case tp: MethodType =>
219-
tp.resultType match
220-
case m: MethodType => // multiple param lists
221-
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(m))
222-
case _ =>
223-
val init :+ last = tp.paramInfos
224-
val vararg = varargArrayType(last)
225-
tp.derivedLambdaType(tp.paramNames, init :+ vararg, tp.resultType)
218+
case tp: PolyType =>
219+
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(tp.resultType))
220+
case tp: MethodType =>
221+
tp.resultType match
222+
case m: MethodType => // multiple param lists
223+
tp.derivedLambdaType(tp.paramNames, tp.paramInfos, toJavaVarArgs(m))
224+
case _ =>
225+
val init :+ last = tp.paramInfos
226+
val vararg = varargArrayType(last)
227+
tp.derivedLambdaType(tp.paramNames, init :+ vararg, tp.resultType)
226228

227229
/** Translate a repeated type T* to an `Array[? <: Upper]`
228230
* such that it is compatible with java varargs.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ object GenericSignatures {
314314
* - If the list contains one or more occurrences of scala.Array with
315315
* type parameters El1, El2, ... then the dominator is scala.Array with
316316
* type parameter of intersectionDominator(List(El1, El2, ...)). <--- @PP: not yet in spec.
317-
* - Otherwise, the list is reduced to a subsequence containing only types
318-
* which are not subtypes of other listed types (the span.)
317+
* - Otherwise, the list is reduced to a subsequence containing only the
318+
* types that are not supertypes of other listed types (the span.)
319319
* - If the span is empty, the dominator is Object.
320320
* - If the span contains a class Tc which is not a trait and which is
321321
* not Object, the dominator is Tc. <--- @PP: "which is not Object" not in spec.

0 commit comments

Comments
 (0)