Skip to content

Regression: invalid implicit search for an uninstantiated higher-kinded type variable is generated, even though the type variable should have been instantiated by previously found implicits #15915

Closed
@neko-kai

Description

@neko-kai

Compiler version

3.1.3 and 3.2.0-RC1

Minimized code

runnable: https://scastie.scala-lang.org/mIVAeZ5HSMCazZQlFl9qHQ

class _Monoid[A]
object _Monoid {
  implicit val Monoid: _Monoid[Int] = new _Monoid[Int]
}

class Lifecycle[A]
object Lifecycle {

  implicit def monoidForLifecycle[Monoid[_], A](
    implicit
    monoidType: GetMonoidType[Monoid],
    monoidA: Monoid[A]
  ): Monoid[Lifecycle[A]] = new _Monoid().asInstanceOf[Monoid[Lifecycle[A]]]

}

sealed class GetMonoidType[C[_]]
object GetMonoidType {
  implicit val getMonoid: GetMonoidType[_Monoid] = new GetMonoidType[_Monoid]
}

object App extends App {
  println(implicitly[_Monoid[Lifecycle[Int]]])
}

Output

No given instance of type _Monoid[Lifecycle[Int]] was found for parameter e of method implicitly in object Predef.
I found:

    Lifecycle.monoidForLifecycle[Monoid, Int](GetMonoidType.getMonoid, 
      /* missing */summon[Monoid[Int]]
    )

But no implicit values were found that match type Monoid[Int].

Note that the type Monoid should be inferred to _Monoid as a result of picking up GetMonoidType.getMonoid, however it's not, and the search is performed on an uninstantiated type variable Monoid, instead of on _Monoid[Int] and cannot succeed.

Expectation

Expected to work, as in Scala 2: https://scastie.scala-lang.org/AXiXnf60R8K2CRaOMm7g8A

This is similar to #13986 in that it involves an HKT type variable not being instantiated correctly during implicit search.
Unlike that issue, there's a workaround for this one, via multiple using clauses:

implicit def monoidForLifecycle[Monoid[_], A](
    using monoidType: GetMonoidType[Monoid]
  )(using monoidA: Monoid[A]
  ): Monoid[Lifecycle[A]] = new _Monoid().asInstanceOf[Monoid[Lifecycle[A]]]

Note that due to #13986, setting a bound on the type variable Monoid[x] <: _Monoid[x] does not workaround the error.

This code is a minimized version of real library code in https://github.com/izumi/izumi which we're trying to port to Scala 3

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