Skip to content

Commit bc2527e

Browse files
Contextual varargs parameters (#18186)
fixes #18090
2 parents f1ca099 + d36bb52 commit bc2527e

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,11 +3334,13 @@ object Parsers {
33343334

33353335
def checkVarArgsRules(vparams: List[ValDef]): Unit = vparams match {
33363336
case Nil =>
3337-
case _ :: Nil if !prefix =>
33383337
case vparam :: rest =>
33393338
vparam.tpt match {
33403339
case PostfixOp(_, op) if op.name == tpnme.raw.STAR =>
3341-
syntaxError(VarArgsParamMustComeLast(), vparam.tpt.span)
3340+
if vparam.mods.isOneOf(GivenOrImplicit) then
3341+
syntaxError(VarArgsParamCannotBeGiven(vparam.mods.is(Given)), vparam.tpt.span)
3342+
if rest.nonEmpty then
3343+
syntaxError(VarArgsParamMustComeLast(), vparam.tpt.span)
33423344
case _ =>
33433345
}
33443346
checkVarArgsRules(rest)

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
201201
case UnimportedAndImportedID // errorNumber: 185
202202
case ImplausiblePatternWarningID // erorNumber: 186
203203
case SynchronizedCallOnBoxedClassID // errorNumber: 187
204+
case VarArgsParamCannotBeGivenID // erorNumber: 188
204205

205206
def errorNumber = ordinal - 1
206207

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,14 @@ extends SyntaxMsg(VarArgsParamMustComeLastID) {
13191319
|"""
13201320
}
13211321

1322+
class VarArgsParamCannotBeGiven(isGiven: Boolean)(using Context)
1323+
extends SyntaxMsg(VarArgsParamCannotBeGivenID) {
1324+
def msg(using Context) = i"repeated parameters are not allowed in a ${if isGiven then "using" else "implicit"} clause"
1325+
def explain(using Context) =
1326+
"It is not possible to define a given with a repeated parameter type. This hypothetical given parameter could always be satisfied by providing 0 arguments, which defeats the purpose of a given argument."
1327+
}
1328+
1329+
13221330
import typer.Typer.BindingPrec
13231331

13241332
class ConstrProxyShadows(proxy: TermRef, shadowed: Type, shadowedIsApply: Boolean)(using Context)

tests/neg/i18090.check

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
-- [E188] Syntax Error: tests/neg/i18090.scala:2:18 --------------------------------------------------------------------
2+
2 |def foo(using xs: Int*) = xs // error
3+
| ^^^^
4+
| repeated parameters are not allowed in a using clause
5+
|
6+
| longer explanation available when compiling with `-explain`
7+
-- [E188] Syntax Error: tests/neg/i18090.scala:3:27 --------------------------------------------------------------------
8+
3 |def foo5(using d: Int, xs: Int*) = xs // error
9+
| ^^^^
10+
| repeated parameters are not allowed in a using clause
11+
|
12+
| longer explanation available when compiling with `-explain`
13+
-- [E188] Syntax Error: tests/neg/i18090.scala:4:22 --------------------------------------------------------------------
14+
4 |def foo2(implicit xs: Int*) = xs // error
15+
| ^^^^
16+
| repeated parameters are not allowed in a implicit clause
17+
|
18+
| longer explanation available when compiling with `-explain`
19+
-- [E188] Syntax Error: tests/neg/i18090.scala:5:35 --------------------------------------------------------------------
20+
5 |def foo3(u: Int)(using d: Int, xs: Int*) = xs // error
21+
| ^^^^
22+
| repeated parameters are not allowed in a using clause
23+
|
24+
| longer explanation available when compiling with `-explain`
25+
-- [E188] Syntax Error: tests/neg/i18090.scala:6:38 --------------------------------------------------------------------
26+
6 |def foo4(u: Int)(implicit d: Int, xs: Int*) = xs // error
27+
| ^^^^
28+
| repeated parameters are not allowed in a implicit clause
29+
|
30+
| longer explanation available when compiling with `-explain`
31+
-- [E188] Syntax Error: tests/neg/i18090.scala:9:20 --------------------------------------------------------------------
32+
9 | def bar(using xs: Float*) = ??? // error
33+
| ^^^^^^
34+
| repeated parameters are not allowed in a using clause
35+
|
36+
| longer explanation available when compiling with `-explain`
37+
-- [E188] Syntax Error: tests/neg/i18090.scala:10:33 -------------------------------------------------------------------
38+
10 | def bar2(using d: Boolean, xs: Float*) = ??? // error
39+
| ^^^^^^
40+
| repeated parameters are not allowed in a using clause
41+
|
42+
| longer explanation available when compiling with `-explain`

tests/neg/i18090.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
def foo(using xs: Int*) = xs // error
3+
def foo5(using d: Int, xs: Int*) = xs // error
4+
def foo2(implicit xs: Int*) = xs // error
5+
def foo3(u: Int)(using d: Int, xs: Int*) = xs // error
6+
def foo4(u: Int)(implicit d: Int, xs: Int*) = xs // error
7+
8+
extension (i: Int)
9+
def bar(using xs: Float*) = ??? // error
10+
def bar2(using d: Boolean, xs: Float*) = ??? // error

0 commit comments

Comments
 (0)