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

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

Closed
neko-kai opened this issue Aug 25, 2022 · 3 comments
Labels
area:implicits related to implicits itype:bug

Comments

@neko-kai
Copy link
Contributor

neko-kai commented Aug 25, 2022

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

@neko-kai neko-kai added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 25, 2022
neko-kai added a commit to 7mind/izumi that referenced this issue Aug 25, 2022
@Kordyjan Kordyjan added area:implicits related to implicits and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 26, 2022
@odersky
Copy link
Contributor

odersky commented Aug 26, 2022

This looks suspiciously like #15820 and might be a consequence of (db5956b). Can we find out? If it is, the new behavior is as expected.

@odersky odersky removed their assignment Aug 26, 2022
@neko-kai
Copy link
Contributor Author

This specific example seems to work now in 3.2.1-RC2 (https://scastie.scala-lang.org/wLkgdKE9TlG2lZgB4kgc2g)

@odersky
Copy link
Contributor

odersky commented Oct 13, 2022

Thanks for the heads-up! Then it was probably also fixed by #16001. I think we can close this then.

@odersky odersky closed this as completed Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits itype:bug
Projects
None yet
Development

No branches or pull requests

3 participants