Skip to content

Enclosing package p.q not visible as q #23069

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,15 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
var cachePoint: Context = NoContext // last context with Resolved cache
var importer: ImportSelector | Null = null // non-null for import context
var precedence = NoPrecedence // of current resolution
var enclosed = false // true if sym is owner of an enclosing context
var done = false
var cached = false
val ctxs = ctx.outersIterator
while !done && ctxs.hasNext do
val cur = ctxs.next()
if cur.owner eq sym then
addCached(cachePoint, Definition)
return // found enclosing definition
else if isLocal then
if cur.owner.userSymbol == sym && !sym.is(Package) then
enclosed = true // found enclosing definition, don't register the reference
if isLocal then
if cur.owner eq sym.owner then
done = true // for local def, just checking that it is not enclosing
else
Expand Down Expand Up @@ -419,7 +419,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
candidate = cur
importer = sel
else if checkMember(cur.owner) then
if sym.srcPos.sourcePos.source == ctx.source then
if sym.is(Package) || sym.srcPos.sourcePos.source == ctx.source then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package sym has no source to compare.

precedence = Definition
candidate = cur
importer = null // ignore import in same scope; we can't check nesting level
Expand All @@ -429,7 +429,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
candidate = cur
end while
// record usage and possibly an import
refInfos.refs.addOne(sym)
if !enclosed then
refInfos.refs.addOne(sym)
if candidate != NoContext && candidate.isImportContext && importer != null then
refInfos.sels.put(importer, ())
// possibly record that we have performed this look-up
Expand Down
25 changes: 25 additions & 0 deletions tests/warn/i23047.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//> using options -Wunused:imports

package some.example:
package demo:

import some.example // no warn because enclosing package example is not available as a simple name in some

object Main {

def generic[T](x: Any): T = null.asInstanceOf[T]

def main(args: Array[String]): Unit = {
generic[example.Util](0)

import some.example.demo.Main // warn
println(Main)

import some.example.demo // warn because enclosing package demo is available as a simple name
println(demo.Main)
}
}

package some.example:

class Util
Loading