Skip to content

Commit 06d1611

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

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

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

+7-30
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,23 +964,6 @@ object CheckUnused:
970964
inline def userSymbol: Symbol=
971965
if sym.denot.is(ModuleClass) then sym.denot.companionModule else sym
972966

973-
extension (cur: Context)
974-
def isSimplePackage(using Context): Boolean = cur.owner.is(Package) && {
975-
val location = cur.owner.showFullName
976-
val enclosing = cur.outersIterator
977-
.takeWhile(c => !c.owner.isEmptyPackage && !(c.isImportContext && c.importInfo.nn.isRootImport))
978-
.find(c => c.owner.userSymbol ne cur.owner.userSymbol)
979-
enclosing match
980-
case Some(outer) =>
981-
outer.owner.is(Package) // if there is such an outer context, its owner should be package
982-
&& !location
983-
.stripPrefix(outer.owner.showFullName) // p.q in p.q.r
984-
.stripPrefix(".") // leading dot in .r
985-
.contains(".") // remaining r so p.q.v.w leaves v.w
986-
case _ =>
987-
!location.contains(".") // no outer context contributes a name, effectively _root_.p.q
988-
}
989-
990967
extension (sel: ImportSelector)
991968
def boundTpe: Type = sel.bound match
992969
case untpd.TypedSplice(tree) => tree.tpe

0 commit comments

Comments
 (0)