Skip to content

Commit 29c869e

Browse files
committed
Permit indented RHS of single line case
1 parent bbb1c60 commit 29c869e

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,15 +3230,20 @@ object Parsers {
32303230
atSpan(self.span)(Block(Nil, EmptyTree))
32313231

32323232
val body = tok match
3233-
case ARROW => atSpan(in.skipToken()):
3234-
if exprOnly then
3235-
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
3236-
warning(em"""Misleading indentation: this expression forms part of the preceding case.
3237-
|If this is intended, it should be indented for clarity.
3238-
|Otherwise, if the handler is intended to be empty, use a multi-line match or catch with
3239-
|an indented case.""")
3240-
expr()
3241-
else block()
3233+
case ARROW =>
3234+
val arrowAt = in.offset
3235+
atSpan(in.skipToken()):
3236+
if exprOnly then
3237+
if in.token == ENDlambda && source.offsetToLine(arrowAt) != source.offsetToLine(in.offset) then
3238+
in.token = NEWLINE
3239+
in.observeIndented()
3240+
if in.indentSyntax && in.isAfterLineEnd && in.token != INDENT then
3241+
warning(em"""Misleading indentation: this expression forms part of the preceding case.
3242+
|If this is intended, it should be indented for clarity.
3243+
|Otherwise, if the handler is intended to be empty, use a multi-line match or catch with
3244+
|an indented case.""")
3245+
expr()
3246+
else block()
32423247
case IF => atSpan(in.skipToken()):
32433248
// a sub match after a guard is parsed the same as one without
32443249
val t = inSepRegion(InCase)(postfixExpr(Location.InGuard))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ object Scanners {
596596
lastWidth = r.width
597597
newlineIsSeparating = lastWidth <= nextWidth || r.isOutermost
598598
indentPrefix = r.prefix
599-
case _: InString => ()
599+
case _: InString | _: SingleLineLambda => ()
600600
case r =>
601601
indentIsSignificant = indentSyntax
602602
r.proposeKnownWidth(nextWidth, lastToken)

tests/neg/i24496.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import scala.language.experimental.relaxedLambdaSyntax
55

66
val three = list
77
.collect: case x =>
8-
(x, x + 1) // error not a member of tuple
9-
.toMap
8+
(x, x + 1)
9+
.toMap // error value toMap is not a member of (Int, Int)
1010

1111
val huh = list
1212
.collect: x => case y => (y, y + 1) // error expecting case at x

tests/pos/i24496.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import scala.language.experimental.relaxedLambdaSyntax
33
@main def Test =
44
val list = List(1, 2, 3)
55

6+
val three = list
7+
.collect: case x =>
8+
val y = x + 1
9+
(x, y)
10+
.toMap
11+
612
val two = list
713
.collect: x => (x, x + 1)
814
.toMap
915

1016
val one = list
1117
.collect: case x => (x, x + 1)
1218
.toMap
13-
14-
//val huh = list
15-
// .collect: x => case y => (y, y + 1) correctly errors expecting case at x

0 commit comments

Comments
 (0)