Skip to content

Instantly share code, notes, and snippets.

@rssh
Last active December 23, 2015 05:29
Show Gist options
  • Save rssh/6587773 to your computer and use it in GitHub Desktop.
Save rssh/6587773 to your computer and use it in GitHub Desktop.
type arguments are not substited in macroses defined inside type provider: https://issues.scala-lang.org/browse/SI-7849
import org.scalatest._
import x._
import language.experimental.macros
class TieSpec extends FlatSpec {
"X" should "substitute macro argument" in {
val x = X.xProvider;
val a = new Arg[Int] {}
x.f(a)
}
}
~
Output is
xtag = WeakTypeTag[H]
atag = WeakTypeTag[A]
//(must be Int instead A)
package x
import language.experimental.macros
import scala.reflect.macros._
import scala.reflect.api._
trait Arg[A]
{
}
object X
{
def xProvider = macro xProviderImpl
def xProviderImpl(c:Context) =
{
import c.universe._
/*
val rtree = q"""
{
class H {
def f[A](x:Arg[A]): Unit = macro X.fImpl[H,A]
}
new H { }
}
"""
*/
import Flag._
val rtree=Block(List(ClassDef(Modifiers(), newTypeName("H"), List(), Template(List(Select(Ident(newTermName("scala")),
newTypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(),
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))),
DefDef(Modifiers(MACRO), newTermName("f"), List(TypeDef(Modifiers(PARAM), newTypeName("A"), List(), TypeBoundsTree(
Select(Select(Ident(nme.ROOTPKG), newTermName("scala")), newTypeName("Nothing")), Select(Select(Ident(nme.ROOTPKG),
newTermName("scala")), newTypeName("Any"))))), List(List(ValDef(Modifiers(PARAM), newTermName("x"), AppliedTypeTree(
Ident(newTypeName("Arg")), List(Ident(newTypeName("A")))), EmptyTree))), Ident(newTypeName("Unit")), TypeApply(
Select(Ident(newTermName("X")), newTermName("fImpl")), List(Ident(newTypeName("H")), Ident(newTypeName("A"))))))))),
Block(List(ClassDef(Modifiers(FINAL), newTypeName("$anon"), List(), Template(List(Ident(newTypeName("H"))), emptyValDef,
List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(
This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))), EmptyTree)))), Apply(Select(New(
Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List())))
//System.err.println(showRaw(rtree))
c.Expr[Any](rtree)
}
def fImpl[X,A](c:Context)(x: c.Expr[A])(implicit xtag: c.WeakTypeTag[X], atag: c.WeakTypeTag[A]) =
{
import c.universe._
val xType = xtag.tpe
val aType = atag.tpe
Console.println("xtag = " + xtag)
Console.println("atag = " + atag)
reify{ () }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment