Skip to content

Commit 1b3a263

Browse files
committed
Don't create Blocks for single-statement indented code
1 parent 6a2d978 commit 1b3a263

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -653,14 +653,16 @@ object Parsers {
653653
lineStart
654654
}
655655

656-
val needsBraces = t match {
657-
case Block(Nil, expr) =>
656+
def needsBraces(t: Any): Boolean = t match {
657+
case Block(stats, expr) =>
658+
stats.nonEmpty || needsBraces(expr)
659+
case expr: Tree =>
658660
followsColon ||
659661
isPartialIf(expr) && in.token == ELSE ||
660662
isBlockFunction(expr)
661663
case _ => true
662664
}
663-
if (needsBraces) {
665+
if (needsBraces(t)) {
664666
patch(source, Span(startOpening, endOpening), " {")
665667
patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
666668
}
@@ -1361,11 +1363,7 @@ object Parsers {
13611363
else t
13621364

13631365
/** The block in a quote or splice */
1364-
def stagedBlock() =
1365-
inDefScopeBraces(block()) match {
1366-
case t @ Block(Nil, expr) if !expr.isEmpty => expr
1367-
case t => t
1368-
}
1366+
def stagedBlock() = inDefScopeBraces(block(simplify = true))
13691367

13701368
/** SimpleEpxr ::= spliceId | ‘$’ ‘{’ Block ‘}’)
13711369
* SimpleType ::= spliceId | ‘$’ ‘{’ Block ‘}’)
@@ -2148,27 +2146,26 @@ object Parsers {
21482146
* BlockExprContents ::= CaseClauses | Block
21492147
*/
21502148
def blockExpr(): Tree = atSpan(in.offset) {
2149+
val simplify = in.token == INDENT
21512150
inDefScopeBraces {
21522151
if (in.token == CASE) Match(EmptyTree, caseClauses(caseClause))
2153-
else block()
2152+
else block(simplify)
21542153
}
21552154
}
21562155

21572156
/** Block ::= BlockStatSeq
21582157
* @note Return tree does not have a defined span.
21592158
*/
2160-
def block(): Tree = {
2159+
def block(simplify: Boolean = false): Tree = {
21612160
val stats = blockStatSeq()
21622161
def isExpr(stat: Tree) = !(stat.isDef || stat.isInstanceOf[Import])
2163-
stats match {
2164-
case (stat : Block) :: Nil =>
2165-
stat // A typical case where this happens is creating a block around a region
2166-
// hat is already indented, e.g. something following a =>.
2167-
case _ :: stats1 if isExpr(stats.last) =>
2168-
Block(stats.init, stats.last)
2169-
case _ =>
2170-
Block(stats, EmptyTree)
2162+
if (stats.nonEmpty && isExpr(stats.last)) {
2163+
val inits = stats.init
2164+
val last = stats.last
2165+
if (inits.isEmpty && (simplify || last.isInstanceOf[Block])) last
2166+
else Block(inits, last)
21712167
}
2168+
else Block(stats, EmptyTree)
21722169
}
21732170

21742171
/** Guard ::= if PostfixExpr

0 commit comments

Comments
 (0)