Skip to content

Commit 1818972

Browse files
Merge pull request #6182 from dotty-staging/fix-#5997
Fix #5997: Add missing parentheses
2 parents 5b53cd8 + 716c064 commit 1818972

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

compiler/test/dotc/pos-recompilation.whitelist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ i4819
300300
i4984
301301
i4999
302302
i5090
303+
i5997
303304
i518
304305
i5188
305306
i523

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ trait Printers
758758
printType(tree.tpe)
759759

760760
case Term.Select(qual, name) =>
761-
printTree(qual)
761+
printQualTree(qual)
762762
if (name != "<init>" && name != "package")
763763
this += "." += name
764764
this
@@ -809,7 +809,7 @@ trait Printers
809809
case Term.Apply(fn, args) =>
810810
fn match {
811811
case Term.Select(Term.This(_), "<init>") => this += "this" // call to constructor inside a constructor
812-
case _ => printTree(fn)
812+
case _ => printQualTree(fn)
813813
}
814814
val args1 = args match {
815815
case init :+ Term.Typed(Term.Repeated(Nil, _), _) => init // drop empty var args at the end
@@ -819,7 +819,7 @@ trait Printers
819819
inParens(printTrees(args1, ", "))
820820

821821
case Term.TypeApply(fn, args) =>
822-
printTree(fn)
822+
printQualTree(fn)
823823
fn match {
824824
case Term.Select(Term.New(TypeTree.Applied(_, _)), "<init>") =>
825825
// type bounds already printed in `fn`
@@ -894,7 +894,7 @@ trait Printers
894894
printTree(elsep)
895895

896896
case Term.Match(selector, cases) =>
897-
printTree(selector)
897+
printQualTree(selector)
898898
this += highlightKeyword(" match", color)
899899
inBlock(printCases(cases, lineBreak()))
900900

@@ -941,6 +941,14 @@ trait Printers
941941

942942
}
943943

944+
def printQualTree(tree: Tree): Buffer = tree match {
945+
case Term.IsIf(_) | Term.IsMatch(_) | Term.IsWhile(_) | Term.IsTry(_) | Term.IsReturn(_) =>
946+
this += "("
947+
printTree(tree)
948+
this += ")"
949+
case _ => printTree(tree)
950+
}
951+
944952
def flatBlock(stats: List[Statement], expr: Term): (List[Statement], Term) = {
945953
val flatStats = List.newBuilder[Statement]
946954
def extractFlatStats(stat: Statement): Unit = stat match {

tests/pos/i5997.decompiled

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Test() {
2+
val v1: scala.Option[scala.Int] = (if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))
3+
val v2: scala.Option[scala.Int] = (try scala.Some.apply[scala.Int](1) finally ()).map[scala.Int](((v: scala.Int) => v.+(1)))
4+
val v3: scala.Option[scala.Int] = (1 match {
5+
case _ =>
6+
scala.Some.apply[scala.Int](1)
7+
}).map[scala.Int](((v: scala.Int) => v.+(1)))
8+
val v4: java.lang.String = (while (true) ()).toString()
9+
def v5: scala.Option[scala.Predef.String] = scala.Some.apply[java.lang.String]((return scala.Some.apply[java.lang.String]("a")).toString())
10+
def foo(x: scala.Boolean): scala.Boolean = x.unary_!
11+
def bar(): scala.Int = (if (true) 1 else 2) match {
12+
case x =>
13+
(x: scala.Int)
14+
}
15+
def baz(): scala.Any = (if (true) ((x: scala.Any) => scala.Predef.identity[scala.Any](x)) else ((x: scala.Any) => scala.Predef.identity[scala.Any](x))).apply(0)
16+
}

tests/pos/i5997.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Test {
2+
val v1 = (if true then Some(1) else None).map(v => v+1)
3+
val v2 = (try Some(1) finally {}).map(v => v+1)
4+
val v3 = (1 match { case _ => Some(1) }).map(v => v+1)
5+
val v4 = (while (true) ()).toString
6+
def v5: Option[String] = Some((return Some("a")).toString)
7+
def foo(x: Boolean) = !x
8+
def bar() = (if true then 1 else 2) match { case x => x }
9+
def baz() = (if true then identity else identity)(0)
10+
}

tests/run-with-compiler/i5997.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))

tests/run-with-compiler/i5997.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
import quoted.Toolbox.Default._
4+
val v = '{ (if true then Some(1) else None).map(v => v+1) }
5+
println(v.show)
6+
}
7+
}

0 commit comments

Comments
 (0)