Try to workaround "a pure expression does nothing" warning

Ref https://github.com/scala/bug/issues/12112

Current app transformation macro creates a tree that looks like:

```scala
FullInstance.app[[T0[x]](T0[Int], T0[Int]), Unit](scala.Tuple2(task2, task1), (($p$macro$3: (Int, Int)) => {
  <synthetic> val $q$macro$2: Int = $p$macro$3._1;
  <synthetic> val $q$macro$1: Int = $p$macro$3._2;
  {
    ($q$macro$1: Int);
    ($q$macro$2: Int);
    ()
  }
}))(AList.tuple2[Int, Int])
```

Starting Scala 2.12.12 the compiler's "pure expression does nothing" has become more enthusiastic/accurate in its reach, and it started warning about the naked reference to `$q$macro$1` that appears to do nothing, even though in reality it would trigger the tasks and do something in the context of sbt. It's just _that_ particular line ends up macroed away into a pure expression.

A somewhat bizarre workaround is to make a fake call to a method just to satisfy this warning. I've chosen `scala.Predef.identity` here so it can be composed together with the existing expression nesting when they exist.
This commit is contained in:
Eugene Yokota 2020-10-17 19:16:16 -04:00
parent 3266d77862
commit 3a1463bb7d
3 changed files with 19 additions and 1 deletions

View File

@ -187,7 +187,9 @@ object Instance {
qual.foreach(checkQual)
val vd = util.freshValDef(tpe, qual.pos, functionSym)
inputs ::= new Input(tpe, qual, vd)
util.refVal(selection, vd)
// try to workaround https://github.com/scala/bug/issues/12112 by calling Predef.identity(...)
val rv = util.refVal(selection, vd)
q"scala.Predef.identity[$tpe]($rv: $tpe)"
}
def sub(name: String, tpe: Type, qual: Tree, replace: Tree): Converted[c.type] = {
val tag = c.WeakTypeTag[T](tpe)

View File

@ -0,0 +1,15 @@
package pkgtest
import sbt._, Keys._
// https://github.com/scala/bug/issues/12112
object PureExpressionPlugin extends AutoPlugin {
lazy val testPureExpression = taskKey[Unit]("")
override def projectSettings: Seq[Setting[_]] = {
testPureExpression := {
updateFull.value
(Compile / compile).value
(Test / test).value
}
}
}

View File

@ -0,0 +1 @@
Compile / scalacOptions += "-Xfatal-warnings"