scala / bug

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

Incorrect diverging implicit expansion using type parameters with type parameters #12786

Open eejbyfeldt opened 1 year ago

eejbyfeldt commented 1 year ago

Reproduction steps

Scala version: 2.13.10 and 2.12.17 but reproduced with all 2.x builds I tried. But seems to be fixed in scala 3

trait S[T]
trait S2[T]
trait TypeClass[U]

object TypeClass {
  implicit def forInt: TypeClass[Int] = null
  implicit def forS[U: TypeClass, C <: S[U]]: TypeClass[C] = null
  implicit def forS2[U: TypeClass]: TypeClass[S2[U]] = null
}

object Test {
  implicitly[TypeClass[Int]]
}

Problem

The code fails to compile with

tmp.scala:12: error: diverging implicit expansion for type example.TypeClass[Int]
starting with method forS2 in object TypeClass
  implicitly[TypeClass[Int]]
            ^
1 error

but I would expect it to compile without errors using the forInt to create the implicit TypeClass[S2]]

eejbyfeldt commented 1 year ago

Maybe related the code

trait S[T]
trait TypeClass[U]

object TypeClass {
  implicit def forS[U: TypeClass, C <: S[U]]: TypeClass[C] = null
}

object Test {
  implicitly[TypeClass[Int]]
}

fails to compile with

error: diverging implicit expansion for type example.TypeClass[Int]
starting with method forS in object TypeClass
  implicitly[TypeClass[Int]]
            ^
1 error

so it seems like the implicit forS diverges with itself?