2015-10-16 97 views
4

我注意到类型检查器分阶段工作。有时候,scalac只会返回一些错误,这使得你几乎认为它已经存在,但是一旦你修复了它们所有的 - 繁荣 - 下一个阶段,你突然间会发现很多错误,而这些错误之前并不存在。类型检查器阶段

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

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

+0

我相信, Odersky从这个[talk](https://www.youtube.com/watch?v=WxyyJyB_Ssc)的幻灯片中可以看到所有阶段。 –

+0

我一定会注意到这一点,但请注意,我(仅)对编译器停止报告错误的阶段感兴趣。 – OlivierBlanvillain

+0

这可能是一个重复的http://stackoverflow.com/questions/4527902/what-is-the-order-of-the-scala-compiler-phases – Felix

回答

1

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

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

在2.12中,可以提供一个自定义的记者,完成同样的事情。他们声称,他们将加强记者API访问上下文,所以你也许可以直接查询当前阶段,检查树木,等等。

这里有你所描述的情况为例:

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

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

为了澄清这个问题,各个阶段除了“打字员”,可以创建和类型检测,树木,还可以强制树被输入后也很好typedness。 (也有其他类型的错误,例如“不能写入输出文件中。”)

对于C,解析器发出错误(-Xfuture下),用于弃用八进制语法中,导电型测量仪报告的类型不匹配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 
+0

我意识到没有人在意,但这是你添加回来的地方。仍然在PR队列中。你为什么不添加更多的上下文? https://github.com/som-snytt/scala/blob/issue/2991-2.12/src/compiler/scala/tools/nsc/typechecker/Contexts.scala#L577 –

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