Skip to content

Commit b92251d

Browse files
committed
Replace symbol travelsal with tree traversal when finding top level experimentals
1 parent 6e32627 commit b92251d

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -804,20 +804,22 @@ object Checking {
804804
*
805805
*/
806806
def checkAndAdaptExperimentalImports(trees: List[Tree])(using Context): Unit =
807-
def nonExperimentalTopLevelDefs(pack: Symbol): Iterator[Symbol] =
808-
def isNonExperimentalTopLevelDefinition(sym: Symbol) =
809-
sym.isDefinedInCurrentRun
810-
&& sym.source == ctx.compilationUnit.source
811-
&& !sym.isConstructor // not constructor of package object
812-
&& !sym.is(Package) && !sym.name.isPackageObjectName
813-
&& !sym.isExperimental
814-
815-
pack.info.decls.toList.iterator.flatMap: sym =>
816-
if sym.isClass && (sym.is(Package) || sym.isPackageObject) then
817-
nonExperimentalTopLevelDefs(sym)
818-
else if isNonExperimentalTopLevelDefinition(sym) then
819-
sym :: Nil
820-
else Nil
807+
def nonExperimentalTopLevelDefs(): Iterator[Symbol] =
808+
new TreeAccumulator[List[Symbol]] {
809+
override def apply(x: List[Symbol], tree: tpd.Tree)(using Context): List[Symbol] =
810+
tree match {
811+
case tpd.PackageDef(_, contents) =>
812+
super.apply(x, contents)
813+
case a @ tpd.TypeDef(_, temp: Template) if a.symbol.isPackageObject =>
814+
super.apply(x, temp.body)
815+
case a @ tpd.TypeDef(_, Template(_, _, _, _)) =>
816+
val sym = a.symbol
817+
if !sym.isExperimental then
818+
sym :: x
819+
else x
820+
case _ => x
821+
}
822+
}.apply(Nil, ctx.compilationUnit.tpdTree).iterator
821823

822824
def unitExperimentalLanguageImports =
823825
def isAllowedImport(sel: untpd.ImportSelector) =
@@ -835,7 +837,7 @@ object Checking {
835837

836838
if ctx.owner.is(Package) || ctx.owner.name.startsWith(str.REPL_SESSION_LINE) then
837839
def markTopLevelDefsAsExperimental(why: String): Unit =
838-
for sym <- nonExperimentalTopLevelDefs(ctx.owner) do
840+
for sym <- nonExperimentalTopLevelDefs() do
839841
sym.addAnnotation(ExperimentalAnnotation(s"Added by $why", sym.span))
840842

841843
unitExperimentalLanguageImports match

tests/pos-macros/i21802/Macro.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class MetricsGroup[A]
2+
object MetricsGroup:
3+
import scala.quoted.*
4+
5+
transparent inline final def refine[A]: MetricsGroup[A] =
6+
${ refineImpl[A] }
7+
8+
private def refineImpl[A](using qctx: Quotes, tpe: Type[A]): Expr[MetricsGroup[A]] =
9+
import qctx.reflect.*
10+
11+
val mt = MethodType(Nil)(_ => Nil, _ => TypeRepr.of[A])
12+
val tpe = Refinement(TypeRepr.of[MetricsGroup[A]], "apply", mt).asType
13+
tpe match
14+
case '[tpe] =>
15+
'{ MetricsGroup[A]().asInstanceOf[MetricsGroup[A] & tpe] }

tests/pos-macros/i21802/Test.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//> using options -experimental -Ydebug
2+
3+
class ProbeFailedException(cause: Exception) extends Exception(cause)
4+
trait Probing:
5+
self: Metrics =>
6+
val probeFailureCounter: MetricsGroup[Counter] =
7+
counters("ustats_probe_failures_count").labelled
8+
9+
10+
trait Counter
11+
class Metrics:
12+
class counters(name: String):
13+
transparent inline final def labelled: MetricsGroup[Counter] = MetricsGroup.refine[Counter]

0 commit comments

Comments
 (0)