diff options
Diffstat (limited to 'compiler/src/dotty/tools')
-rw-r--r-- | compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 22 | ||||
-rw-r--r-- | compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala | 12 |
2 files changed, 33 insertions, 1 deletions
diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 65c7a290d..b644c94cc 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -12,7 +12,7 @@ import core._ import Flags._ import Contexts._ import Names._ -import ast.Positioned +import ast.{Positioned, Trees, untpd} import ast.Trees._ import Decorators._ import StdNames._ @@ -20,6 +20,7 @@ import util.Positions._ import Constants._ import ScriptParsers._ import Comments._ + import scala.annotation.{tailrec, switch} import util.DotClass import rewrite.Rewrites.patch @@ -1800,6 +1801,10 @@ object Parsers { case _ => syntaxError(AuxConstructorNeedsNonImplicitParameter(), start) } } + val listOfErrors = checkVarArgsRules(result) + listOfErrors.foreach { vparam => + syntaxError(VarArgsParamMustComeLast(), vparam.tpt.pos) + } result } @@ -1921,6 +1926,21 @@ object Parsers { } } + + + private def checkVarArgsRules(vparamss: List[List[untpd.ValDef]]): List[untpd.ValDef] = { + def isVarArgs(tpt: Trees.Tree[Untyped]): Boolean = tpt match { + case PostfixOp(_, op) if op.name == nme.raw.STAR => true + case _ => false + } + + vparamss.flatMap { params => + if (params.nonEmpty) { + params.init.filter(valDef => isVarArgs(valDef.tpt)) + } else List() + } + } + /** DefDef ::= DefSig (`:' Type [`=' Expr] | "=" Expr) * | this ParamClause ParamClauses `=' ConstrExpr * DefDcl ::= DefSig `:' Type diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 6fa056646..57365658e 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1209,4 +1209,16 @@ object messages { |${parents.mkString(" - ", "\n - ", "")} |""".stripMargin } + + case class VarArgsParamMustComeLast()(implicit ctx: Context) + extends Message(IncorrectRepeatedParameterSyntaxID) { + override def msg: String = "varargs parameter must come last" + + override def kind: String = "Syntax" + + override def explanation: String = + hl"""|The varargs field must be the last field in the method signature. + |Attempting to define a field in a method signature after a varargs field is an error. + |""".stripMargin + } } |