From 327eea839e97e1d7cac92aa9cd61e3338d12cb12 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 27 Jan 2014 10:29:28 +0100 Subject: Prohibit views targeting AnyVal Library changes in Scala 2.10 mean that we are left with the unfortunate situation of admitting: scala> "": AnyVal res0: AnyVal = We already have explicit checks in place to prevent views targeting `AnyRef`. This commit balances this out by prohibiting `AnyVal`, as well. The enclosed test shows that this case is now prevented. If multiple implicits views are applicable, the ambiguity error is still raised; these check comes right at the end. Maybe that ought to be changed, but I don't think it matters too much. I've also disabled this prohibition under -Xsource:2.10. --- test/files/neg/no-implicit-to-anyref-any-val.check | 34 ++++++++++++++++++++++ test/files/neg/no-implicit-to-anyref-any-val.scala | 33 +++++++++++++++++++++ test/files/neg/no-implicit-to-anyref.check | 22 -------------- test/files/neg/no-implicit-to-anyref.scala | 29 ------------------ test/files/pos/implicit-anyval-2.10.flags | 1 + test/files/pos/implicit-anyval-2.10.scala | 3 ++ 6 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 test/files/neg/no-implicit-to-anyref-any-val.check create mode 100644 test/files/neg/no-implicit-to-anyref-any-val.scala delete mode 100644 test/files/neg/no-implicit-to-anyref.check delete mode 100644 test/files/neg/no-implicit-to-anyref.scala create mode 100644 test/files/pos/implicit-anyval-2.10.flags create mode 100644 test/files/pos/implicit-anyval-2.10.scala (limited to 'test') diff --git a/test/files/neg/no-implicit-to-anyref-any-val.check b/test/files/neg/no-implicit-to-anyref-any-val.check new file mode 100644 index 0000000000..5953e1bd6d --- /dev/null +++ b/test/files/neg/no-implicit-to-anyref-any-val.check @@ -0,0 +1,34 @@ +no-implicit-to-anyref-any-val.scala:11: error: the result type of an implicit conversion must be more specific than AnyRef + 1: AnyRef + ^ +no-implicit-to-anyref-any-val.scala:17: error: type mismatch; + found : Any + required: AnyRef + (null: Any): AnyRef + ^ +no-implicit-to-anyref-any-val.scala:21: error: type mismatch; + found : AnyVal + required: AnyRef + (0: AnyVal): AnyRef + ^ +no-implicit-to-anyref-any-val.scala:27: error: type mismatch; + found : Test.AV + required: AnyRef +Note that AV extends Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + new AV(0): AnyRef + ^ +no-implicit-to-anyref-any-val.scala:30: error: the result type of an implicit conversion must be more specific than AnyVal + "": AnyVal + ^ +no-implicit-to-anyref-any-val.scala:32: error: type mismatch; + found : Object + required: AnyVal +Note that implicit conversions are not applicable because they are ambiguous: + both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A] + and method Ensuring in object Predef of type [A](self: A)Ensuring[A] + are possible conversion functions from Object to AnyVal + new Object() : AnyVal + ^ +6 errors found diff --git a/test/files/neg/no-implicit-to-anyref-any-val.scala b/test/files/neg/no-implicit-to-anyref-any-val.scala new file mode 100644 index 0000000000..f5daf541af --- /dev/null +++ b/test/files/neg/no-implicit-to-anyref-any-val.scala @@ -0,0 +1,33 @@ +// Checks that the state of standard implicits in Predef._ and scala._ +// doesn't allow us to unambiguously and implicitly convert AnyVal +// and subtypes to AnyRef. +// +// In the days before value classes, this was precariously held be +// the competing implicits Any => StringAdd and Any => StringFormat. +// Since then, these have both become value classes, but seeing as +// this happened simultaneously, we're still okay. +object Test { + locally { + 1: AnyRef + } + + locally { + // before this test case was added and ContextErrors was tweaked, this + // emitted: "Note that Any extends Any, not AnyRef." + (null: Any): AnyRef + } + + locally { + (0: AnyVal): AnyRef + } + + class AV(val a: Int) extends AnyVal + + locally { + new AV(0): AnyRef + } + + "": AnyVal + + new Object() : AnyVal +} diff --git a/test/files/neg/no-implicit-to-anyref.check b/test/files/neg/no-implicit-to-anyref.check deleted file mode 100644 index fe417ad8b0..0000000000 --- a/test/files/neg/no-implicit-to-anyref.check +++ /dev/null @@ -1,22 +0,0 @@ -no-implicit-to-anyref.scala:11: error: the result type of an implicit conversion must be more specific than AnyRef - 1: AnyRef - ^ -no-implicit-to-anyref.scala:17: error: type mismatch; - found : Any - required: AnyRef - (null: Any): AnyRef - ^ -no-implicit-to-anyref.scala:21: error: type mismatch; - found : AnyVal - required: AnyRef - (0: AnyVal): AnyRef - ^ -no-implicit-to-anyref.scala:27: error: type mismatch; - found : Test.AV - required: AnyRef -Note that AV extends Any, not AnyRef. -Such types can participate in value classes, but instances -cannot appear in singleton types or in reference comparisons. - new AV(0): AnyRef - ^ -four errors found diff --git a/test/files/neg/no-implicit-to-anyref.scala b/test/files/neg/no-implicit-to-anyref.scala deleted file mode 100644 index 3e3d373e38..0000000000 --- a/test/files/neg/no-implicit-to-anyref.scala +++ /dev/null @@ -1,29 +0,0 @@ -// Checks that the state of standard implicits in Predef._ and scala._ -// doesn't allow us to unambiguously and implicitly convert AnyVal -// and subtypes to AnyRef. -// -// In the days before value classes, this was precariously held be -// the competing implicits Any => StringAdd and Any => StringFormat. -// Since then, these have both become value classes, but seeing as -// this happened simultaneously, we're still okay. -object Test { - locally { - 1: AnyRef - } - - locally { - // before this test case was added and ContextErrors was tweaked, this - // emitted: "Note that Any extends Any, not AnyRef." - (null: Any): AnyRef - } - - locally { - (0: AnyVal): AnyRef - } - - class AV(val a: Int) extends AnyVal - - locally { - new AV(0): AnyRef - } -} diff --git a/test/files/pos/implicit-anyval-2.10.flags b/test/files/pos/implicit-anyval-2.10.flags new file mode 100644 index 0000000000..94c8056747 --- /dev/null +++ b/test/files/pos/implicit-anyval-2.10.flags @@ -0,0 +1 @@ +-Xsource:2.10 diff --git a/test/files/pos/implicit-anyval-2.10.scala b/test/files/pos/implicit-anyval-2.10.scala new file mode 100644 index 0000000000..3082af73b8 --- /dev/null +++ b/test/files/pos/implicit-anyval-2.10.scala @@ -0,0 +1,3 @@ +object Test { + "": AnyVal // newly prohibited in 2.11, allowed under -Xsourse:2.10 +} \ No newline at end of file -- cgit v1.2.3