Skip to content

Make refined type printing more source compatible #16303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ class PlainPrinter(_ctx: Context) extends Printer {

/** String representation of a refinement */
protected def toTextRefinement(rt: RefinedType): Text =
(refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close
val keyword = rt.refinedInfo match {
case _: ExprType | _: MethodOrPoly => "def "
case _: TypeBounds => "type "
case _: TypeProxy => "val "
case _ => ""
}
(keyword ~ refinementNameString(rt) ~ toTextRHS(rt.refinedInfo)).close

protected def argText(arg: Type): Text = homogenizeArg(arg) match {
case arg: TypeBounds => "?" ~ toText(arg)
Expand Down
17 changes: 17 additions & 0 deletions compiler/test-resources/type-printer/source-compatible
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
scala> case class Bag() extends reflect.Selectable
// defined case class Bag
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() }
val m:
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];
def p[T](t: T): String
} = Bag()
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 }
// defined alias 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
}
8 changes: 4 additions & 4 deletions tests/neg-custom-args/captures/i15116.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
3 | val x = Foo(m) // error
| ^^^^^^^^^^^^^^
| Non-local value x cannot have an inferred type
| {Bar.this.m} Foo{m: {Bar.this.m} String}
| {Bar.this.m} Foo{val m: {Bar.this.m} String}
| with non-empty capture set {Bar.this.m}.
| The type needs to be declared explicitly.
-- Error: tests/neg-custom-args/captures/i15116.scala:5:6 --------------------------------------------------------------
5 | val x = Foo(m) // error
| ^^^^^^^^^^^^^^
| Non-local value x cannot have an inferred type
| {Baz.this} Foo{m: {*} String}
| {Baz.this} Foo{val m: {*} String}
| with non-empty capture set {Baz.this}.
| The type needs to be declared explicitly.
-- Error: tests/neg-custom-args/captures/i15116.scala:7:6 --------------------------------------------------------------
7 | val x = Foo(m) // error
| ^^^^^^^^^^^^^^
| Non-local value x cannot have an inferred type
| {Bar1.this.m} Foo{m: {Bar1.this.m} String}
| {Bar1.this.m} Foo{val m: {Bar1.this.m} String}
| with non-empty capture set {Bar1.this.m}.
| The type needs to be declared explicitly.
-- Error: tests/neg-custom-args/captures/i15116.scala:9:6 --------------------------------------------------------------
9 | val x = Foo(m) // error
| ^^^^^^^^^^^^^^
| Non-local value x cannot have an inferred type
| {Baz2.this} Foo{m: {*} String}
| {Baz2.this} Foo{val m: {*} String}
| with non-empty capture set {Baz2.this}.
| The type needs to be declared explicitly.
6 changes: 3 additions & 3 deletions tests/neg-custom-args/captures/i15772.check
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:20:49 ---------------------------------------
20 | val boxed1 : (({*} C) => Unit) -> Unit = box1(c) // error
| ^^^^^^^
| Found: {c} ({*} ({c} C{arg: {*} C}) -> Unit) -> Unit
| Found: {c} ({*} ({c} C{val arg: {*} C}) -> Unit) -> Unit
| Required: (({*} C) => Unit) -> Unit
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:27:38 ---------------------------------------
27 | val boxed2 : Observe[{*} C] = box2(c) // error
| ^^^^^^^
| Found: {c} ({*} ({c} C{arg: {*} C}) -> Unit) -> Unit
| Found: {c} ({*} ({c} C{val arg: {*} C}) -> Unit) -> Unit
| Required: Observe[{*} C]
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:33:37 ---------------------------------------
33 | val boxed2 : Observe[{*} C] = box2(c) // error
| ^
| Found: {*} C
| Required: box {*} C{arg: ? C}
| Required: box {*} C{val arg: ? C}
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15772.scala:44:2 ----------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/neg-custom-args/captures/lazylist.check
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylist.scala:35:29 -------------------------------------
35 | val ref1c: LazyList[Int] = ref1 // error
| ^^^^
| Found: (ref1 : {cap1} lazylists.LazyCons[Int]{xs: {cap1} () -> {*} lazylists.LazyList[Int]})
| Required: lazylists.LazyList[Int]
| Found: (ref1 : {cap1} lazylists.LazyCons[Int]{val xs: {cap1} () -> {*} lazylists.LazyList[Int]})
| Required: lazylists.LazyList[Int]
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazylist.scala:37:36 -------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions tests/neg-custom-args/captures/lazyref.check
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:19:28 --------------------------------------
19 | val ref1c: LazyRef[Int] = ref1 // error
| ^^^^
| Found: (ref1 : {cap1} LazyRef[Int]{elem: {cap1} () -> Int})
| Found: (ref1 : {cap1} LazyRef[Int]{val elem: {cap1} () -> Int})
| Required: LazyRef[Int]
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:21:35 --------------------------------------
21 | val ref2c: {cap2} LazyRef[Int] = ref2 // error
| ^^^^
| Found: (ref2 : {cap2, ref1} LazyRef[Int]{elem: {*} () -> Int})
| Found: (ref2 : {cap2, ref1} LazyRef[Int]{val elem: {*} () -> Int})
| Required: {cap2} LazyRef[Int]
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:23:35 --------------------------------------
23 | val ref3c: {ref1} LazyRef[Int] = ref3 // error
| ^^^^
| Found: (ref3 : {cap2, ref1} LazyRef[Int]{elem: {*} () -> Int})
| Found: (ref3 : {cap2, ref1} LazyRef[Int]{val elem: {*} () -> Int})
| Required: {ref1} LazyRef[Int]
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/lazyref.scala:25:35 --------------------------------------
25 | val ref4c: {cap1} LazyRef[Int] = ref4 // error
| ^^^^
| Found: (ref4 : {cap2, cap1} LazyRef[Int]{elem: {*} () -> Int})
| Found: (ref4 : {cap2, cap1} LazyRef[Int]{val elem: {*} () -> Int})
| Required: {cap1} LazyRef[Int]
|
| longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-- [E170] Type Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:17:27 -----------------------------
17 | val d = js.constructorOf[NativeJSClass { def bar: Int }] // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| NativeJSClass{bar: Int} is not a class type
| NativeJSClass{def bar: Int} is not a class type
-- Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:19:27 -----------------------------------------
19 | val e = js.constructorOf[JSTrait] // error
| ^^^^^^^
Expand All @@ -29,7 +29,7 @@
-- [E170] Type Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:23:27 -----------------------------
23 | val h = js.constructorOf[JSClass { def bar: Int }] // error
| ^^^^^^^^^^^^^^^^^^^^^^^^
| JSClass{bar: Int} is not a class type
| JSClass{def bar: Int} is not a class type
-- [E170] Type Error: tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.scala:25:42 -----------------------------
25 | def foo[A <: js.Any] = js.constructorOf[A] // error
| ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-- [E170] Type Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:17:59 ----------------------------
17 | val d = js.constructorTag[NativeJSClass { def bar: Int }] // error
| ^
| NativeJSClass{bar: Int} is not a class type
| NativeJSClass{def bar: Int} is not a class type
-- Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:19:36 ----------------------------------------
19 | val e = js.constructorTag[JSTrait] // error
| ^
Expand All @@ -29,7 +29,7 @@
-- [E170] Type Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:23:53 ----------------------------
23 | val h = js.constructorTag[JSClass { def bar: Int }] // error
| ^
| JSClass{bar: Int} is not a class type
| JSClass{def bar: Int} is not a class type
-- [E170] Type Error: tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.scala:25:45 ----------------------------
25 | def foo[A <: js.Any] = js.constructorTag[A] // error
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/classOf.check
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
-- [E170] Type Error: tests/neg/classOf.scala:9:18 ---------------------------------------------------------------------
9 | val y = classOf[C { type I = String }] // error
| ^^^^^^^^^^^^^^^^^^^^^
| Test.C{I = String} is not a class type
| Test.C{type I = String} is not a class type
4 changes: 2 additions & 2 deletions tests/neg/i14025.check
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Error: tests/neg/i14025.scala:1:88 ----------------------------------------------------------------------------------
1 |val foo = summon[deriving.Mirror.Product { type MirroredType = [X] =>> [Y] =>> (X, Y) }] // error
| ^
|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 `* -> *`)
|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 `* -> *`)
-- Error: tests/neg/i14025.scala:2:90 ----------------------------------------------------------------------------------
2 |val bar = summon[deriving.Mirror.Sum { type MirroredType = [X] =>> [Y] =>> List[(X, Y)] }] // error
| ^
|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 `* -> *`)
|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 `* -> *`)
4 changes: 2 additions & 2 deletions tests/neg/i4986c.check
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
-- Error: tests/neg/i4986c.scala:45:87 ---------------------------------------------------------------------------------
45 | implicitly[Outer[Option[String] | List[Iterable[Char]]] { type MyType = BigDecimal }] // error
| ^
|Missing Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{MyType = BigDecimal}#OuterMember
|Missing Outer[Option[String] | List[Iterable[Char]]] with OuterMember = pkg.Outer[Option[String] | List[Iterable[Char]]]{type MyType = BigDecimal}#OuterMember
-- Error: tests/neg/i4986c.scala:46:106 --------------------------------------------------------------------------------
46 | implicitly[(Outer[Option[String] | List[Iterable[Char]]] { type MyType = BigDecimal })#Inner[Byte, Seq]] // error
| ^
|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
|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
-- Error: tests/neg/i4986c.scala:47:33 ---------------------------------------------------------------------------------
47 | implicitly[Outer[Int] @myAnnot] // error
| ^
Expand Down
2 changes: 1 addition & 1 deletion tests/patmat/aliasing.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
14: Pattern Match Exhaustivity: _: Trait & Test.Alias1, _: Clazz & Test.Alias1
19: Pattern Match Exhaustivity: _: Trait & Test.Alias2
23: Pattern Match Exhaustivity: _: Trait & (Test.Alias2 & OpenTrait2){x: Int}
23: Pattern Match Exhaustivity: _: Trait & (Test.Alias2 & OpenTrait2){val x: Int}
18 changes: 9 additions & 9 deletions tests/patmat/andtype-refinedtype-interaction.check
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
32: Pattern Match Exhaustivity: _: Trait & C1{x: Int}
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}
54: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){x: Int} & C3{x: Int}
59: Pattern Match Exhaustivity: _: Trait & (C1 & C2){x: Int}
65: Pattern Match Exhaustivity: _: Trait & (C1 | C2){x: Int} & (C3 | SubC1){x: Int}
72: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){x: Int} & (T2 & (C2 | C3 | SubC1)){x: Int} &
SubSubC1{x: Int}
79: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){x: Int} & (T2 & (C2 | C3 | SubC1)){x: Int} &
SubSubC2{x: Int}
32: Pattern Match Exhaustivity: _: Trait & C1{val x: Int}
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}
54: Pattern Match Exhaustivity: _: Trait & (C1 | (C2 | T1)){val x: Int} & C3{val x: Int}
59: Pattern Match Exhaustivity: _: Trait & (C1 & C2){val x: Int}
65: Pattern Match Exhaustivity: _: Trait & (C1 | C2){val x: Int} & (C3 | SubC1){val x: Int}
72: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){val x: Int} & (T2 & (C2 | C3 | SubC1)){val x: Int} &
SubSubC1{val x: Int}
79: Pattern Match Exhaustivity: _: Trait & (T1 & (C1 | SubC2)){val x: Int} & (T2 & (C2 | C3 | SubC1)){val x: Int} &
SubSubC2{val x: Int}