我想写自己的断言:
import scala.annotation.elidable
object Util {
// doesn't matter if true or false
def someCondition(checking: Any): Boolean = false
@elidable(elidable.ASSERTION)
def assertSomething(checking: Any)(
msg: => String = s"Something didn't check out for $checking"
): Unit = {
assert(someCondition(checking), msg)
}
}
编译得很好并在适当的时候抛出AssertionError。
但是,如果我尝试使用scalacOption" -Xelide-below MAXIMUM"进行编译,我会收到以下错误:
exception when typing new StringContext("Something didn\'t check out for ", "").s(checking)/class scala.reflect.internal.Trees$Apply
too many arguments (2) for constructor StringContext: (parts: Seq)StringContext in file Util.scala
scala.reflect.internal.Types$TypeError: too many arguments (2) for constructor StringContext: (parts: Seq)StringContext
at scala.tools.nsc.typechecker.Contexts$ThrowingReporter.handleError(Contexts.scala:1402)
at scala.tools.nsc.typechecker.Contexts$ContextReporter.issue(Contexts.scala:1254)
at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:576)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typed1$27(Typers.scala:4648)
at scala.tools.nsc.typechecker.Typers$Typer.reportError$2(Typers.scala:4648)
at scala.tools.nsc.typechecker.Typers$Typer.onError$3(Typers.scala:4693)
at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4718)
at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4745)
at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5510)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5527)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.transformedOrTyped(Typers.scala:5774)
at scala.tools.nsc.typechecker.Typers$Typer.typedValDefImpl(Typers.scala:2068)
at scala.tools.nsc.typechecker.Typers$Typer.typedValDef(Typers.scala:2024)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedDefDef$6(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedDefDef$5(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.typedDefDef(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5475)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5624)
at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:3075)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$9(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedTemplate(Typers.scala:1987)
at scala.tools.nsc.typechecker.Typers$Typer.typedClassDef(Typers.scala:1811)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5476)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5624)
at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:3075)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$9(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedPackageDef$1(Typers.scala:5176)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5479)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5620)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.$anonfun$transform$4(Erasure.scala:1237)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.transform(Erasure.scala:1233)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.transform(Erasure.scala:808)
at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:140)
at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:30)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$applyPhase$1(Global.scala:423)
at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:416)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1(Global.scala:387)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1$adapted(Global.scala:387)
at scala.tools.nsc.Global$GlobalPhase$$Lambda$267/586617651.apply(Unknown Source)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:387)
at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1427)
at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1412)
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1407)
at scala.tools.nsc.Global$Run.compile(Global.scala:1501)
at scala.tools.nsc.MainClass.doCompile(Main.scala:24)
at scala.tools.nsc.Driver.process(Driver.scala:55)
at scala.tools.nsc.Driver.main(Driver.scala:68)
at scala.tools.nsc.Main.main(Main.scala)
error:
unrecoverable error
while compiling: Util.scala
during phase: globalPhase=erasure, enteringPhase=posterasure
library version: version 2.12.2
compiler version: version 2.12.2
reconstructed args: -Xelide-below 2147483647
last tree to typer: TypeTree(class StringContext)
tree position: line 9 of Util.scala
tree tpe: StringContext
symbol: case class StringContext in package scala
symbol definition: case class StringContext extends Product with Serializable (a ClassSymbol)
symbol package: scala
symbol owners: class StringContext
call site: package <root> in <none>
== Source file context for tree position ==
6
7 @elidable(elidable.ASSERTION)
8 def assertSomething(checking: Any)(
9 msg: => String = s"Something didn't check out for $checking"
10 ): Unit = {
11 assert(someCondition(checking), msg)
12 }
error: scala.reflect.internal.FatalError:
unrecoverable error
while compiling: Util.scala
during phase: globalPhase=erasure, enteringPhase=posterasure
library version: version 2.12.2
compiler version: version 2.12.2
reconstructed args: -Xelide-below 2147483647
last tree to typer: TypeTree(class StringContext)
tree position: line 9 of Util.scala
tree tpe: StringContext
symbol: case class StringContext in package scala
symbol definition: case class StringContext extends Product with Serializable (a ClassSymbol)
symbol package: scala
symbol owners: class StringContext
call site: package <root> in <none>
== Source file context for tree position ==
6
7 @elidable(elidable.ASSERTION)
8 def assertSomething(checking: Any)(
9 msg: => String = s"Something didn't check out for $checking"
10 ): Unit = {
11 assert(someCondition(checking), msg)
12 }
at scala.reflect.internal.Reporting.abort(Reporting.scala:61)
at scala.reflect.internal.Reporting.abort$(Reporting.scala:57)
at scala.reflect.internal.SymbolTable.abort(SymbolTable.scala:16)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:762)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.transformedOrTyped(Typers.scala:5774)
at scala.tools.nsc.typechecker.Typers$Typer.typedValDefImpl(Typers.scala:2068)
at scala.tools.nsc.typechecker.Typers$Typer.typedValDef(Typers.scala:2024)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedDefDef$6(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedDefDef$5(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.typedDefDef(Typers.scala:2260)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5475)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5624)
at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:3075)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$9(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedTemplate(Typers.scala:1987)
at scala.tools.nsc.typechecker.Typers$Typer.typedClassDef(Typers.scala:1811)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5476)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5624)
at scala.tools.nsc.typechecker.Typers$Typer.typedStat$1(Typers.scala:3075)
at scala.tools.nsc.typechecker.Typers$Typer.$anonfun$typedStats$9(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3219)
at scala.tools.nsc.typechecker.Typers$Typer.typedPackageDef$1(Typers.scala:5176)
at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5479)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5526)
at scala.tools.nsc.transform.Erasure$Eraser.typed1(Erasure.scala:755)
at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5563)
at scala.tools.nsc.typechecker.Typers$Typer.typedInternal(Typers.scala:5593)
at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5537)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5541)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5620)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.$anonfun$transform$4(Erasure.scala:1237)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.transform(Erasure.scala:1233)
at scala.tools.nsc.transform.Erasure$ErasureTransformer.transform(Erasure.scala:808)
at scala.tools.nsc.ast.Trees$Transformer.transformUnit(Trees.scala:140)
at scala.tools.nsc.transform.Transform$Phase.apply(Transform.scala:30)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$applyPhase$1(Global.scala:423)
at scala.tools.nsc.Global$GlobalPhase.applyPhase(Global.scala:416)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1(Global.scala:387)
at scala.tools.nsc.Global$GlobalPhase.$anonfun$run$1$adapted(Global.scala:387)
at scala.tools.nsc.Global$GlobalPhase$$Lambda$267/586617651.apply(Unknown Source)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
at scala.tools.nsc.Global$GlobalPhase.run(Global.scala:387)
at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1427)
at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1412)
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1407)
at scala.tools.nsc.Global$Run.compile(Global.scala:1501)
at scala.tools.nsc.MainClass.doCompile(Main.scala:24)
at scala.tools.nsc.Driver.process(Driver.scala:55)
at scala.tools.nsc.Driver.main(Driver.scala:68)
at scala.tools.nsc.Main.main(Main.scala)
现在,一个简单的解决方法是使用连接而不是插值。 但是,我仍然想知道为什么会这样。 Afaik,elide-below只删除函数调用,并且不会更改elidable函数本身,对吧?