0

我正在编写一个宏,可以在编译期间记录一条短消息,使用 scala 2.13 的模式匹配和常量类型特性:

class EmitMsg[T, SS <: EmitMsg.EmitLevel] {}

object EmitMsg {

  trait EmitLevel

  trait Info extends EmitLevel
  trait Warning extends EmitLevel
  trait Error extends EmitLevel
  trait Abort extends EmitLevel

  def create[A, SS <: EmitMsg.EmitLevel]: EmitMsg[A, SS] = new EmitMsg[A, SS]

  implicit def emit[A, SS <: EmitMsg.EmitLevel]: EmitMsg[A, SS] = macro Macros.emit[A, SS]

  final class Macros(val c: whitebox.Context) {
    val u = c.universe
    import u._

    def outer: EmitMsg.type = EmitMsg.this

    def emit[A: c.WeakTypeTag, LL: c.WeakTypeTag]: c.Tree = {

      val aa: Type = weakTypeOf[A]
      val v = aa match {
        case v: u.ConstantType => v.value.value
        case _ =>
          throw new UnsupportedOperationException(
            s"type $aa is not a constant"
          )
      }
      val ss = "" + v
      val ll: Type = weakTypeOf[LL]

      // if inherited from multiple traits, take the most serious one
      if (ll <:< weakTypeOf[Abort]) {
        c.abort(c.enclosingPosition, ss)
      } else if (ll <:< typeOf[Error]) {
        c.error(c.enclosingPosition, ss)
      } else if (ll <:< typeOf[Warning]) {
        c.warning(c.enclosingPosition, ss)
      } else if (ll <:< typeOf[Info]) {
        c.info(c.enclosingPosition, ss, force = true)
      } else {
        throw new UnsupportedOperationException(
          s"type $ll is not an EmitLevel"
        )
      }

      q"$liftOuter.create[$aa, $ll]"
    }
  }
}

在测试的时候,我发现直接调用宏大部分时间都是有效的:

EmitMsg.emit["ABC", EmitMsg.Error]
EmitMsg.emit["ABC", EmitMsg.Warning]
...

(这会生成正确的编译消息):


[Error] /home/peng/git/shapesafe/macro/src/test/scala/org/shapesafe/m/EmitMsgSpec.scala:19: ABC
[Warn] /home/peng/git/shapesafe/macro/src/test/scala/org/shapesafe/m/EmitMsgSpec.scala:20: ABC
one warning found

...但如果是隐式宏模式的一部分(https://docs.scala-lang.org/overviews/macros/implicits.html),则很少起作用:

  it("can emit error") 

    type TT = EmitMsg["ABC", EmitMsg.Error]
    implicitly[TT] //(EmitMsg.emit)
  }
/*
 Generates the following Message:

[Error] /home/peng/git/shapesafe/macro/src/test/scala/org/shapesafe/m/EmitMsgSpec.scala:15: could not find implicit value for parameter e: TT
*/

  it("can emit warning") {

    type TT = EmitMsg["ABC", EmitMsg.Warning]
    implicitly[TT] //(EmitMsg.emit)
  }
/*
Doesn't do a thing
*/

  it("can emit info") {

    type TT = EmitMsg["ABC", EmitMsg.Info]
    implicitly[TT] //(EmitMsg.emit)
  }
/*
Doesn't do a thing
*/

所以我的问题是:

  • 调用 c.info/warning/error 是登录编译时的正确方法吗?

  • 如果是这样,为什么它们永远不会作为隐式宏模式的一部分?

非常感谢您的建议!

更新 1刚刚在https://github.com/fthomas/singleton-ops/blob/204195838ada34de7e453401fb06810ace2c99b0/src/main/scala/singleton/ops/impl/GeneralMacros.scala#L102找到了一个案例(错误)的解决方案

    val tree0 =
      c.typecheck(
        q"""
          new _root_.scala.annotation.implicitNotFound("dummy")
        """,
        silent = false
      )

    class SubstMessage extends Transformer {
      val global = c.universe.asInstanceOf[scala.tools.nsc.Global]

      override def transform(tree: Tree): Tree = {
        super.transform {
          tree match {
            case Literal(Constant("dummy")) => Literal(Constant(msg))
            case t => t
          }
        }
      }
    }

    val tree = new SubstMessage().transform(tree0)

    annotatedSym.setAnnotations(Annotation(tree))
    ()

这是一个复杂的 hack,它通过在隐式函数的定义上添加 @implicitNotFound 注释来工作。到目前为止,我不知道警告和信息案例有任何类似的解决方案。然而,更简单的解决方案始终是首选

4

1 回答 1

0

如果您不想在隐式解析期间推迟编译错误,只需制作c一个blackbox.Context而不是。whitebox.Context

于 2021-06-20T12:59:37.827 回答