Skip to content

Syntax change for single cases #7507

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

Merged
merged 3 commits into from
Nov 7, 2019
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
33 changes: 19 additions & 14 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,9 @@ object Parsers {
// note: next is defined here because current == NEWLINE
if (in.token == NEWLINE && in.next.token == token) in.nextToken()

def newLinesOptWhenFollowedBy(token: Int): Unit =
if in.isNewLine && in.next.token == token then in.nextToken()

def newLinesOptWhenFollowedBy(name: Name): Unit =
if in.isNewLine && in.next.token == IDENTIFIER && in.next.name == name then
in.nextToken()
Expand Down Expand Up @@ -1742,8 +1745,7 @@ object Parsers {
* | [SimpleExpr `.'] id `=' Expr
* | SimpleExpr1 ArgumentExprs `=' Expr
* | Expr2
* | [‘inline’] Expr2 `match' `{' CaseClauses `}'
* | `given' `match' `{' ImplicitCaseClauses `}'
* | [‘inline’] Expr2 `match' (`{' CaseClauses `}' | CaseClause)
* Bindings ::= `(' [Binding {`,' Binding}] `)'
* Binding ::= (id | `_') [`:' Type]
* Expr2 ::= PostfixExpr [Ascription]
Expand Down Expand Up @@ -1829,11 +1831,12 @@ object Parsers {
atSpan(in.skipToken()) {
val body = expr()
val (handler, handlerStart) =
if (in.token == CATCH) {
if in.token == CATCH then
val span = in.offset
in.nextToken()
(subExpr(), span)
}
(if in.token == CASE then Match(EmptyTree, caseClause() :: Nil)
else subExpr(),
span)
else (EmptyTree, -1)

handler match {
Expand Down Expand Up @@ -1955,20 +1958,20 @@ object Parsers {
mkIf(cond, thenp, elsep)
}

/** `match' { CaseClauses }
/** `match' (`{' CaseClauses `}' | CaseClause)
*/
def matchExpr(t: Tree, start: Offset, mkMatch: (Tree, List[CaseDef]) => Match) =
indentRegion(MATCH) {
atSpan(start, in.skipToken()) {
mkMatch(t, inBracesOrIndented(caseClauses(caseClause)))
mkMatch(t, casesExpr(caseClause))
}
}

/** `match' { TypeCaseClauses }
/** `match' (`{' TypeCaseClauses `}' | TypeCaseClause)
*/
def matchType(t: Tree): MatchTypeTree =
atSpan(t.span.start, accept(MATCH)) {
inBracesOrIndented(MatchTypeTree(EmptyTree, t, caseClauses(typeCaseClause)))
MatchTypeTree(EmptyTree, t, casesExpr(typeCaseClause))
}

/** FunParams ::= Bindings
Expand Down Expand Up @@ -2399,8 +2402,11 @@ object Parsers {
}
}

def casesExpr(clause: () => CaseDef): List[CaseDef] =
if in.token == CASE then clause() :: Nil
else inBracesOrIndented(caseClauses(clause))

/** CaseClauses ::= CaseClause {CaseClause}
* ImplicitCaseClauses ::= ImplicitCaseClause {ImplicitCaseClause}
* TypeCaseClauses ::= TypeCaseClause {TypeCaseClause}
*/
def caseClauses(clause: () => CaseDef): List[CaseDef] = {
Expand All @@ -2410,9 +2416,8 @@ object Parsers {
buf.toList
}

/** CaseClause ::= ‘case’ Pattern [Guard] `=>' Block
* ImplicitCaseClause ::= ‘case’ PatVar [Ascription] [Guard] `=>' Block
*/
/** CaseClause ::= ‘case’ Pattern [Guard] `=>' Block
*/
val caseClause: () => CaseDef = () => atSpan(in.offset) {
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
accept(CASE)
Expand All @@ -2430,7 +2435,7 @@ object Parsers {
}
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
val t = typ()
if (isStatSep) in.nextToken()
newLinesOptWhenFollowedBy(CASE)
t
})
}
Expand Down
9 changes: 5 additions & 4 deletions docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ FunArgTypes ::= InfixType
| ‘(’ [ ‘[given]’ FunArgType {‘,’ FunArgType } ] ‘)’
| ‘(’ ‘[given]’ TypedFunParam {‘,’ TypedFunParam } ‘)’
TypedFunParam ::= id ‘:’ Type
MatchType ::= InfixType `match` TypeCaseClauses
MatchType ::= InfixType `match`
(‘{’ TypeCaseClauses ‘}’ | TypeCaseClause)
InfixType ::= RefinedType {id [nl] RefinedType} InfixOp(t1, op, t2)
RefinedType ::= WithType {[nl | ‘with’] Refinement} RefinedTypeTree(t, ds)
WithType ::= AnnotType {‘with’ AnnotType} (deprecated)
Expand Down Expand Up @@ -198,12 +199,12 @@ Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl}
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
| SimpleExpr1 ArgumentExprs ‘=’ Expr Assign(expr, expr)
| Expr2
| [‘inline’] Expr2 ‘match’ ‘{’ CaseClauses ‘}’ Match(expr, cases) -- point on match
| ‘given’ ‘match’ ‘{’ ImplicitCaseClauses ‘}’
| [‘inline’] Expr2 ‘match’
(‘{’ CaseClauses ‘}’ | CaseClause) Match(expr, cases) -- point on match
Expr2 ::= PostfixExpr [Ascription]
Ascription ::= ‘:’ InfixType Typed(expr, tp)
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)
Catches ::= ‘catch’ Expr
Catches ::= ‘catch’ (Expr | CaseClause)
PostfixExpr ::= InfixExpr [id] PostfixOp(expr, op)
InfixExpr ::= PrefixExpr
| InfixExpr id [nl] InfixExpr InfixOp(expr, op, expr)
Expand Down
9 changes: 9 additions & 0 deletions tests/neg/case-semi.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test with
type X = Int
val x: X = 1

type T2 = X match { case Int => String case String => Int }
x match { case 1 => 3; case 2 => 4 }
x match { case 1 => 3 case 2 => 4 }

type T1 = X match { case Int => String; case String => Int } // error // error
11 changes: 11 additions & 0 deletions tests/pos/single-case.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
object test with

type T[X] = X match case Int => X

try
println("hi")
catch case ex: java.io.IOException => println("ho")

1 match case x: Int => println(x)


2 changes: 1 addition & 1 deletion tests/run-macros/tasty-tree-map/quoted_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Test {
println(identityMaped({ val x: Int | Any = 60; x }))
println(identityMaped({ def f(x: => Int): Int = x; f(61) }))
println(identityMaped({ type T[X] = X; val x: T[Int] = 62; x }))
println(identityMaped({ type T[X] = X match { case Int => String; case String => Int }; val x: T[String] = 63; x }))
println(identityMaped({ type T[X] = X match { case Int => String case String => Int }; val x: T[String] = 63; x }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the current state of matches without semicolons? We should have neg/pos tests with at least the folowing cases

X match { case Int => String; case String => Int }
X match { case Int => String case String => Int }
x match { case 1 => 3; case 2 => 4 }
x match { case 1 => 3 case 2 => 4 }

println(identityMaped((Nil: List[Int]) match { case _: List[t] => 64 }))
println(identityMaped({ object F { type T = Int }; val x: F.T = 65; x }))
println(identityMaped({ val x: Foo { type T = Int } = new Foo { type T = Int; def y: Int = 66 }; x.y }))
Expand Down