scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Order of implicits in file matters; could we at least fix this for implicit objects? #8697

Open scabug opened 10 years ago

scabug commented 10 years ago

The following works:

trait T[_]
sealed trait A
sealed trait C

object A {
  implicit object AA extends T[A]
}

object C {
  implicit object CC extends T[C]
}

object B {
  implicitly[T[A]]
  implicitly[T[C]]
}

While the the following can not find the implicit T[C] instance:

trait T[_]
sealed trait A
sealed trait C

object A {
  implicit object AA extends T[A]
}

object B {
  implicitly[T[A]]
  implicitly[T[C]]
}

object C {
  implicit object CC extends T[C]
}

The only difference is the order of appearance for the B and C objects.

scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8697?orig=1 Reporter: @puffnfresh Affected Versions: 2.10.4, 2.11.1 See #3466

scabug commented 10 years ago

@retronym said: Implicit objects are the same as implicit vals without explicit type annotations. We don't know there type without typechecking there body.

Implicit search will never force inference of an implicit down lower in the same file as the implicit call site. This avoids spurious cycles in inference.

Dotty, Martin's current research compiler, actually requires implicits to have explicit return types. IIRC, he does allow implicit objects. I'm not sure how he avoids the potential cycles.

I'll use this ticket as a reminder to dig into his approach and see if we can backport the treatment of implicit objects to Scala 2.12.

You can use explicitly type annotated implicit lazy vals as a more reliable tool.

scabug commented 10 years ago

@puffnfresh said: @retronym thank you. That's a big help. I've been meaning to add lack of explicit types as a wart:

https://github.com/typelevel/wartremover/issues/105

scabug commented 8 years ago

@SethTisue said: there are a lot of tickets in this general space; see e.g. the links at #9674, #3466, etc.