followup #18362: make UnusedImport work robustly (#18366)

* warnDuplicateModuleImport => hintDuplicateModuleImport
* improve DuplicateModuleImport msg, add test
This commit is contained in:
Timothee Cour 2021-06-27 11:39:16 -07:00 • committed by GitHub
commit 0b7361e938
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 186 additions and 25 deletions

View file

@ -12,7 +12,7 @@
import
intsets, ast, astalgo, msgs, options, idents, lookups,
semdata, modulepaths, sigmatch, lineinfos, sets,
modulegraphs, wordrecg
modulegraphs, wordrecg, tables
from strutils import `%`
proc readExceptSet*(c: PContext, n: PNode): IntSet =
@ -239,6 +239,7 @@ proc importModuleAs(c: PContext; n: PNode, realModule: PSym, importHidden: bool)
if importHidden:
result.options.incl optImportHidden
c.unusedImports.add((result, n.info))
c.importModuleMap[result.id] = realModule.id
proc transformImportAs(c: PContext; n: PNode): tuple[node: PNode, importHidden: bool] =
var ret: typeof(result)
@ -296,6 +297,13 @@ proc myImportModule(c: PContext, n: var PNode, importStmtResult: PNode): PSym =
importStmtResult.add newSymNode(result, n.info)
#newStrNode(toFullPath(c.config, f), n.info)
proc afterImport(c: PContext, m: PSym) =
# fixes bug #17510, for re-exported symbols
let realModuleId = c.importModuleMap[m.id]
for s in allSyms(c.graph, m):
if s.owner.id != realModuleId:
c.exportIndirections.incl((m.id, s.id))
proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
var it = it
let m = myImportModule(c, it, importStmtResult)
@ -304,9 +312,7 @@ proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
addDecl(c, m, it.info) # add symbol to symbol table of module
importAllSymbols(c, m)
#importForwarded(c, m.ast, emptySet, m)
for s in allSyms(c.graph, m): # fixes bug #17510, for re-exported symbols
if s.owner != m:
c.exportIndirections.incl((m.id, s.id))
afterImport(c, m)
proc evalImport*(c: PContext, n: PNode): PNode =
result = newNodeI(nkImportStmt, n.info)
@ -345,6 +351,7 @@ proc evalFrom*(c: PContext, n: PNode): PNode =
if n[i].kind != nkNilLit:
importSymbol(c, n[i], m, im.imported)
c.addImport im
afterImport(c, m)
proc evalImportExcept*(c: PContext, n: PNode): PNode =
result = newNodeI(nkImportStmt, n.info)
@ -355,3 +362,4 @@ proc evalImportExcept*(c: PContext, n: PNode): PNode =
addDecl(c, m, n.info) # add symbol to symbol table of module
importAllSymbolsExcept(c, m, readExceptSet(c, n))
#importForwarded(c, m.ast, exceptSet, m)
afterImport(c, m)