Skip to content

Commit 162f8d6

Browse files
committed
Remove inStatSeq and other requested changes
1 parent e07408a commit 162f8d6

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

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

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ object Parsers:
532532
def inBraces[T](body: => T): T = enclosed(LBRACE, body)
533533
def inBrackets[T](body: => T): T = enclosed(LBRACKET, body)
534534

535-
def inBracesOrIndented[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
535+
def inBracesOrIndented[T](body: => T, rewriteWithColon: Boolean = false): T =
536536
if in.token == INDENT then
537537
// braces are always optional after `=>` so none should be inserted
538538
val afterArrow = testChars(in.lastOffset - 3, " =>")
@@ -542,11 +542,11 @@ object Parsers:
542542
else if rewriteToIndent then enclosed(INDENT, toIndentedRegion(body))
543543
else enclosed(INDENT, body)
544544
else
545-
if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
545+
if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
546546
else inBraces(body)
547547

548-
def inDefScopeBraces[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
549-
inBracesOrIndented(body, inStatSeq, rewriteWithColon)
548+
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
549+
inBracesOrIndented(body, rewriteWithColon)
550550

551551
/** <part> {`,` <part>} */
552552
def commaSeparated[T](part: () => T): List[T] =
@@ -717,6 +717,9 @@ object Parsers:
717717
def blankLinesAround(start: Offset, end: Offset): (Offset, Offset) =
718718
(skipBlanks(start - 1, -1) + 1, skipBlanks(end, 1))
719719

720+
private val bracesToIndentPredecessors =
721+
colonEOLPredecessors | canStartIndentTokens | BitSet(IDENTIFIER)
722+
720723
/** Parse brace-enclosed `body` and rewrite it to be an indentation region instead, if possible.
721724
* If possible means:
722725
* 1. not inside (...), [...], case ... =>
@@ -725,31 +728,23 @@ object Parsers:
725728
* 4. there is at least one token between the braces
726729
* 5. the closing brace is also at the end of the line, or it is followed by one of
727730
* `then`, `else`, `do`, `catch`, `finally`, `yield`, or `match`.
728-
* 6. the opening brace does not follow a closing brace
731+
* 6. the opening brace follows an colonEOLPredecessors, a canStartIndentTokens or an identifier
729732
* 7. last token is not a leading operator
730-
* 8. not a block in a sequence of statements
731-
* 9. cannot rewrite if colon required after a NEWLINE, e.g.
732-
* true ||
733-
* {
734-
* false
735-
* }
736-
*/
737-
def bracesToIndented[T](body: => T, inStatSeq: Boolean, rewriteWithColon: Boolean): T =
733+
*/
734+
def bracesToIndented[T](body: => T, rewriteWithColon: Boolean): T =
738735
import IndentRewriteState.*
739736
val prevSaved = prev.saveCopy
740737
val lastOffsetSaved = in.lastOffset
741738
val underColonSyntax = possibleColonOffset == in.lastOffset
742739
val colonRequired = rewriteWithColon || underColonSyntax
743740
val (startOpening, endOpening) = elimRegion(in.offset)
744741
def isBracesOrIndented(r: Region): Boolean = r match
745-
case r: Indented => true
746-
case r: InBraces => true
742+
case r: (Indented | InBraces) => true
747743
case _ => false
748-
var canRewrite = isBracesOrIndented(in.currentRegion) && // test (1)
749-
prevSaved.token != RBRACE && // test (6)
750-
!(prevSaved.isOperator && prevSaved.isAfterLineEnd) && // test (7)
751-
!inStatSeq && // test (8)
752-
(!colonRequired || !in.isAfterLineEnd) // test (9)
744+
var canRewrite =
745+
isBracesOrIndented(in.currentRegion) // test (1)
746+
&& bracesToIndentPredecessors.contains(prevSaved.token) // test (6)
747+
&& !(prevSaved.isOperator && prevSaved.isAfterLineEnd) // test (7)
753748
val t = enclosed(LBRACE, {
754749
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
755750
toIndentedRegion:
@@ -2073,7 +2068,7 @@ object Parsers:
20732068

20742069
def subExpr() = subPart(expr)
20752070

2076-
def expr(location: Location, inStatSeq: Boolean = false): Tree = {
2071+
def expr(location: Location): Tree = {
20772072
val start = in.offset
20782073
in.token match
20792074
case IMPLICIT =>
@@ -2100,7 +2095,7 @@ object Parsers:
21002095
else new WildcardFunction(placeholderParams.reverse, t)
21012096
finally placeholderParams = saved
21022097

2103-
val t = expr1(location, inStatSeq)
2098+
val t = expr1(location)
21042099
if in.isArrow then
21052100
placeholderParams = Nil // don't interpret `_' to the left of `=>` as placeholder
21062101
wrapPlaceholders(closureRest(start, location, convertToParams(t)))
@@ -2111,7 +2106,7 @@ object Parsers:
21112106
checkNonParamTuple(t)
21122107
wrapPlaceholders(t)
21132108

2114-
def expr1(location: Location = Location.ElseWhere, inStatSeq: Boolean = false): Tree = in.token match
2109+
def expr1(location: Location = Location.ElseWhere): Tree = in.token match
21152110
case IF =>
21162111
ifExpr(in.offset, If)
21172112
case WHILE =>
@@ -2201,7 +2196,7 @@ object Parsers:
22012196
case t =>
22022197
syntaxError(em"`inline` must be followed by an `if` or a `match`", start)
22032198
t
2204-
else expr1Rest(postfixExpr(location, inStatSeq), location)
2199+
else expr1Rest(postfixExpr(location), location)
22052200
end expr1
22062201

22072202
def expr1Rest(t: Tree, location: Location): Tree =
@@ -2346,8 +2341,8 @@ object Parsers:
23462341
* | InfixExpr id ColonArgument
23472342
* | InfixExpr MatchClause
23482343
*/
2349-
def postfixExpr(location: Location = Location.ElseWhere, inStatSeq: Boolean = false): Tree =
2350-
val t = postfixExprRest(prefixExpr(location, inStatSeq), location)
2344+
def postfixExpr(location: Location = Location.ElseWhere): Tree =
2345+
val t = postfixExprRest(prefixExpr(location), location)
23512346
if location.inArgs && followingIsVararg() then
23522347
Typed(t, atSpan(skipToken()) { Ident(tpnme.WILDCARD_STAR) })
23532348
else
@@ -2360,7 +2355,7 @@ object Parsers:
23602355
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
23612356
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
23622357
*/
2363-
def prefixExpr(location: Location, inStatSeq: Boolean = false): Tree =
2358+
val prefixExpr: Location => Tree = location =>
23642359
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
23652360
&& in.canStartExprTokens.contains(in.lookahead.token)
23662361
then
@@ -2370,7 +2365,7 @@ object Parsers:
23702365
simpleExprRest(literal(start), location, canApply = true)
23712366
else
23722367
atSpan(start) { PrefixOp(op, simpleExpr(location)) }
2373-
else simpleExpr(location, inStatSeq)
2368+
else simpleExpr(location)
23742369

23752370
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]
23762371
* | ‘new’ TemplateBody
@@ -2396,7 +2391,7 @@ object Parsers:
23962391
* Quoted ::= ‘'’ ‘{’ Block ‘}’
23972392
* | ‘'’ ‘[’ Type ‘]’
23982393
*/
2399-
def simpleExpr(location: Location, inStatSeq: Boolean = false): Tree = {
2394+
def simpleExpr(location: Location): Tree = {
24002395
var canApply = true
24012396
val t = in.token match
24022397
case XMLSTART =>
@@ -2417,7 +2412,7 @@ object Parsers:
24172412
atSpan(in.offset) { makeTupleOrParens(inParens(exprsInParensOrBindings())) }
24182413
case LBRACE | INDENT =>
24192414
canApply = false
2420-
blockExpr(inStatSeq)
2415+
blockExpr()
24212416
case QUOTE =>
24222417
atSpan(skipToken()) {
24232418
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
@@ -2568,12 +2563,12 @@ object Parsers:
25682563

25692564
/** BlockExpr ::= <<< (CaseClauses | Block) >>>
25702565
*/
2571-
def blockExpr(inStatSeq: Boolean = false): Tree = atSpan(in.offset) {
2566+
def blockExpr(): Tree = atSpan(in.offset) {
25722567
val simplify = in.token == INDENT
25732568
inDefScopeBraces({
25742569
if (in.token == CASE) Match(EmptyTree, caseClauses(() => caseClause()))
25752570
else block(simplify)
2576-
}, inStatSeq = inStatSeq)
2571+
})
25772572
}
25782573

25792574
/** Block ::= BlockStatSeq
@@ -3938,7 +3933,7 @@ object Parsers:
39383933
Template(constr, parents, derived, self, stats)
39393934

39403935
def templateBody(parents: List[Tree], rewriteWithColon: Boolean = true): (ValDef, List[Tree]) =
3941-
val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon = rewriteWithColon)
3936+
val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon)
39423937
if in.token == WITH && parents.isEmpty then
39433938
syntaxError(EarlyDefinitionsNotSupported())
39443939
nextToken()
@@ -4055,7 +4050,7 @@ object Parsers:
40554050
else if (isDefIntro(modifierTokensOrCase))
40564051
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
40574052
else if (isExprIntro)
4058-
stats += expr1(inStatSeq = true)
4053+
stats += expr1()
40594054
else
40604055
empty = true
40614056
statSepOrEnd(stats, noPrevStat = empty)
@@ -4129,7 +4124,7 @@ object Parsers:
41294124
if (in.token == IMPORT)
41304125
stats ++= importClause()
41314126
else if (isExprIntro)
4132-
stats += expr(Location.InBlock, inStatSeq = true)
4127+
stats += expr(Location.InBlock)
41334128
else if in.token == IMPLICIT && !in.inModifierPosition() then
41344129
stats += closure(in.offset, Location.InBlock, modifiers(BitSet(IMPLICIT)))
41354130
else if isIdent(nme.extension) && followingIsExtension() then
@@ -4190,7 +4185,7 @@ object Parsers:
41904185
def skipBracesHook(): Option[Tree] =
41914186
if (in.token == XMLSTART) Some(xmlLiteral()) else None
41924187

4193-
override def blockExpr(inStatSeq: Boolean): Tree = {
4188+
override def blockExpr(): Tree = {
41944189
skipBraces()
41954190
EmptyTree
41964191

compiler/src/dotty/tools/dotc/rewrites/Rewrites.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ object Rewrites:
2525
pbuf += Patch(span, replacement)
2626

2727
def patchOver(span: Span, replacement: String): Unit =
28-
pbuf.indices.reverse.find(i => span.contains(pbuf(i).span)).foreach(pbuf.remove)
28+
val prevPatchIdx = pbuf.lastIndexWhere(p => span.contains(p.span))
29+
if prevPatchIdx >= 0 then pbuf.remove(prevPatchIdx)
2930
pbuf += Patch(span, replacement)
3031

3132
def apply(cs: Array[Char]): Array[Char] = {

0 commit comments

Comments
 (0)