From bdc47d581947e84636a591eadbf623be8da4d288 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 27 Jun 2019 17:44:06 +0200 Subject: [PATCH] Allow a newline after the first parameter list of an extension method This was already postulated by the syntax, but the parser did not implement it. Maybe this change is enough to allow a formatting that makes defined extension methods easy to find when scanning code. --- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 5 +++-- docs/docs/internals/syntax.md | 2 +- tests/run/extension-methods.scala | 10 +++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 6740559ff204..3f4b8c8f19ef 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2537,7 +2537,7 @@ object Parsers { /** DefDef ::= DefSig [(‘:’ | ‘<:’) Type] ‘=’ Expr * | this ParamClause ParamClauses `=' ConstrExpr * DefDcl ::= DefSig `:' Type - * DefSig ::= ‘(’ DefParam ‘)’ [nl] id [DefTypeParamClause] ParamClauses + * DefSig ::= [‘(’ DefParam ‘)’ [nl]] id [DefTypeParamClause] ParamClauses */ def defDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) { def scala2ProcedureSyntax(resultTypeStr: String) = { @@ -2567,7 +2567,8 @@ object Parsers { } else { val (leadingParamss, flags) = if (in.token == LPAREN) - (paramClause(prefix = true) :: Nil, Method | Extension) + try (paramClause(prefix = true) :: Nil, Method | Extension) + finally newLineOpt() else (Nil, Method) val mods1 = addFlag(mods, flags) diff --git a/docs/docs/internals/syntax.md b/docs/docs/internals/syntax.md index e156394143fa..8ffd972129e3 100644 --- a/docs/docs/internals/syntax.md +++ b/docs/docs/internals/syntax.md @@ -357,7 +357,7 @@ Dcl ::= RefineDcl ValDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree) VarDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree) DefDcl ::= DefSig [‘:’ Type] DefDef(_, name, tparams, vparamss, tpe, EmptyTree) -DefSig ::= ‘(’ DefParam ‘)’ [nl] id +DefSig ::= [‘(’ DefParam ‘)’ [nl]] id [DefTypeParamClause] DefParamClauses TypeDcl ::= id [TypeParamClause] SubtypeBounds [‘=’ Type] TypeDefTree(_, name, tparams, bound diff --git a/tests/run/extension-methods.scala b/tests/run/extension-methods.scala index dd0091c9c8c4..16001afc1f71 100644 --- a/tests/run/extension-methods.scala +++ b/tests/run/extension-methods.scala @@ -61,7 +61,8 @@ object Test extends App { } class ListOrd[T: Ord] extends Ord[List[T]] { - def (xs: List[T]) compareTo (ys: List[T]): Int = (xs, ys) match { + def (xs: List[T]) + compareTo (ys: List[T]): Int = (xs, ys) match { case (Nil, Nil) => 0 case (Nil, _) => -1 case (_, Nil) => +1 @@ -87,8 +88,11 @@ object Test extends App { } trait Monad[F[_]] extends Functor[F] { - def (x: F[A]) flatMap [A, B](f: A => F[B]): F[B] - def (x: F[A]) map [A, B](f: A => B) = x.flatMap(f `andThen` pure) + def (x: F[A]) + flatMap [A, B](f: A => F[B]): F[B] + + def (x: F[A]) + map [A, B](f: A => B) = x.flatMap(f `andThen` pure) def pure[A](x: A): F[A] }