Skip to content

Commit b841bec

Browse files
som-snytttgodzik
authored andcommitted
Always resolve but restrict imports
1 parent b47f058 commit b841bec

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc.transform
1+
package dotty.tools.dotc
2+
package transform
23

34
import dotty.tools.dotc.ast.desugar.{ForArtifact, PatternVar}
45
import dotty.tools.dotc.ast.tpd.*
@@ -60,14 +61,14 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6061
val resolving =
6162
tree.srcPos.isUserCode
6263
|| tree.srcPos.isZeroExtentSynthetic // take as summonInline
63-
if resolving && !ignoreTree(tree) then
64+
if !ignoreTree(tree) then
6465
def loopOverPrefixes(prefix: Type, depth: Int): Unit =
6566
if depth < 10 && prefix.exists && !prefix.classSymbol.isEffectiveRoot then
66-
resolveUsage(prefix.classSymbol, nme.NO_NAME, NoPrefix)
67+
resolveUsage(prefix.classSymbol, nme.NO_NAME, NoPrefix, imports = resolving)
6768
loopOverPrefixes(prefix.normalizedPrefix, depth + 1)
6869
if tree.srcPos.isZeroExtentSynthetic then
6970
loopOverPrefixes(tree.typeOpt.normalizedPrefix, depth = 0)
70-
resolveUsage(tree.symbol, tree.name, tree.typeOpt.importPrefix.skipPackageObject)
71+
resolveUsage(tree.symbol, tree.name, tree.typeOpt.importPrefix.skipPackageObject, imports = resolving)
7172
else if tree.hasType then
7273
resolveUsage(tree.tpe.classSymbol, tree.name, tree.tpe.importPrefix.skipPackageObject)
7374
refInfos.isAssignment = false
@@ -286,8 +287,11 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
286287
* e.g., in `scala.Int`, `scala` is in scope for typer, but here we reverse-engineer the attribution.
287288
* For Select, lint does not look up `<empty>.scala` (so top-level syms look like magic) but records `scala.Int`.
288289
* For Ident, look-up finds the root import as usual. A competing import is OK because higher precedence.
290+
*
291+
* The `imports` flag is whether an identifier can mark an import as used: the flag is false
292+
* for inlined code, except for `summonInline` (and related constructs) which are resolved at inlining.
289293
*/
290-
def resolveUsage(sym0: Symbol, name: Name, prefix: Type)(using Context): Unit =
294+
def resolveUsage(sym0: Symbol, name: Name, prefix: Type, imports: Boolean = true)(using Context): Unit =
291295
import PrecedenceLevels.*
292296
val sym = sym0.userSymbol
293297

@@ -391,7 +395,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
391395
// record usage and possibly an import
392396
if !enclosed then
393397
refInfos.addRef(sym)
394-
if candidate != NoContext && candidate.isImportContext && importer != null then
398+
if imports && candidate != NoContext && candidate.isImportContext && importer != null then
395399
refInfos.sels.put(importer, ())
396400
end resolveUsage
397401

0 commit comments

Comments
 (0)