scala / scala3

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

Regression in `7mind/izumi` for implicit search / typer #19942

Closed WojciechMazur closed 4 months ago

WojciechMazur commented 4 months ago

Based on Open CB failure found in 7mind/izumi - build logs When the Identity type is placed in different scope then rest of the code, implicit cannot be found.

Compiler version

Last good release: 3.4.2-RC1-bin-20240222-98efdab-NIGHTLY First bad release: 3.4.2-RC1-bin-20240226-e0cb1e7-NIGHTLY Bisect points to 729e5ba099688a1e92c7d5cdf9611e7d1469379c

Minimized code

type LifecycleF = [_] =>> Any
trait Lifecycle[+F[_], +A]

trait LifecycleTag[R]
object LifecycleTag:
  implicit def resourceTag[R <: Lifecycle[F0, A0], F0[_], A0]: LifecycleTag[R] = ???

trait MakeDSL[T]:
  final def fromResource[R <: Lifecycle[LifecycleF, T]](implicit tag: LifecycleTag[R]): Any = ???

object distage:
  // Cannot be defined in the same scope as rest of code
  final type Identity[+A] = A
import distage.*

trait Resource
trait DependentResource() extends Lifecycle[Identity, Resource]

@main def Test =  {
  val dsl: MakeDSL[Resource] = ???
  val fails = dsl.fromResource[DependentResource]
  val works = dsl.fromResource[DependentResource](using LifecycleTag.resourceTag[DependentResource, Identity, Resource])
}

Output

Compiling project (Scala 3.4.2-RC1-bin-20240312-3694d95-NIGHTLY, JVM (17))
[error] ./test.scala:50:50
[error] No given instance of type LifecycleTag[DependentResource] was found for parameter tag of method fromResource in trait MakeDSL.
[error] I found:
[error] 
[error]     LifecycleTag.resourceTag[R, F0, A0]
[error] 
[error] But method resourceTag in object LifecycleTag does not match type LifecycleTag[DependentResource].
[error]   val fails = dsl.fromResource[DependentResource]
[error]                                                  ^
Error compiling project (Scala 3.4.2-RC1-bin-20240312-3694d95-NIGHTLY, JVM (17))

Expectation

Should compile

WojciechMazur commented 4 months ago

The bisect points to the same commit also in typelevel/kittens(build logs) when companion object defining type alias is defined AFTER class/trait refers to type alias in implicit search

Minimization

trait Alternative[F[_]]

opaque type Derived[A] = A
object Derived:
  extension [A](derived: Derived[A]) def instance: A = derived
  infix type <<<[F[_], G[_]] = [x] =>> F[G[x]]

import Derived.*
import scala.compiletime.summonInline

type DerivedAlternative[F[_]] = Derived[Alternative[F]]
object DerivedAlternative:
  inline def apply[F[_]]: Alternative[F] =
    import DerivedAlternative.given
    summonInline[DerivedAlternative[F]].instance
  given nested[F[_], G[_]]: DerivedAlternative[F <<< G] = ???

object auto:
  object alternative:
    transparent inline given [F[_]]: Alternative[F] = DerivedAlternative[F]

trait Test:
  import Test.*
  import auto.alternative.given
  val fails = summon[Alternative[OptList]]

// Fails if companion object defined AFTER trait
object Test:
  type OptList[A] = Option[List[A]]

Output:

-- [E172] Type Error: /Users/wmazur/projects/scala3/bisect/main.scala:25:42 ----
25 |  val fails = summon[Alternative[OptList]]
   |                                          ^
   |No given instance of type main$package$_this.DerivedAlternative[[A] =>> Test.OptList[A]] was found.
   |I found:
   |
   |    auto.alternative.given_Alternative_F[[A] =>> Test.OptList[A]]
   |
   |But given instance given_Alternative_F in object alternative does not match type main$package$_this.DerivedAlternative[[A] =>> Test.OptList[A]].
   |----------------------------------------------------------------------------
   |Inline stack trace
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from main.scala:15
15 |    summonInline[DerivedAlternative[F]].instance
   |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |This location contains code that was inlined from main.scala:15
20 |    transparent inline given [F[_]]: Alternative[F] = DerivedAlternative[F]
   |                                                      ^^^^^^^^^^^^^^^^^^^^^
    ----------------------------------------------------------------------------
1 error found