From 3a1463bb7dfa2163c69715151329f2cee93ff2e6 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Sat, 17 Oct 2020 19:16:16 -0400 Subject: [PATCH] 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)) => { val $q$macro$2: Int = $p$macro$3._1; 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. --- .../sbt/internal/util/appmacro/Instance.scala | 4 +++- .../project/PureExpressionPlugin.scala | 15 +++++++++++++++ .../project/setting-macro/project/plugins.sbt | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 sbt/src/sbt-test/project/setting-macro/project/PureExpressionPlugin.scala create mode 100644 sbt/src/sbt-test/project/setting-macro/project/plugins.sbt diff --git a/core-macros/src/main/scala/sbt/internal/util/appmacro/Instance.scala b/core-macros/src/main/scala/sbt/internal/util/appmacro/Instance.scala index f077ed5ab..5c15a3346 100644 --- a/core-macros/src/main/scala/sbt/internal/util/appmacro/Instance.scala +++ b/core-macros/src/main/scala/sbt/internal/util/appmacro/Instance.scala @@ -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) diff --git a/sbt/src/sbt-test/project/setting-macro/project/PureExpressionPlugin.scala b/sbt/src/sbt-test/project/setting-macro/project/PureExpressionPlugin.scala new file mode 100644 index 000000000..f0eea984b --- /dev/null +++ b/sbt/src/sbt-test/project/setting-macro/project/PureExpressionPlugin.scala @@ -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 + } + } +} diff --git a/sbt/src/sbt-test/project/setting-macro/project/plugins.sbt b/sbt/src/sbt-test/project/setting-macro/project/plugins.sbt new file mode 100644 index 000000000..9674e9d5f --- /dev/null +++ b/sbt/src/sbt-test/project/setting-macro/project/plugins.sbt @@ -0,0 +1 @@ +Compile / scalacOptions += "-Xfatal-warnings"