Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextual varargs parameters #18186

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3334,11 +3334,13 @@ object Parsers {

def checkVarArgsRules(vparams: List[ValDef]): Unit = vparams match {
case Nil =>
case _ :: Nil if !prefix =>
case vparam :: rest =>
vparam.tpt match {
case PostfixOp(_, op) if op.name == tpnme.raw.STAR =>
syntaxError(VarArgsParamMustComeLast(), vparam.tpt.span)
if vparam.mods.isOneOf(GivenOrImplicit) then
syntaxError(VarArgsParamCannotBeGiven(vparam.mods.is(Given)), vparam.tpt.span)
if rest.nonEmpty then
syntaxError(VarArgsParamMustComeLast(), vparam.tpt.span)
case _ =>
}
checkVarArgsRules(rest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case UnimportedAndImportedID // errorNumber: 185
case ImplausiblePatternWarningID // erorNumber: 186
case SynchronizedCallOnBoxedClassID // errorNumber: 187
case VarArgsParamCannotBeGivenID // erorNumber: 188

def errorNumber = ordinal - 1

Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,14 @@ extends SyntaxMsg(VarArgsParamMustComeLastID) {
|"""
}

class VarArgsParamCannotBeGiven(isGiven: Boolean)(using Context)
extends SyntaxMsg(VarArgsParamCannotBeGivenID) {
def msg(using Context) = i"repeated parameters are not allowed in a ${if isGiven then "using" else "implicit"} clause"
def explain(using Context) =
"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."
}


import typer.Typer.BindingPrec

class ConstrProxyShadows(proxy: TermRef, shadowed: Type, shadowedIsApply: Boolean)(using Context)
Expand Down
42 changes: 42 additions & 0 deletions tests/neg/i18090.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- [E188] Syntax Error: tests/neg/i18090.scala:2:18 --------------------------------------------------------------------
2 |def foo(using xs: Int*) = xs // error
| ^^^^
| repeated parameters are not allowed in a using clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:3:27 --------------------------------------------------------------------
3 |def foo5(using d: Int, xs: Int*) = xs // error
| ^^^^
| repeated parameters are not allowed in a using clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:4:22 --------------------------------------------------------------------
4 |def foo2(implicit xs: Int*) = xs // error
| ^^^^
| repeated parameters are not allowed in a implicit clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:5:35 --------------------------------------------------------------------
5 |def foo3(u: Int)(using d: Int, xs: Int*) = xs // error
| ^^^^
| repeated parameters are not allowed in a using clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:6:38 --------------------------------------------------------------------
6 |def foo4(u: Int)(implicit d: Int, xs: Int*) = xs // error
| ^^^^
| repeated parameters are not allowed in a implicit clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:9:20 --------------------------------------------------------------------
9 | def bar(using xs: Float*) = ??? // error
| ^^^^^^
| repeated parameters are not allowed in a using clause
|
| longer explanation available when compiling with `-explain`
-- [E188] Syntax Error: tests/neg/i18090.scala:10:33 -------------------------------------------------------------------
10 | def bar2(using d: Boolean, xs: Float*) = ??? // error
| ^^^^^^
| repeated parameters are not allowed in a using clause
|
| longer explanation available when compiling with `-explain`
10 changes: 10 additions & 0 deletions tests/neg/i18090.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

def foo(using xs: Int*) = xs // error
def foo5(using d: Int, xs: Int*) = xs // error
def foo2(implicit xs: Int*) = xs // error
def foo3(u: Int)(using d: Int, xs: Int*) = xs // error
def foo4(u: Int)(implicit d: Int, xs: Int*) = xs // error

extension (i: Int)
def bar(using xs: Float*) = ??? // error
def bar2(using d: Boolean, xs: Float*) = ??? // error