Skip to content

Commit

Permalink
Optimize checkAndAdaptExperimentalImports to avoid O(n^2) behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 18, 2024
1 parent 9973893 commit ec0b92c
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -826,29 +826,39 @@ object Checking {
case Nil =>
Nil

for case imp @ Import(qual, selectors) <- trees do
def unitExperimentalLanguageImports =
def isAllowedImport(sel: untpd.ImportSelector) =
val name = Feature.experimental(sel.name)
name == Feature.scala2macros
|| name == Feature.captureChecking
trees.filter {
case Import(qual, selectors) =>
languageImport(qual) match
case Some(nme.experimental) =>
!selectors.forall(isAllowedImport) && !ctx.owner.isInExperimentalScope
case _ => false
case _ => false
}

languageImport(qual) match
case Some(nme.experimental)
if !ctx.owner.isInExperimentalScope && !selectors.forall(isAllowedImport) =>
if ctx.owner.is(Package) || ctx.owner.name.startsWith(str.REPL_SESSION_LINE) then
// mark all top-level definitions as @experimental
for tree <- nonExperimentalStats(trees) do
tree match
case tree: MemberDef =>
// TODO move this out of checking (into posttyper?)
val sym = tree.symbol
if !sym.isExperimental then
sym.addAnnotation(ExperimentalAnnotation(i"Added by top level $imp", sym.span))
case tree =>
// There is no definition to attach the @experimental annotation
report.error("Implementation restriction: top-level `val _ = ...` is not supported with experimental language imports.", tree.srcPos)
else Feature.checkExperimentalFeature("feature local import", imp.srcPos)
if ctx.owner.is(Package) || ctx.owner.name.startsWith(str.REPL_SESSION_LINE) then
unitExperimentalLanguageImports match
case imp :: _ =>
// mark all top-level definitions as @experimental
for tree <- nonExperimentalStats(trees) do
tree match
case tree: MemberDef =>
// TODO move this out of checking (into posttyper?)
val sym = tree.symbol
if !sym.isExperimental then
sym.addAnnotation(ExperimentalAnnotation(i"Added by top level $imp", sym.span))
case tree =>
// There is no definition to attach the @experimental annotation
report.error("Implementation restriction: top-level `val _ = ...` is not supported with experimental language imports.", tree.srcPos)
case _ =>
else
for imp <- unitExperimentalLanguageImports do
Feature.checkExperimentalFeature("feature local import", imp.srcPos)

end checkAndAdaptExperimentalImports

/** Checks that PolyFunction only have valid refinements.
Expand Down

0 comments on commit ec0b92c

Please sign in to comment.