Compilation failure is confirmed at the moment of accessing typeSignature of a specific symbol at macro expansion

倾然丶 夕夏残阳落幕 提交于 2019-12-13 02:57:04

问题


I had to look at specific declarations in the macro and examine them individually. Many such errors occurred when trying to examine the akka stream declaration.

def getType(symbol: Symbol): Type = {
  symbol.typeSignature
}
[error] error while loading SmallSortedMap$Entry, class file '/Users/xxx/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: K)

In this way, it seems that an error occurs when accessing a typeSygnature for a specific symbol.

I want to ignore this, hide it and compile successfully

def getType(symbol: Symbol): Option[Type] = {
  Try {
    symbol.typeSignature
  }.getOrElse(None) // Can not capture
}

However, it seems that "class is broken" can not be caught by "Try". For example, packages with this symbol are excluded as blacklists. As a result, complex maintenance may occur each time a dependency is added.

  if (symbol.isNotBroken) {
    symbol.typeSignature
  }

Is there a way to solve this way?






Try

I tried typeCheck.

implicit class RichVectorSymbol(value: Vector[Symbol]) {
    def accessible: Vector[Symbol] = {
      value.flatMap { x =>
        scala.util.Try {
          print(s"typecheck ${x.fullName} ")
          c.typecheck(q"${c.parse(x.fullName)}", silent = true)
        } match {
          case Success(r) if r.nonEmpty =>
            println("Success")
            Some(r.symbol)
          case Failure(e) =>
            println("Fail")
            c.warning(c.enclosingPosition, e.getMessage)
            None
          case _ =>
            println("Empty")
            None
        }
      }
    }
  }

as a result.

// Success case
typecheck akka.event.jul.Logger Success
typecheck akka.io.dns.CachePolicy Success
typecheck akka.io.dns.DnsSettings Success

// Fail case
typecheck com.fasterxml.jackson.databind.ObjectMapper$2 [error] error while loading ObjectMapper$2, class file '/Users/xxxxx/.ivy2/cache/com.fasterxml.jackson.core/jackson-databind/bundles/jackson-databind-2.9.8.jar(com/fasterxml/jackson/databind/ObjectMapper$2.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: T)
Empty
typecheck akka.protobuf.SmallSortedMap$Entry [error] error while loading SmallSortedMap$Entry, class file '/Users/xxxxx/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken
[error] (class java.util.NoSuchElementException/key not found: K)
Empty

Like this, I've been forced to compile error. The sample is here. https://github.com/giiita/scaladia/blob/master/scaladia-macro/src/main/scala/com/phylage/scaladia/internal/AutoDIExtractor.scala






Debugging

After reading the scala source code, I was throwing a clear IOException internally, but I could not catch it, so I reported the issue just in case https://github.com/scala/bug/issues/11611


回答1:


In macro you can try

c.typecheck(q"${... some tree ...}", silent = true)

If tree doesn't typecheck this returns empty tree.


The thing seems to be in dollar sign in akka.protobuf.SmallSortedMap$Entry. If we replace $ with # or . then error class file is broken changes to class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf, this is because SmallSortedMap has package-private (Java default) access.

I managed to catch the error when I feed type as a string parameter to macro and use # or . instead of $.

  def foo[T]: Unit = macro fooImpl[T]

  def fooImpl[T: c.WeakTypeTag](c: blackbox.Context): c.Tree = {
    import c.universe._
    try {
      println(weakTypeOf[T].typeSymbol.typeSignature)
    } catch {
      case ex: Throwable => println(ex)
    }
    q"()"
  }

  def foo1(tpe: String): Unit = macro foo1Impl

  def foo1Impl(c: blackbox.Context)(tpe: c.Tree): c.Tree = {
    import c.universe._
    val q"${tpeStr: String}" = tpe
    try {
      println(c.typecheck(c.parse(s"val ${c.freshName()}: $tpeStr = ???")))
//      println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode))
//      println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).symbol.companion.typeSignature)
//      println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).tpe.typeSymbol.typeSignature)
//      println(c.typecheck(c.parse(tpeStr), mode = c.TYPEmode).tpe)
    } catch {
      case ex: Throwable => println(ex)
    }
    q"()"
  }

  foo[Int]
//  foo[akka.protobuf.SmallSortedMap$Entry]//Error:scalac: error while loading SmallSortedMap$Entry, class file '.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken(class java.util.NoSuchElementException/key not found: K)
//  foo[akka.protobuf.SmallSortedMap#Entry]//Error: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
//  foo[akka.protobuf.SmallSortedMap.Entry]//Error:class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf

//  foo1("Int")
//  foo1("akka.protobuf.SmallSortedMap$Entry")//Error:scalac: error while loading SmallSortedMap$Entry, class file '.ivy2/cache/com.typesafe.akka/akka-protobuf_2.13/jars/akka-protobuf_2.13-2.5.23.jar(akka/protobuf/SmallSortedMap$Entry.class)' is broken (class java.util.NoSuchElementException/key not found: K)
  foo1("akka.protobuf.SmallSortedMap#Entry")//Warning:scalac: scala.reflect.macros.TypecheckException: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf
  foo1("akka.protobuf.SmallSortedMap.Entry")//Warning:scalac: scala.reflect.macros.TypecheckException: class SmallSortedMap in package protobuf cannot be accessed in package akka.protobuf

Otherwise when you call foo[... SomeType ...], SomeType is typechecked before macro foo is expanded.

https://stackoverflow.com/a/56754290/5249621

http://www.scala-archive.org/Expand-macros-before-typechecking-its-arguments-trees-td4641188.html



来源:https://stackoverflow.com/questions/56890996/compilation-failure-is-confirmed-at-the-moment-of-accessing-typesignature-of-a-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!