Skip to content

Commit

Permalink
Try to fix deprecated discover warning (#3711)
Browse files Browse the repository at this point in the history
fixes #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
```
  • Loading branch information
lihaoyi authored Oct 10, 2024
1 parent 365635e commit a4d3e94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
11 changes: 10 additions & 1 deletion main/define/src/mill/define/Discover.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
20 changes: 10 additions & 10 deletions runner/src/mill/runner/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit a4d3e94

Please sign in to comment.