Skip to content

Commit cc4c279

Browse files
authored
Make refined type printing more source compatible (#16303)
Fixes #12939
2 parents d3c1c4e + b2f4cbe commit cc4c279

File tree

13 files changed

+56
-33
lines changed

13 files changed

+56
-33
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,13 @@ class PlainPrinter(_ctx: Context) extends Printer {
112112

113113
/** String representation of a refinement */
114114
protected def toTextRefinement(rt: RefinedType): Text =
115-
(refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close
115+
val keyword = rt.refinedInfo match {
116+
case _: ExprType | _: MethodOrPoly => "def "
117+
case _: TypeBounds => "type "
118+
case _: TypeProxy => "val "
119+
case _ => ""
120+
}
121+
(keyword ~ refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close
116122

117123
protected def argText(arg: Type): Text = homogenizeArg(arg) match {
118124
case arg: TypeBounds => "?" ~ toText(arg)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
scala> case class Bag() extends reflect.Selectable
2+
// defined case class Bag
3+
scala> val m = new Bag { val f = 23; def g = 47; def h(i: Int): Int = i; var i = 101; type N = Int; val l = List(42); def p[T](t: T) = t.toString() }
4+
val m:
5+
Bag{
6+
val f: Int; def g: Int; def h(i: Int): Int; val i: Int;
7+
def i_=(x$1: Int): Unit; type N = Int; val l: List[Int];
8+
def p[T](t: T): String
9+
} = Bag()
10+
scala> type t = Bag { val f: Int; def g: Int; def h(i: Int): Int; val i: Int; def i_=(x$1: Int): Unit; type N = Int; val l: List[Int]; val s: String @unchecked }
11+
// defined alias type t
12+
=
13+
Bag{
14+
val f: Int; def g: Int; def h(i: Int): Int; val i: Int;
15+
def i_=(x$1: Int): Unit; type N = Int; val l: List[Int];
16+
val s: String @unchecked
17+
}

tests/neg-custom-args/captures/i15116.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22
3 | val x = Foo(m) // error
33
| ^^^^^^^^^^^^^^
44
| Non-local value x cannot have an inferred type
5-
| {Bar.this.m} Foo{m: {Bar.this.m} String}
5+
| {Bar.this.m} Foo{val m: {Bar.this.m} String}
66
| with non-empty capture set {Bar.this.m}.
77
| The type needs to be declared explicitly.
88
-- Error: tests/neg-custom-args/captures/i15116.scala:5:6 --------------------------------------------------------------
99
5 | val x = Foo(m) // error
1010
| ^^^^^^^^^^^^^^
1111
| Non-local value x cannot have an inferred type
12-
| {Baz.this} Foo{m: {*} String}
12+
| {Baz.this} Foo{val m: {*} String}
1313
| with non-empty capture set {Baz.this}.
1414
| The type needs to be declared explicitly.
1515
-- Error: tests/neg-custom-args/captures/i15116.scala:7:6 --------------------------------------------------------------
1616
7 | val x = Foo(m) // error
1717
| ^^^^^^^^^^^^^^
1818
| Non-local value x cannot have an inferred type
19-
| {Bar1.this.m} Foo{m: {Bar1.this.m} String}
19+
| {Bar1.this.m} Foo{val m: {Bar1.this.m} String}
2020
| with non-empty capture set {Bar1.this.m}.
2121
| The type needs to be declared explicitly.
2222
-- Error: tests/neg-custom-args/captures/i15116.scala:9:6 --------------------------------------------------------------
2323
9 | val x = Foo(m) // error
2424
| ^^^^^^^^^^^^^^
2525
| Non-local value x cannot have an inferred type
26-
| {Baz2.this} Foo{m: {*} String}
26+
| {Baz2.this} Foo{val m: {*} String}
2727
| with non-empty capture set {Baz2.this}.
2828
| The type needs to be declared explicitly.

tests/neg-custom-args/captures/i15772.check

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:20:49 ---------------------------------------
22
20 | val boxed1 : (({*} C) => Unit) -> Unit = box1(c) // error
33
| ^^^^^^^
4-
| Found: {c} ({*} ({c} C{arg: {*} C}) -> Unit) -> Unit
4+
| Found: {c} ({*} ({c} C{val arg: {*} C}) -> Unit) -> Unit
55
| Required: (({*} C) => Unit) -> Unit
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:27:38 ---------------------------------------
99
27 | val boxed2 : Observe[{*} C] = box2(c) // error
1010
| ^^^^^^^
11-
| Found: {c} ({*} ({c} C{arg: {*} C}) -> Unit) -> Unit
11+
| Found: {c} ({*} ({c} C{val arg: {*} C}) -> Unit) -> Unit
1212
| Required: Observe[{*} C]
1313
|
1414
| longer explanation available when compiling with `-explain`
1515
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:33:37 ---------------------------------------
1616
33 | val boxed2 : Observe[{*} C] = box2(c) // error
1717
| ^
1818
| Found: {*} C
19-
| Required: box {*} C{arg: ? C}
19+
| Required: box {*} C{val arg: ? C}
2020
|
2121
| longer explanation available when compiling with `-explain`
2222
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:44:2 ----------------------------------------

tests/neg-custom-args/captures/lazylist.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylist.scala:35:29 -------------------------------------
1616
35 | val ref1c: LazyList[Int] = ref1 // error
1717
| ^^^^
18-
| Found: (ref1 : {cap1} lazylists.LazyCons[Int]{xs: {cap1} () -> {*} lazylists.LazyList[Int]})
19-
| Required: lazylists.LazyList[Int]
18+
| Found: (ref1 : {cap1} lazylists.LazyCons[Int]{val xs: {cap1} () -> {*} lazylists.LazyList[Int]})
19+
| Required: lazylists.LazyList[Int]
2020
|
2121
| longer explanation available when compiling with `-explain`
2222
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylist.scala:37:36 -------------------------------------
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:19:28 --------------------------------------
22
19 | val ref1c: LazyRef[Int] = ref1 // error
33
| ^^^^
4-
| Found: (ref1 : {cap1} LazyRef[Int]{elem: {cap1} () -> Int})
4+
| Found: (ref1 : {cap1} LazyRef[Int]{val elem: {cap1} () -> Int})
55
| Required: LazyRef[Int]
66
|
77
| longer explanation available when compiling with `-explain`
88
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:21:35 --------------------------------------
99
21 | val ref2c: {cap2} LazyRef[Int] = ref2 // error
1010
| ^^^^
11-
| Found: (ref2 : {cap2, ref1} LazyRef[Int]{elem: {*} () -> Int})
11+
| Found: (ref2 : {cap2, ref1} LazyRef[Int]{val elem: {*} () -> Int})
1212
| Required: {cap2} LazyRef[Int]
1313
|
1414
| longer explanation available when compiling with `-explain`
1515
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:23:35 --------------------------------------
1616
23 | val ref3c: {ref1} LazyRef[Int] = ref3 // error
1717
| ^^^^
18-
| Found: (ref3 : {cap2, ref1} LazyRef[Int]{elem: {*} () -> Int})
18+
| Found: (ref3 : {cap2, ref1} LazyRef[Int]{val elem: {*} () -> Int})
1919
| Required: {ref1} LazyRef[Int]
2020
|
2121
| longer explanation available when compiling with `-explain`
2222
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:25:35 --------------------------------------
2323
25 | val ref4c: {cap1} LazyRef[Int] = ref4 // error
2424
| ^^^^
25-
| Found: (ref4 : {cap2, cap1} LazyRef[Int]{elem: {*} () -> Int})
25+
| Found: (ref4 : {cap2, cap1} LazyRef[Int]{val elem: {*} () -> Int})
2626
| Required: {cap1} LazyRef[Int]
2727
|
2828
| longer explanation available when compiling with `-explain`

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
-- [E170] Type 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{def 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
-- [E170] Type 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{def bar: Int} is not a class type
3333
-- [E170] Type 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
-- [E170] Type 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{def 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
-- [E170] Type 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{def bar: Int} is not a class type
3333
-- [E170] Type 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/classOf.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
-- [E170] Type Error: tests/neg/classOf.scala:9:18 ---------------------------------------------------------------------
1212
9 | val y = classOf[C { type I = String }] // error
1313
| ^^^^^^^^^^^^^^^^^^^^^
14-
| Test.C{I = String} is not a class type
14+
| Test.C{type I = String} is not a class type

tests/neg/i14025.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- Error: tests/neg/i14025.scala:1:88 ----------------------------------------------------------------------------------
22
1 |val foo = summon[deriving.Mirror.Product { type MirroredType = [X] =>> [Y] =>> (X, Y) }] // error
33
| ^
4-
|No given instance of type deriving.Mirror.Product{MirroredType[X] = [Y] =>> (X, Y)} was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Product{MirroredType[X] = [Y] =>> (X, Y)}: type `[X] =>> [Y] =>> (X, Y)` is not a generic product because its subpart `[X] =>> [Y] =>> (X, Y)` is not a supported kind (either `*` or `* -> *`)
4+
|No given instance of type deriving.Mirror.Product{type MirroredType[X] = [Y] =>> (X, Y)} was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Product{type MirroredType[X] = [Y] =>> (X, Y)}: type `[X] =>> [Y] =>> (X, Y)` is not a generic product because its subpart `[X] =>> [Y] =>> (X, Y)` is not a supported kind (either `*` or `* -> *`)
55
-- Error: tests/neg/i14025.scala:2:90 ----------------------------------------------------------------------------------
66
2 |val bar = summon[deriving.Mirror.Sum { type MirroredType = [X] =>> [Y] =>> List[(X, Y)] }] // error
77
| ^
8-
|No given instance of type deriving.Mirror.Sum{MirroredType[X] = [Y] =>> List[(X, Y)]} was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Sum{MirroredType[X] = [Y] =>> List[(X, Y)]}: type `[X] =>> [Y] =>> List[(X, Y)]` is not a generic sum because its subpart `[X] =>> [Y] =>> List[(X, Y)]` is not a supported kind (either `*` or `* -> *`)
8+
|No given instance of type deriving.Mirror.Sum{type MirroredType[X] = [Y] =>> List[(X, Y)]} was found for parameter x of method summon in object Predef. Failed to synthesize an instance of type deriving.Mirror.Sum{type MirroredType[X] = [Y] =>> List[(X, Y)]}: type `[X] =>> [Y] =>> List[(X, Y)]` is not a generic sum because its subpart `[X] =>> [Y] =>> List[(X, Y)]` is not a supported kind (either `*` or `* -> *`)

tests/neg/i4986c.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
-- Error: tests/neg/i4986c.scala:45:87 ---------------------------------------------------------------------------------
2626
45 | implicitly[Outer[Option[String] | List[Iterable[Char]]] { type MyType = BigDecimal }] // error
2727
| ^
28-
|Missing Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{MyType = BigDecimal}#OuterMember
28+
|Missing Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{type MyType = BigDecimal}#OuterMember
2929
-- Error: tests/neg/i4986c.scala:46:106 --------------------------------------------------------------------------------
3030
46 | implicitly[(Outer[Option[String] | List[Iterable[Char]]] { type MyType = BigDecimal })#Inner[Byte, Seq]] // error
3131
| ^
32-
|Missing Inner[Byte, Seq] with InnerMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{MyType = BigDecimal}#Inner[Byte, Seq]#InnerMember from Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{MyType = BigDecimal}#OuterMember
32+
|Missing Inner[Byte, Seq] with InnerMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{type MyType = BigDecimal}#Inner[Byte, Seq]#InnerMember from Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{type MyType = BigDecimal}#OuterMember
3333
-- Error: tests/neg/i4986c.scala:47:33 ---------------------------------------------------------------------------------
3434
47 | implicitly[Outer[Int] @myAnnot] // error
3535
| ^

tests/patmat/aliasing.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
14: Pattern Match Exhaustivity: _: Trait & Test.Alias1, _: Clazz & Test.Alias1
22
19: Pattern Match Exhaustivity: _: Trait & Test.Alias2
3-
23: Pattern Match Exhaustivity: _: Trait & (Test.Alias2 & OpenTrait2){x: Int}
3+
23: Pattern Match Exhaustivity: _: Trait & (Test.Alias2 & OpenTrait2){val x: Int}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
32: Pattern Match Exhaustivity: _: Trait & C1{x: Int}
2-
48: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){x: Int} & (C3 | (C4 | T2)){x: Int}, _: Clazz & (C1 | (C2 | T1)){x: Int} & (C3 | (C4 | T2)){x: Int}
3-
54: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){x: Int} & C3{x: Int}
4-
59: Pattern Match Exhaustivity: _: Trait & (C1 & C2){x: Int}
5-
65: Pattern Match Exhaustivity: _: Trait & (C1 | C2){x: Int} & (C3 | SubC1){x: Int}
6-
72: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){x: Int} & (T2 & (C2 | C3 | SubC1)){x: Int} &
7-
SubSubC1{x: Int}
8-
79: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){x: Int} & (T2 & (C2 | C3 | SubC1)){x: Int} &
9-
SubSubC2{x: Int}
1+
32: Pattern Match Exhaustivity: _: Trait & C1{val x: Int}
2+
48: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){val x: Int} & (C3 | (C4 | T2)){val x: Int}, _: Clazz & (C1 | (C2 | T1)){val x: Int} & (C3 | (C4 | T2)){val x: Int}
3+
54: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){val x: Int} & C3{val x: Int}
4+
59: Pattern Match Exhaustivity: _: Trait & (C1 & C2){val x: Int}
5+
65: Pattern Match Exhaustivity: _: Trait & (C1 | C2){val x: Int} & (C3 | SubC1){val x: Int}
6+
72: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){val x: Int} & (T2 & (C2 | C3 | SubC1)){val x: Int} &
7+
SubSubC1{val x: Int}
8+
79: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){val x: Int} & (T2 & (C2 | C3 | SubC1)){val x: Int} &
9+
SubSubC2{val x: Int}

0 commit comments

Comments
 (0)