Skip to content

Commit fe87092

Browse files
authored
Merge pull request #406 from scala/backport-lts-3.3-23069
Backport "Enclosing package p.q not visible as q" to 3.3 LTS
2 parents ba7071d + bc0917b commit fe87092

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,15 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
338338
var cachePoint: Context = NoContext // last context with Resolved cache
339339
var importer: ImportSelector | Null = null // non-null for import context
340340
var precedence = NoPrecedence // of current resolution
341+
var enclosed = false // true if sym is owner of an enclosing context
341342
var done = false
342343
var cached = false
343344
val ctxs = ctx.outersIterator
344345
while !done && ctxs.hasNext do
345346
val cur = ctxs.next()
346-
if cur.owner eq sym then
347-
addCached(cachePoint, Definition)
348-
return // found enclosing definition
349-
else if isLocal then
347+
if cur.owner.userSymbol == sym && !sym.is(Package) then
348+
enclosed = true // found enclosing definition, don't register the reference
349+
if isLocal then
350350
if cur.owner eq sym.owner then
351351
done = true // for local def, just checking that it is not enclosing
352352
else
@@ -387,7 +387,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
387387
candidate = cur
388388
importer = sel
389389
else if checkMember(cur.owner) then
390-
if sym.srcPos.sourcePos.source == ctx.source then
390+
if sym.is(Package) || sym.srcPos.sourcePos.source == ctx.source then
391391
precedence = Definition
392392
candidate = cur
393393
importer = null // ignore import in same scope; we can't check nesting level
@@ -397,7 +397,8 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
397397
candidate = cur
398398
end while
399399
// record usage and possibly an import
400-
refInfos.refs.addOne(sym)
400+
if !enclosed then
401+
refInfos.refs.addOne(sym)
401402
if candidate != NoContext && candidate.isImportContext && importer != null then
402403
refInfos.sels.put(importer, ())
403404
// possibly record that we have performed this look-up

tests/warn/i23047.scala

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//> using options -Wunused:imports
2+
3+
package some.example:
4+
package demo:
5+
6+
import some.example // no warn because enclosing package example is not available as a simple name in some
7+
8+
object Main {
9+
10+
def generic[T](x: Any): T = null.asInstanceOf[T]
11+
12+
def main(args: Array[String]): Unit = {
13+
generic[example.Util](0)
14+
15+
import some.example.demo.Main // warn
16+
println(Main)
17+
18+
import some.example.demo // warn because enclosing package demo is available as a simple name
19+
println(demo.Main)
20+
}
21+
}
22+
23+
package some.example:
24+
25+
class Util

0 commit comments

Comments
 (0)