Skip to content

Type parameters should be instantiated before doing an implicit search #739

Closed
@smarter

Description

@smarter

This is a separate issue from #738 but I think that they will need to be fixed together to get something that makes sense.
Consider the following code:

class Foo

object Test {
  def foo[T](x: T)(implicit ev: T): T = ???

  def test: Unit = {
    implicit val evidence: Foo = new Foo
    foo(new Foo)
  }
}

It currently fails with:

try/constrimpl.scala:9: error: ambiguous implicits: both getter StringCanBuildFrom in object Predef$ and getter NothingClassTag in object DottyPredef$ match type (T? >: Foo) of parameter ev of method foo in object Test$
    foo(new Foo)
                ^
try/constrimpl.scala:10: error: ambiguous implicits: both getter StringCanBuildFrom in object Predef$ and getter NothingClassTag in object DottyPredef$ match type (T? >: Foo) of parameter ev of method foo in object Test$
  }
   ^
two errors found

When we do the implicit search, T is not instantiated yet, the only thing we know is that its lower bound is Foo as the error message says: ... match type (T? >: Foo) of parameter ev of method foo .... I don't know how scalac deals with this exactly but it seems like it simply instantiate the type parameter before doing the implicit search, we should consider doing the same thing.

Note that this is also needed to properly fix #553, if we don't instantiate CC to List, then CC is only lower-bounded by List, and lower bounds of abstract types are not part of the implicit scope, this means that the implicit List.canBuildFrom will not be in the scope.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions