Skip to content

Commit 85da96e

Browse files
authored
Merge pull request #253 from scala/backport-lts-3.3-22611
Backport "Allow observing an indent after conditional" to 3.3 LTS
2 parents 66316de + bb8eb47 commit 85da96e

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ object Parsers {
9797
private val InCase: Region => Region = Scanners.InCase(_)
9898
private val InCond: Region => Region = Scanners.InParens(LPAREN, _)
9999
private val InFor : Region => Region = Scanners.InBraces(_)
100+
private val InOldCond: Region => Region = // old-style Cond to allow indent when InParens, see #22608
101+
case p: Scanners.InParens => Scanners.Indented(p.indentWidth, p.prefix, p)
102+
case r => r
100103

101104
abstract class ParserCommon(val source: SourceFile)(using Context) {
102105

@@ -2129,25 +2132,25 @@ object Parsers {
21292132
def condExpr(altToken: Token): Tree =
21302133
val t: Tree =
21312134
if in.token == LPAREN then
2132-
var t: Tree = atSpan(in.offset):
2133-
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
2134-
if in.token != altToken then
2135-
if toBeContinued(altToken) then
2136-
t = inSepRegion(InCond) {
2135+
inSepRegion(InOldCond): // allow inferred NEWLINE for observeIndented below
2136+
atSpan(in.offset):
2137+
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
2138+
.pipe: t =>
2139+
if in.token == altToken then t
2140+
else if toBeContinued(altToken) then
2141+
inSepRegion(InCond):
21372142
expr1Rest(
21382143
postfixExprRest(
21392144
simpleExprRest(t, Location.ElseWhere),
21402145
Location.ElseWhere),
21412146
Location.ElseWhere)
2142-
}
21432147
else
21442148
if rewriteToNewSyntax(t.span) then
2145-
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
2149+
dropParensOrBraces(t.span.start, tokenString(altToken))
21462150
in.observeIndented()
21472151
return t
2148-
t
21492152
else if in.isNestedStart then
2150-
try expr() finally newLinesOpt()
2153+
expr().tap(_ => newLinesOpt())
21512154
else
21522155
inSepRegion(InCond)(expr())
21532156
if rewriteToOldSyntax(t.span.startPos) then revertToParens(t)

tests/pos/i22608.scala

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
def f(i: Int) = i
3+
def g(i: Int, j: Int) = i+j
4+
5+
def t =
6+
val y = f(
7+
if (true)// then
8+
val x = 1
9+
5
10+
else 7
11+
)
12+
y
13+
14+
def u(j: Int) =
15+
val y = g(
16+
if (true)// then
17+
val x = 1
18+
5
19+
else 7,
20+
j
21+
)
22+
y
23+
24+
def b(k: Boolean): Int =
25+
f(
26+
if (
27+
k
28+
&& b(!k) > 0
29+
) then 27
30+
else 42
31+
)
32+
33+
def p(b: Boolean) =
34+
import collection.mutable.ListBuffer
35+
val xs, ys = ListBuffer.empty[String]
36+
(if (b)
37+
xs
38+
else
39+
ys) += "hello, world"
40+
(xs.toString, ys.toString)
41+
42+
def q(b: Boolean) =
43+
import collection.mutable.ListBuffer
44+
val xs, ys = ListBuffer.empty[String]
45+
(if (b)
46+
then xs
47+
else ys) += "hello, world"
48+
(xs.toString, ys.toString)

0 commit comments

Comments
 (0)