@@ -375,12 +375,16 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
375
375
val ctxs = ctx.outersIterator
376
376
while ! done && ctxs.hasNext do
377
377
val cur = ctxs.next()
378
- if cur.owner eq sym then
379
- if importer != null && sym.is(Package ) then
380
- done = true // an enclosing package might be imported because p.q not visible as q
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
381
386
else
382
- addCached(cachePoint, Definition )
383
- return // found enclosing definition
387
+ return // found enclosing definition, don't register the reference
384
388
else if isLocal then
385
389
if cur.owner eq sym.owner then
386
390
done = true // for local def, just checking that it is not enclosing
@@ -966,6 +970,23 @@ object CheckUnused:
966
970
inline def userSymbol : Symbol =
967
971
if sym.denot.is(ModuleClass ) then sym.denot.companionModule else sym
968
972
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
+
969
990
extension (sel : ImportSelector )
970
991
def boundTpe : Type = sel.bound match
971
992
case untpd.TypedSplice (tree) => tree.tpe
0 commit comments