Skip to content

Commit a0d28a3

Browse files
committed
Remove inStatSeq and other requested changes
1 parent 351b5f5 commit a0d28a3

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
@@ -559,7 +559,7 @@ object Parsers {
559559
def inBraces[T](body: => T): T = enclosed(LBRACE, body)
560560
def inBrackets[T](body: => T): T = enclosed(LBRACKET, body)
561561

562-
def inBracesOrIndented[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
562+
def inBracesOrIndented[T](body: => T, rewriteWithColon: Boolean = false): T =
563563
if in.token == INDENT then
564564
// braces are always optional after `=>` so none should be inserted
565565
val afterArrow = testChars(in.lastOffset - 3, " =>")
@@ -569,11 +569,11 @@ object Parsers {
569569
else if rewriteToIndent then enclosed(INDENT, toIndentedRegion(body))
570570
else enclosed(INDENT, body)
571571
else
572-
if in.rewriteToIndent then bracesToIndented(body, inStatSeq, rewriteWithColon)
572+
if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
573573
else inBraces(body)
574574

575-
def inDefScopeBraces[T](body: => T, inStatSeq: Boolean = false, rewriteWithColon: Boolean = false): T =
576-
inBracesOrIndented(body, inStatSeq, rewriteWithColon)
575+
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
576+
inBracesOrIndented(body, rewriteWithColon)
577577

578578
/** <part> {`,` <part>} */
579579
def commaSeparated[T](part: () => T): List[T] =
@@ -748,6 +748,9 @@ object Parsers {
748748
def blankLinesAround(start: Offset, end: Offset): (Offset, Offset) =
749749
(skipBlanks(start - 1, -1) + 1, skipBlanks(end, 1))
750750

751+
private val bracesToIndentPredecessors =
752+
colonEOLPredecessors | canStartIndentTokens | BitSet(IDENTIFIER)
753+
751754
/** Parse brace-enclosed `body` and rewrite it to be an indentation region instead, if possible.
752755
* If possible means:
753756
* 1. not inside (...), [...], case ... =>
@@ -756,31 +759,23 @@ object Parsers {
756759
* 4. there is at least one token between the braces
757760
* 5. the closing brace is also at the end of the line, or it is followed by one of
758761
* `then`, `else`, `do`, `catch`, `finally`, `yield`, or `match`.
759-
* 6. the opening brace does not follow a closing brace
762+
* 6. the opening brace follows an colonEOLPredecessors, a canStartIndentTokens or an identifier
760763
* 7. last token is not a leading operator
761-
* 8. not a block in a sequence of statements
762-
* 9. cannot rewrite if colon required after a NEWLINE, e.g.
763-
* true ||
764-
* {
765-
* false
766-
* }
767-
*/
768-
def bracesToIndented[T](body: => T, inStatSeq: Boolean, rewriteWithColon: Boolean): T =
764+
*/
765+
def bracesToIndented[T](body: => T, rewriteWithColon: Boolean): T =
769766
import IndentRewriteState.*
770767
val prevSaved = prev.saveCopy
771768
val lastOffsetSaved = in.lastOffset
772769
val underColonSyntax = possibleColonOffset == in.lastOffset
773770
val colonRequired = rewriteWithColon || underColonSyntax
774771
val (startOpening, endOpening) = elimRegion(in.offset)
775772
def isBracesOrIndented(r: Region): Boolean = r match
776-
case r: Indented => true
777-
case r: InBraces => true
773+
case r: (Indented | InBraces) => true
778774
case _ => false
779-
var canRewrite = isBracesOrIndented(in.currentRegion) && // test (1)
780-
prevSaved.token != RBRACE && // test (6)
781-
!(prevSaved.isOperator && prevSaved.isAfterLineEnd) && // test (7)
782-
!inStatSeq && // test (8)
783-
(!colonRequired || !in.isAfterLineEnd) // test (9)
775+
var canRewrite =
776+
isBracesOrIndented(in.currentRegion) // test (1)
777+
&& bracesToIndentPredecessors.contains(prevSaved.token) // test (6)
778+
&& !(prevSaved.isOperator && prevSaved.isAfterLineEnd) // test (7)
784779
val t = enclosed(LBRACE, {
785780
if in.isAfterLineEnd && in.token != RBRACE then // test (2)(4)
786781
toIndentedRegion:
@@ -2180,7 +2175,7 @@ object Parsers {
21802175

21812176
def subExpr() = subPart(expr)
21822177

2183-
def expr(location: Location, inStatSeq: Boolean = false): Tree = {
2178+
def expr(location: Location): Tree = {
21842179
val start = in.offset
21852180
in.token match
21862181
case IMPLICIT =>
@@ -2208,7 +2203,7 @@ object Parsers {
22082203
else new WildcardFunction(placeholderParams.reverse, t)
22092204
finally placeholderParams = saved
22102205

2211-
val t = expr1(location, inStatSeq)
2206+
val t = expr1(location)
22122207
if in.isArrow then
22132208
placeholderParams = Nil // don't interpret `_' to the left of `=>` as placeholder
22142209
wrapPlaceholders(closureRest(start, location, convertToParams(t)))
@@ -2220,7 +2215,7 @@ object Parsers {
22202215
wrapPlaceholders(t)
22212216
}
22222217

2223-
def expr1(location: Location = Location.ElseWhere, inStatSeq: Boolean = false): Tree = in.token match
2218+
def expr1(location: Location = Location.ElseWhere): Tree = in.token match
22242219
case IF =>
22252220
ifExpr(in.offset, If)
22262221
case WHILE =>
@@ -2318,7 +2313,7 @@ object Parsers {
23182313
case t =>
23192314
syntaxError(em"`inline` must be followed by an `if` or a `match`", start)
23202315
t
2321-
else expr1Rest(postfixExpr(location, inStatSeq), location)
2316+
else expr1Rest(postfixExpr(location), location)
23222317
end expr1
23232318

23242319
def expr1Rest(t: Tree, location: Location): Tree =
@@ -2476,8 +2471,8 @@ object Parsers {
24762471
* | InfixExpr id ColonArgument
24772472
* | InfixExpr MatchClause
24782473
*/
2479-
def postfixExpr(location: Location = Location.ElseWhere, inStatSeq: Boolean = false): Tree =
2480-
val t = postfixExprRest(prefixExpr(location, inStatSeq), location)
2474+
def postfixExpr(location: Location = Location.ElseWhere): Tree =
2475+
val t = postfixExprRest(prefixExpr(location), location)
24812476
if location.inArgs && followingIsVararg() then
24822477
Typed(t, atSpan(skipToken()) { Ident(tpnme.WILDCARD_STAR) })
24832478
else
@@ -2490,7 +2485,7 @@ object Parsers {
24902485
/** PrefixExpr ::= [PrefixOperator'] SimpleExpr
24912486
* PrefixOperator ::= ‘-’ | ‘+’ | ‘~’ | ‘!’ (if not backquoted)
24922487
*/
2493-
def prefixExpr(location: Location, inStatSeq: Boolean = false): Tree =
2488+
val prefixExpr: Location => Tree = location =>
24942489
if in.token == IDENTIFIER && nme.raw.isUnary(in.name)
24952490
&& in.canStartExprTokens.contains(in.lookahead.token)
24962491
then
@@ -2500,7 +2495,7 @@ object Parsers {
25002495
simpleExprRest(literal(start), location, canApply = true)
25012496
else
25022497
atSpan(start) { PrefixOp(op, simpleExpr(location)) }
2503-
else simpleExpr(location, inStatSeq)
2498+
else simpleExpr(location)
25042499

25052500
/** SimpleExpr ::= ‘new’ ConstrApp {`with` ConstrApp} [TemplateBody]
25062501
* | ‘new’ TemplateBody
@@ -2526,7 +2521,7 @@ object Parsers {
25262521
* Quoted ::= ‘'’ ‘{’ Block ‘}’
25272522
* | ‘'’ ‘[’ Type ‘]’
25282523
*/
2529-
def simpleExpr(location: Location, inStatSeq: Boolean = false): Tree = {
2524+
def simpleExpr(location: Location): Tree = {
25302525
var canApply = true
25312526
val t = in.token match {
25322527
case XMLSTART =>
@@ -2547,7 +2542,7 @@ object Parsers {
25472542
atSpan(in.offset) { makeTupleOrParens(inParens(exprsInParensOrBindings())) }
25482543
case LBRACE | INDENT =>
25492544
canApply = false
2550-
blockExpr(inStatSeq)
2545+
blockExpr()
25512546
case QUOTE =>
25522547
atSpan(skipToken()) {
25532548
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
@@ -2711,12 +2706,12 @@ object Parsers {
27112706

27122707
/** BlockExpr ::= <<< (CaseClauses | Block) >>>
27132708
*/
2714-
def blockExpr(inStatSeq: Boolean = false): Tree = atSpan(in.offset) {
2709+
def blockExpr(): Tree = atSpan(in.offset) {
27152710
val simplify = in.token == INDENT
27162711
inDefScopeBraces({
27172712
if (in.token == CASE) Match(EmptyTree, caseClauses(() => caseClause()))
27182713
else block(simplify)
2719-
}, inStatSeq = inStatSeq)
2714+
})
27202715
}
27212716

27222717
/** Block ::= BlockStatSeq
@@ -4177,7 +4172,7 @@ object Parsers {
41774172
Template(constr, parents, derived, self, stats)
41784173

41794174
def templateBody(parents: List[Tree], rewriteWithColon: Boolean = true): (ValDef, List[Tree]) =
4180-
val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon = rewriteWithColon)
4175+
val r = inDefScopeBraces(templateStatSeq(), rewriteWithColon)
41814176
if in.token == WITH && parents.isEmpty then
41824177
syntaxError(EarlyDefinitionsNotSupported())
41834178
nextToken()
@@ -4299,7 +4294,7 @@ object Parsers {
42994294
else if (isDefIntro(modifierTokensOrCase))
43004295
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
43014296
else if (isExprIntro)
4302-
stats += expr1(inStatSeq = true)
4297+
stats += expr1()
43034298
else
43044299
empty = true
43054300
statSepOrEnd(stats, noPrevStat = empty)
@@ -4376,7 +4371,7 @@ object Parsers {
43764371
if (in.token == IMPORT)
43774372
stats ++= importClause()
43784373
else if (isExprIntro)
4379-
stats += expr(Location.InBlock, inStatSeq = true)
4374+
stats += expr(Location.InBlock)
43804375
else if in.token == IMPLICIT && !in.inModifierPosition() then
43814376
stats += closure(in.offset, Location.InBlock, modifiers(BitSet(IMPLICIT)))
43824377
else if isIdent(nme.extension) && followingIsExtension() then
@@ -4445,7 +4440,7 @@ object Parsers {
44454440
def skipBracesHook(): Option[Tree] =
44464441
if (in.token == XMLSTART) Some(xmlLiteral()) else None
44474442

4448-
override def blockExpr(inStatSeq: Boolean): Tree = {
4443+
override def blockExpr(): Tree = {
44494444
skipBraces()
44504445
EmptyTree
44514446
}

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

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

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

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

0 commit comments

Comments
 (0)