Skip to content

Commit ca9750b

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

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

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

+27-17
Original file line numberDiff line numberDiff line change
@@ -826,29 +826,39 @@ object Checking {
826826
case Nil =>
827827
Nil
828828

829-
for case imp @ Import(qual, selectors) <- trees do
829+
def unitExperimentalLanguageImports =
830830
def isAllowedImport(sel: untpd.ImportSelector) =
831831
val name = Feature.experimental(sel.name)
832832
name == Feature.scala2macros
833833
|| name == Feature.captureChecking
834+
trees.filter {
835+
case Import(qual, selectors) =>
836+
languageImport(qual) match
837+
case Some(nme.experimental) =>
838+
!selectors.forall(isAllowedImport) && !ctx.owner.isInExperimentalScope
839+
case _ => false
840+
case _ => false
841+
}
834842

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

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

0 commit comments

Comments
 (0)