diff options
author | Igor Mielientiev <igor.melent@gmail.com> | 2017-03-22 16:24:15 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2017-03-22 15:24:15 +0100 |
commit | 5fd7a9503e44b8e95c0ca5aa4366270a3cd90155 (patch) | |
tree | 3c2f26df17a935c89f8a0238501419c30ed6f793 /compiler/src/dotty/tools | |
parent | 3af0ebbde81ff6429175ff1661f4ba927dc0d295 (diff) | |
download | dotty-5fd7a9503e44b8e95c0ca5aa4366270a3cd90155.tar.gz dotty-5fd7a9503e44b8e95c0ca5aa4366270a3cd90155.tar.bz2 dotty-5fd7a9503e44b8e95c0ca5aa4366270a3cd90155.zip |
Fix varargs in methods and constructors (#2135)
* Fix varargs in methods (Issue: #1625)
* Fix minor comments
* Change varargs parameter message
* Fix failed test, fix case for constructor
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 + } } |