【问题标题】:Type checker phases类型检查器阶段
【发布时间】:2015-10-16 13:51:17
【问题描述】:

我注意到类型检查器是分阶段工作的。有时 scalac 只返回一些错误,这让您认为几乎存在,但是一旦您将它们全部修复 - 繁荣 - 下一个阶段,您会突然收到很多以前没有的错误。

类型检查器的不同阶段是什么?

有没有办法知道类型检查器在哪个阶段放弃了我的代码(除了识别错误)?

【问题讨论】:

  • 我相信来自 talk 的 Odersky 的一张幻灯片显示了所有阶段。
  • 我一定会看的,但请注意,我(仅)对编译器停止报告错误的阶段感兴趣。
  • 另一方面,我认为编译器应该在用户代码中报告错误的阶段很少,因为大多数阶段似乎处理中间代码,即语法和语义错误可能在早期的几个阶段。

标签: scala compiler-errors


【解决方案1】:

正如@Felix 指出的那样,this answer 列出了编译阶段:

$ scalac -version
Scala compiler version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL
$ scalac -Xshow-phases
    phase name  id  description
    ----------  --  -----------
        parser   1  parse source into ASTs, perform simple desugaring
         namer   2  resolve names, attach symbols to named trees
packageobjects   3  load package objects
         typer   4  the meat and potatoes: type the trees
        patmat   5  translate match expressions
superaccessors   6  add super accessors in traits and nested classes
    extmethods   7  add extension methods for inline classes
       pickler   8  serialize symbol tables
     refchecks   9  reference/override checking, translate nested objects
       uncurry  10  uncurry, translate function values to anonymous classes
     tailcalls  11  replace tail calls by jumps
    specialize  12  @specialized-driven class and method specialization
 explicitouter  13  this refs to outer pointers
       erasure  14  erase types, add interfaces for traits
   posterasure  15  clean up erased inline classes
      lazyvals  16  allocate bitmaps, translate lazy vals into lazified defs
    lambdalift  17  move nested functions to top level
  constructors  18  move field definitions into constructors
       flatten  19  eliminate inner classes
         mixin  20  mixin composition
       cleanup  21  platform-specific cleanups, generate reflective calls
    delambdafy  22  remove lambdas
         icode  23  generate portable intermediate code
           jvm  24  generate JVM bytecode
      terminal  25  the last phase during a compilation run

有没有办法知道类型检查器在哪个阶段放弃了我的代码(除了识别错误)?

如果您将-verbose 标志添加到scalac,它将打印每个阶段的名称以及该阶段完成后所用的时间。然后你可以推断出哪个阶段失败了

我不认为scalac 暴露了不同的输入阶段,只有编译阶段。 typer 被列为单个编译阶段。

【讨论】:

    【解决方案2】:

    为此的编译器选项是-Yissue-debug。它在 2.10 中在发出错误时输出堆栈跟踪。

    支持它的代码在 2.11 的报告重构过程中被删除,但该选项仍然有效。 (我在某个时候恢复了它,因为事实上,这是查看发出错误的最快方法;但显然 PR 死在了藤蔓上并消失了。可能是push -f 的受害者。)

    在 2.12 中,您可以提供一个自定义报告器,该报告器的功能几乎相同。他们声称他们将通过访问上下文来增强报告 API,因此您也许可以直接查询当前阶段、检查树等等。

    这是你描述的情况的一个例子:

    class C { def f: Int ; def g: Int = "" ; def h = "\000" }
    

    有三个错误,但一次只报告一个,因为它们是在不同的编译器阶段发出的。

    为了澄清这个问题,除了“typer”之外的各个阶段都可以创建和类型检查树,并且即使在树被键入之后也可以强制执行良好的类型化。 (还有其他类型的错误,比如“无法写入输出文件”。)

    对于C,解析器针对已弃用的八进制语法发出错误(在-Xfuture 下),类型器报告g 中的类型不匹配,并且抓取袋refchecks 阶段检查已声明但未定义(空)会员f。您通常会一次修复一个错误。如果解析器错误作为警告发出,则警告将被抑制,直到错误得到修复,所以这将是最后一个弹出而不是第一个弹出。

    这是一个示例报告器,它试图做的不仅仅是输出巨大的堆栈跟踪。

    package myrep
    
    import scala.tools.nsc.Settings
    import scala.tools.nsc.reporters.ConsoleReporter
    
    import scala.reflect.internal.util._
    
    class DebugReporter(ss: Settings) extends ConsoleReporter(ss) {
      override def warning(pos: Position, msg: String) = debug {
        super.warning(pos, msg)
      }
      override def error(pos: Position, msg: String) = debug {
        super.error(pos, msg)
      }
      // let it ride
      override def hasErrors = false
    
      private def debug(body: => Unit): Unit = {
        val pkgs = Set("nsc.ast.parser", "nsc.typechecker", "nsc.transform")
        def compilerPackages(e: StackTraceElement): Boolean = pkgs exists (e.getClassName contains _)
        def classname(e: StackTraceElement): String = (e.getClassName split """\.""").last
        if (ss.Yissuedebug) echo {
          ((new Throwable).getStackTrace filter compilerPackages map classname).distinct mkString ("Issued from: ", ",", "\n")
        }
        body
      }
    }
    

    关键在于没有错误,这样编译器就不会提前中止。

    它将以这种方式调用,报告者类位于“工具类路径”上:

    scalacm -toolcp repdir -Xreporter myrep.DebugReporter -Yissue-debug -deprecation errs.scala
    

    在哪里

    $ scalacm -version
    Scala compiler version 2.12.0-M2 -- Copyright 2002-2013, LAMP/EPFL
    

    样本输出:

    Issued from: Scanners$UnitScanner,Scanners$Scanner,Parsers$Parser,Parsers$Parser$$anonfun$templateStat$1,Parsers$Parser$$anonfun$topStat$1,Parsers$SourceFileParser,Parsers$UnitParser,SyntaxAnalyzer,SyntaxAnalyzer$ParserPhase
    errs.scala:4: warning: Octal escape literals are deprecated, use \u0000 instead.
    class C { def f: Int ; def g: Int = "" ; def h = "\000" }
                                                      ^
    Issued from: Contexts$ImmediateReporter,Contexts$ContextReporter,Contexts$Context,ContextErrors$ErrorUtils$,ContextErrors$TyperContextErrors$TyperErrorGen$,Typers$Typer,Analyzer$typerFactory$$anon$3
    errs.scala:4: error: type mismatch;
     found   : String("")
     required: Int
    class C { def f: Int ; def g: Int = "" ; def h = "\000" }
                                        ^
    Issued from: RefChecks$RefCheckTransformer,Transform$Phase
    errs.scala:4: error: class C needs to be abstract, since method f is not defined
    class C { def f: Int ; def g: Int = "" ; def h = "\000" }
          ^
    one warning found
    two errors found
    

    【讨论】:

    • 我知道没人在乎,但这是你添加回来的地方。仍在 PR 队列中。为什么不添加更多的上下文呢? github.com/som-snytt/scala/blob/issue/2991-2.12/src/compiler/…
    • 链接失效了,有新链接吗?我认为这个选项不会在 scalac 代码中的任何地方使用。我的建议是添加-Ydebug-error,就像scala 3一样
    • @tribbloid 它成为了一个无关问题的公关github.com/scala/scala/pull/4440
    • 非常感谢!刚刚意识到你是一个 scala 提交者
    • PR 已集成,但编译器任何地方都没有使用“-Yissue-debug”选项。在我的 PR 中:github.com/scala/scala/pull/9824。该选项被完全删除,没有任何影响。
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多