Skip to content

Commit 8ac2d3d

Browse files
committed
Don't accidentally drop comments when rewriting to old syntax
Previously, a comment between a condition and a `then` or a `do` would be dropped when rewriting to old syntax with parentheses.
1 parent 687c2fa commit 8ac2d3d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,8 @@ object Parsers {
575575

576576
/** Drop (...) or { ... }, replacing the closing element with `endStr` */
577577
def dropParensOrBraces(start: Offset, endStr: String): Unit = {
578-
patch(source, Span(start, start + 1), "")
578+
patch(source, Span(start, start + 1),
579+
if (testChar(start - 1, Chars.isIdentifierPart)) " " else "")
579580
val closingStartsLine = testChar(skipBlanks(in.lastOffset - 2, -1), Chars.LF)
580581
val preFill = if (closingStartsLine || endStr.isEmpty) "" else " "
581582
val postFill = if (in.lastOffset == in.offset) " " else ""
@@ -585,11 +586,25 @@ object Parsers {
585586
patch(source, Span(startClosing, endClosing), s"$preFill$endStr$postFill")
586587
}
587588

589+
/** Drop current token, which is assumed to be `then` or `do`. */
590+
def dropTerminator(): Unit = {
591+
var startOffset = in.offset
592+
var endOffset = in.lastCharOffset
593+
if (in.isAfterLineEnd()) {
594+
if (testChar(endOffset, ' ')) endOffset += 1
595+
}
596+
else {
597+
if (testChar(startOffset - 1, ' ')) startOffset -= 1
598+
}
599+
patch(source, Span(startOffset, endOffset), "")
600+
}
601+
588602
/** rewrite code with (...) around the source code of `t` */
589603
def revertToParens(t: Tree): Unit =
590604
if (t.span.exists) {
591605
patch(source, t.span.startPos, "(")
592-
patch(source, Span(t.span.end, in.lastOffset), ")")
606+
patch(source, t.span.endPos, ")")
607+
dropTerminator()
593608
}
594609

595610
/** In the tokens following the current one, does `query` precede any of the tokens that
@@ -1352,16 +1367,16 @@ object Parsers {
13521367
expr1Rest(postfixExprRest(simpleExprRest(t)), Location.ElseWhere)
13531368
}
13541369
if (in.token == altToken) {
1355-
in.nextToken()
13561370
if (in.rewriteOldSyntax) revertToParens(t)
1371+
in.nextToken()
13571372
}
13581373
else if (in.rewriteNewSyntax)
13591374
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
13601375
t
13611376
} else {
13621377
val t = inSepRegion(LPAREN, RPAREN)(expr())
1363-
accept(altToken)
13641378
if (in.rewriteOldSyntax) revertToParens(t)
1379+
accept(altToken)
13651380
t
13661381
}
13671382
}
@@ -2027,17 +2042,7 @@ object Parsers {
20272042
ForYield(enums, expr())
20282043
}
20292044
else if (in.token == DO) {
2030-
if (in.rewriteOldSyntax) {
2031-
var startOffset = in.offset
2032-
var endOffset = in.lastCharOffset
2033-
if (in.isAfterLineEnd()) {
2034-
if (testChar(endOffset, ' ')) endOffset += 1
2035-
}
2036-
else {
2037-
if (testChar(startOffset - 1, ' ')) startOffset -= 1
2038-
}
2039-
patch(source, Span(startOffset, endOffset), "")
2040-
}
2045+
if (in.rewriteOldSyntax) dropTerminator()
20412046
in.nextToken()
20422047
ForDo(enums, expr())
20432048
}

0 commit comments

Comments
 (0)