Skip to content

Commit 1564f74

Browse files
committed
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)`.
1 parent 43f8cdb commit 1564f74

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Diff for: compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ object Parsers {
105105
private val InCase: Region => Region = Scanners.InCase(_)
106106
private val InCond: Region => Region = Scanners.InParens(LPAREN, _)
107107
private val InFor : Region => Region = Scanners.InBraces(_)
108+
private val InBrk : Region => Region = _.match
109+
case p: Scanners.InParens => Scanners.Indented(p.indentWidth, p.prefix, p)
110+
case r => r
108111

109112
def unimplementedExpr(using Context): Select =
110113
Select(scalaDot(nme.Predef), nme.???)
@@ -2325,8 +2328,10 @@ object Parsers {
23252328
def condExpr(altToken: Token): Tree =
23262329
val t: Tree =
23272330
if in.token == LPAREN then
2328-
var t: Tree = atSpan(in.offset):
2329-
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
2331+
var t: Tree =
2332+
inSepRegion(InBrk): // allow inferred NEWLINE for observeIndented below
2333+
atSpan(in.offset):
2334+
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
23302335
if in.token != altToken then
23312336
if toBeContinued(altToken) then
23322337
t = inSepRegion(InCond) {

Diff for: tests/pos/i22608.scala

+48
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)