Skip to content

Commit eb67a9c

Browse files
committed
Always resolve but restrict imports
1 parent 1eb4e79 commit eb67a9c

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
package dotty.tools.dotc.transform
2-
3-
import dotty.tools.dotc.ast.desugar.{ForArtifact, PatternVar}
4-
import dotty.tools.dotc.ast.tpd.*
5-
import dotty.tools.dotc.ast.untpd, untpd.ImportSelector
6-
import dotty.tools.dotc.config.ScalaSettings
7-
import dotty.tools.dotc.core.Contexts.*
8-
import dotty.tools.dotc.core.Flags.*
9-
import dotty.tools.dotc.core.Names.{Name, SimpleName, DerivedName, TermName, termName}
10-
import dotty.tools.dotc.core.NameKinds.{
11-
BodyRetainerName, ContextBoundParamName, ContextFunctionParamName, DefaultGetterName, WildcardParamName}
12-
import dotty.tools.dotc.core.NameOps.{isAnonymousFunctionName, isReplWrapperName, setterName}
13-
import dotty.tools.dotc.core.Scopes.newScope
14-
import dotty.tools.dotc.core.StdNames.nme
15-
import dotty.tools.dotc.core.Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
16-
import dotty.tools.dotc.core.Types.*
17-
import dotty.tools.dotc.report
18-
import dotty.tools.dotc.reporting.{CodeAction, UnusedSymbol}
19-
import dotty.tools.dotc.rewrites.Rewrites
20-
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
21-
import dotty.tools.dotc.typer.{ImportInfo, Typer}
22-
import dotty.tools.dotc.typer.Deriving.OriginalTypeClass
23-
import dotty.tools.dotc.typer.Implicits.{ContextualImplicits, RenamedImplicitRef}
24-
import dotty.tools.dotc.util.{Property, Spans, SrcPos}, Spans.Span
25-
import dotty.tools.dotc.util.Chars.{isLineBreakChar, isWhitespace}
26-
import dotty.tools.dotc.util.chaining.*
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import ast.*, desugar.{ForArtifact, PatternVar}, tpd.*, untpd.ImportSelector
5+
import config.ScalaSettings
6+
import core.*, Contexts.*, Flags.*
7+
import Names.{Name, SimpleName, DerivedName, TermName, termName}
8+
import NameKinds.{BodyRetainerName, ContextBoundParamName, ContextFunctionParamName, DefaultGetterName, WildcardParamName}
9+
import NameOps.{isAnonymousFunctionName, isReplWrapperName, setterName}
10+
import Scopes.newScope
11+
import StdNames.nme
12+
import Symbols.{ClassSymbol, NoSymbol, Symbol, defn, isDeprecated, requiredClass, requiredModule}
13+
import Types.*
14+
import reporting.{CodeAction, UnusedSymbol}
15+
import rewrites.Rewrites
16+
17+
import MegaPhase.MiniPhase
18+
import typer.{ImportInfo, Typer}
19+
import typer.Deriving.OriginalTypeClass
20+
import typer.Implicits.{ContextualImplicits, RenamedImplicitRef}
21+
import util.{Property, Spans, SrcPos}, Spans.Span
22+
import util.Chars.{isLineBreakChar, isWhitespace}
23+
import util.chaining.*
2724

2825
import java.util.IdentityHashMap
2926

@@ -60,14 +57,14 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6057
val resolving =
6158
tree.srcPos.isUserCode
6259
|| tree.srcPos.isZeroExtentSynthetic // take as summonInline
63-
if resolving && !ignoreTree(tree) then
60+
if !ignoreTree(tree) then
6461
def loopOverPrefixes(prefix: Type, depth: Int): Unit =
6562
if depth < 10 && prefix.exists && !prefix.classSymbol.isEffectiveRoot then
66-
resolveUsage(prefix.classSymbol, nme.NO_NAME, NoPrefix)
63+
resolveUsage(prefix.classSymbol, nme.NO_NAME, NoPrefix, imports = resolving)
6764
loopOverPrefixes(prefix.normalizedPrefix, depth + 1)
6865
if tree.srcPos.isZeroExtentSynthetic then
6966
loopOverPrefixes(tree.typeOpt.normalizedPrefix, depth = 0)
70-
resolveUsage(tree.symbol, tree.name, tree.typeOpt.importPrefix.skipPackageObject)
67+
resolveUsage(tree.symbol, tree.name, tree.typeOpt.importPrefix.skipPackageObject, imports = resolving)
7168
else if tree.hasType then
7269
resolveUsage(tree.tpe.classSymbol, tree.name, tree.tpe.importPrefix.skipPackageObject)
7370
refInfos.isAssignment = false
@@ -318,8 +315,11 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
318315
* e.g., in `scala.Int`, `scala` is in scope for typer, but here we reverse-engineer the attribution.
319316
* For Select, lint does not look up `<empty>.scala` (so top-level syms look like magic) but records `scala.Int`.
320317
* For Ident, look-up finds the root import as usual. A competing import is OK because higher precedence.
318+
*
319+
* The `imports` flag is whether an identifier can mark an import as used: the flag is false
320+
* for inlined code, except for `summonInline` (and related constructs) which are resolved at inlining.
321321
*/
322-
def resolveUsage(sym0: Symbol, name: Name, prefix: Type)(using Context): Unit =
322+
def resolveUsage(sym0: Symbol, name: Name, prefix: Type, imports: Boolean = true)(using Context): Unit =
323323
import PrecedenceLevels.*
324324
val sym = sym0.userSymbol
325325

@@ -423,7 +423,7 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
423423
// record usage and possibly an import
424424
if !enclosed then
425425
refInfos.addRef(sym)
426-
if candidate != NoContext && candidate.isImportContext && importer != null then
426+
if imports && candidate != NoContext && candidate.isImportContext && importer != null then
427427
refInfos.sels.put(importer, ())
428428
end resolveUsage
429429

0 commit comments

Comments
 (0)