1

I'm not sure whether its duplicate of Type Parameters on Scala Macro Annotations or not.

I'm trying to get type parameter on macro annotation:

class builder extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro builderMacro.impl
}
//....
val q"new $_[$tpt]().macroTransform(..$_)" = c.macroApplication
val tpe = c.typecheck(tpt).tpe
// also tried
// val tpe = c.typecheck(q"None.asInstanceOf[$tpt]").tpe

Code that uses macro:

object Test2 {
  trait TestBuilders

  @builder[TestBuilders]
  case class TestClass(x: Int, opt1: Option[String], opt2: Option[String])  {
    val opts = (opt1, opt2)
  }
}

and exception i get:

[error] scala.reflect.macros.TypecheckException: not found: type TestBuilders
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2$$anonfun$apply$1.apply(Typers.scala:34)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2$$anonfun$apply$1.apply(Typers.scala:28)
[error]         at scala.tools.nsc.typechecker.Contexts$Context.withMode(Contexts.scala:374)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$3.apply(Typers.scala:24)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$3.apply(Typers.scala:24)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$withContext$1$1.apply(Typers.scala:25)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$withContext$1$1.apply(Typers.scala:25)
[error]         at scala.tools.nsc.typechecker.Contexts$Context.withMode(Contexts.scala:374)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$1.apply(Typers.scala:23)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$1.apply(Typers.scala:23)
[error]         at scala.reflect.macros.contexts.Typers$class.withContext$1(Typers.scala:25)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2.apply(Typers.scala:28)
[error]         at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2.apply(Typers.scala:28)
[error]         at scala.reflect.internal.Trees$class.wrappingIntoTerm(Trees.scala:1691)
[error]         at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:16)
[error]         at scala.reflect.macros.contexts.Typers$class.withWrapping$1(Typers.scala:26)
[error]         at scala.reflect.macros.contexts.Typers$class.typecheck(Typers.scala:28)
[error]         at scala.reflect.macros.contexts.Context.typecheck(Context.scala:6)
[error]         at scala.reflect.macros.contexts.Context.typecheck(Context.scala:6)
[error]         at builderMacro$.impl(Macros.scala:55)

what am i doing wrong?

Community
  • 1
  • 1
wedens
  • 1,782
  • 14
  • 18
  • ok.. i was able to use just `tpt` Tree without actual Type. but i still don't understand why i can't get type here – wedens Jul 07 '14 at 04:56

1 Answers1

3

This is a known issue in current macro paradise: https://github.com/scalamacros/paradise/issues/14. Note that if TestBuilders is declared in a different scope, everything should work out.

Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59