Skip to content

Commit c387ef2

Browse files
committed
fix: Fix incremental compilation of macros
**Problem** sbt-deps phase runs after the Typer, but prior to the Inliner. This means that the Zinc won't be able to track the symbolic dependency from the use site to the generated code, unless the code exists as a form of quotation. This often results in under-compilation of the macro use sites. **Solution** 1. Move the sbt-deps phase after the Inliner, which expands the macros 2. Traverse the Inlined tree, and treat it like a normal code
1 parent 39ebf6b commit c387ef2

File tree

10 files changed

+51
-3
lines changed

10 files changed

+51
-3
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class Compiler {
3535
List(new TyperPhase) :: // Compiler frontend: namer, typer
3636
List(CheckUnused.PostTyper(), CheckShadowing()) :: // Check for unused, shadowed elements
3737
List(new YCheckPositions) :: // YCheck positions
38-
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
3938
List(new semanticdb.ExtractSemanticDB.ExtractSemanticInfo) :: // Extract info into .semanticdb files
4039
List(new PostTyper) :: // Additional checks and cleanups after type checking
4140
List(new UnrollDefinitions) :: // Unroll annotated methods if detected in PostTyper
@@ -48,6 +47,7 @@ class Compiler {
4847
List(new Pickler) :: // Generate TASTY info
4948
List(new sbt.ExtractAPI) :: // Sends a representation of the API of classes to sbt via callbacks
5049
List(new Inlining) :: // Inline and execute macros
50+
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
5151
List(new PostInlining) :: // Add mirror support for inlined code
5252
List(new Staging) :: // Check staging levels and heal staged types
5353
List(new Splicing) :: // Replace level 1 splices with holes

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ private class ExtractDependenciesCollector(rec: DependencyRecorder) extends tpd.
227227
}
228228

229229
tree match {
230-
case tree: Inlined if !tree.inlinedFromOuterScope =>
230+
case tree: Inlined =>
231231
// The inlined call is normally ignored by TreeTraverser but we need to
232232
// record it as a dependency
233-
traverse(tree.call)
233+
if !tree.inlinedFromOuterScope then
234+
traverse(tree.call)
235+
traverseChildren(tree)
234236
case vd: ValDef if vd.symbol.is(ModuleVal) =>
235237
// Don't visit module val
236238
case t: Template if t.symbol.owner.is(ModuleClass) =>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
class D0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
class D1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
class D2
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package example
2+
3+
import com.softwaremill.macwire.*
4+
5+
object Test:
6+
try
7+
val d = wire[D0]
8+
val d1 = wire[D1]
9+
val d2 = wire[D2]
10+
catch
11+
case e: Throwable =>
12+
e.printStackTrace()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name := "add-dep"
2+
libraryDependencies ++= Seq(
3+
"com.softwaremill.macwire" %% "macros" % "2.6.6" % Provided,
4+
"com.softwaremill.macwire" %% "util" % "2.6.6",
5+
)
6+
Compile / incOptions ~= { _.withRecompileAllFraction(1.0) }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
class D0(de1: D1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion")
10+
)
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> compile
2+
3+
$ copy-file changes/D0.scala D0.scala
4+
5+
-> compile

0 commit comments

Comments
 (0)