Skip to content

Commit e3ad970

Browse files
committed
: _* is only valid when the parameter is repeated
Fixes #9749. Fixes #9912.
1 parent 99548bd commit e3ad970

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,8 @@ trait Applications extends Compatibility {
539539
* in the remaining formal parameters.
540540
*/
541541
def addTyped(arg: Arg, formal: Type): List[Type] =
542+
if isVarArg(arg) && !formal.isRepeatedParam then
543+
fail(i"Sequence argument type annotation `: _*` cannot be used here: the corresponding parameter has type $formal is not a repeated parameter type", arg)
542544
addArg(typedArg(arg, formal), formal)
543545
if methodType.isParamDependent && typeOfArg(arg).exists then
544546
// `typeOfArg(arg)` could be missing because the evaluation of `arg` produced type errors

tests/neg/i9749.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class A {
2+
def f(x: Any) = 1
3+
4+
def foo(x: List[String]): Unit = {
5+
f(x: _*) // error
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class JavaLogger {
2+
public void info(String format, Object arg) {}
3+
4+
public void info(String in, Object... args){}
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Test {
2+
val logger = new JavaLogger
3+
def log(): Unit = {
4+
logger.info(
5+
"My {} String {} with multiple args {}",
6+
Array("a", "b", "c"): _*
7+
)
8+
}
9+
}

tests/pos/i9749.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class A {
2+
def f(x: Any) = 1
3+
def f(x: String*) = 1
4+
5+
def foo(x: List[String]): Unit = {
6+
f(x: _*)
7+
}
8+
}

0 commit comments

Comments
 (0)