Skip to content

Commit 8297843

Browse files
committed
SI-6434 Pretty print function types with by name arg as (=> A) => B
We were pretty printing a function type with one by name arg as => A => B, but because => is right associative that's formally equivalent to => (A => B) and that's entirely a different thing. This commit changes the pretty printer in Typers.scala to check for a byname argument on a function type and wrap it in parens. A REPL test is included.
1 parent 884737c commit 8297843

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,8 +2481,10 @@ trait Types extends api.Types { self: SymbolTable =>
24812481
// from (T1, T2) => R.
24822482
targs match {
24832483
case in :: out :: Nil if !isTupleType(in) =>
2484-
// A => B => C should be (A => B) => C or A => (B => C)
2485-
val in_s = if (isFunctionType(in)) "(" + in + ")" else "" + in
2484+
// A => B => C should be (A => B) => C or A => (B => C).
2485+
// Also if A is byname, then we want (=> A) => B because => is right associative and => A => B
2486+
// would mean => (A => B) which is a different type
2487+
val in_s = if (isFunctionType(in) || isByNameParamType(in)) "(" + in + ")" else "" + in
24862488
val out_s = if (isFunctionType(out)) "(" + out + ")" else "" + out
24872489
in_s + " => " + out_s
24882490
case xs =>

test/files/run/t6434.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Type in expressions to have them evaluated.
2+
Type :help for more information.
3+
4+
scala> def f(x: => Int): Int = x
5+
f: (x: => Int)Int
6+
7+
scala> f _
8+
res0: (=> Int) => Int = <function1>
9+
10+
scala>

test/files/run/t6434.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.tools.partest.ReplTest
2+
3+
object Test extends ReplTest {
4+
def code =
5+
"""def f(x: => Int): Int = x
6+
f _
7+
"""
8+
}

0 commit comments

Comments
 (0)