Skip to content

Commit a04b6d1

Browse files
oderskysmarter
authored andcommitted
Make indentation significant in old-style control syntax
Indentation was significant only after new-style if-then, while-do, for-yield, for-do. This was prone to cause gotchas. We now treat indentation as significant also if the conditions or enumerators of these constructs use parens or braces.
1 parent 3d7bb5b commit a04b6d1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,10 @@ object Parsers {
16361636
if (rewriteToOldSyntax()) revertToParens(t)
16371637
in.nextToken()
16381638
}
1639-
else if (rewriteToNewSyntax(t.span))
1640-
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
1639+
else
1640+
in.observeIndented()
1641+
if (rewriteToNewSyntax(t.span))
1642+
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
16411643
t
16421644
}
16431645
else {
@@ -2296,6 +2298,7 @@ object Parsers {
22962298
dropParensOrBraces(start, if (in.token == YIELD || in.token == DO) "" else "do")
22972299
}
22982300
}
2301+
in.observeIndented()
22992302
res
23002303
}
23012304
else {

tests/pos/indent4.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
object testindent
2+
3+
if (true)
4+
val x = 1
5+
println(x)
6+
7+
while (false)
8+
val x = 1
9+
println(x)
10+
11+
for (x <- List(1, 2, 3))
12+
val y = x
13+
println(y)
14+
15+
for { x <- List(1, 2, 3) }
16+
val y = x
17+
println(y)
18+
19+
for {
20+
x <- List(1, 2, 3)
21+
}
22+
val y = x
23+
println(y)
24+

0 commit comments

Comments
 (0)