fixes #12029; finish the 'unused import' feature (#12064)

This commit is contained in:
Andreas Rumpf 2019-08-27 19:18:56 +02:00 • committed by GitHub
commit 114da04cbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 4 deletions

View file

@ -58,6 +58,10 @@
- The Nim compiler now does not recompile the Nim project via ``nim c -r`` if - The Nim compiler now does not recompile the Nim project via ``nim c -r`` if
no dependent Nim file changed. This feature can be overridden by no dependent Nim file changed. This feature can be overridden by
the ``--forceBuild`` command line option. the ``--forceBuild`` command line option.
- The Nim compiler now warns about unused module imports. You can use a
top level ``{.used.}`` pragma in the module that you want to be importable
without producing this warning.
### Compiler changes ### Compiler changes

View file

@ -97,3 +97,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimFixedOwned") defineSymbol("nimFixedOwned")
defineSymbol("nimHasStyleChecks") defineSymbol("nimHasStyleChecks")
defineSymbol("nimHasUsed")

View file

@ -50,7 +50,6 @@ proc partialInitModule(result: PSym; graph: ModuleGraph; fileIdx: FileIndex; fil
setLen(graph.modules, int(fileIdx) + 1) setLen(graph.modules, int(fileIdx) + 1)
graph.modules[result.position] = result graph.modules[result.position] = result
incl(result.flags, sfUsed)
initStrTable(result.tab) initStrTable(result.tab)
strTableAdd(result.tab, result) # a module knows itself strTableAdd(result.tab, result) # a module knows itself
strTableAdd(packSym.tab, result) strTableAdd(packSym.tab, result)

View file

@ -49,7 +49,7 @@ const
wDeprecated, wDeprecated,
wFloatChecks, wInfChecks, wNanChecks, wPragma, wEmit, wUnroll, wFloatChecks, wInfChecks, wNanChecks, wPragma, wEmit, wUnroll,
wLinearScanEnd, wPatterns, wTrMacros, wEffects, wNoForward, wReorder, wComputedGoto, wLinearScanEnd, wPatterns, wTrMacros, wEffects, wNoForward, wReorder, wComputedGoto,
wInjectStmt, wDeprecated, wExperimental, wThis} wInjectStmt, wDeprecated, wExperimental, wThis, wUsed}
lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl, lambdaPragmas* = {FirstCallConv..LastCallConv, wImportc, wExportc, wNodecl,
wNoSideEffect, wSideEffect, wNoreturn, wDynlib, wHeader, wNoSideEffect, wSideEffect, wNoreturn, wDynlib, wHeader,
wDeprecated, wExtern, wThread, wImportCpp, wImportObjC, wAsmNoStackFrame, wDeprecated, wExtern, wThread, wImportCpp, wImportObjC, wAsmNoStackFrame,

View file

@ -9,6 +9,11 @@
# This module implements the renderer of the standard Nim representation. # This module implements the renderer of the standard Nim representation.
when defined(nimHasUsed):
# 'import renderer' is so useful for debugging
# that Nim shouldn't produce a warning for that:
{.used.}
import import
lexer, options, idents, strutils, ast, msgs, lineinfos lexer, options, idents, strutils, ast, msgs, lineinfos

View file

@ -614,6 +614,7 @@ proc myProcess(context: PPassContext, n: PNode): PNode =
proc reportUnusedModules(c: PContext) = proc reportUnusedModules(c: PContext) =
for i in 0..high(c.unusedImports): for i in 0..high(c.unusedImports):
if sfUsed notin c.unusedImports[i][0].flags:
message(c.config, c.unusedImports[i][1], warnUnusedImportX, c.unusedImports[i][0].name.s) message(c.config, c.unusedImports[i][1], warnUnusedImportX, c.unusedImports[i][0].name.s)
proc myClose(graph: ModuleGraph; context: PPassContext, n: PNode): PNode = proc myClose(graph: ModuleGraph; context: PPassContext, n: PNode): PNode =

View file

@ -6276,6 +6276,17 @@ is particularly useful when the symbol was generated by a macro:
implementArithOps(int) implementArithOps(int)
echoAdd 3, 5 echoAdd 3, 5
``used`` can also be used as a top level statement to mark a module as "used".
This prevents the "Unused import" warning:
.. code-block:: nim
# module: debughelper.nim
when defined(nimHasUsed):
# 'import debughelper' is so useful for debugging
# that Nim shouldn't produce a warning for that import,
# even if currently unused:
{.used.}
experimental pragma experimental pragma

View file

@ -0,0 +1,3 @@
{.used.}
proc nothing* = echo "nothing to see here"

View file

@ -10,7 +10,7 @@ tunused_imports.nim(25, 8) Warning: imported and not used: 'strutils' [UnusedImp
{.warning: "BEGIN".} {.warning: "BEGIN".}
import net import net, dontmentionme
echo AF_UNIX echo AF_UNIX