From b2f4cbe93d76a60f535c0cece5bbcab90851ea70 Mon Sep 17 00:00:00 2001 From: Jan-Pieter van den Heuvel Date: Fri, 11 Nov 2022 14:57:48 +0100 Subject: [PATCH] Make refined type printing more source compatible Co-authored-by: Denis Zolkin --- .../tools/dotc/printing/PlainPrinter.scala | 8 +++++++- .../type-printer/source-compatible | 17 +++++++++++++++++ tests/neg-custom-args/captures/i15116.check | 8 ++++---- tests/neg-custom-args/captures/i15772.check | 6 +++--- tests/neg-custom-args/captures/lazylist.check | 4 ++-- tests/neg-custom-args/captures/lazyref.check | 8 ++++---- ...sconstructorof-error-in-prepjsinterop.check | 4 ++-- ...constructortag-error-in-prepjsinterop.check | 4 ++-- tests/neg/classOf.check | 2 +- tests/neg/i14025.check | 4 ++-- tests/neg/i4986c.check | 4 ++-- tests/patmat/aliasing.check | 2 +- .../andtype-refinedtype-interaction.check | 18 +++++++++--------- 13 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 compiler/test-resources/type-printer/source-compatible diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index c9c64f458ad0..f0479f818c9f 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -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) diff --git a/compiler/test-resources/type-printer/source-compatible b/compiler/test-resources/type-printer/source-compatible new file mode 100644 index 000000000000..d0773a11a795 --- /dev/null +++ b/compiler/test-resources/type-printer/source-compatible @@ -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 + } diff --git a/tests/neg-custom-args/captures/i15116.check b/tests/neg-custom-args/captures/i15116.check index 8b8d2d2e091b..7c73a7ff52ff 100644 --- a/tests/neg-custom-args/captures/i15116.check +++ b/tests/neg-custom-args/captures/i15116.check @@ -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. diff --git a/tests/neg-custom-args/captures/i15772.check b/tests/neg-custom-args/captures/i15772.check index 765586ac5e27..a587f2d262ed 100644 --- a/tests/neg-custom-args/captures/i15772.check +++ b/tests/neg-custom-args/captures/i15772.check @@ -1,14 +1,14 @@ -- [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` @@ -16,7 +16,7 @@ 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 ---------------------------------------- diff --git a/tests/neg-custom-args/captures/lazylist.check b/tests/neg-custom-args/captures/lazylist.check index 1ad705556b88..ec620b39da08 100644 --- a/tests/neg-custom-args/captures/lazylist.check +++ b/tests/neg-custom-args/captures/lazylist.check @@ -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 ------------------------------------- diff --git a/tests/neg-custom-args/captures/lazyref.check b/tests/neg-custom-args/captures/lazyref.check index fcd98d0d67bd..7471f8f4f686 100644 --- a/tests/neg-custom-args/captures/lazyref.check +++ b/tests/neg-custom-args/captures/lazyref.check @@ -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` diff --git a/tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.check b/tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.check index 301111860aa7..7687543ea75f 100644 --- a/tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.check +++ b/tests/neg-scalajs/jsconstructorof-error-in-prepjsinterop.check @@ -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 | ^^^^^^^ @@ -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 | ^ diff --git a/tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.check b/tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.check index e1f5a038f2c1..142de318efd3 100644 --- a/tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.check +++ b/tests/neg-scalajs/jsconstructortag-error-in-prepjsinterop.check @@ -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 | ^ @@ -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 | ^ diff --git a/tests/neg/classOf.check b/tests/neg/classOf.check index c3873aff7391..e3be3ca17026 100644 --- a/tests/neg/classOf.check +++ b/tests/neg/classOf.check @@ -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 diff --git a/tests/neg/i14025.check b/tests/neg/i14025.check index 3c67b954297b..804d45523deb 100644 --- a/tests/neg/i14025.check +++ b/tests/neg/i14025.check @@ -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 `* -> *`) diff --git a/tests/neg/i4986c.check b/tests/neg/i4986c.check index a5fe0cee26bf..30f746e5af7f 100644 --- a/tests/neg/i4986c.check +++ b/tests/neg/i4986c.check @@ -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 | ^ diff --git a/tests/patmat/aliasing.check b/tests/patmat/aliasing.check index d7c21e8d0605..c367626d6f1e 100644 --- a/tests/patmat/aliasing.check +++ b/tests/patmat/aliasing.check @@ -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} diff --git a/tests/patmat/andtype-refinedtype-interaction.check b/tests/patmat/andtype-refinedtype-interaction.check index 9f57c5ba4867..d9512b5cb3e4 100644 --- a/tests/patmat/andtype-refinedtype-interaction.check +++ b/tests/patmat/andtype-refinedtype-interaction.check @@ -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}