Skip to content

Commit 29a2750

Browse files
committed
Optimize findRef
Use simple test firsts in Import#selection (where not finding something is the norm), and avoid re-selection of `ctx.owner`.
1 parent e0d2756 commit 29a2750

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,26 @@ class Typer extends Namer
191191
if found eq previous then checkNewOrShadowed(found, prevPrec)
192192
else found
193193

194-
def selection(imp: ImportInfo, name: Name, checkBounds: Boolean) =
195-
if imp.sym.isCompleting then
196-
report.warning(i"cyclic ${imp.sym}, ignored", pos)
197-
NoType
198-
else if unimported.nonEmpty && unimported.contains(imp.site.termSymbol) then
199-
NoType
200-
else
201-
val pre = imp.site
202-
var denot = pre.memberBasedOnFlags(name, required, EmptyFlags)
203-
.accessibleFrom(pre)(using refctx)
194+
def selection(imp: ImportInfo, name: Name, checkBounds: Boolean): Type =
195+
imp.sym.info match
196+
case ImportType(expr) =>
197+
val pre = expr.tpe
198+
var denot = pre.memberBasedOnFlags(name, required, EmptyFlags)
199+
.accessibleFrom(pre)(using refctx)
204200
// Pass refctx so that any errors are reported in the context of the
205201
// reference instead of the context of the import scope
206-
if checkBounds && denot.exists then
207-
denot = denot.filterWithPredicate { mbr =>
208-
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
209-
}
210-
if reallyExists(denot) then pre.select(name, denot) else NoType
202+
if denot.exists then
203+
if checkBounds then
204+
denot = denot.filterWithPredicate { mbr =>
205+
mbr.matchesImportBound(if mbr.symbol.is(Given) then imp.givenBound else imp.wildcardBound)
206+
}
207+
if reallyExists(denot) then
208+
if unimported.isEmpty || !unimported.contains(pre.termSymbol) then
209+
return pre.select(name, denot)
210+
case _ =>
211+
if imp.sym.isCompleting then
212+
report.warning(i"cyclic ${imp.sym}, ignored", pos)
213+
NoType
211214

212215
/** The type representing a named import with enclosing name when imported
213216
* from given `site` and `selectors`.
@@ -356,7 +359,7 @@ class Typer extends Namer
356359
if !curOwner.is(Package) || isDefinedInCurrentUnit(defDenot) then
357360
result = checkNewOrShadowed(found, Definition) // no need to go further out, we found highest prec entry
358361
found match
359-
case found: NamedType if ctx.owner.isClass && isInherited(found.denot) =>
362+
case found: NamedType if curOwner.isClass && isInherited(found.denot) =>
360363
checkNoOuterDefs(found.denot, ctx, ctx)
361364
case _ =>
362365
else
@@ -373,8 +376,8 @@ class Typer extends Namer
373376
val outer = ctx.outer
374377
val curImport = ctx.importInfo
375378
def updateUnimported() =
376-
if (curImport.unimported.exists) unimported += curImport.unimported
377-
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
379+
if (curImport.unimported ne NoSymbol) unimported += curImport.unimported
380+
if (curOwner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
378381
previous // no more conflicts possible in this case
379382
else if (isPossibleImport(NamedImport) && (curImport ne outer.importInfo)) {
380383
val namedImp = namedImportRef(curImport)

0 commit comments

Comments
 (0)