键入检查阶段

时间:2015-10-16 13:51:17

标签: scala compiler-errors

我注意到类型检查器分阶段工作。有时scalac只返回一些错误,这让你觉得它几乎就在那里,但是一旦你修复它们 - 繁荣 - 下一阶段,你会突然遇到很多错误,而不是之前的错误。

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

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

2 个答案:

答案 0 :(得分:3)

正如@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被列为单个编译阶段。

答案 1 :(得分:1)

此编译器选项为-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下),typer报告g中的类型不匹配,并且抓包重新检查阶段检查声明但未定义(空)成员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
相关问题