From a4d3e944037359cd7869e98068ed1774892e8847 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Thu, 10 Oct 2024 15:31:02 +0200 Subject: [PATCH] Try to fix deprecated discover warning (#3711) fixes https://github.com/com-lihaoyi/mill/issues/3385 Somehow the `@nowarn` annotations don't seem to work after we moved the `millDiscover` definition into the main module body (???) so instead of relying on those I try to fix the deprecated warning in the `Discover` macro itself by identifying type annotations and explicitly asking not to dereference them Manually tested by running the commands below, observed that no deprecation warning was printed ``` rm -rf /Users/lihaoyi/Github/mill/example/javalib/basic/1-simple/out ./mill -i dist.run example/javalib/basic/1-simple -i run -t hello ``` --- main/define/src/mill/define/Discover.scala | 11 ++++++++++- runner/src/mill/runner/CodeGen.scala | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/main/define/src/mill/define/Discover.scala b/main/define/src/mill/define/Discover.scala index c3136c2a0a0..03316412f80 100644 --- a/main/define/src/mill/define/Discover.scala +++ b/main/define/src/mill/define/Discover.scala @@ -141,11 +141,20 @@ object Discover { } if overridesRoutes._1.nonEmpty || overridesRoutes._2.nonEmpty || overridesRoutes._3.nonEmpty } yield { + val lhs0 = discoveredModuleType match { + // Explicitly do not de-alias type refs, so type aliases to deprecated + // types do not result in spurious deprecation warnings appearing + case tr: TypeRef => tr + // Other types are fine + case _ => discoveredModuleType.typeSymbol.asClass.toType + } + + val lhs = q"classOf[$lhs0]" + // by wrapping the `overridesRoutes` in a lambda function we kind of work around // the problem of generating a *huge* macro method body that finally exceeds the // JVM's maximum allowed method size val overridesLambda = q"(() => $overridesRoutes)()" - val lhs = q"classOf[${discoveredModuleType.typeSymbol.asClass}]" q"$lhs -> $overridesLambda" } diff --git a/runner/src/mill/runner/CodeGen.scala b/runner/src/mill/runner/CodeGen.scala index fa75337cb4c..58d549d69a5 100644 --- a/runner/src/mill/runner/CodeGen.scala +++ b/runner/src/mill/runner/CodeGen.scala @@ -161,11 +161,7 @@ object CodeGen { newScriptCode = objectData.name.applyTo(newScriptCode, wrapperObjectName) newScriptCode = objectData.obj.applyTo(newScriptCode, "abstract class") - val millDiscover = - if (segments.nonEmpty) "" - else - """@_root_.scala.annotation.nowarn - | override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]""".stripMargin + val millDiscover = discoverSnippet(segments) s"""$pkgLine |$aliasImports @@ -224,11 +220,7 @@ object CodeGen { s"extends _root_.mill.main.RootModule.Subfolder " } - val millDiscover = - if (segments.nonEmpty) "" - else - """@_root_.scala.annotation.nowarn - | override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]""".stripMargin + val millDiscover = discoverSnippet(segments) // User code needs to be put in a separate class for proper submodule // object initialization due to https://github.com/scala/scala3/issues/21444 @@ -240,6 +232,14 @@ object CodeGen { } + def discoverSnippet(segments: Seq[String]): String = { + if (segments.nonEmpty) "" + else + """override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type] + |""".stripMargin + + } + private case class Snippet(var text: String = null, var start: Int = -1, var end: Int = -1) { def applyTo(s: String, replacement: String): String = s.patch(start, replacement.padTo(end - start, ' '), end - start)