Skip to content

Commit 8d8dfff

Browse files
committed
Fix #4068: Fix pretty-printing precedence for function types
Using `atPrec(GlobalPrec)` ensures that `argText(args.head)` never adds parentheses around its output, but that's inappropriate for a function type `T1 => T2`. The nested calls to `atPrec` make no sense, but `argText` hid that before it was refactored.
1 parent df2189b commit 8d8dfff

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
136136
changePrec(GlobalPrec) {
137137
val argStr: Text =
138138
if (args.length == 2 && !defn.isTupleType(args.head))
139-
atPrec(InfixPrec) { atPrec(GlobalPrec) { argText(args.head) } }
139+
atPrec(InfixPrec) { argText(args.head) }
140140
else
141141
toTextTuple(args.init)
142142
(keywordText("erased ") provided isErased) ~ (keywordText("implicit ") provided isImplicit) ~ argStr ~ " => " ~ argText(args.last)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
scala> val toInt: Any => Int = new { def apply(a: Any) = 1; override def toString() = "<func1>" }
22
val toInt: Any => Int = <func1>
3+
scala> val hoFun: (Int => Int) => Int = new { def apply(a: Int => Int) = 1; override def toString() = "<func2>" }
4+
val hoFun: (Int => Int) => Int = <func2>
5+
scala> val curriedFun: Int => (Int => Int) = new { def apply(a: Int) = _ => 1; override def toString() = "<func3>" }
6+
val curriedFun: Int => Int => Int = <func3>
7+
scala> val tupFun: ((Int, Int)) => Int = new { def apply(a: (Int, Int)) = 1; override def toString() = "<func4>" }
8+
val tupFun: ((Int, Int)) => Int = <func4>
9+
scala> val binFun: (Int, Int) => Int = new { def apply(a: Int, b: Int) = 1; override def toString() = "<func5>" }
10+
val binFun: (Int, Int) => Int = <func5>

0 commit comments

Comments
 (0)