Skip to content

SI-6899, prohibit dangerous, useless implicit conversions. #2625

Merged
merged 1 commit into from Jun 19, 2013

6 participants

@paulp
paulp commented Jun 4, 2013

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.

@paulp paulp 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.
2f0e5ec
@xeno-by xeno-by commented on the diff Jun 4, 2013
test/files/neg/t4158.check
@@ -1,19 +1,7 @@
-t4158.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: Integer)Int
- are possible conversion functions from Null(null) to Int
+t4158.scala:3: error: an expression of type Null is ineligible for implicit conversion
@xeno-by
The Scala Programming Language member
xeno-by added a note Jun 4, 2013

how about reformulating this in a bit lighter manner, e.g. nulls are ineligible for implicit conversion?

@paulp
paulp added a note Jun 4, 2013

I find that misleading, because they're not, and not everything being prohibited here is a null.

// That's not a null.
scala> def f[T <: Null](x: T): Int = x
<console>:7: error: an expression of type Null is ineligible for implicit conversion
       def f[T <: Null](x: T): Int = x
                                     ^
// That IS a null.
scala> def g: Seq[Int] = (null: Array[Int])
g: Seq[Int]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@soc
The Scala Programming Language member
soc commented Jun 5, 2013

Although I'm concerned about introducing more special cases, this seems to be a huge win. LGTM.

@retronym
The Scala Programming Language member
retronym commented Jun 9, 2013

LGTM. But I think we should run this one past @odersky this week, too.

@gkossakowski
The Scala Programming Language member

@retronym: did you have a chance to show this to @odersky? If not, what do we do about it?

@retronym
The Scala Programming Language member

Nope, I forgot. But it definitely needs wider review. I'll email @odersky to see if I can evade his spam filter...

@gkossakowski
The Scala Programming Language member

Ok. I'll try to help to get your message through.

@retronym
The Scala Programming Language member

Oh right, he's still down your way. I designate you as the messenger!

@gkossakowski
The Scala Programming Language member

Ok, I took the ✉️ and I'll see what can I do about it!

@adriaanm
The Scala Programming Language member

LGTM by @odersky acquired by me

@gkossakowski gkossakowski merged commit e2aaf80 into scala:master Jun 19, 2013

1 check passed

Details default pr-checkin-per-commit Took 45 min.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.