summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2013-06-04 11:50:05 -0700
committerPaul Phillips <paulp@improving.org>2013-06-04 12:25:43 -0700
commit2f0e5ec1e96a6e4806841069736eda844d3a8dd6 (patch)
tree1a72792e2f08519f51e444e98eb19ffa2a855168 /src/library
parent803d451a28824af17f0cab446e4c76f51003fd01 (diff)
downloadscala-2f0e5ec1e96a6e4806841069736eda844d3a8dd6.tar.gz
scala-2f0e5ec1e96a6e4806841069736eda844d3a8dd6.tar.bz2
scala-2f0e5ec1e96a6e4806841069736eda844d3a8dd6.zip
SI-6899, prohibit dangerous, useless implicit conversions.
Increase eligibility requirements for implicit conversions, such that T => U is ineligible if T <: Null <or> AnyRef <: U This has the salutary effect of allowing us to ditch 16 ridiculous implicits from Predef, since they existed solely to work around the absence of this restriction. There was one tiny impact on actual source code (one line in one file) shown here, necessitated because the literal null is not eligible to be implicitly converted to A via <:<. def f[A](implicit ev: Null <:< A): A = null // before def f[A](implicit ev: Null <:< A): A = ev(null) // after As impositions go it's on the tame side.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/Option.scala4
-rw-r--r--src/library/scala/Predef.scala31
2 files changed, 2 insertions, 33 deletions
diff --git a/src/library/scala/Option.scala b/src/library/scala/Option.scala
index 4b071166c7..905e925f57 100644
--- a/src/library/scala/Option.scala
+++ b/src/library/scala/Option.scala
@@ -128,7 +128,7 @@ sealed abstract class Option[+A] extends Product with Serializable {
* val textField = new JComponent(initalText.orNull,20)
* }}}
*/
- @inline final def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse null
+ @inline final def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = this getOrElse ev(null)
/** Returns a $some containing the result of applying $f to this $option's
* value if this $option is nonempty.
@@ -210,7 +210,7 @@ sealed abstract class Option[+A] extends Product with Serializable {
}
/** Tests whether the option contains a given value as an element.
- *
+ *
* @param elem the element to test.
* @return `true` if the option has an element that is equal (as
* determined by `==`) to `elem`, `false` otherwise.
diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala
index 569157de20..5ba38600b6 100644
--- a/src/library/scala/Predef.scala
+++ b/src/library/scala/Predef.scala
@@ -346,19 +346,6 @@ object Predef extends LowPriorityImplicits with DeprecatedPredef {
implicit def double2Double(x: Double) = java.lang.Double.valueOf(x)
implicit def boolean2Boolean(x: Boolean) = java.lang.Boolean.valueOf(x)
- // These next eight implicits exist solely to exclude AnyRef methods from the
- // eight implicits above so that primitives are not coerced to AnyRefs. They
- // only create such conflict for AnyRef methods, so the methods on the java.lang
- // boxed types are unambiguously reachable.
- implicit def byte2ByteConflict(x: Byte) = new AnyRef
- implicit def short2ShortConflict(x: Short) = new AnyRef
- implicit def char2CharacterConflict(x: Char) = new AnyRef
- implicit def int2IntegerConflict(x: Int) = new AnyRef
- implicit def long2LongConflict(x: Long) = new AnyRef
- implicit def float2FloatConflict(x: Float) = new AnyRef
- implicit def double2DoubleConflict(x: Double) = new AnyRef
- implicit def boolean2BooleanConflict(x: Boolean) = new AnyRef
-
implicit def Byte2byte(x: java.lang.Byte): Byte = x.byteValue
implicit def Short2short(x: java.lang.Short): Short = x.shortValue
implicit def Character2char(x: java.lang.Character): Char = x.charValue
@@ -481,24 +468,6 @@ private[scala] abstract class LowPriorityImplicits {
@inline implicit def doubleWrapper(x: Double) = new runtime.RichDouble(x)
@inline 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 template 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)