-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Mixing macros doesn't work if the scala 2 macro needs a WeakTypeTag of a type parameter to an enclosing class #16630
Comments
Doesn't actually work because of a bug in scala 3: scala/scala3#16630
Another workaround seems to be to make import scala.language.experimental.macros
import scala.quoted.{Quotes, Expr, Type}
class Foo[A]
object Foo {
def apply[A] = new Foo[A]
}
trait TraitWithTypeMember {
type A
protected inline def foo: Foo[A] = ${ MacrosImpl.fooImpl[A] }
protected def foo: Foo[A] = macro MacrosImpl.compatFooImpl[A]
}
trait TraitWithTypeParam[A0] extends TraitWithTypeMember {
type A = A0
}
object MacrosImpl {
def fooImpl[A: Type](using quotes: Quotes): Expr[Foo[A]] = '{new Foo[A]}
def compatFooImpl[A: c.WeakTypeTag](c: scala.reflect.macros.blackbox.Context): c.Tree = {
import c.universe._
TypeApply(
Select(
Ident(c.mirror.staticModule(classOf[Foo[A]].getName)),
TermName("apply")
),
List(TypeTree(weakTypeOf[A])),
)
}
} This compiles however I haven't tested it at use site |
Hmm that does compile, but when you call the macro from scala 2, the
at the call-site in scala 2. I appreciate the suggestion but it doesn't appear to actually be effective 🙃 |
This is still happening in scala 3.3.1, it's blocking scala-logging to be updated to work on mixed scala 2.13/3 projects. |
Fixes #16630 This PR fixes the above issue which affects cross scala2/3 projects that use a common macro library as per https://docs.scala-lang.org/scala3/guides/migration/tutorial-macro-mixing.html , e.g. scala-logging is blocked from fixing this issue lightbend-labs/scala-logging#317 The fix makes the scala 2 macro check read the Erased flag from the initial flags rather than completing the RHS first. This will work in the case of scala 2 macros because the erased flag is explicitly added rather than being in the source code. However it relies on using an "UNSAFE" value.
Compiler version
3.2.1
Minimized code
build.sbt:
Output
Expectation
This should compile and work as described in https://docs.scala-lang.org/scala3/guides/migration/tutorial-macro-mixing.html. Instead it doesn't compile.
Note that moving the
[A]
type parameter to the methods (i.e.) or commenting out the scala 2 macro results in the code compiling. So it seems like attempting to call a scala 2 macro that takes a
WeakTypeTag
for a type that's a parameter to a class messes up the type tree for the class type parameter somehowThe text was updated successfully, but these errors were encountered: