From 3017bfcd07466a2c3aa9bc5fe4760929db8ae0ed Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Fri, 7 Mar 2014 17:48:31 +0100 Subject: [PATCH] Fix task macro's handling of Symbol owners in .value The qualifier of the `.value` call may contain `DefTree`s (e.g. vals, defs) or `Function` trees. When we snip them out of the tree and graft them into a new context, we must also call `changeOwner`, so that the symbol owner structure and the tree structure are coherent. Failure to do so resulted in a crash in the compiler backend. Fixes #1150 --- .../sbt-test/project/setting-macro/build.sbt | 9 ++++++++ .../main/scala/sbt/appmacro/ContextUtil.scala | 22 ++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sbt/src/sbt-test/project/setting-macro/build.sbt b/sbt/src/sbt-test/project/setting-macro/build.sbt index d48af8bc9..add6ef28e 100644 --- a/sbt/src/sbt-test/project/setting-macro/build.sbt +++ b/sbt/src/sbt-test/project/setting-macro/build.sbt @@ -15,3 +15,12 @@ demo := { val (n, s) = parser.parsed s * n } + +// Tests for correct Symbol owner structure in the lifted qualifiers of +// the `.value` macro within a task macro. (#1150) +val touchIfChanged = taskKey[Unit]("") + +touchIfChanged := { + val foo = (sourceDirectory in Compile).apply(base => base).value.get + () +} diff --git a/util/appmacro/src/main/scala/sbt/appmacro/ContextUtil.scala b/util/appmacro/src/main/scala/sbt/appmacro/ContextUtil.scala index c0c849fab..389fd33f8 100644 --- a/util/appmacro/src/main/scala/sbt/appmacro/ContextUtil.scala +++ b/util/appmacro/src/main/scala/sbt/appmacro/ContextUtil.scala @@ -226,17 +226,19 @@ final class ContextUtil[C <: Context](val ctx: C) object appTransformer extends Transformer { override def transform(tree: Tree): Tree = - tree match - { - case ApplyTree(TypeApply(Select(_, nme), targ :: Nil), qual :: Nil) => subWrapper(nme.decoded, targ.tpe, qual, tree) match { - case Converted.Success(t, finalTx) => finalTx(t) - case Converted.Failure(p,m) => ctx.abort(p, m) - case _: Converted.NotApplicable[_] => super.transform(tree) - } + tree match { + case ApplyTree(TypeApply(Select(_, nme), targ :: Nil), qual :: Nil) => + changeOwner(qual, currentOwner, initialOwner) // Fixes https://github.com/sbt/sbt/issues/1150 + subWrapper(nme.decoded, targ.tpe, qual, tree) match { + case Converted.Success(t, finalTx) => finalTx(t) + case Converted.Failure(p,m) => ctx.abort(p, m) + case _: Converted.NotApplicable[_] => super.transform(tree) + } case _ => super.transform(tree) } } - - appTransformer.transform(t) + appTransformer.atOwner(initialOwner) { + appTransformer.transform(t) + } } -} \ No newline at end of file +}