Skip to content

Transparent implicit macros cause spurious widening of a covariant type parameter #24678

@neko-kai

Description

@neko-kai

Compiler version

3.8.0-RC3, 3.7.4, 3.3.6

Minimized code

//> using scala 3.8.0-RC3

class BlackboxImplicit[T]
object BlackboxImplicit {
  implicit inline def materialize[A]: BlackboxImplicit[A] = new BlackboxImplicit[A]
}

class WhiteboxImplicit[T]
object WhiteboxImplicit {
  implicit transparent inline def materialize[A]: WhiteboxImplicit[A] = new WhiteboxImplicit[A]
}

class XAType[+E]
object XAType {
  def noError: XAType[Nothing] = new XAType[Nothing]

  extension [C](aType: XAType[C]) {
    def toLayerExtensionBlackbox(using tagC: BlackboxImplicit[C]): List[XAType[C]] = null
    def toLayerExtensionWhitebox(using tagC: WhiteboxImplicit[C]): List[XAType[C]] = null
  }
}

@main def main(): Unit = {
  val ok: XAType[Nothing] = XAType.noError

  val layerExtB = ok.toLayerExtensionBlackbox
  val codeExtB = compiletime.codeOf(identity(layerExtB))
  println(s"blackbox: $codeExtB")
  require(codeExtB.contains("XAType[Nothing]"))

  val layerExtW = ok.toLayerExtensionWhitebox
  val codeExtW = compiletime.codeOf(identity(layerExtW))
  println(s"whitebox: $codeExtW")
  require(codeExtW.contains("XAType[Nothing]"))
}

Scastie: https://scastie.scala-lang.org/B4G9FnOdRfenJcKc4Y6eDA

Output

blackbox: identity[List[XAType[Nothing]]](layerExtB)
whitebox: identity[List[XAType[Any]]](layerExtW)
Exception in thread "main" java.lang.IllegalArgumentException: requirement failed
        at scala.Predef$.require(Predef.scala:381)
        at minimized$package$.main(minimized.scala:34)
        at main.main(minimized.scala:23)

Expectation

Expected .toLayerExtensionWhitebox to not widen Nothing type parameter to Any, same as .toLayerExtensionBlackbox which preserves it

Minimized from zio/zio#8924

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