scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.72k stars 1.04k forks source link

Incorrect warning about ambiguous implicit #20572

Closed OlegYch closed 1 day ago

OlegYch commented 3 weeks ago

Compiler version

3.5.0-RC1-3.5.1-RC1-bin-20240613-61a15b2-NIGHTLY

Minimized code

can't really minimize, but i hope the error message is enough here is an approximation but for some reason it doesn't produce a warning: https://scastie.scala-lang.org/OlegYch/io8Z28sZTC2qIUA3lqJtSw/36

Output

instead i get a warning:

[warn] 124 |              "metal"      -> Money[CurrencyCode.Any](1, CurrencyCode.Gold),
[warn]     |                                                                           ^
[warn]     |Given search preference for play.api.libs.json.Writes[com.Money[com.CurrencyCode.CurrencyValue]] between alternatives (com.Money._MoneyFormat :
[warn]     |  [C <: com.CurrencyCode.Any]
[warn]     |    (implicit evidence$1: play.api.libs.json.Format[C]): play.api.libs.json.Format[com.Money[C]]
[warn]     |) and (play.api.libs.json.Writes.enumNameWrites :
[warn]     |  [E <: Enumeration](using e: ValueOf[E]): play.api.libs.json.Writes[e.value.Value]) will change
[warn]     |Current choice           : the first alternative
[warn]     |New choice from Scala 3.6: none - it's ambiguous

for some reason it tries to apply enumNameWrites from play, but it's only applicable for scala.Enumeration, not enumeratum

Expectation

no warning

OlegYch commented 2 weeks ago

i was able to minimize it (using 3.5.0-RC2):

trait Writes[T]
trait Format[T] extends Writes[T]
given [T: List]: Writes[T] = null
given [T]: Format[T] = null

summon[Writes[Int]]

gives

Given search preference for Playground.Writes[Int] between alternatives (Playground.given_Format_T : [T]: Playground.Format[T]) and (Playground.given_Writes_T :
  [T](implicit evidence$1: List[T]): Playground.Writes[T]) will change
Current choice           : the first alternative
New choice from Scala 3.6: the second alternative

https://scastie.scala-lang.org/OlegYch/io8Z28sZTC2qIUA3lqJtSw/146

mrdziuban commented 1 week ago

I've just run into this on Scala 3.5.0-RC2 as well with circe -- https://scastie.scala-lang.org/jVQZCmzbRFaKxJk6AroUsw

import io.circe.Encoder

case class Foo[A]() derives Encoder
case class Bar[A](foo: Foo[A]) derives Encoder

yields an error

Given search preference for io.circe.Encoder[Playground.Foo[A]] between alternatives (Playground.Foo.derived$Encoder :
  [A²](implicit x$0: io.circe.Encoder[A²]): io.circe.Encoder[Playground.Foo[A²]]
  ) and (io.circe.Encoder.encodeIterable :
  [A², C[_$20]]
    (implicit encodeA: io.circe.Encoder[A²], ev: C[A²] => Iterable[A²]):
      io.circe.Encoder.AsArray[C[A²]]
) will change
Current choice           : none - it's ambiguous
New choice from Scala 3.6: the first alternative

where:    A  is a type in given instance derived$Encoder²
          A² is a type variable