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

Confusing error message - diverging or ambiguous implicit instead of not found #11823

joroKr21 opened this issue Dec 7, 2019 · 3 comments · Fixed by scala/scala#8596


Copy link

@joroKr21 joroKr21 commented Dec 7, 2019

Apologies if it's a duplicate. There are many issues about confusing error messages, but I couldn't find this one.


scala> :paste
// Entering paste mode (ctrl-D to finish)

trait Foo[A]
trait Bar[A]
implicit val fooInt: Foo[Int] = ???
implicit val fooLong: Foo[Long] = ???
def bar[A](implicit foo: Foo[A]): Bar[A] = ???
val barString: Bar[String] = bar

// Exiting paste mode, now interpreting.

val barString: Bar[String] = bar
<pastie>:6: error: ambiguous implicit values:
 both value fooInt of type => Foo[Int]
 and value fooLong of type => Foo[Long]
 match expected type Foo[A]

This often comes up when deriving typeclasses because then we tend to write:
implicit val instance: Tc[Foo] = deriveTc
In practice we also get diverging implicit expansion errors as well (for bigger examples).

We could of course annotate with deriveTc[Foo], but this is not satisfactory.
Why don't we get the implicit not found message when clearly from the expected pattern we are searching for Foo[String] (both Foo and Bar are invariant)?


This comment has been minimized.

Copy link

@som-snytt som-snytt commented Dec 13, 2019

Maybe #9427


This comment has been minimized.

Copy link

@joroKr21 joroKr21 commented Dec 13, 2019

Maybe, but there is no covariance here.

I got interested in the cause and I find it here:

The original typed with pt gives the implicit not found error but we ignore it and retry without a pt which is of course ambiguous.


This comment has been minimized.

Copy link

@joroKr21 joroKr21 commented Dec 13, 2019

I have a suggestion - how about reporting the original errors if the retyping with pt = ? fails as well?
I don't know how to implement it though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
3 participants
You can’t perform that action at this time.