Skip to content

Commit

Permalink
Refining the previous to only apply to symbolic methods, not quoted, …
Browse files Browse the repository at this point in the history
…standard or op-suffix method names
  • Loading branch information
Lucy Martin committed Apr 2, 2024
1 parent 2128990 commit 327a253
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
18 changes: 13 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -814,8 +814,8 @@ object Parsers {
* 6. the opening brace does not follow a `=>`. The reason for this condition is that
* rewriting back to braces does not work after `=>` (since in most cases braces are omitted
* after a `=>` it would be annoying if braces were inserted).
* 7. not a code block being the input to a direct function call `inst method {\n expr \n}` cannot become
* `inst method :\n expr`
* 7. not a code block being the input to a direct symbolic function call `inst method {\n expr \n}` cannot
* become `inst method :\n expr` for a fully symbolic method
*/
def bracesToIndented[T](body: => T, rewriteWithColon: Boolean): T = {
val underColonSyntax = possibleColonOffset == in.lastOffset
Expand All @@ -830,16 +830,24 @@ object Parsers {
var canRewrite = allBraces(in.currentRegion) && // test (1)
!testChars(in.lastOffset - 3, " =>") // test(6)

def isStartOfFunction: Boolean =
opStack.headOption.exists(x => x.offset > startOpening && x.offset < endOpening)
def isStartOfSymbolicFunction: Boolean =
opStack.headOption.exists { x =>
val bq = x.operator.isBackquoted
val op = x.operator.name.toSimpleName.decode.forall {
Chars.isOperatorPart
}
val loc = startOpening < x.offset && x.offset < endOpening
val res = !bq && op && loc
res
}
val t = enclosed(LBRACE, {
canRewrite &= in.isAfterLineEnd // test (2)
val curOffset = in.offset
try {
val bodyResolved = body
bodyResolved match
case x:(Match | Block) =>
canRewrite &= !isStartOfFunction // test (7)
canRewrite &= !isStartOfSymbolicFunction // test (7)
case _ =>
bodyResolved
}
Expand Down
19 changes: 19 additions & 0 deletions tests/rewrites/i20002.check
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ object Reactions:
case 1 =>
}

Reactions run:
case 0 =>
case 1 =>

Reactions run_+ :
case 0 =>
case 1 =>

Reactions `+=`:
case 0 =>
case 1 =>

def bar: Int = ???

bar match
Expand All @@ -27,6 +39,13 @@ object Reactions:
Reactions += {
partialFunction
}

def +=(f: PartialFunction[Int, Unit]) =
???

def run (f: PartialFunction[Int, Unit]) =
???

def run_+ (f: PartialFunction[Int, Unit]) =
???

24 changes: 24 additions & 0 deletions tests/rewrites/i20002.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ object Reactions {
case 1 =>
}

Reactions run {
case 0 =>
case 1 =>
}

Reactions run_+ {
case 0 =>
case 1 =>
}

Reactions `+=` {
case 0 =>
case 1 =>
}

def bar: Int = ???

bar match {
Expand All @@ -31,8 +46,17 @@ object Reactions {
partialFunction
}
}

def +=(f: PartialFunction[Int, Unit]) = {
???
}

def run (f: PartialFunction[Int, Unit]) = {
???
}

def run_+ (f: PartialFunction[Int, Unit]) = {
???
}

}

0 comments on commit 327a253

Please sign in to comment.