Skip to content

Commit b92e62d

Browse files
committed
Optimize checkAndAdaptExperimentalImports to avoid O(n^2) behavior
1 parent ee8277d commit b92e62d

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -821,29 +821,39 @@ object Checking {
821821
case Nil =>
822822
Nil
823823

824-
for case imp @ Import(qual, selectors) <- trees do
824+
def unitExperimentalLanguageImports =
825825
def isAllowedImport(sel: untpd.ImportSelector) =
826826
val name = Feature.experimental(sel.name)
827827
name == Feature.scala2macros
828828
|| name == Feature.captureChecking
829+
trees.filter {
830+
case Import(qual, selectors) =>
831+
languageImport(qual) match
832+
case Some(nme.experimental) =>
833+
!selectors.forall(isAllowedImport) && !ctx.owner.isInExperimentalScope
834+
case _ => false
835+
case _ => false
836+
}
829837

830-
languageImport(qual) match
831-
case Some(nme.experimental)
832-
if !ctx.owner.isInExperimentalScope && !selectors.forall(isAllowedImport) =>
833-
if ctx.owner.is(Package) || ctx.owner.name.startsWith(str.REPL_SESSION_LINE) then
834-
// mark all top-level definitions as @experimental
835-
for tree <- nonExperimentalStats(trees) do
836-
tree match
837-
case tree: MemberDef =>
838-
// TODO move this out of checking (into posttyper?)
839-
val sym = tree.symbol
840-
if !sym.isExperimental then
841-
sym.addAnnotation(ExperimentalAnnotation(i"Added by top level $imp", sym.span))
842-
case tree =>
843-
// There is no definition to attach the @experimental annotation
844-
report.error("Implementation restriction: top-level `val _ = ...` is not supported with experimental language imports.", tree.srcPos)
845-
else Feature.checkExperimentalFeature("feature local import", imp.srcPos)
838+
if ctx.owner.is(Package) || ctx.owner.name.startsWith(str.REPL_SESSION_LINE) then
839+
unitExperimentalLanguageImports match
840+
case imp :: _ =>
841+
// mark all top-level definitions as @experimental
842+
for tree <- nonExperimentalStats(trees) do
843+
tree match
844+
case tree: MemberDef =>
845+
// TODO move this out of checking (into posttyper?)
846+
val sym = tree.symbol
847+
if !sym.isExperimental then
848+
sym.addAnnotation(ExperimentalAnnotation(i"Added by top level $imp", sym.span))
849+
case tree =>
850+
// There is no definition to attach the @experimental annotation
851+
report.error("Implementation restriction: top-level `val _ = ...` is not supported with experimental language imports.", tree.srcPos)
846852
case _ =>
853+
else
854+
for imp <- unitExperimentalLanguageImports do
855+
Feature.checkExperimentalFeature("feature local import", imp.srcPos)
856+
847857
end checkAndAdaptExperimentalImports
848858

849859
/** Checks that PolyFunction only have valid refinements.

0 commit comments

Comments
 (0)