Skip to content

Commit 35c2ef3

Browse files
authored
Add accessible check for import usage (#23348)
Fixes #23347 The code for deciding whether a selector is used to import a symbol was missing a check for accessibility (importability). 3.6.4 doesn't warn, so it would be nice to know why it excludes the member. The algorithm is different but the code for choosing import selectors is similar. 3.7.0 also doesn't warn, but no change stands out as a culprit. Possibly the selector was spuriously excluded (by result caching in 3.7.0).
2 parents 74d7ae0 + c76b42f commit 35c2ef3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,10 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
305305
def matchingSelector(info: ImportInfo): ImportSelector | Null =
306306
val qtpe = info.site
307307
def hasAltMember(nm: Name) = qtpe.member(nm).hasAltWith: alt =>
308-
alt.symbol == sym
309-
|| nm.isTypeName && alt.symbol.isAliasType && alt.info.dealias.typeSymbol == sym
308+
val sameSym =
309+
alt.symbol == sym
310+
|| nm.isTypeName && alt.symbol.isAliasType && alt.info.dealias.typeSymbol == sym
311+
sameSym && alt.symbol.isAccessibleFrom(qtpe)
310312
def loop(sels: List[ImportSelector]): ImportSelector | Null = sels match
311313
case sel :: sels =>
312314
val matches =

tests/warn/i23347.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//> using options -Wunused:all
2+
3+
object USED {
4+
case class A(value: Int)
5+
}
6+
7+
object UNUSED {
8+
// In reality UNUSED would contain several other necessary members!
9+
private type A = USED.A // warn private
10+
class B
11+
}
12+
13+
object Test {
14+
import USED.*
15+
import UNUSED.*
16+
17+
def foo(a: A): Int = a.value
18+
19+
def g(b: B) = ()
20+
21+
}
22+

0 commit comments

Comments
 (0)