Skip to content

Commit b8fb81b

Browse files
Set the inlining phase in the Context used for checking macro trees (#20087)
Fixes #17009 The problem here was with the sub typing check for `line.split(" ").nn`, which needed to confirm that: ```scala (?2 : Array[String]) & Array[String] <: (?1 : Array[String]) & Array[String] ``` `TypeComparer` would eventually try to compare two skolem types: ```scala (?2 : Array[String]) <: (?1 : Array[String]) ``` The behavior of `TypeComparer` differs here when executed during the typer phase, where it always returns false for two skolem types, without checking the sub typing further. This makes sense for Typer, but not so much for the macro checks, which for `transparent inline`s end up being executed during Typer. I think the best solution here is to artificially change the phase in the checkingContext, so the checks done for transparent and non-transparent macros are the same.
2 parents f3139cd + 9f5618b commit b8fb81b

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

+10
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,19 @@ object TreeChecker {
837837

838838
def checkMacroGeneratedTree(original: tpd.Tree, expansion: tpd.Tree)(using Context): Unit =
839839
if ctx.settings.XcheckMacros.value then
840+
// We want make sure that transparent inline macros are checked in the same way that
841+
// non transparent macros are, so we try to prepare a context which would make
842+
// the checks behave the same way for both types of macros.
843+
//
844+
// E.g. Different instances of skolem types are by definition not able to be a subtype of
845+
// one another, however in practice this is only upheld during typer phase, and we do not want
846+
// it to be upheld during this check.
847+
// See issue: #17009
840848
val checkingCtx = ctx
841849
.fresh
842850
.setReporter(new ThrowingReporter(ctx.reporter))
851+
.setPhase(ctx.base.inliningPhase)
852+
843853
val phases = ctx.base.allPhases.toList
844854
val treeChecker = new LocalChecker(previousPhases(phases))
845855

tests/pos-macros/i17009/Macro_1.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted._
2+
3+
object Macro {
4+
transparent inline def transform[T](inline expr: T): T = ${ transformImpl[T]('expr) }
5+
def transformImpl[T: Type](f: Expr[T])(using Quotes): Expr[T] = f
6+
}

tests/pos-macros/i17009/Main_2.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def processLine(line: String): Unit = {
2+
Macro.transform {
3+
line.split(" ").nn
4+
???
5+
}
6+
}

0 commit comments

Comments
 (0)