Skip to content

Commit

Permalink
added check for repeated params in using clause
Browse files Browse the repository at this point in the history
refactor and more tests

Update error message

Co-authored-by: Nicolas Stucki <[email protected]>

fix error code
  • Loading branch information
iusildra committed Jul 12, 2023
1 parent 5982592 commit d36bb52
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
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

0 comments on commit d36bb52

Please sign in to comment.