Skip to content

Instantly share code, notes, and snippets.

@squito
Last active August 23, 2017 05:45
Show Gist options
  • Save squito/6597917 to your computer and use it in GitHub Desktop.
Save squito/6597917 to your computer and use it in GitHub Desktop.
The horrors of compiler errors when working with macros ...
scala> def classExpandMacroImpl(c: Context)(s: c.Expr[Any]) : c.Expr[Any] = {
| import c.universe._
|
| val cdef = s.tree match {
| case Block(List(x:ClassDef), _) => x
| case _ => c.abort(c.enclosingPosition, "Was expecting a block w/ a ClassDef")
| }
|
| val q"class $name { ..$body }" = cdef
| val newdefs = List[c.universe.Tree](q"def x: Int = z + 7", q"def y: Float = z + 3.2f")
| val mergedefs = body.asInstanceOf[List[c.universe.Tree]] ++ newdefs
| println(showRaw(q"class $name extends AnyRef { ..${mergedefs}}"))
| // reify{()}
| reify[c.Expr[ClassDef]]{ c.Expr[ClassDef](q"class $name extends AnyRef { ..${mergedefs}}")}
| }
<console>:26: error: Cannot materialize c.Expr[c.universe.ClassDef](scala.reflect.api.QuasiquoteCompat.SyntacticClassDef.apply(c.universe).apply(scala.reflect.api.QuasiquoteCompat.Modifiers.apply(c.universe).apply(c.universe.build.flagsFromBits(0L), c.universe.newTypeName(""), immutable.this.Nil), name, immutable.this.Nil, scala.reflect.api.QuasiquoteCompat.Modifiers.apply(c.universe).apply(c.universe.build.flagsFromBits(0L), c.universe.newTypeName(""), immutable.this.Nil), scala.collection.immutable.List.apply[List[Nothing]](immutable.this.Nil), scala.collection.immutable.List.apply[c.universe.Ident](c.universe.Ident.apply(c.universe.newTypeName("AnyRef"))), scala.collection.immutable.List.apply[List[Nothing]](immutable.this.Nil), c.universe.emptyValDef, mergedefs))({
val $u: c.universe.type = c.universe;
val $m: $u.Mirror = c.universe.rootMirror;
$u.TypeTag.apply[c.universe.ClassDef]($m, {
final class $typecreator1 extends TypeCreator {
def <init>(): $typecreator1 = {
$typecreator1.super.<init>();
()
};
def apply[U >: Nothing <: scala.reflect.api.Universe with Singleton]($m$untyped: scala.reflect.api.Mirror[U]): U#Type = {
val $u: U = $m$untyped.universe;
val $m: $u.Mirror = $m$untyped.asInstanceOf[$u.Mirror];
val free$c1: $u.FreeTermSymbol = $u.build.newFreeTerm("c", c, $u.build.flagsFromBits(17592190246912L), "defined by classExpandMacroImpl in <console>:13:35");
$u.build.setTypeSignature[$u.FreeTermSymbol](free$c1, $m.staticClass("scala.reflect.macros.Context").asType.toTypeConstructor);
$u.TypeRef.apply($u.SingleType.apply($u.SingleType.apply($u.NoPrefix, free$c1), $u.build.selectTerm($m.staticClass("scala.reflect.macros.Context"), "universe")), $u.build.selectType($m.staticClass("scala.reflect.api.Trees"), "ClassDef"), immutable.this.Nil)
}
};
new $typecreator1()
})
}) as {
val $u: c.universe.type = c.universe;
val $m: $u.Mirror = c.universe.rootMirror;
$u.Expr.apply[c.Expr[c.universe.ClassDef]]($m, {
final class $treecreator1 extends TreeCreator {
def <init>(): $treecreator1 = {
$treecreator1.super.<init>();
()
};
def apply[U >: Nothing <: scala.reflect.api.Universe with Singleton]($m$untyped: Mirror[U]): U#Tree = {
val $u: U = $m$untyped.universe;
val $m: $u.Mirror = $m$untyped.asInstanceOf[$u.Mirror];
val free$c2: $u.FreeTermSymbol = $u.build.newFreeTerm("c", c, $u.build.flagsFromBits(17592190246912L), "defined by classExpandMacroImpl in <console>:13:35");
val free$name1: $u.FreeTermSymbol = $u.build.newFreeTerm("name", name, $u.build.flagsFromBits(17592190238720L), "defined by classExpandMacroImpl in <console>:21:25");
val free$mergedefs1: $u.FreeTermSymbol = $u.build.newFreeTerm("mergedefs", mergedefs, $u.build.flagsFromBits(17592190238720L), "defined by classExpandMacroImpl in <console>:23:16");
$u.build.setTypeSignature(free$c2, $m.staticClass("scala.reflect.macros.Context").asType.toTypeConstructor);
val symdef$x$11: $u.Symbol = $u.build.newNestedSymbol($u.build.selectTerm($m.staticModule("$iw").asModule.moduleClass, "classExpandMacroImpl"), $u.newTermName("x$1"), $u.NoPosition, $u.build.flagsFromBits(17592188665860L), false);
val symdef$_106$type1: $u.Symbol = $u.build.newNestedSymbol(symdef$x$11, $u.newTypeName("_106.type"), $u.NoPosition, $u.build.flagsFromBits(34359738384L), false);
val free$u1: <error> = $u.build.newFreeTerm("u", <u: error>, $u.build.flagsFromBits(138412112L), "defined by <refinement> in embeddedFile--QuasiquoteCompat.scala@a126b96c589e42e5b7f6f12c44aea603:94:88");
$u.build.setTypeSignature(free$name1, $u.ExistentialType(scala.collection.immutable.List.apply(symdef$_106$type1), $u.TypeRef($u.SingleType($u.TypeRef($u.NoPrefix, symdef$_106$type1, scala.collection.immutable.List.apply()), free$u1), $u.build.selectType($m.staticClass("scala.reflect.api.Names"), "TypeName"), scala.collection.immutable.List.apply())));
$u.build.setTypeSignature(free$mergedefs1, $u.TypeRef($u.ThisType($m.staticPackage("scala.collection.immutable").asModule.moduleClass), $m.staticClass("scala.collection.immutable.List"), scala.collection.immutable.List.apply($u.TypeRef($u.SingleType($u.SingleType($u.NoPrefix, free$c2), $u.build.selectTerm($m.staticClass("scala.reflect.macros.Context"), "universe")), $u.build.selectType($m.staticClass("scala.reflect.macros.Universe"), "Tree"), scala.collection.immutable.List.apply()))));
val symdef$_106$type2: $u.Symbol = $u.build.newNestedSymbol(symdef$x$11, $u.newTypeName("_106.type"), $u.NoPosition, $u.build.flagsFromBits(34359738384L), false);
$u.build.setTypeSignature(symdef$x$11, $u.ExistentialType(scala.collection.immutable.List.apply(symdef$_106$type2), $u.TypeRef($u.ThisType($m.staticPackage("scala").asModule.moduleClass), $m.staticClass("scala.Tuple2"), scala.collection.immutable.List.apply($u.TypeRef($u.SingleType($u.TypeRef($u.NoPrefix, symdef$_106$type2, scala.collection.immutable.List.apply()), free$u1), $u.build.selectType($m.staticClass("scala.reflect.api.Names"), "TypeName"), scala.collection.immutable.List.apply()), $u.TypeRef($u.ThisType($m.staticPackage("scala.collection.immutable").asModule.moduleClass), $m.staticClass("scala.collection.immutable.List"), scala.collection.immutable.List.apply($u.TypeRef($u.SingleType($u.TypeRef($u.NoPrefix, symdef$_106$type2, scala.collection.immutable.List.apply()), free$u1), $u.build.selectType($m.staticClass("scala.reflect.macros.Universe"), "Tree"), scala.collection.immutable.List.apply())))))));
val symdef$$refinement$1: $u.Symbol = $u.build.newNestedSymbol($m.RootClass, $u.newTypeName("<refinement>"), $u.NoPosition, $u.build.flagsFromBits(0L), true);
val symdef$$refinement$2: $u.Symbol = $u.build.newNestedSymbol($m.staticModule("scala.reflect.api.QuasiquoteCompat.SyntacticClassDef").asModule.moduleClass, $u.newTypeName("<refinement>"), $u.NoPosition, $u.build.flagsFromBits(0L), true);
$u.build.setTypeSignature(symdef$_106$type1, $u.TypeBounds($m.staticClass("scala.Nothing").asType.toTypeConstructor, $u.RefinedType(scala.collection.immutable.List.apply($u.RefinedType(scala.collection.immutable.List.apply($m.staticClass("scala.reflect.api.QuasiquoteCompat.SyntacticClassDefExtractor").asType.toTypeConstructor), $u.newScopeWith(free$u1), symdef$$refinement$2), $m.staticClass("scala.Singleton").asType.toTypeConstructor), $u.newScopeWith(), symdef$$refinement$1)));
$u.build.setTypeSignature(free$u1, $u.NullaryMethodType($u.SingleType($u.SingleType($u.NoPrefix, free$c2), $u.build.selectTerm($m.staticClass("scala.reflect.macros.Context"), "universe"))));
$u.build.setTypeSignature(symdef$_106$type2, $u.TypeBounds($m.staticClass("scala.Nothing").asType.toTypeConstructor, $u.RefinedType(scala.collection.immutable.List.apply($u.RefinedType(scala.collection.immutable.List.apply($m.staticClass("scala.reflect.api.QuasiquoteCompat.SyntacticClassDefExtractor").asType.toTypeConstructor), $u.newScopeWith(free$u1), symdef$$refinement$2), $m.staticClass("scala.Singleton").asType.toTypeConstructor), $u.newScopeWith(), symdef$$refinement$1)));
$u.build.setTypeSignature(symdef$$refinement$1, $u.RefinedType(scala.collection.immutable.List.apply($u.RefinedType(scala.collection.immutable.List.apply($m.staticClass("scala.reflect.api.QuasiquoteCompat.SyntacticClassDefExtractor").asType.toTypeConstructor), $u.newScopeWith(free$u1), symdef$$refinement$2), $m.staticClass("scala.Singleton").asType.toTypeConstructor), $u.newScopeWith(), symdef$$refinement$1));
$u.build.setTypeSignature(symdef$$refinement$2, $u.RefinedType(scala.collection.immutable.List.apply($m.staticClass("scala.reflect.api.QuasiquoteCompat.SyntacticClassDefExtractor").asType.toTypeConstructor), $u.newScopeWith(free$u1), symdef$$refinement$2));
$u.Apply($u.Apply($u.TypeApply($u.Select($u.build.Ident(free$c2), $u.newTermName("Expr")), scala.collection.immutable.List.apply($u.build.TypeTree($u.TypeRef($u.SingleType($u.SingleType($u.NoPrefix, free$c2), $u.build.selectTerm($m.staticClass("scala.reflect.macros.Context"), "universe")), $u.build.selectType($m.staticClass("scala.reflect.api.Trees"), "ClassDef"), scala.collection.immutable.List.apply())))), scala.collection.immutable.List.apply($u.Apply($u.Select($u.Select($u.Apply($u.Select($u.Select($u.build.Ident(free$c2), $u.newTermName("universe")), $u.newTermName("Quasiquote")), scala.collection.immutable.List.apply($u.Apply($u.Select($u.build.Ident($m.staticModule("scala.StringContext")), $u.newTermName("apply")), scala.collection.immutable.List.apply($u.Literal($u.Constant("class ")), $u.Literal($u.Constant(" extends AnyRef { ..")), $u.Literal($u.Constant("}")))))), $u.newTermName("q")), $u.newTermName("apply")), scala.collection.immutable.List.apply($u.build.Ident(free$name1), $u.build.Ident(free$mergedefs1))))), scala.collection.immutable.List.apply($u.Select($u.build.This($m.staticModule("scala.Predef").asModule.moduleClass), $u.newTermName("implicitly"))))
}
};
new $treecreator1()
})($u.WeakTypeTag.apply[c.Expr[c.universe.ClassDef]]($m, {
final class $typecreator3 extends TypeCreator {
def <init>() = {
super.<init>();
()
};
def apply[U >: Nothing <: Universe with Singleton]($m$untyped: Mirror[U]): U#Type = {
val $u: U = $m$untyped.universe;
val $m: $u.Mirror = $m$untyped.asInstanceOf[$u.Mirror];
val free$c3 = $u.build.newFreeTerm("c", c, $u.build.flagsFromBits(17592190246912L), "defined by classExpandMacroImpl in <console>:13:35");
$u.build.setTypeSignature(free$c3, $m.staticClass("scala.reflect.macros.Context").asType.toTypeConstructor);
$u.TypeRef($u.SingleType($u.NoPrefix, free$c3), $u.build.selectType($m.staticClass("scala.reflect.macros.Aliases"), "Expr"), scala.collection.immutable.List.apply($u.TypeRef($u.SingleType($u.SingleType($u.NoPrefix, free$c3), $u.build.selectTerm($m.staticClass("scala.reflect.macros.Context"), "universe")), $u.build.selectType($m.staticClass("scala.reflect.api.Trees"), "ClassDef"), scala.collection.immutable.List.apply())))
}
};
new $typecreator3()
}))
} because:
scala.reflect.macros.TypecheckException: not found: value u
reify[c.Expr[ClassDef]]{ c.Expr[ClassDef](q"class $name extends AnyRef { ..${mergedefs}}")}
^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment