Skip to content

Commit ed70022

Browse files
authored
Merge pull request #12934 from wg-romank/2021-03-18-Parameterless-methods-printer
Closes #11201 Incorrectly rendered signatures for parameterless methods
2 parents b3ade17 + b0a3145 commit ed70022

File tree

7 files changed

+34
-33
lines changed

7 files changed

+34
-33
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
253253

254254
protected def paramsText(lam: LambdaType): Text = {
255255
def paramText(name: Name, tp: Type) =
256-
toText(name) ~ lambdaHash(lam) ~ toTextRHS(tp)
256+
toText(name) ~ lambdaHash(lam) ~ toTextRHS(tp, isParameter = true)
257257
Text(lam.paramNames.lazyZip(lam.paramInfos).map(paramText), ", ")
258258
}
259259

@@ -371,7 +371,7 @@ class PlainPrinter(_ctx: Context) extends Printer {
371371
end decomposeLambdas
372372

373373
/** String representation of a definition's type following its name */
374-
protected def toTextRHS(tp: Type): Text = controlled {
374+
protected def toTextRHS(tp: Type, isParameter: Boolean = false): Text = controlled {
375375
homogenize(tp) match {
376376
case tp: TypeBounds =>
377377
val (tparamStr, rhs) = decomposeLambdas(tp)
@@ -401,7 +401,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
401401
case mt: MethodType =>
402402
toTextGlobal(mt)
403403
case tp: ExprType =>
404-
": => " ~ toTextGlobal(tp.widenExpr)
404+
// parameterless methods require special treatment, see #11201
405+
(if (isParameter) ": => " else ": ") ~ toTextGlobal(tp.widenExpr)
405406
case tp =>
406407
": " ~ toTextGlobal(tp)
407408
}

tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
-- Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:17:27 -----------------------------------------
1414
17 | val d = js.constructorOf[NativeJSClass { def bar: Int }] // error
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16-
| NativeJSClass{bar: => Int} is not a class type
16+
| NativeJSClass{bar: Int} is not a class type
1717
-- Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:19:27 -----------------------------------------
1818
19 | val e = js.constructorOf[JSTrait] // error
1919
| ^^^^^^^
@@ -29,7 +29,7 @@
2929
-- Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:23:27 -----------------------------------------
3030
23 | val h = js.constructorOf[JSClass { def bar: Int }] // error
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^
32-
| JSClass{bar: => Int} is not a class type
32+
| JSClass{bar: Int} is not a class type
3333
-- Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:25:42 -----------------------------------------
3434
25 | def foo[A <: js.Any] = js.constructorOf[A] // error
3535
| ^

tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
-- Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:17:59 ----------------------------------------
1414
17 | val d = js.constructorTag[NativeJSClass { def bar: Int }] // error
1515
| ^
16-
| NativeJSClass{bar: => Int} is not a class type
16+
| NativeJSClass{bar: Int} is not a class type
1717
-- Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:19:36 ----------------------------------------
1818
19 | val e = js.constructorTag[JSTrait] // error
1919
| ^
@@ -29,7 +29,7 @@
2929
-- Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:23:53 ----------------------------------------
3030
23 | val h = js.constructorTag[JSClass { def bar: Int }] // error
3131
| ^
32-
| JSClass{bar: => Int} is not a class type
32+
| JSClass{bar: Int} is not a class type
3333
-- Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:25:45 ----------------------------------------
3434
25 | def foo[A <: js.Any] = js.constructorTag[A] // error
3535
| ^

tests/neg/abstract-givens.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/neg/abstract-givens.scala:11:8 -------------------------------------------------------------------------
22
11 | given s[T](using T): Seq[T] with // error
33
| ^
4-
|instance cannot be created, since def iterator: => Iterator[A] in trait IterableOnce in package scala.collection is not defined
4+
|instance cannot be created, since def iterator: Iterator[A] in trait IterableOnce in package scala.collection is not defined
55
-- [E164] Declaration Error: tests/neg/abstract-givens.scala:8:8 -------------------------------------------------------
66
8 | given y(using Int): String = summon[Int].toString * 22 // error
77
| ^

tests/neg/doubleDefinition.check

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@
2020
| ^
2121
| Double definition:
2222
| val foo: Int in class Test4 at line 25 and
23-
| def foo: => Int in class Test4 at line 26
23+
| def foo: Int in class Test4 at line 26
2424
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:31:4 ----------------------------------------------------------
2525
31 | val foo = 1 // error
2626
| ^
2727
| Double definition:
28-
| def foo: => Int in class Test4b at line 30 and
28+
| def foo: Int in class Test4b at line 30 and
2929
| val foo: Int in class Test4b at line 31
3030
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:36:4 ----------------------------------------------------------
3131
36 | var foo = 1 // error
3232
| ^
3333
| Double definition:
34-
| def foo: => Int in class Test4c at line 35 and
34+
| def foo: Int in class Test4c at line 35 and
3535
| var foo: Int in class Test4c at line 36
3636
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:41:4 ----------------------------------------------------------
3737
41 | def foo = 2 // error
3838
| ^
3939
| Double definition:
4040
| var foo: Int in class Test4d at line 40 and
41-
| def foo: => Int in class Test4d at line 41
41+
| def foo: Int in class Test4d at line 41
4242
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:55:4 ----------------------------------------------------------
4343
55 | def foo(x: List[B]): Function1[B, B] = ??? // error: same jvm signature
4444
| ^
@@ -61,70 +61,70 @@
6161
| ^
6262
| Double definition:
6363
| val foo: Int in class Test8 at line 66 and
64-
| def foo: => Int in class Test8 at line 67
64+
| def foo: Int in class Test8 at line 67
6565
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:72:4 ----------------------------------------------------------
6666
72 | val foo = 1 // error
6767
| ^
6868
| Double definition:
69-
| def foo: => Int in class Test8b at line 71 and
69+
| def foo: Int in class Test8b at line 71 and
7070
| val foo: Int in class Test8b at line 72
7171
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:77:4 ----------------------------------------------------------
7272
77 | var foo = 1 // error
7373
| ^
7474
| Double definition:
75-
| def foo: => Int in class Test8c at line 76 and
75+
| def foo: Int in class Test8c at line 76 and
7676
| var foo: Int in class Test8c at line 77
7777
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:82:4 ----------------------------------------------------------
7878
82 | def foo = 2 // error
7979
| ^
8080
| Double definition:
8181
| var foo: Int in class Test8d at line 81 and
82-
| def foo: => Int in class Test8d at line 82
82+
| def foo: Int in class Test8d at line 82
8383
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:88:4 ----------------------------------------------------------
8484
88 | def foo: String // error
8585
| ^
8686
| Double definition:
8787
| val foo: Int in class Test9 at line 87 and
88-
| def foo: => String in class Test9 at line 88
88+
| def foo: String in class Test9 at line 88
8989
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:92:4 ----------------------------------------------------------
9090
92 | def foo: Int // error
9191
| ^
9292
| Double definition:
9393
| val foo: Int in class Test10 at line 91 and
94-
| def foo: => Int in class Test10 at line 92
94+
| def foo: Int in class Test10 at line 92
9595
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:96:4 ----------------------------------------------------------
9696
96 | def foo: String // error
9797
| ^
9898
| Double definition:
9999
| val foo: Int in class Test11 at line 95 and
100-
| def foo: => String in class Test11 at line 96
100+
| def foo: String in class Test11 at line 96
101101
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:100:4 ---------------------------------------------------------
102102
100 | def foo: Int // error
103103
| ^
104104
| Double definition:
105105
| val foo: Int in class Test12 at line 99 and
106-
| def foo: => Int in class Test12 at line 100
106+
| def foo: Int in class Test12 at line 100
107107
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:104:4 ---------------------------------------------------------
108108
104 | def foo: String // error
109109
| ^
110110
| Double definition:
111111
| var foo: Int in class Test13 at line 103 and
112-
| def foo: => String in class Test13 at line 104
112+
| def foo: String in class Test13 at line 104
113113
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:108:4 ---------------------------------------------------------
114114
108 | def foo: Int // error
115115
| ^
116116
| Double definition:
117117
| var foo: Int in class Test14 at line 107 and
118-
| def foo: => Int in class Test14 at line 108
118+
| def foo: Int in class Test14 at line 108
119119
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:112:4 ---------------------------------------------------------
120120
112 | def foo: String // error
121121
| ^
122122
| Double definition:
123123
| var foo: Int in class Test15 at line 111 and
124-
| def foo: => String in class Test15 at line 112
124+
| def foo: String in class Test15 at line 112
125125
-- [E120] Naming Error: tests/neg/doubleDefinition.scala:116:4 ---------------------------------------------------------
126126
116 | def foo: Int // error
127127
| ^
128128
| Double definition:
129129
| var foo: Int in class Test16 at line 115 and
130-
| def foo: => Int in class Test16 at line 116
130+
| def foo: Int in class Test16 at line 116

tests/neg/exports.check

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
23 | export printUnit.{stat => _, _} // error: double definition
1717
| ^
1818
| Double definition:
19-
| def status: => List[String] in class Copier at line 28 and
20-
| final def status: => List[String] in class Copier at line 23
19+
| def status: List[String] in class Copier at line 28 and
20+
| final def status: List[String] in class Copier at line 23
2121
| have the same type after erasure.
2222
|
2323
| Consider adding a @targetName annotation to one of the conflicting definitions
@@ -26,8 +26,8 @@
2626
24 | export scanUnit._ // error: double definition
2727
| ^
2828
| Double definition:
29-
| final def status: => List[String] in class Copier at line 23 and
30-
| final def status: => List[String] in class Copier at line 24
29+
| final def status: List[String] in class Copier at line 23 and
30+
| final def status: List[String] in class Copier at line 24
3131
| have the same type after erasure.
3232
|
3333
| Consider adding a @targetName annotation to one of the conflicting definitions
@@ -36,8 +36,8 @@
3636
26 | export printUnit.status // error: double definition
3737
| ^
3838
| Double definition:
39-
| final def status: => List[String] in class Copier at line 24 and
40-
| final def status: => List[String] in class Copier at line 26
39+
| final def status: List[String] in class Copier at line 24 and
40+
| final def status: List[String] in class Copier at line 26
4141
| have the same type after erasure.
4242
|
4343
| Consider adding a @targetName annotation to one of the conflicting definitions
@@ -57,4 +57,4 @@
5757
| ^
5858
| Double definition:
5959
| val bar: Bar in class Baz at line 45 and
60-
| final def bar: => (Baz.this.bar.bar : => (Baz.this.bar.baz.bar : Bar)) in class Baz at line 46
60+
| final def bar: (Baz.this.bar.bar : => (Baz.this.bar.baz.bar : Bar)) in class Baz at line 46

tests/neg/i9329.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Error: tests/neg/i9329.scala:8:6 ------------------------------------------------------------------------------------
22
8 |class GrandSon extends Son // error
33
| ^
4-
|class GrandSon needs to be abstract, since def name: => String in trait Parent is not defined
5-
|(The class implements abstract override def name: => String in trait Son but that definition still needs an implementation)
4+
|class GrandSon needs to be abstract, since def name: String in trait Parent is not defined
5+
|(The class implements abstract override def name: String in trait Son but that definition still needs an implementation)

0 commit comments

Comments
 (0)