From 35316be5a89b2c9622e92594245aaf72a3820c88 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 21 Aug 2012 07:40:28 +0200 Subject: Better errors for Any/AnyRef issues. When an error occurs because some type does not conform to AnyRef (and an AnyRef-derived type would have sufficed) try to say something useful about the situation. This commit also initializes scope members before printing error messages because the + version seems more useful than the - version (taken from one of the checkfile diffs.) - def : - def methodIntIntInt: + def (): X + def methodIntIntInt(x: scala.Int,y: scala.Int): scala.Int --- test/files/neg/any-vs-anyref.check | 64 +++++++++++++++++++++++++++++++++ test/files/neg/any-vs-anyref.scala | 29 +++++++++++++++ test/files/neg/not-possible-cause.check | 7 ++-- test/files/neg/t3614.check | 4 +-- test/files/neg/t6263.check | 3 ++ test/files/neg/t900.check | 3 ++ 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/files/neg/any-vs-anyref.check create mode 100644 test/files/neg/any-vs-anyref.scala (limited to 'test/files/neg') diff --git a/test/files/neg/any-vs-anyref.check b/test/files/neg/any-vs-anyref.check new file mode 100644 index 0000000000..63c4853130 --- /dev/null +++ b/test/files/neg/any-vs-anyref.check @@ -0,0 +1,64 @@ +any-vs-anyref.scala:6: error: type mismatch; + found : a.type (with underlying type A) + required: AnyRef +Note that A is bounded only by Equals, which means AnyRef is not a known parent. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo1[A <: Product](a: A) = { type X = a.type } + ^ +any-vs-anyref.scala:7: error: type mismatch; + found : a.type (with underlying type A) + required: AnyRef +Note that A is bounded only by Product, Quux, which means AnyRef is not a known parent. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo2[A <: Product with Quux](a: A) = { type X = a.type } + ^ +any-vs-anyref.scala:8: error: type mismatch; + found : a.type (with underlying type Product) + required: AnyRef +Note that Product extends Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo3(a: Product) = { type X = a.type } + ^ +any-vs-anyref.scala:9: error: type mismatch; + found : Product with Quux + required: AnyRef +Note that the parents of this type (Product, Quux) extend Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo4(a: Product with Quux) = { type X = a.type } + ^ +any-vs-anyref.scala:10: error: value eq is not a member of Quux with Product +Note that the parents of this type (Quux, Product) extend Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo5(x: Quux with Product) = (x eq "abc") && ("abc" eq x) + ^ +any-vs-anyref.scala:11: error: value eq is not a member of Quux with Product{def f: Int} +Note that the parents of this type (Quux, Product) extend Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo6(x: Quux with Product { def f: Int }) = (x eq "abc") && ("abc" eq x) + ^ +any-vs-anyref.scala:12: error: type mismatch; + found : Quux with Product{def eq(other: String): Boolean} + required: AnyRef +Note that the parents of this type (Quux, Product) extend Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def foo7(x: Quux with Product { def eq(other: String): Boolean }) = (x eq "abc") && ("abc" eq x) + ^ +any-vs-anyref.scala:22: error: value eq is not a member of Bippy +Note that Bippy extends Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + def bad1(x: Bippy, y: Bippy) = x eq y + ^ +any-vs-anyref.scala:27: error: type mismatch; + found : Quux{def g(x: String): String} + required: Quux{def g(x: Int): Int} + f(new Quux { def g(x: String) = x }) + ^ +9 errors found diff --git a/test/files/neg/any-vs-anyref.scala b/test/files/neg/any-vs-anyref.scala new file mode 100644 index 0000000000..8d237fbaec --- /dev/null +++ b/test/files/neg/any-vs-anyref.scala @@ -0,0 +1,29 @@ +trait Quux extends Any +trait QuuxRef extends AnyRef +final class Bippy(val x: Any) extends AnyVal with Quux + +object Foo { + def foo1[A <: Product](a: A) = { type X = a.type } + def foo2[A <: Product with Quux](a: A) = { type X = a.type } + def foo3(a: Product) = { type X = a.type } + def foo4(a: Product with Quux) = { type X = a.type } + def foo5(x: Quux with Product) = (x eq "abc") && ("abc" eq x) + def foo6(x: Quux with Product { def f: Int }) = (x eq "abc") && ("abc" eq x) + def foo7(x: Quux with Product { def eq(other: String): Boolean }) = (x eq "abc") && ("abc" eq x) + + def ok1[A <: QuuxRef](a: A) = { type X = a.type } + def ok2[A <: Product with QuuxRef](a: A) = { type X = a.type } + def ok3(a: QuuxRef) = { type X = a.type } + def ok4(a: Product with QuuxRef) = { type X = a.type } + def ok5(x: QuuxRef with Product) = (x eq "abc") && ("abc" eq x) + def ok6(x: QuuxRef with Product { def f: Int }) = (x eq "abc") && ("abc" eq x) + def ok7(x: QuuxRef { def eq(other: String): Boolean }) = (x eq "abc") && ("abc" eq x) + + def bad1(x: Bippy, y: Bippy) = x eq y +} + +object Bar { + def f(x: Quux { def g(x: Int): Int }): Int = x g 5 + f(new Quux { def g(x: String) = x }) + f(new Quux { def g(x: Int) = x }) +} diff --git a/test/files/neg/not-possible-cause.check b/test/files/neg/not-possible-cause.check index 9111cd0d3d..5c09fa1545 100644 --- a/test/files/neg/not-possible-cause.check +++ b/test/files/neg/not-possible-cause.check @@ -1,10 +1,9 @@ not-possible-cause.scala:2: error: type mismatch; found : a.type (with underlying type A) required: AnyRef -Note that implicit conversions are not applicable because they are ambiguous: - both method any2stringfmt in object Predef of type (x: Any)scala.runtime.StringFormat - and method any2stringadd in object Predef of type (x: Any)scala.runtime.StringAdd - are possible conversion functions from a.type to AnyRef +Note that A is bounded only by Equals, which means AnyRef is not a known parent. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. def foo[A <: Product](a: A) { type X = a.type } ^ one error found diff --git a/test/files/neg/t3614.check b/test/files/neg/t3614.check index 5fdb5cbf1f..0f9c83aa0d 100644 --- a/test/files/neg/t3614.check +++ b/test/files/neg/t3614.check @@ -1,4 +1,4 @@ -t3614.scala:2: error: class type required but AnyRef{def a: } found +t3614.scala:2: error: class type required but AnyRef{def a: Int} found def v = new ({ def a=0 }) ^ -one error found \ No newline at end of file +one error found diff --git a/test/files/neg/t6263.check b/test/files/neg/t6263.check index 1c5834e1ca..9e9c7c615b 100644 --- a/test/files/neg/t6263.check +++ b/test/files/neg/t6263.check @@ -1,6 +1,9 @@ t6263.scala:5: error: type mismatch; found : A.this.c.type (with underlying type C) required: AnyRef +Note that C extends Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. type t = c.type ^ one error found diff --git a/test/files/neg/t900.check b/test/files/neg/t900.check index ff5304a135..6fe26a31ac 100644 --- a/test/files/neg/t900.check +++ b/test/files/neg/t900.check @@ -1,6 +1,9 @@ t900.scala:4: error: type mismatch; found : Foo.this.x.type (with underlying type Foo.this.bar) required: AnyRef +Note that bar is unbounded, which means AnyRef is not a known parent. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. def break(): x.type ^ one error found -- cgit v1.2.3