Skip to content

Commit bea7edc

Browse files
committed
Prefer lookup in package to special casing
1 parent 60e1fcc commit bea7edc

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+9-13
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,15 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
370370
var cachePoint: Context = NoContext // last context with Resolved cache
371371
var importer: ImportSelector | Null = null // non-null for import context
372372
var precedence = NoPrecedence // of current resolution
373+
var enclosed = false // true if sym is owner of an enclosing context
373374
var done = false
374375
var cached = false
375376
val ctxs = ctx.outersIterator
376377
while !done && ctxs.hasNext do
377378
val cur = ctxs.next()
378-
if cur.owner.userSymbol eq sym then
379-
if sym.is(Package) then
380-
if cur.isSimplePackage then
381-
precedence = Definition
382-
importer = null
383-
// else package p.q is not available as a simple name but it might be imported
384-
end if
385-
done = true
386-
else
387-
return // found enclosing definition, don't register the reference
388-
else if isLocal then
379+
if cur.owner.userSymbol == sym && !sym.is(Package) then
380+
enclosed = true // found enclosing definition, don't register the reference
381+
if isLocal then
389382
if cur.owner eq sym.owner then
390383
done = true // for local def, just checking that it is not enclosing
391384
else
@@ -426,7 +419,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
426419
candidate = cur
427420
importer = sel
428421
else if checkMember(cur.owner) then
429-
if sym.srcPos.sourcePos.source == ctx.source then
422+
if sym.is(Package) || sym.srcPos.sourcePos.source == ctx.source then
430423
precedence = Definition
431424
candidate = cur
432425
importer = null // ignore import in same scope; we can't check nesting level
@@ -436,7 +429,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
436429
candidate = cur
437430
end while
438431
// record usage and possibly an import
439-
refInfos.refs.addOne(sym)
432+
if !enclosed then
433+
refInfos.refs.addOne(sym)
440434
if candidate != NoContext && candidate.isImportContext && importer != null then
441435
refInfos.sels.put(importer, ())
442436
// possibly record that we have performed this look-up
@@ -970,6 +964,7 @@ object CheckUnused:
970964
inline def userSymbol: Symbol=
971965
if sym.denot.is(ModuleClass) then sym.denot.companionModule else sym
972966

967+
/*
973968
extension (cur: Context)
974969
def isSimplePackage(using Context): Boolean = cur.owner.is(Package) && {
975970
val location = cur.owner.showFullName
@@ -986,6 +981,7 @@ object CheckUnused:
986981
case _ =>
987982
!location.contains(".") // no outer context contributes a name, effectively _root_.p.q
988983
}
984+
*/
989985

990986
extension (sel: ImportSelector)
991987
def boundTpe: Type = sel.bound match

0 commit comments

Comments
 (0)