Skip to content

Commit 6d245fb

Browse files
Improve @varargs error messages
1 parent 3e6f0b1 commit 6d245fb

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
5454
|""".stripMargin,
5555
sym.sourcePos)
5656
else
57-
addVarArgsForwarder(sym, isJavaOverride)
57+
addVarArgsForwarder(sym, isJavaOverride, hasAnnotation)
5858
else if hasAnnotation
5959
report.error("A method without repeated parameters cannot be annotated with @varargs", sym.sourcePos)
6060
end
@@ -260,7 +260,7 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
260260
* The solution is to add a method that converts its argument from `Array[? <: T]` to `Seq[T]` and
261261
* forwards it to the original method.
262262
*/
263-
private def addVarArgsForwarder(original: Symbol, isBridge: Boolean)(using Context): Unit =
263+
private def addVarArgsForwarder(original: Symbol, isBridge: Boolean, hasAnnotation: Boolean)(using Context): Unit =
264264
val owner = original.owner
265265
if !owner.isClass then
266266
report.error("inner methods cannot be annotated with @varargs", original.sourcePos)
@@ -289,7 +289,11 @@ class ElimRepeated extends MiniPhase with InfoTransformer { thisPhase =>
289289
}
290290
conflict match
291291
case Some(conflict) =>
292-
report.error(s"@varargs produces a forwarder method that conflicts with ${conflict.showDcl}", original.sourcePos)
292+
val src =
293+
if hasAnnotation then "@varargs"
294+
else if isBridge then "overriding a java varargs method"
295+
else "@varargs (on overriden method)"
296+
report.error(s"$src produces a forwarder method that conflicts with ${conflict.showDcl}", original.sourcePos)
293297
case None =>
294298
decls.enter(forwarder.enteredAfter(thisPhase))
295299

tests/neg/varargs-annot.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ object Test {
1111
@varargs def v1(a: Int, b: String*) = a + b.length // error
1212
}
1313

14+
trait C {
15+
@varargs def v(i: Int*) = ()
16+
}
17+
18+
class D extends C {
19+
override def v(i: Int*) = () // error
20+
def v(i: Array[Int]) = () // error
21+
}
22+
1423
@varargs def nov(a: Int) = 0 // error: A method without repeated parameters cannot be annotated with @varargs
1524
@varargs def v(a: Int, b: String*) = a + b.length // ok
1625
def v(a: Int, b: String) = a // ok

0 commit comments

Comments
 (0)