Skip to content

Commit 7fa9ca9

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

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import util.Stats.record
99
import util.{SrcPos, NoSourcePosition, SourceFile}
1010
import Trees.Untyped
1111
import Contexts._
12+
import Phases._
1213
import Flags._
1314
import Symbols._
1415
import Denotations.Denotation
@@ -539,6 +540,8 @@ trait Applications extends Compatibility {
539540
* in the remaining formal parameters.
540541
*/
541542
def addTyped(arg: Arg, formal: Type): List[Type] =
543+
if (!elimRepeatedPhase.exists || ctx.phase <= elimRepeatedPhase) && isVarArg(arg) && !formal.isRepeatedParam then
544+
fail(i"Sequence argument type annotation `: _*` cannot be used here: the corresponding parameter has type $formal which is not a repeated parameter type", arg)
542545
addArg(typedArg(arg, formal), formal)
543546
if methodType.isParamDependent && typeOfArg(arg).exists then
544547
// `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+
}

tests/pos/test-desugar.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ object desugar {
7676
val pair: Int ~ String = 1 -> "abc"
7777
def foo(xs: Int*) = xs.length
7878
foo(list: _*)
79-
println(list: _*)
8079
(list length)
8180
- desugar.x
8281
def bar(x: => Int) = x

0 commit comments

Comments
 (0)