Skip to content

Polymorphic macro bundle fails when compiled by Scala 3.0.0-RC1 #12299

Open
@bishabosha

Description

@bishabosha

reproduction steps

using Scala 2.13.5,

Compile with Scala 3.0.0-RC1:

// TpeTag.scala
package testframework

final case class TpeTag[T](repr: String)
// package.scala
package object testframework {

  import scala.language.experimental.macros

  implicit def mkTpeTag[T]: TpeTag[T] = macro TpeTagImpl.mkTag[T]

  inline given mkTpeTag[T]: TpeTag[T] = ${ ??? } // unused placeholder to satisfy compiler
}
// TpeTagImpl.scala
package testframework

// using "org.scala-lang" % "scala-reflect" % "2.13.5"

import scala.reflect.macros.blackbox.Context

class TpeTagImpl(val c: Context) {
  import c.universe._

  def mkTag[T](implicit T: c.WeakTypeTag[T]): Tree = {
    val tpeString = Literal(Constant(T.tpe.toString))
    val tpeTagCls = c.mirror.staticClass(classOf[testframework.TpeTag[_]].getName())
    val tpeTagOfT = appliedType(tpeTagCls.toType, T.tpe)
    New(tpeTagOfT, tpeString) // new testframework.TpeTag[T](T.tpe.toString)
  }

}

Compile with Scala 2.13.5:

// Main.scala
import testframework._

object Main extends App {
  println(implicitly[TpeTag[List[String]]])
}

problem

When I compile Main.scala using sbt and scala sandwich I get the error, presumably linked to reflective invocation of the macro:

[error] Main.scala:6:21: exception during macro expansion: testframework / Compile / compileIncremental 0s
[error] java.lang.IllegalArgumentException: wrong number of arguments
[error]   println(implicitly[TpeTag[List[String]]])
[error]                     ^
[error] one error found

This error goes away if I rewrite TpeTagImpl.mkTag as a standard macro and not a bundle, or if I compile the bundle with Scala 2.13.5. I can't see a difference in the bytecode except the scala 3 version is missing some inserted checkcast instructions before calling ScalaRunTime.wrapRefArray, also the tasty for testframework.mkTpeTag is the same when the bundle is compiled by either Scala version.

Also, there is no failure for a bundle which is not polymorphic.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions