Skip to content

Commit 1a2cf42

Browse files
committed
Revert to previous syntax for inline match and inline if
Better not to mix up to syntax changes in one PR. Also, we might prefer inline match and inline if in the end.
1 parent c764daf commit 1a2cf42

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ object Parsers {
983983
}
984984
else
985985
val t = reduceStack(base, top, minPrec, leftAssoc = true, in.name, isType)
986-
if !isType && in.token == MATCH then recur(matchClause(t, t.span.start, Match))
986+
if !isType && in.token == MATCH then recur(matchClause(t))
987987
else t
988988

989989
recur(first)
@@ -1027,8 +1027,7 @@ object Parsers {
10271027
/** Accept identifier or match clause acting as a selector on given tree `t` */
10281028
def selector(t: Tree): Tree =
10291029
atSpan(startOffset(t), in.offset) {
1030-
if in.token == MATCH then matchClause(t, t.span.start, Match)
1031-
else Select(t, ident())
1030+
if in.token == MATCH then matchClause(t) else Select(t, ident())
10321031
}
10331032

10341033
/** Selectors ::= id { `.' id }
@@ -1736,11 +1735,10 @@ object Parsers {
17361735
* | HkTypeParamClause ‘=>’ Expr
17371736
* | [SimpleExpr `.'] id `=' Expr
17381737
* | SimpleExpr1 ArgumentExprs `=' Expr
1739-
* | Expr2
1740-
* | [‘inline’] Expr2 `match' (`{' CaseClauses `}' | CaseClause)
1738+
* | PostfixExpr [Ascription]
1739+
* | ‘inline’ InfixExpr MatchClause
17411740
* Bindings ::= `(' [Binding {`,' Binding}] `)'
17421741
* Binding ::= (id | `_') [`:' Type]
1743-
* Expr2 ::= PostfixExpr [Ascription]
17441742
* Ascription ::= `:' InfixType
17451743
* | `:' Annotation {Annotation}
17461744
* | `:' `_' `*'
@@ -1779,7 +1777,7 @@ object Parsers {
17791777
}
17801778
}
17811779

1782-
def expr1(location: Location.Value = Location.ElseWhere): Tree = in.token match {
1780+
def expr1(location: Location.Value = Location.ElseWhere): Tree = in.token match
17831781
case IF =>
17841782
in.endMarkerScope(IF) { ifExpr(in.offset, If) }
17851783
case WHILE =>
@@ -1876,7 +1874,7 @@ object Parsers {
18761874
}
18771875
}
18781876
case _ =>
1879-
if isIdent(nme.inline) // TODO: drop this clause
1877+
if isIdent(nme.inline)
18801878
&& !in.inModifierPosition()
18811879
&& in.lookaheadIn(in.canStartExprTokens)
18821880
then
@@ -1885,13 +1883,14 @@ object Parsers {
18851883
case IF =>
18861884
ifExpr(start, InlineIf)
18871885
case _ =>
1888-
val t = prefixExpr()
1889-
if (in.token == MATCH) matchClause(t, start, InlineMatch)
1890-
else
1891-
syntaxErrorOrIncomplete(i"`match` or `if` expected but ${in} found")
1892-
t
1886+
postfixExpr() match
1887+
case t @ Match(scrut, cases) =>
1888+
InlineMatch(scrut, cases).withSpan(t.span)
1889+
case t =>
1890+
syntaxError(em"`inline` must be followed by an `if` or a `match`", start)
1891+
t
18931892
else expr1Rest(postfixExpr(), location)
1894-
}
1893+
end expr1
18951894

18961895
def expr1Rest(t: Tree, location: Location.Value): Tree = in.token match
18971896
case EQUALS =>
@@ -1933,14 +1932,11 @@ object Parsers {
19331932
}
19341933
}
19351934

1936-
/** `if' [‘inline’] `(' Expr `)' {nl} Expr [[semi] else Expr]
1937-
* `if' [‘inline’] Expr `then' Expr [[semi] else Expr]
1935+
/** `if' `(' Expr `)' {nl} Expr [[semi] else Expr]
1936+
* `if' Expr `then' Expr [[semi] else Expr]
19381937
*/
1939-
def ifExpr(start: Offset, mkIf0: (Tree, Tree, Tree) => If): If =
1938+
def ifExpr(start: Offset, mkIf: (Tree, Tree, Tree) => If): If =
19401939
atSpan(start, in.skipToken()) {
1941-
val mkIf =
1942-
if isIdent(nme.inline) then { in.nextToken(); InlineIf }
1943-
else mkIf0
19441940
val cond = condExpr(THEN)
19451941
newLinesOpt()
19461942
val thenp = subExpr()
@@ -1951,13 +1947,10 @@ object Parsers {
19511947

19521948
/** MatchClause ::= `match' [‘inline’] `{' CaseClauses `}'
19531949
*/
1954-
def matchClause(t: Tree, start: Offset, mkMatch0: (Tree, List[CaseDef]) => Match) =
1950+
def matchClause(t: Tree): Match =
19551951
in.endMarkerScope(MATCH) {
1956-
atSpan(start, in.skipToken()) {
1957-
val mkMatch =
1958-
if isIdent(nme.inline) then { in.nextToken(); InlineMatch }
1959-
else mkMatch0
1960-
mkMatch(t, inBracesOrIndented(caseClauses(caseClause)))
1952+
atSpan(t.span.start, in.skipToken()) {
1953+
Match(t, inBracesOrIndented(caseClauses(caseClause)))
19611954
}
19621955
}
19631956

docs/docs/internals/syntax.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ BlockResult ::= [‘implicit’] FunParams ‘=>’ Block
184184
FunParams ::= Bindings
185185
| id
186186
| ‘_’
187-
Expr1 ::= ‘if’ [‘inline’]‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr] If(Parens(cond), thenp, elsep?)
188-
| ‘if’ [‘inline’] Expr ‘then’ Expr [[semi] ‘else’ Expr] If(cond, thenp, elsep?)
187+
Expr1 ::= [‘inline’] ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr] If(Parens(cond), thenp, elsep?)
188+
| [‘inline’] ‘if’ Expr ‘then’ Expr [[semi] ‘else’ Expr] If(cond, thenp, elsep?)
189189
| ‘while’ ‘(’ Expr ‘)’ {nl} Expr WhileDo(Parens(cond), body)
190190
| ‘while’ Expr ‘do’ Expr WhileDo(cond, body)
191191
| ‘try’ Expr Catches [‘finally’ Expr] Try(expr, catches, expr?)
@@ -196,16 +196,16 @@ Expr1 ::= ‘if’ [‘inline’]‘(’ Expr ‘)’ {nl} Expr [[s
196196
| HkTypeParamClause ‘=>’ Expr PolyFunction(ts, expr)
197197
| [SimpleExpr ‘.’] id ‘=’ Expr Assign(expr, expr)
198198
| SimpleExpr1 ArgumentExprs ‘=’ Expr Assign(expr, expr)
199-
| Expr2
200-
Expr2 ::= PostfixExpr [Ascription]
199+
| PostfixExpr [Ascription]
200+
| ‘inline’ InfixExpr MatchClause
201201
Ascription ::= ‘:’ InfixType Typed(expr, tp)
202202
| ‘:’ Annotation {Annotation} Typed(expr, Annotated(EmptyTree, annot)*)
203203
Catches ::= ‘catch’ (Expr | CaseClause)
204204
PostfixExpr ::= InfixExpr [id] PostfixOp(expr, op)
205205
InfixExpr ::= PrefixExpr
206206
| InfixExpr id [nl] InfixExpr InfixOp(expr, op, expr)
207207
| InfixExpr MatchClause
208-
MatchClause ::= ‘match’ [‘inline’] ‘{’ CaseClauses ‘}’ Match(expr, cases)
208+
MatchClause ::= ‘match’ ‘{’ CaseClauses ‘}’ Match(expr, cases)
209209
PrefixExpr ::= [‘-’ | ‘+’ | ‘~’ | ‘!’] SimpleExpr PrefixOp(expr, op)
210210
SimpleExpr ::= Path
211211
| Literal
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
-- Error: tests/neg/cannot-reduce-inline-match.scala:3:13 --------------------------------------------------------------
22
3 | inline x match { // error
3-
| ^
4-
| cannot reduce inline match with
5-
| scrutinee: {
6-
| "f"
7-
| } : String("f")
8-
| patterns : case _:Int
3+
| ^
4+
| cannot reduce inline match with
5+
| scrutinee: {
6+
| "f"
7+
| } : String("f")
8+
| patterns : case _:Int
99
| This location is in code that was inlined at cannot-reduce-inline-match.scala:9
1010
4 | case _: Int =>
1111
5 | }

tests/run/typeclass-derivation2.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ object Pickler {
293293
}
294294

295295
inline def pickleElems[Elems <: Tuple](buf: mutable.ListBuffer[Int], elems: Mirror, n: Int): Unit =
296-
erasedValue[Elems] match inline {
296+
inline erasedValue[Elems] match {
297297
case _: (elem *: elems1) =>
298298
tryPickle[elem](buf, elems(n).asInstanceOf[elem])
299299
pickleElems[elems1](buf, elems, n + 1)
@@ -304,9 +304,9 @@ object Pickler {
304304
pickleElems[Elems](buf, r.reflect(x), 0)
305305

306306
inline def pickleCases[T, Alts <: Tuple](r: Reflected[T], buf: mutable.ListBuffer[Int], x: T, n: Int): Unit =
307-
erasedValue[Alts] match inline {
307+
inline erasedValue[Alts] match {
308308
case _: (Shape.Case[alt, elems] *: alts1) =>
309-
typeOf[alt] match inline {
309+
inline typeOf[alt] match {
310310
case _: Subtype[T] =>
311311
x match {
312312
case x: `alt` =>
@@ -326,7 +326,7 @@ object Pickler {
326326
}
327327

328328
inline def unpickleElems[Elems <: Tuple](buf: mutable.ListBuffer[Int], elems: Array[AnyRef], n: Int): Unit =
329-
erasedValue[Elems] match inline {
329+
inline erasedValue[Elems] match {
330330
case _: (elem *: elems1) =>
331331
elems(n) = tryUnpickle[elem](buf).asInstanceOf[AnyRef]
332332
unpickleElems[elems1](buf, elems, n + 1)

0 commit comments

Comments
 (0)