Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect UnusedImport warning in presence of import foo as bar #17511

Closed
tersec opened this issue Mar 25, 2021 · 0 comments · Fixed by #18362
Closed

Incorrect UnusedImport warning in presence of import foo as bar #17511

tersec opened this issue Mar 25, 2021 · 0 comments · Fixed by #18362

Comments

@tersec
Copy link
Contributor

tersec commented Mar 25, 2021

When module bar imports and exports module foo (here, sequtils) using import ... as ... and module baz imports bar to get access to foo, either one or two UnusedImport warning is emitted, depending on Nim version.

Example

test.nim:

import sequtils as su2
export su2

test2.nim:

import "."/test
echo repeat("b", 3)

Current Output

$ ~/Nim/bin/nim c -r test2.nim
Hint: used config file 'Nim/config/nim.cfg' [Conf]
Hint: used config file 'Nim/config/config.nims' [Conf]
.......
test.nim(1, 17) Warning: imported and not used: 'sequtils' [UnusedImport]
test2.nim(1, 11) Warning: imported and not used: 'test' [UnusedImport]
CC: stdlib_assertions.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: test2.nim
Hint:  [Link]
Hint: 25592 lines; 0.530s; 31.934MiB peakmem; Debug build; proj: test2.nim; out: test2 [SuccessX]
Hint: test2  [Exec]
@["b", "b", "b"]
user@tertiva:~$ ~/Nim/bin/nim --version
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-03-25
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 8573160a44439ffe3fbbdd354188db9c976a2081
active boot switches: -d:release

Older versions also display variations of this behavior.
1.4.2:

$ nim c -r test2.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
.......
test.nim(1, 17) Warning: imported and not used: 'sequtils' [UnusedImport]
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_sequtils.nim
CC: test2.nim
Hint:  [Link]
Hint: 25242 lines; 0.542s; 25.52MiB peakmem; Debug build; proj: test2.nim; out: test2 [SuccessX]
Hint: test2  [Exec]
@["b", "b", "b"]
$ nim --version
Nim Compiler Version 1.4.2 [Linux: amd64]
Compiled at 2020-12-02
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

1.2.6:

$ Nim1.2.6/bin/nim c -r test2.nim
Hint: used config file 'Nim1.2.6/config/nim.cfg' [Conf]
Hint: used config file 'Nim1.2.6/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: test2 [Processing]
Hint: test [Processing]
Hint: sequtils [Processing]
Hint: macros [Processing]
/home/user/test.nim(1, 17) Warning: imported and not used: 'sequtils' [UnusedImport]
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_sequtils.nim
CC: test2.nim
Hint:  [Link]
Hint: 25264 LOC; 0.532 sec; 30.262MiB peakmem; Debug build; proj: test2.nim; out: test2 [SuccessX]
Hint: test2  [Exec]
@["b", "b", "b"]
$ Nim1.2.6/bin/nim --version
Nim Compiler Version 1.2.6 [Linux: amd64]
Compiled at 2021-03-24
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: f05ba3c667157f46f141ff700121c7aff86cf07a
active boot switches: -d:release

1.0.4:

$ nim c -r test2.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: test2 [Processing]
Hint: test [Processing]
Hint: sequtils [Processing]
Hint: macros [Processing]
test.nim(1, 17) Warning: imported and not used: 'sequtils' [UnusedImport]
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_sequtils.nim
CC: test2.nim
Hint:  [Link]
Hint: operation successful (25045 lines compiled; 0.523 sec total; 29.773MiB peakmem; Debug Build) [SuccessX]
Hint: test2  [Exec]
@["b", "b", "b"]
$ nim --version
Nim Compiler Version 1.0.4 [Linux: amd64]
Compiled at 2020-02-04
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release

Expected Output

No incorrect UnusedImport warning(s).

Additional Information

#17510 is related, but only appears after Nim 1.4.2 and doesn't need import foo as bar / export bar, just import foo / export foo.

Araq pushed a commit that referenced this issue Jun 26, 2021
…eclaredButNotUsed, etc; fix #17511, #17510, #14246 (without realModule) (#18362)

* {.used: symbol}

* add tests

* fix tests with --import

* --import works without giving spurious unused warnings

* new warning warnDuplicateModuleImport for `import foo; import foo`

* fix test, add resolveModuleAlias, use proper line info for module aliases

* fix spurious warnings

* fix deprecation msg for deprecated modules even with `import foo as bar`

* disable a test for i386 pending sorting XDeclaredButNotUsed errors

* UnusedImport now works with re-exported symbols

* fix typo [skip ci]

* ic support

* add genPNode to allow writing PNode-based compiler code similarly to `genAst`

* fix DuplicateModuleImport warning

* adjust test

* fixup

* fixup

* fixup

* fix after rebase

* fix for IC

* keep the proc inline, move the const out

* [skip ci] fix changelog

* experiment: remove calls to resolveModuleAlias

* followup

* fixup

* fix tests/modules/tselfimport.nim

* workaround tests/deprecated/tmodule1.nim

* fix properly

* simplify
PMunch pushed a commit to PMunch/Nim that referenced this issue Mar 28, 2022
…port, XDeclaredButNotUsed, etc; fix nim-lang#17511, nim-lang#17510, nim-lang#14246 (without realModule) (nim-lang#18362)

* {.used: symbol}

* add tests

* fix tests with --import

* --import works without giving spurious unused warnings

* new warning warnDuplicateModuleImport for `import foo; import foo`

* fix test, add resolveModuleAlias, use proper line info for module aliases

* fix spurious warnings

* fix deprecation msg for deprecated modules even with `import foo as bar`

* disable a test for i386 pending sorting XDeclaredButNotUsed errors

* UnusedImport now works with re-exported symbols

* fix typo [skip ci]

* ic support

* add genPNode to allow writing PNode-based compiler code similarly to `genAst`

* fix DuplicateModuleImport warning

* adjust test

* fixup

* fixup

* fixup

* fix after rebase

* fix for IC

* keep the proc inline, move the const out

* [skip ci] fix changelog

* experiment: remove calls to resolveModuleAlias

* followup

* fixup

* fix tests/modules/tselfimport.nim

* workaround tests/deprecated/tmodule1.nim

* fix properly

* simplify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment