diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-15 17:41:32 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-12-15 17:42:22 -0800 |
commit | 4ec6dca8c3432339620bbc6298fdf75a4b930fb4 (patch) | |
tree | 1c17bb9bb3477be1b498c72ff2c74b9d235dd4bd /core | |
parent | 3a40842d7b3aeedddb9ab5e8261dd48ea4e024b6 (diff) | |
download | mill-4ec6dca8c3432339620bbc6298fdf75a4b930fb4.tar.gz mill-4ec6dca8c3432339620bbc6298fdf75a4b930fb4.tar.bz2 mill-4ec6dca8c3432339620bbc6298fdf75a4b930fb4.zip |
First pass at using a compiler plugin to remove the need for the `override` keyword when overriding a field within a `mill.Module`
This only applies to `mill.Module`s, not overrides elsewhere which still require the keyword. `mill.Module`s tend to have lots and lots of overriding, so the keyword is basically noise.
Also includes the necessary build changes to enable the locally-built Scalac plugin when compiling the test suite. Note that no changes are necessary for the executable assembly, because the `scalac-plugin.xml` will be included in the assembly and get picked up by the Ammonite scalac plugin classloader automatically
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/mill/define/Cacher.scala | 33 | ||||
-rw-r--r-- | core/src/main/scala/mill/define/Task.scala | 8 | ||||
-rw-r--r-- | core/src/test/scala/mill/define/CacherTests.scala | 15 |
3 files changed, 18 insertions, 38 deletions
diff --git a/core/src/main/scala/mill/define/Cacher.scala b/core/src/main/scala/mill/define/Cacher.scala deleted file mode 100644 index 09200203..00000000 --- a/core/src/main/scala/mill/define/Cacher.scala +++ /dev/null @@ -1,33 +0,0 @@ -package mill.define - -import scala.collection.mutable -import scala.reflect.macros.blackbox.Context - - -trait Cacher[C[_]]{ - private[this] val cacherLazyMap = mutable.Map.empty[sourcecode.Enclosing, C[_]] - def wrapCached[T](in: C[T], enclosing: String): C[T] - protected[this] def cachedTarget[T](t: => C[T]) - (implicit c: sourcecode.Enclosing): C[T] = synchronized{ - cacherLazyMap.getOrElseUpdate(c, wrapCached(t, c.value)).asInstanceOf[C[T]] - } -} -object Cacher{ - def impl0[M[_], T: c.WeakTypeTag](c: Context)(t: c.Expr[M[T]]): c.Expr[M[T]] = { - c.Expr[M[T]](wrapCached(c)(t.tree)) - } - def wrapCached(c: Context)(t: c.Tree) = { - - import c.universe._ - val owner = c.internal.enclosingOwner - val ownerIsCacherClass = - owner.owner.isClass && - owner.owner.asClass.baseClasses.exists(_.fullName == "mill.define.Cacher") - - if (ownerIsCacherClass && owner.isMethod) q"this.cachedTarget($t)" - else c.abort( - c.enclosingPosition, - "T{} members must be defs defined in a Cacher class/trait/object body" - ) - } -}
\ No newline at end of file diff --git a/core/src/main/scala/mill/define/Task.scala b/core/src/main/scala/mill/define/Task.scala index e88ad8c6..8393acfb 100644 --- a/core/src/main/scala/mill/define/Task.scala +++ b/core/src/main/scala/mill/define/Task.scala @@ -58,7 +58,7 @@ object Target extends Applicative.Applyer[Task, Task, Result, Ctx]{ import c.universe._ c.Expr[Persistent[T]]( - mill.define.Cacher.wrapCached(c)( + mill.plugin.Cacher.wrapCached(c)( q"new ${weakTypeOf[Persistent[T]]}(${Applicative.impl[Task, T, Ctx](c)(t).tree})" ) ) @@ -74,7 +74,7 @@ object Target extends Applicative.Applyer[Task, Task, Result, Ctx]{ def targetTaskImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[Task[T]]): c.Expr[Target[T]] = { import c.universe._ c.Expr[Target[T]]( - mill.define.Cacher.wrapCached(c)( + mill.plugin.Cacher.wrapCached(c)( q"new ${weakTypeOf[TargetImpl[T]]}($t, _root_.sourcecode.Enclosing())" ) ) @@ -82,7 +82,7 @@ object Target extends Applicative.Applyer[Task, Task, Result, Ctx]{ def targetImpl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]): c.Expr[Target[T]] = { import c.universe._ c.Expr[Target[T]]( - mill.define.Cacher.wrapCached(c)( + mill.plugin.Cacher.wrapCached(c)( q"new ${weakTypeOf[TargetImpl[T]]}(${Applicative.impl0[Task, T, Ctx](c)(q"mill.eval.Result.Success($t)").tree}, _root_.sourcecode.Enclosing())" ) ) @@ -159,7 +159,7 @@ object Task { trait TaskModule extends Module { def defaultCommandName(): String } - trait Module extends mill.define.Cacher[Target]{ + trait Module extends mill.plugin.Cacher[Target]{ def wrapCached[T](t: Target[T], enclosing: String): Target[T] = t } diff --git a/core/src/test/scala/mill/define/CacherTests.scala b/core/src/test/scala/mill/define/CacherTests.scala index bacfa598..4b8e31ab 100644 --- a/core/src/test/scala/mill/define/CacherTests.scala +++ b/core/src/test/scala/mill/define/CacherTests.scala @@ -14,7 +14,7 @@ object CacherTests extends TestSuite{ } object Middle extends Middle trait Middle extends Base{ - override def value = T{ super.value() + 2} + def value = T{ super.value() + 2} def overriden = T{ super.value()} } object Terminal extends Terminal @@ -49,6 +49,19 @@ object CacherTests extends TestSuite{ eval(Terminal, Terminal.value) == 7, eval(Terminal, Terminal.overriden) == 1 ) + // Doesn't fail, presumably compileError doesn't go far enough in the + // compilation pipeline to hit the override checks + // + // 'overrideOutsideModuleFails - { + // compileError(""" + // trait Foo{ + // def x = 1 + // } + // object Bar extends Foo{ + // def x = 2 + // } + // """) + // } } } |