claimant icon indicating copy to clipboard operation
claimant copied to clipboard

Nested anonymous functions fail to compile on 2.11

Open non opened this issue 6 years ago • 0 comments

I noticed that certain kinds of expressions seem to cause problems under 2.11:

val xs = List(1,2,3,4)
val prop0 = Claim(xs.flatMap(x => List(x).filter(_ => false)) == Nil)

If you compile under 2.12 or 2.13, things are fine. But if you compile for 2.11, the macro does run and produces a tree, but the compiler crashes in a later phase:

[error] scala.reflect.internal.Types$TypeError: value <none> is not a member of scala.runtime.AbstractFunction0[org.sca
lacheck.Prop] with Serializable
[error]         at scala.tools.nsc.typechecker.Contexts$ThrowingReporter.handleError(Contexts.scala:1402)
[error]         at scala.tools.nsc.typechecker.Contexts$ContextReporter.issue(Contexts.scala:1254)
[error]         at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:573)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.reportError$2(Typers.scala:4512)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.onError$3(Typers.scala:4556)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4581)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4608)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5370)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5387)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5423)
[error]         at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typ
ers.scala:5450)

< stack trace continues for many lines >

However, these two very similar expressions both works fine under 2.11:

val xs = List(1,2,3,4)

val p = (x: Int) => false
val prop1 = Claim(xs.flatMap(x => List(x).filter(p)) == Nil)

val f = (x: Int) => List(x).filter(_ => false)
val prop2 = Claim(xs.flatMap(f))

I spent some time trying to track this down. As far as I can tell it's a bug with quasiquoting, which we're using heavily to take trees apart and put them back together. The problem occurs when constructing the label, which can be as simple as q"($tree).toString" while still having problems.

It's possible we will be able to change the code so this works in 2.11, but I've exhausted my budget for looking into this. The current work-arounds are either to refactor your code as above, or just build a Prop directly without a label:

val prop3 = Prop(xs.flatMap(x => List(x).filter(_ => false)) == Nil)

I'm planning to add a test to track this, to make it easy for me (or others) to pick this up later.

non avatar Jul 18 '19 19:07 non