From 8c713da3d02131bb3931c152a6bda9785e1f464e Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 18 Jan 2011 18:54:27 +0000 Subject: What is, I think, the only possible solution to... What is, I think, the only possible solution to bug #4158 given the current ways of controlling implicits. Let's just be glad there's one instead of zero. Closes #4158, review by moors. --- src/library/scala/LowPriorityImplicits.scala | 18 ++++++++++++++++++ test/files/neg/bug4158.check | 19 +++++++++++++++++++ test/files/neg/bug4158.scala | 4 ++++ 3 files changed, 41 insertions(+) create mode 100644 test/files/neg/bug4158.check create mode 100644 test/files/neg/bug4158.scala diff --git a/src/library/scala/LowPriorityImplicits.scala b/src/library/scala/LowPriorityImplicits.scala index 6fce638061..61cd60cb52 100644 --- a/src/library/scala/LowPriorityImplicits.scala +++ b/src/library/scala/LowPriorityImplicits.scala @@ -36,6 +36,24 @@ class LowPriorityImplicits { implicit def doubleWrapper(x: Double) = new runtime.RichDouble(x) implicit def booleanWrapper(x: Boolean) = new runtime.RichBoolean(x) + // These eight implicits exist solely to exclude Null from the domain of + // the boxed types, so that e.g. "var x: Int = null" is a compile time + // error rather than a delayed null pointer exception by way of the + // conversion from java.lang.Integer. If defined in the same file as + // Integer2int, they would have higher priority because Null is a subtype + // of Integer. We balance that out and create conflict by moving the + // definition into the superclass. + // + // Caution: do not adjust tightrope tension without safety goggles in place. + implicit def Byte2byteNullConflict(x: Null): Byte = sys.error("value error") + implicit def Short2shortNullConflict(x: Null): Short = sys.error("value error") + implicit def Character2charNullConflict(x: Null): Char = sys.error("value error") + implicit def Integer2intNullConflict(x: Null): Int = sys.error("value error") + implicit def Long2longNullConflict(x: Null): Long = sys.error("value error") + implicit def Float2floatNullConflict(x: Null): Float = sys.error("value error") + implicit def Double2doubleNullConflict(x: Null): Double = sys.error("value error") + implicit def Boolean2booleanNullConflict(x: Null): Boolean = sys.error("value error") + implicit def genericWrapArray[T](xs: Array[T]): WrappedArray[T] = if (xs eq null) null else WrappedArray.make(xs) diff --git a/test/files/neg/bug4158.check b/test/files/neg/bug4158.check new file mode 100644 index 0000000000..0d9873dc32 --- /dev/null +++ b/test/files/neg/bug4158.check @@ -0,0 +1,19 @@ +bug4158.scala:3: error: type mismatch; + found : Null(null) + required: Int +Note that implicit conversions are not applicable because they are ambiguous: + both method Integer2intNullConflict in class LowPriorityImplicits of type (x: Null)Int + and method Integer2int in object Predef of type (x: java.lang.Integer)Int + are possible conversion functions from Null(null) to Int + var y = null: Int + ^ +bug4158.scala:2: error: type mismatch; + found : Null(null) + required: Int +Note that implicit conversions are not applicable because they are ambiguous: + both method Integer2intNullConflict in class LowPriorityImplicits of type (x: Null)Int + and method Integer2int in object Predef of type (x: java.lang.Integer)Int + are possible conversion functions from Null(null) to Int + var x: Int = null + ^ +two errors found diff --git a/test/files/neg/bug4158.scala b/test/files/neg/bug4158.scala new file mode 100644 index 0000000000..07aa69a95c --- /dev/null +++ b/test/files/neg/bug4158.scala @@ -0,0 +1,4 @@ +class A { + var x: Int = null + var y = null: Int +} \ No newline at end of file -- cgit v1.2.3