From c5fb061023438a6cc1c8cf4fac2a991d4d13d9af Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 28 Mar 2019 11:33:13 +0100 Subject: [PATCH 1/2] Fix #5997: Add missing parentheses --- compiler/test/dotc/pos-recompilation.whitelist | 1 + library/src/scala/tasty/reflect/Printers.scala | 10 +++++++++- tests/pos/i5997.decompiled | 10 ++++++++++ tests/pos/i5997.scala | 7 +++++++ tests/run-with-compiler/i5997.check | 1 + tests/run-with-compiler/i5997.scala | 7 +++++++ 6 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i5997.decompiled create mode 100644 tests/pos/i5997.scala create mode 100644 tests/run-with-compiler/i5997.check create mode 100644 tests/run-with-compiler/i5997.scala diff --git a/compiler/test/dotc/pos-recompilation.whitelist b/compiler/test/dotc/pos-recompilation.whitelist index 90b0cfb3314b..bea58e3d9894 100644 --- a/compiler/test/dotc/pos-recompilation.whitelist +++ b/compiler/test/dotc/pos-recompilation.whitelist @@ -300,6 +300,7 @@ i4819 i4984 i4999 i5090 +i5997 i518 i5188 i523 diff --git a/library/src/scala/tasty/reflect/Printers.scala b/library/src/scala/tasty/reflect/Printers.scala index de7c658d11c3..7ed6852ee3a1 100644 --- a/library/src/scala/tasty/reflect/Printers.scala +++ b/library/src/scala/tasty/reflect/Printers.scala @@ -758,7 +758,7 @@ trait Printers printType(tree.tpe) case Term.Select(qual, name) => - printTree(qual) + printQualTree(qual) if (name != "" && name != "package") this += "." += name this @@ -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 { diff --git a/tests/pos/i5997.decompiled b/tests/pos/i5997.decompiled new file mode 100644 index 000000000000..2b0a31328331 --- /dev/null +++ b/tests/pos/i5997.decompiled @@ -0,0 +1,10 @@ +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()) +} diff --git a/tests/pos/i5997.scala b/tests/pos/i5997.scala new file mode 100644 index 000000000000..e95df90952ec --- /dev/null +++ b/tests/pos/i5997.scala @@ -0,0 +1,7 @@ +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) +} diff --git a/tests/run-with-compiler/i5997.check b/tests/run-with-compiler/i5997.check new file mode 100644 index 000000000000..d5cb09f92a92 --- /dev/null +++ b/tests/run-with-compiler/i5997.check @@ -0,0 +1 @@ +(if (true) scala.Some.apply[scala.Int](1) else scala.None).map[scala.Int](((v: scala.Int) => v.+(1))) diff --git a/tests/run-with-compiler/i5997.scala b/tests/run-with-compiler/i5997.scala new file mode 100644 index 000000000000..01d8a70aa47a --- /dev/null +++ b/tests/run-with-compiler/i5997.scala @@ -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) + } +} From 716c064c484db340b014b5c4713a5df0425b1f0e Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 28 Mar 2019 16:23:35 +0100 Subject: [PATCH 2/2] Add other missing parentheses --- library/src/scala/tasty/reflect/Printers.scala | 6 +++--- tests/pos/i5997.decompiled | 6 ++++++ tests/pos/i5997.scala | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/library/src/scala/tasty/reflect/Printers.scala b/library/src/scala/tasty/reflect/Printers.scala index 7ed6852ee3a1..4c731578a5b1 100644 --- a/library/src/scala/tasty/reflect/Printers.scala +++ b/library/src/scala/tasty/reflect/Printers.scala @@ -809,7 +809,7 @@ trait Printers case Term.Apply(fn, args) => fn match { case Term.Select(Term.This(_), "") => 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 @@ -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(_, _)), "") => // type bounds already printed in `fn` @@ -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())) diff --git a/tests/pos/i5997.decompiled b/tests/pos/i5997.decompiled index 2b0a31328331..4a61210ebb13 100644 --- a/tests/pos/i5997.decompiled +++ b/tests/pos/i5997.decompiled @@ -7,4 +7,10 @@ class Test() { }).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) } diff --git a/tests/pos/i5997.scala b/tests/pos/i5997.scala index e95df90952ec..6c7234cf55e3 100644 --- a/tests/pos/i5997.scala +++ b/tests/pos/i5997.scala @@ -4,4 +4,7 @@ class Test { 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) }