为什么类型检测,返回NoType在,甚至当它计算出一个有效的符号?(Why does typeche

2019-10-19 14:26发布

从继: 如何进行类型检查一个DefDef

首先,从我的微距一些片段:

object log {
  def err(msg: String): Unit = c.error(c.enclosingPosition, msg)
  def warn(msg: String): Unit = c.warning(c.enclosingPosition, msg)
  def info(msg: String): Unit = c.info(c.enclosingPosition, msg, force=true)
  def rawInfo(name: String, obj: Any): Unit = info(name + " = " + showRaw(obj))
}

methodsIn(body) foreach { dd => //dd: DefDef
  val name = dd.name.toString
  log.rawInfo(name, dd)
  log.rawInfo(name + ".rhs", dd.rhs)

  try {
    val typechecked = ctx.typecheck(dd.duplicate)
    log.rawInfo(name + ".typechecked", typechecked)
    log.info(name + ".typechecked.symbol = " + typechecked.symbol)
    log.rawInfo(name + ".typechecked.symbol [raw]", typechecked.symbol)
    log.info(name + ".typechecked.symbol.info = " + typechecked.symbol.info)
    log.rawInfo(name + ".typechecked.symbol.info [raw]", typechecked.symbol.info)
    log.rawInfo(name + ".typechecked.tpe", typechecked.tpe)
  } catch { case e: Throwable => log.warn(e.toString)}
}

然后我喂这个类的宏:

class BorgMe(@mymacro val param: Nanites) {
  def one(s: String) = s
}

是的...这是一个天堂注释宏。 但我不认为这是相关的。

什么是困惑我是从代码,它看起来像输出日志:

one = DefDef(
  Modifiers(),
  TermName("one"),
  List(),
  List(List(ValDef(
    Modifiers(PARAM),
    TermName("s"),
    Ident(TypeName("String")),
    EmptyTree
  ))),
  TypeTree(),
  Ident(TermName("s"))
)

one.rhs = Ident(TermName("s"))

one.typechecked = DefDef(
  Modifiers(),
  TermName("one"),
  List(),
  List(List(ValDef(
    Modifiers(PARAM),
    TermName("s"),
    TypeTree().setOriginal(
      Select(
        Select(
          This(TypeName("scala")),
          scala.Predef
        ),
        TypeName("String")
      )
    ),
    EmptyTree
  ))),
  TypeTree(),
  Ident(TermName("s"))
)

one.typechecked.symbol = method one
one.typechecked.symbol [raw] = TermName("one")

one.typechecked.symbol.info = (s: String)String
one.typechecked.symbol.info [raw] =
  MethodType(
    List(TermName("s")),
    TypeRef(
      SingleType(ThisType(scala), scala.Predef),
      TypeName("String"),
      List()
    )
  )

one.typechecked.tpe = NoType

鉴于该类型检测,正在取得成功,而且很明显,我们都期望符号信息:

one.typechecked.symbol = method one
one.typechecked.symbol.info = (s: String)String

为什么tpe的方法还是出来为NoType

one.typechecked.tpe = NoType

Answer 1:

这scalac是如何工作的。 Typechecked定义(即子类DefTree )被指定给NoType 。 为了对比,无类型定义有null在他们的tpe (就像其它类型化树),所以它可以识别出它们。



文章来源: Why does typecheck return NoType, even when it's calculated a valid symbol?