Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit lookup interacts with import order #9208

Open
scabug opened this issue Mar 8, 2015 · 6 comments
Open

Implicit lookup interacts with import order #9208

scabug opened this issue Mar 8, 2015 · 6 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) implicit typer
Milestone

Comments

@scabug
Copy link

scabug commented Mar 8, 2015

This code compiles if the order of the imports is reversed.

Since the order of the imports correctly doesn't affect visibility or accessibility of unprefixed global, then it shouldn't affect the implicitness either.

package object bound2 {
    implicit lazy val global: concurrent.ExecutionContextExecutor = scala.concurrent.ExecutionContext.global
}

package bound {
  // the order of these imports in the same scope should not matter
  import scala.concurrent.ExecutionContext.Implicits.global
  import bound2.B._

  object Test extends App {
    val f = concurrent.Future(42)  //(global) // explicit arg works
    Console println concurrent.Await.result(f, concurrent.duration.Duration.Inf)
  }
}

Reported on SO with respect to a language import, where SIP-18 was also suspected of harboring a bug.

@scabug
Copy link
Author

scabug commented Mar 8, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9208?orig=1
Reporter: @som-snytt
Affected Versions: 2.11.6
See #3453, #4270

@scabug
Copy link
Author

scabug commented Mar 24, 2015

@retronym said:
Shaved the example down a little:

trait M

object O1 {
  implicit def global: M = ???
}

object O2 {
  implicit def global: M = ???
}
 
class Test {
  def test1 = {
    // the order of these imports in the same scope should not matter
    import O1._
    import O2.global
    global
    implicitly[M]
  }

  def test2 = {
    import O2.global
    import O1._
    global
    implicitly[M] // .. but, "error: could not find implicit value for parameter e: M"
  }
}

@scabug
Copy link
Author

scabug commented Mar 24, 2015

@retronym said:
scala/scala#4395

@scabug
Copy link
Author

scabug commented May 2, 2016

@Ichoran said:
Here's another interaction that hopefully will also be caught (shadowing a val in this case). Credit to TimSoethout for finding this and bringing it up on the gitter channel.

object P {
  case class C(content: String)
  object C { implicit val x = C("companion") }

  // Uncomment this and u and v will resolve the same (to the companion above!)
  //val x = "fish"

  object O {
    object First { implicit val x = C("First") }
    object Second { implicit val x = C("Second") }
    import First._
    val u = implicitly[C]
    import Second._
    val v = implicitly[C]
  }

  val weird = (O.u,O.v)
}
P.weird

@scabug
Copy link
Author

scabug commented Nov 23, 2016

@som-snytt said:
Example without any interactions. It incorrectly thinks inherited x is shadowed.

object X { implicit def x: Int = 42 }

trait T { implicit def x: Int = 17 }

object Y extends T {
  import X._
  def f: Int = implicitly[Int]
}

http://stackoverflow.com/q/40709731/1296806

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@som-snytt som-snytt added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Dec 10, 2019
@som-snytt
Copy link

The Ichoran example is fixed in Scala 3, where implicits don't shadow by name.

The OP no longer works because of import path checking, apparently.

The retronym repro is still apropo.

Relatedly, #12793

Instead of filtering while collecting implicits, it should collect all implicits offered by enclosing contexts, then filter by "accessibility" with respect to the current context. That just means lookup the identifier, which accounts for normal shadowing and access.

That was suggested by a comment about isEligibleInfos, but is not the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) implicit typer
Projects
None yet
Development

No branches or pull requests

3 participants