Skip to content

Wrong type inference within implicit resolution #18578

Closed
@KuceraMartin

Description

@KuceraMartin

Compiler version

3.3.1

Minimized code

trait Animal
class Dog extends Animal

trait Ev[T]
given Ev[Dog] = ???
given Ev[Animal] = ???
given [T: Ev]: Ev[Set[T]] = ???

def f[T: Ev](v: T): Unit = ???

def main =
  val s = Set(new Dog)
  f(s)
  f(Set(new Dog))

Output

[error] Ambiguous given instances: both given instance given_Ev_Dog and given instance given_Ev_Animal match type Ev[T] of an implicit parameter of method f
[error]   f(Set(new Dog))
[error]                  ^

Note that the error is from the very last line. The first call f(s) is okay.

From typer:

package <empty> {
  trait Animal() extends Object {}
  class Dog() extends Object(), Animal {}
  trait Ev[T >: Nothing <: Any]() extends Object {
    T
  }
  final lazy module val implicit-variance$package: implicit-variance$package =
    new implicit-variance$package()
  final module class implicit-variance$package() extends Object() {
    this: implicit-variance$package.type =>
    final lazy given val given_Ev_Dog: Ev[Dog] = ???
    final lazy given val given_Ev_Animal: Ev[Animal] = ???
    final given def given_Ev_Set[T >: Nothing <: Any](implicit evidence$1: Ev[T]
      ): Ev[Set[T]] = ???
    def f[T >: Nothing <: Any](v: T)(implicit evidence$2: Ev[T]): Unit = ???
    def main:

        <error both given instance given_Ev_Dog and given instance given_Ev_Animal match type Ev[T]>

     =
      {
        val s: Set[Dog] = Set.apply[Dog]([new Dog() : Dog]*)
        f[Set[Dog]](s)(given_Ev_Set[Dog](given_Ev_Dog))
        f[Set[Dog]](Set.apply[Dog]([new Dog() : Dog]*))(
          given_Ev_Set[T](
            /* ambiguous: both given instance given_Ev_Dog and given instance given_Ev_Animal match type Ev[T] */
              summon[Ev[T]]
          )
        )
      }
  }
}

Expectation

It should compile.

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