Scala 3:“断言失败:表示类 Int 在运行 1 中无效。”

发布于 2025-01-15 20:42:02 字数 2982 浏览 4 评论 0原文

我将 Scala 3 的编译器称为库,它在编译后为每个源提供 CompilationUnit 。它有 tpdTree,听起来应该包含类型信息。

我试图在树上行走以获得任何类型符号:

 atPhase(Phases.typerPhase.next) {
   // traverse unit.tpdTree...
 }

树行走看起来像:

  class ValExtractor(tpes: Set[String]) extends tpd.TreeTraverser:
    def isAcceptableType(tpe: Types.Type)(using ctx: Context): Boolean =
      tpe.baseClasses.exists { sym =>
        tpes.contains(sym.fullName.toString)
      }
    override def traverse(tree: tpd.Tree)(using ctx: Context): Unit =
      tree match
        case tpd.ValDef(name, tpt, _) if isAcceptableType(tpt.tpe) =>
          println("do something")
        case t: tpd.Template   => this((), t.body)
        case t: tpd.PackageDef => this((), t.stats)
        case t: tpd.TypeDef    => this((), t.rhs)
        case _                 => ()
  end ValExtractor

我明白

[info]   assertion failed: denotation class Int invalid in run 1. ValidFor: Period(1..55, run = 2)
[info]     scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.updateValidity(Denotations.scala:719)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:744)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:803)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:877)
[info]     dotty.tools.dotc.core.Symbols$Symbol.recomputeDenot(Symbols.scala:122)
[info]     dotty.tools.dotc.core.Symbols$Symbol.computeDenot(Symbols.scala:116)
[info]     dotty.tools.dotc.core.Symbols$Symbol.denot(Symbols.scala:109)
[info]     dotty.tools.dotc.core.Symbols$.toDenot(Symbols.scala:502)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.updateValidity(Denotations.scala:718)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:744)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:803)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:877)
[info]     dotty.tools.dotc.core.Types$NamedType.computeDenot(Types.scala:2253)
[info]     dotty.tools.dotc.core.Types$NamedType.denot(Types.scala:2213)
[info]     dotty.tools.dotc.core.Types$NamedType.info(Types.scala:2201)
[info]     dotty.tools.dotc.core.Types$TypeRef.underlying(Types.scala:2693)
[info]     dotty.tools.dotc.core.Types$Type.baseClasses(Types.scala:600)

我做错了什么?

解决方案

在我的例子中,我的方法中有 (using ctx: Context) ,但显然这与运行上下文不匹配。按如下方式显式传递它可以修复它:

atPhase(Phases.typerPhase.next) {
  (new ValExtractor(valTypes.toSet)).getVals(unit.tpdTree)
}(using run.runContext)

I am calling Scala 3's compiler as a library, which gives you CompilationUnit per source after compilation. This has tpdTree, which by the sound of it should contain type information.

I'm trying to walk the tree to get any type symbol as:

 atPhase(Phases.typerPhase.next) {
   // traverse unit.tpdTree...
 }

Where tree walking looks like:

  class ValExtractor(tpes: Set[String]) extends tpd.TreeTraverser:
    def isAcceptableType(tpe: Types.Type)(using ctx: Context): Boolean =
      tpe.baseClasses.exists { sym =>
        tpes.contains(sym.fullName.toString)
      }
    override def traverse(tree: tpd.Tree)(using ctx: Context): Unit =
      tree match
        case tpd.ValDef(name, tpt, _) if isAcceptableType(tpt.tpe) =>
          println("do something")
        case t: tpd.Template   => this((), t.body)
        case t: tpd.PackageDef => this((), t.stats)
        case t: tpd.TypeDef    => this((), t.rhs)
        case _                 => ()
  end ValExtractor

I get

[info]   assertion failed: denotation class Int invalid in run 1. ValidFor: Period(1..55, run = 2)
[info]     scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.updateValidity(Denotations.scala:719)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:744)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:803)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:877)
[info]     dotty.tools.dotc.core.Symbols$Symbol.recomputeDenot(Symbols.scala:122)
[info]     dotty.tools.dotc.core.Symbols$Symbol.computeDenot(Symbols.scala:116)
[info]     dotty.tools.dotc.core.Symbols$Symbol.denot(Symbols.scala:109)
[info]     dotty.tools.dotc.core.Symbols$.toDenot(Symbols.scala:502)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.updateValidity(Denotations.scala:718)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.bringForward(Denotations.scala:744)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.toNewRun$1(Denotations.scala:803)
[info]     dotty.tools.dotc.core.Denotations$SingleDenotation.current(Denotations.scala:877)
[info]     dotty.tools.dotc.core.Types$NamedType.computeDenot(Types.scala:2253)
[info]     dotty.tools.dotc.core.Types$NamedType.denot(Types.scala:2213)
[info]     dotty.tools.dotc.core.Types$NamedType.info(Types.scala:2201)
[info]     dotty.tools.dotc.core.Types$TypeRef.underlying(Types.scala:2693)
[info]     dotty.tools.dotc.core.Types$Type.baseClasses(Types.scala:600)

What am I doing wrong?

Resolution

In my case I had (using ctx: Context) in my method, but apparently that did not match the run context. Passing it explicitly as follows fixed it:

atPhase(Phases.typerPhase.next) {
  (new ValExtractor(valTypes.toSet)).getVals(unit.tpdTree)
}(using run.runContext)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2025-01-22 20:42:02

您需要在 run 值与创建定义时相同的上下文中运行查询(或者是稍后运行,但较早运行无效)。

您看到的错误消息是您在运行 1 中询问符号的表示,从概念上讲,该表示是在运行 2 中创建之前的。

请参阅 https://www.youtube.com/watch?v=WxyyJyB_Ssc 观看解释运行/阶段概念的视频。

You need to run the query in a context where the run value is the same as when the definitions were created (or is a later run, but an earlier run is not valid).

The error message you are seeing is that you ask about a denotation of a symbol at run 1, which is conceptually before it was created at run 2.

See https://www.youtube.com/watch?v=WxyyJyB_Ssc for a video where the run/phase concept is explained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文