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
parent d8177a3980
commit 114da04cbb
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
no dependent Nim file changed. This feature can be overridden by
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

View File

@@ -97,3 +97,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimFixedOwned")
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)
graph.modules[result.position] = result
incl(result.flags, sfUsed)
initStrTable(result.tab)
strTableAdd(result.tab, result) # a module knows itself
strTableAdd(packSym.tab, result)

View File

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

View File

@@ -9,6 +9,11 @@
# 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
lexer, options, idents, strutils, ast, msgs, lineinfos

View File

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

View File

@@ -6276,6 +6276,17 @@ is particularly useful when the symbol was generated by a macro:
implementArithOps(int)
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

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".}
import net
import net, dontmentionme
echo AF_UNIX