Skip to content

Commit 19c6688

Browse files
committed
Try to workaround "a pure expression does nothing" warning
Ref scala/bug#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.
1 parent 3266d77 commit 19c6688

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

core-macros/src/main/scala/sbt/internal/util/appmacro/Instance.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ object Instance {
187187
qual.foreach(checkQual)
188188
val vd = util.freshValDef(tpe, qual.pos, functionSym)
189189
inputs ::= new Input(tpe, qual, vd)
190-
util.refVal(selection, vd)
190+
// try to workaround https://github.com/scala/bug/issues/12112 by calling Predef.identity(...)
191+
q"scala.Predef.identity[$tpe](${vd.name}: $tpe)"
191192
}
192193
def sub(name: String, tpe: Type, qual: Tree, replace: Tree): Converted[c.type] = {
193194
val tag = c.WeakTypeTag[T](tpe)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pkgtest
2+
3+
import sbt._, Keys._
4+
5+
// https://github.com/scala/bug/issues/12112
6+
object PureExpressionPlugin extends AutoPlugin {
7+
lazy val testPureExpression = taskKey[Unit]("")
8+
override def projectSettings: Seq[Setting[_]] = {
9+
testPureExpression := {
10+
updateFull.value
11+
(Compile / compile).value
12+
(Test / test).value
13+
}
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Compile / scalacOptions += "-Xfatal-warnings"

0 commit comments

Comments
 (0)