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

Regression in `alterationx10/opinionated-zio` match types #20858

Open EugeneFlesselle opened 2 days ago

EugeneFlesselle commented 2 days ago

One more affected project alterationx10/opinionated-zio it's bisect points to the same commits. This time we don't have ambigious extension methods, yet, still type reduction fails

// 1_defns.scala
import scala.compiletime.*
import scala.deriving.*

sealed trait ZIO[-R, +E, +A]
sealed abstract class ZLayer[-RIn, +E, +ROut]
object ZLayer:
    def apply[RIn, E, ROut](zio: => ZIO[RIn, E, ROut]): ZLayer[RIn, E, ROut] = ???
type URIO[-R, +A] = ZIO[R, Nothing, A]
type IAnyType[T <: Tuple] = Tuple.Fold[T, Any, [x, y] =>> x & y]
type UAnyType[T <: Tuple] = Tuple.Fold[T, Any, [x, y] =>> x | y]

trait AutoLayer[A]:
  def zlayer(using
      p: Mirror.ProductOf[A]
  ): ZLayer[IAnyType[p.MirroredElemTypes], Nothing, A]

object AutoLayer:
  inline given derived[A](using p: Mirror.ProductOf[A]): AutoLayer[A] = {
    val a: ZIO[IAnyType[p.MirroredElemTypes], Nothing, A] = ???
    new AutoLayer[A]:
      override def zlayer(using
          pp: Mirror.ProductOf[A]
      ): ZLayer[IAnyType[pp.MirroredElemTypes], Nothing, A] = ZLayer {
        a.asInstanceOf[ZIO[IAnyType[pp.MirroredElemTypes], Nothing, A]]
      }
    }
// 2_usage.scala
case class TestService(port: Int) derives AutoLayer

yields:

[error] Found:    ZLayer[Tuple.Fold[pp.MirroredElemTypes, Any, [x, y] =>> x & y], Nothing,
[error]   TestService]
[error] Required: ZLayer[IAnyType[pp.MirroredElemTypes], Nothing, TestService]
[error] 
[error] Note: a match type could not be fully reduced:
[error] 
[error]   trying to reduce  Tuple.Fold[pp.MirroredElemTypes, Any, [x, y] =>> x & y]
[error]   failed since selector pp.MirroredElemTypes
[error]   does not match  case EmptyTuple => Any
[error]   and cannot be shown to be disjoint from it either.
[error]   Therefore, reduction cannot advance to the remaining case
[error] 
[error]     case h *: t => h & Tuple.Fold[t, Any, [x, y] =>> x & y]
[error] case class TestService(port: Int) derives AutoLayer

Originally posted by @WojciechMazur in https://github.com/scala/scala3/issues/20288#issuecomment-2085298894

EugeneFlesselle commented 2 days ago

Related to #18261