From fa1819eb2d3b01d103a27f94ec0fe6e7bebc9359 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 11 Oct 2024 13:00:05 +0300 Subject: [PATCH] make linter use lineinfo to check originating package (#24270) fixes #24269, refs #20095 Instead of checking the package of the *used sym* to determine whether a stylecheck should trigger, we check the package of the lineinfo instead. Before #20095 this checked for the current compilation context module instead which caused issues with generic procs, but the lineinfo should more closely match the AST. I figured this might cause issues with includes etc but the foreign package test specifically tests for an include and passes, so maybe the package determining logic accounts for this already. This still might not be the correct logic, I'm not too familiar with the package handling in the compiler. Package PRs, both merged: - json_rpc: https://github.com/status-im/nim-json-rpc/pull/226 - json_serialization: https://github.com/status-im/nim-json-serialization/pull/99 (cherry picked from commit aaf6c408c69f881b4e7e95512dcc9c9b0a02bf6d) --- compiler/linter.nim | 8 ++++---- compiler/packages.nim | 8 ++++++++ tests/stdlib/tdeques.nim | 2 +- tests/stylecheck/tforeign_package.nim | 11 ++++++++++- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/compiler/linter.nim b/compiler/linter.nim index a80c377e95..b8358f5de9 100644 --- a/compiler/linter.nim +++ b/compiler/linter.nim @@ -12,7 +12,7 @@ import std/strutils from std/sugar import dup -import options, ast, msgs, idents, lineinfos, wordrecg, astmsgs, semdata, packages +import options, ast, msgs, idents, lineinfos, wordrecg, astmsgs, semdata, packages, modulegraphs export packages const @@ -97,7 +97,7 @@ template styleCheckDef*(ctx: PContext; info: TLineInfo; sym: PSym; k: TSymKind) if optStyleCheck in ctx.config.options and # ignore if styleChecks are off {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # check only if hint/error is enabled hintName in ctx.config.notes and # ignore if name checks are not requested - ctx.config.belongsToProjectPackage(sym) and # ignore foreign packages + ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)) and # ignore foreign packages optStyleUsages notin ctx.config.globalOptions and # ignore if requested to only check name usage sym.kind != skResult and # ignore `result` sym.kind != skTemp and # ignore temporary variables created by the compiler @@ -138,7 +138,7 @@ template styleCheckUse*(ctx: PContext; info: TLineInfo; sym: PSym) = ## Check symbol uses match their definition's style. if {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # ignore if styleChecks are off hintName in ctx.config.notes and # ignore if name checks are not requested - ctx.config.belongsToProjectPackage(sym) and # ignore foreign packages + ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)) and # ignore foreign packages sym.kind != skTemp and # ignore temporary variables created by the compiler sym.name.s[0] in Letters and # ignore operators TODO: what about unicode symbols??? sfAnon notin sym.flags: # ignore temporary variables created by the compiler @@ -154,5 +154,5 @@ template checkPragmaUse*(ctx: PContext; info: TLineInfo; w: TSpecialWord; pragma ## Note: This only applies to builtin pragmas, not user pragmas. if {optStyleHint, optStyleError} * ctx.config.globalOptions != {} and # ignore if styleChecks are off hintName in ctx.config.notes and # ignore if name checks are not requested - (sym != nil and ctx.config.belongsToProjectPackage(sym)): # ignore foreign packages + ctx.config.belongsToProjectPackageMaybeNil(getModule(ctx.graph, info.fileIndex)): # ignore foreign packages checkPragmaUseImpl(ctx.config, info, w, pragmaName) diff --git a/compiler/packages.nim b/compiler/packages.nim index bb54d61546..63879acd26 100644 --- a/compiler/packages.nim +++ b/compiler/packages.nim @@ -51,3 +51,11 @@ func belongsToProjectPackage*(conf: ConfigRef, sym: PSym): bool = ## See Also: ## * `modulegraphs.belongsToStdlib` conf.mainPackageId == sym.getPackageId + +func belongsToProjectPackageMaybeNil*(conf: ConfigRef, sym: PSym): bool = + ## Return whether the symbol belongs to the project's package. + ## Returns `false` if `sym` is nil. + ## + ## See Also: + ## * `modulegraphs.belongsToStdlib` + sym != nil and conf.mainPackageId == sym.getPackageId diff --git a/tests/stdlib/tdeques.nim b/tests/stdlib/tdeques.nim index 39ff996d11..b8eca55e96 100644 --- a/tests/stdlib/tdeques.nim +++ b/tests/stdlib/tdeques.nim @@ -224,7 +224,7 @@ proc main() = block: var a, b = initDeque[int]() - for i in countDown(100, 1): + for i in countdown(100, 1): a.addFirst(i) for i in 1..100: b.addLast(i) diff --git a/tests/stylecheck/tforeign_package.nim b/tests/stylecheck/tforeign_package.nim index 8594ad802e..eb55851398 100644 --- a/tests/stylecheck/tforeign_package.nim +++ b/tests/stylecheck/tforeign_package.nim @@ -13,4 +13,13 @@ import foreign_package/foreign_package # - user pragma usage violations # - definition violations in foreign packages are ignored # - usage violations in foreign packages are ignored -genericProc[int]() +generic_proc[int]() +# issue #24269, stdlib: +proc c(_: openArray[int]) = discard +static: + doAssert compiles(generic_proc[int]()) + doAssert not compiles(genericProc[int]()) + doAssert not (compiles do: + proc c(_: openarray[int]) = discard) + doAssert (compiles do: + proc d(_: openArray[int]) = discard)