From 69e0cdb6c0f1efafc558b36af77f3a07964d3c7e Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Thu, 12 Dec 2024 06:55:55 +1100 Subject: [PATCH] Fix line info for import (#24523) Refs #24158 Fixes the line info of the module symbol (cases like `import as` and grouped imports had wrong line info). Since that symbol's line info is now used for the warnings, there isn't a separate line info stored for `unusedImports` Examples of fixed cases ```nim import strutils as test #[ ^ before ^ after ]# # This case was fixed by #24158, but only for unused imports import std/[strutils, strutils] #[ ^ before ^ after ]# from strutils import split #[ ^ before ^ after ]# ``` --- compiler/importer.nim | 11 ++++++----- compiler/lookups.nim | 2 +- compiler/sem.nim | 6 +++--- tests/import/tduplicate_imports.nim | 17 +++++++++++++++++ tests/msgs/tused2.nim | 2 +- tests/tools/tunused_imports.nim | 14 ++++++++------ 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 tests/import/tduplicate_imports.nim diff --git a/compiler/importer.nim b/compiler/importer.nim index ffb7e03056..7296f8f070 100644 --- a/compiler/importer.nim +++ b/compiler/importer.nim @@ -246,8 +246,9 @@ proc importModuleAs(c: PContext; n: PNode, realModule: PSym, importHidden: bool) result = createModuleAliasImpl(realModule.name) if importHidden: result.options.incl optImportHidden - let moduleIdent = if n.kind == nkInfix: n[^1] else: n - c.unusedImports.add((result, moduleIdent.info)) + let moduleIdent = if n.kind in {nkInfix, nkImportAs}: n[^1] else: n + result.info = moduleIdent.info + c.unusedImports.add((result, result.info)) c.importModuleMap[result.id] = realModule.id c.importModuleLookup.mgetOrPut(result.name.id, @[]).addUnique realModule.id @@ -335,7 +336,7 @@ proc impMod(c: PContext; it: PNode; importStmtResult: PNode) = let m = myImportModule(c, it, importStmtResult) if m != nil: # ``addDecl`` needs to be done before ``importAllSymbols``! - addDecl(c, m, it.info) # add symbol to symbol table of module + addDecl(c, m) # add symbol to symbol table of module importAllSymbols(c, m) #importForwarded(c, m.ast, emptySet, m) afterImport(c, m) @@ -372,7 +373,7 @@ proc evalFrom*(c: PContext, n: PNode): PNode = var m = myImportModule(c, n[0], result) if m != nil: n[0] = newSymNode(m) - addDecl(c, m, n.info) # add symbol to symbol table of module + addDecl(c, m) # add symbol to symbol table of module var im = ImportedModule(m: m, mode: importSet, imported: initIntSet()) for i in 1..