Skip to content

Commit 2128990

Browse files
author
Lucy Martin
committed
Extra check to avoid converting block expressions on the rhs of an infix expression.
Tests added for: * Original cast as per the ticket should not be changed * Similar match statement that should update * Code blocks in this position, as opposed to a partial function, cant update here * Simple change that should apply but in a code position where the op stack is nonempty * Equivalent code, but passing in the partial function as a single parameter, again, not updating
1 parent 43ed9fd commit 2128990

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ object Parsers {
814814
* 6. the opening brace does not follow a `=>`. The reason for this condition is that
815815
* rewriting back to braces does not work after `=>` (since in most cases braces are omitted
816816
* after a `=>` it would be annoying if braces were inserted).
817+
* 7. not a code block being the input to a direct function call `inst method {\n expr \n}` cannot become
818+
* `inst method :\n expr`
817819
*/
818820
def bracesToIndented[T](body: => T, rewriteWithColon: Boolean): T = {
819821
val underColonSyntax = possibleColonOffset == in.lastOffset
@@ -827,10 +829,20 @@ object Parsers {
827829
}
828830
var canRewrite = allBraces(in.currentRegion) && // test (1)
829831
!testChars(in.lastOffset - 3, " =>") // test(6)
832+
833+
def isStartOfFunction: Boolean =
834+
opStack.headOption.exists(x => x.offset > startOpening && x.offset < endOpening)
830835
val t = enclosed(LBRACE, {
831836
canRewrite &= in.isAfterLineEnd // test (2)
832837
val curOffset = in.offset
833-
try body
838+
try {
839+
val bodyResolved = body
840+
bodyResolved match
841+
case x:(Match | Block) =>
842+
canRewrite &= !isStartOfFunction // test (7)
843+
case _ =>
844+
bodyResolved
845+
}
834846
finally {
835847
canRewrite &= in.isAfterLineEnd && in.offset != curOffset // test (3)(4)
836848
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class CompilationTests {
7575
compileFile("tests/rewrites/i12340.scala", unindentOptions.and("-rewrite")),
7676
compileFile("tests/rewrites/i17187.scala", unindentOptions.and("-rewrite")),
7777
compileFile("tests/rewrites/i17399.scala", unindentOptions.and("-rewrite")),
78+
compileFile("tests/rewrites/i20002.scala", defaultOptions.and("-indent", "-rewrite")),
7879
).checkRewrites()
7980
}
8081

tests/rewrites/i20002.check

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
object Reactions:
2+
def main: Unit =
3+
Reactions += {
4+
case 0 =>
5+
case 1 =>
6+
}
7+
8+
def bar: Int = ???
9+
10+
bar match
11+
case 0 =>
12+
case 1 =>
13+
14+
def partPartial(i: Int): PartialFunction[Int, Unit] =
15+
case `i` =>
16+
17+
Reactions += {
18+
val pp1 = partPartial(1)
19+
val pp2 = partPartial(2)
20+
def codeBlock =
21+
???
22+
???
23+
pp1 orElse pp2
24+
}
25+
26+
val partialFunction = partPartial(1) orElse partPartial(2)
27+
Reactions += {
28+
partialFunction
29+
}
30+
def +=(f: PartialFunction[Int, Unit]) =
31+
???
32+

tests/rewrites/i20002.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
object Reactions {
2+
def main: Unit = {
3+
Reactions += {
4+
case 0 =>
5+
case 1 =>
6+
}
7+
8+
def bar: Int = ???
9+
10+
bar match {
11+
case 0 =>
12+
case 1 =>
13+
}
14+
15+
def partPartial(i: Int): PartialFunction[Int, Unit] = {
16+
case `i` =>
17+
}
18+
19+
Reactions += {
20+
val pp1 = partPartial(1)
21+
val pp2 = partPartial(2)
22+
def codeBlock = {
23+
???
24+
???
25+
}
26+
pp1 orElse pp2
27+
}
28+
29+
val partialFunction = partPartial(1) orElse partPartial(2)
30+
Reactions += {
31+
partialFunction
32+
}
33+
}
34+
def +=(f: PartialFunction[Int, Unit]) = {
35+
???
36+
}
37+
38+
}

0 commit comments

Comments
 (0)