Skip to content

Commit b71066a

Browse files
som-snytttgodzik
authored andcommitted
Allow observing an indent after conditional
Normally do not infer NEWLINE within parens, but special case old syntax for conditionals, so that it can observe indented syntax. The mechanism is to inject an Indented region when parsing a parenthesized condition which is within an InParens region, such as an arg list. The effect is not to advance past EOL after `(true)`. [Cherry-picked 4ba89e9]
1 parent ac5c0db commit b71066a

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

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

Lines changed: 7 additions & 2 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 InBrk : Region => Region = _.match
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,8 +2132,10 @@ 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)))
2135+
var t: Tree =
2136+
inSepRegion(InBrk): // allow inferred NEWLINE for observeIndented below
2137+
atSpan(in.offset):
2138+
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
21342139
if in.token != altToken then
21352140
if toBeContinued(altToken) then
21362141
t = inSepRegion(InCond) {

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)