diff --git a/changelog.md b/changelog.md index f29a4c69db..749959e3c6 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index ce295c8b93..15a6254723 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -97,3 +97,4 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimFixedOwned") defineSymbol("nimHasStyleChecks") + defineSymbol("nimHasUsed") diff --git a/compiler/modules.nim b/compiler/modules.nim index 13845e6e9e..02b4d8ac42 100644 --- a/compiler/modules.nim +++ b/compiler/modules.nim @@ -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) diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 9e3976e733..12b0cff874 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -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, diff --git a/compiler/renderer.nim b/compiler/renderer.nim index cfc414aaf1..dfe66de035 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -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 diff --git a/compiler/sem.nim b/compiler/sem.nim index 119393e258..a13fd138b7 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -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) diff --git a/doc/manual.rst b/doc/manual.rst index f0945f78dd..1f8f23310b 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -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 diff --git a/tests/tools/dontmentionme.nim b/tests/tools/dontmentionme.nim new file mode 100644 index 0000000000..218823aca3 --- /dev/null +++ b/tests/tools/dontmentionme.nim @@ -0,0 +1,3 @@ +{.used.} + +proc nothing* = echo "nothing to see here" diff --git a/tests/tools/tunused_imports.nim b/tests/tools/tunused_imports.nim index c9cfcfe906..1c5732c83e 100644 --- a/tests/tools/tunused_imports.nim +++ b/tests/tools/tunused_imports.nim @@ -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