我应该报告这个断言失败吗?什么是$ asInstanceOf呢?

时间:2012-09-12 17:48:20

标签: scala scala-2.9 scala-2.10

如果你用这个非常糟糕的代码片段来打扰Scala编译器(2.9.1或2.10.0-M7)......

null.$asInstanceOf[Int]

...它会导致断言失败。问题:什么是$asInstanceOf?我应该将此报告为错误吗?

这是堆栈跟踪:

java.lang.AssertionError: assertion failed: null.$asInstanceOf[Int]()
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.scala$tools$nsc$backend$icode$GenICode$ICodePhase$$genLoad(GenICode.scala:754)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.scala$tools$nsc$backend$icode$GenICode$ICodePhase$$genLoad(GenICode.scala:850)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$genLoadArguments$1.apply(GenICode.scala:1226)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$genLoadArguments$1.apply(GenICode.scala:1224)
        at scala.collection.LinearSeqOptimized$class.foldLeft(LinearSeqOptimized.scala:111)
        at scala.collection.immutable.List.foldLeft(List.scala:76)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.genLoadArguments(GenICode.scala:1224)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.scala$tools$nsc$backend$icode$GenICode$ICodePhase$$genLoad(GenICode.scala:924)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:114)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$gen$1.apply(GenICode.scala:69)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$gen$1.apply(GenICode.scala:69)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
        at scala.collection.immutable.List.foreach(List.scala:76)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:69)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:136)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:88)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$gen$1.apply(GenICode.scala:69)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$gen$1.apply(GenICode.scala:69)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
        at scala.collection.immutable.List.foreach(List.scala:76)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:69)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:79)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.gen(GenICode.scala:65)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.apply(GenICode.scala:61)
        at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:329)
        at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297)
        at scala.tools.nsc.Global$GlobalPhase$$anonfun$run$1.apply(Global.scala:297)
        at scala.collection.Iterator$class.foreach(Iterator.scala:772)
        at scala.collection.mutable.ListBuffer$$anon$1.foreach(ListBuffer.scala:318)
        at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:297)
        at scala.tools.nsc.backend.icode.GenICode$ICodePhase.run(GenICode.scala:54)
        at scala.tools.nsc.Global$Run.compileSources(Global.scala:953)
        at scala.tools.nsc.Global$Run.compile(Global.scala:1041)
        at xsbt.CachedCompiler0.run(CompilerInterface.scala:90)
        at xsbt.CachedCompiler0.liftedTree1$1(CompilerInterface.scala:72)
        at xsbt.CachedCompiler0.run(CompilerInterface.scala:72)
        at xsbt.CompilerInterface.run(CompilerInterface.scala:26)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:73)
        at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:35)
        at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:29)
        at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply$mcV$sp(AggressiveCompile.scala:71)
        at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply(AggressiveCompile.scala:71)
        at sbt.compiler.AggressiveCompile$$anonfun$4$$anonfun$compileScala$1$1.apply(AggressiveCompile.scala:71)
        at sbt.compiler.AggressiveCompile.sbt$compiler$AggressiveCompile$$timed(AggressiveCompile.scala:101)
        at sbt.compiler.AggressiveCompile$$anonfun$4.compileScala$1(AggressiveCompile.scala:70)
        at sbt.compiler.AggressiveCompile$$anonfun$4.apply(AggressiveCompile.scala:88)
        at sbt.compiler.AggressiveCompile$$anonfun$4.apply(AggressiveCompile.scala:60)
        at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:24)
        at sbt.inc.IncrementalCompile$$anonfun$doCompile$1.apply(Compile.scala:22)
        at sbt.inc.Incremental$.cycle(Incremental.scala:39)
        at sbt.inc.Incremental$.compile(Incremental.scala:26)
        at sbt.inc.IncrementalCompile$.apply(Compile.scala:20)
        at sbt.compiler.AggressiveCompile.compile2(AggressiveCompile.scala:96)
        at sbt.compiler.AggressiveCompile.compile1(AggressiveCompile.scala:44)
        at sbt.compiler.AggressiveCompile.apply(AggressiveCompile.scala:31)
        at sbt.Compiler$.apply(Compiler.scala:79)
        at sbt.Defaults$$anonfun$compileTask$1.apply(Defaults.scala:571)
        at sbt.Defaults$$anonfun$compileTask$1.apply(Defaults.scala:571)
        at sbt.Scoped$$anonfun$hf2$1.apply(Structure.scala:578)
        at sbt.Scoped$$anonfun$hf2$1.apply(Structure.scala:578)
        at scala.Function1$$anonfun$compose$1.apply(Function1.scala:49)
        at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
        at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311)
        at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
        at sbt.std.Transform$$anon$5.work(System.scala:71)
        at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
        at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:232)
        at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
        at sbt.Execute.work(Execute.scala:238)
        at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
        at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:232)
        at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
        at sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:679)

1 个答案:

答案 0 :(得分:2)

问题与您在名称中包含一美元$有关。来自Scala语言规范(第1.1节标识符):

  

'$'字符保留用于编译器合成的标识符。用户程序不应定义包含'$'字符的标识符。

虽然没有明确说明,但暗示用户程序也不应使用此类标识符。

我猜这是一个关于结果应该有多糟糕的判断问题,当你偏离保留并使用这样的事情时,以及编译器崩溃是否包含在这种允许的结果中。