Skip to content

Fix #5997: Add missing parentheses #6182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/test/dotc/pos-recompilation.whitelist
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ i4819
i4984
i4999
i5090
i5997
i518
i5188
i523
Expand Down
16 changes: 12 additions & 4 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ trait Printers
printType(tree.tpe)

case Term.Select(qual, name) =>
printTree(qual)
printQualTree(qual)
if (name != "<init>" && name != "package")
this += "." += name
this
Expand Down Expand Up @@ -809,7 +809,7 @@ trait Printers
case Term.Apply(fn, args) =>
fn match {
case Term.Select(Term.This(_), "<init>") => this += "this" // call to constructor inside a constructor
case _ => printTree(fn)
case _ => printQualTree(fn)
}
val args1 = args match {
case init :+ Term.Typed(Term.Repeated(Nil, _), _) => init // drop empty var args at the end
Expand All @@ -819,7 +819,7 @@ trait Printers
inParens(printTrees(args1, ", "))

case Term.TypeApply(fn, args) =>
printTree(fn)
printQualTree(fn)
fn match {
case Term.Select(Term.New(TypeTree.Applied(_, _)), "<init>") =>
// type bounds already printed in `fn`
Expand Down Expand Up @@ -894,7 +894,7 @@ trait Printers
printTree(elsep)

case Term.Match(selector, cases) =>
printTree(selector)
printQualTree(selector)
this += highlightKeyword(" match", color)
inBlock(printCases(cases, lineBreak()))

Expand Down Expand Up @@ -941,6 +941,14 @@ trait Printers

}

def printQualTree(tree: Tree): Buffer = tree match {
case Term.IsIf(_) | Term.IsMatch(_) | Term.IsWhile(_) | Term.IsTry(_) | Term.IsReturn(_) =>
this += "("
printTree(tree)
this += ")"
case _ => printTree(tree)
}

def flatBlock(stats: List[Statement], expr: Term): (List[Statement], Term) = {
val flatStats = List.newBuilder[Statement]
def extractFlatStats(stat: Statement): Unit = stat match {
Expand Down
16 changes: 16 additions & 0 deletions tests/pos/i5997.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Test() {
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)))
val v2: scala.Option[scala.Int] = (try scala.Some.apply[scala.Int](1) finally ()).map[scala.Int](((v: scala.Int) => v.+(1)))
val v3: scala.Option[scala.Int] = (1 match {
case _ =>
scala.Some.apply[scala.Int](1)
}).map[scala.Int](((v: scala.Int) => v.+(1)))
val v4: java.lang.String = (while (true) ()).toString()
def v5: scala.Option[scala.Predef.String] = scala.Some.apply[java.lang.String]((return scala.Some.apply[java.lang.String]("a")).toString())
def foo(x: scala.Boolean): scala.Boolean = x.unary_!
def bar(): scala.Int = (if (true) 1 else 2) match {
case x =>
(x: scala.Int)
}
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)
}
10 changes: 10 additions & 0 deletions tests/pos/i5997.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Test {
val v1 = (if true then Some(1) else None).map(v => v+1)
val v2 = (try Some(1) finally {}).map(v => v+1)
val v3 = (1 match { case _ => Some(1) }).map(v => v+1)
val v4 = (while (true) ()).toString
def v5: Option[String] = Some((return Some("a")).toString)
def foo(x: Boolean) = !x
def bar() = (if true then 1 else 2) match { case x => x }
def baz() = (if true then identity else identity)(0)
}
1 change: 1 addition & 0 deletions tests/run-with-compiler/i5997.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1)))
7 changes: 7 additions & 0 deletions tests/run-with-compiler/i5997.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def main(args: Array[String]): Unit = {
import quoted.Toolbox.Default._
val v = '{ (if true then Some(1) else None).map(v => v+1) }
println(v.show)
}
}