-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
{.used: symbol}
: fixes lots of issues with UnusedImport, XDeclaredButNotUsed, etc; fix #13185, #17511, #17510, #14246
#17938
Conversation
compiler/passes.nim
Outdated
of nkImportStmt: code = "import $1 as $2\n{.used: $2.}" % [quoted, name] | ||
of nkIncludeStmt: code = "include $1" % quoted | ||
else: assert false | ||
let node = parseString(code, graph.cache, graph.config, filename = "", line = 0, errorHandler = nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this pattern via parseString
simplifies code generation a lot in the compiler, we should use this more in the future instead of manually constructing the AST via PNode
apis, which is error prone, hard to read/write/edit.
(for the same reasons, quote/genAst simplifies macros, but here with raw PNode, it's even more pronounced)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What? That's terrible. Why not use an AST construction macro instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this so terrible? the traditional way based on manually constructing the AST via newNodeI
etc is arguably more complex to write and maintain; even with the above simple snippet ("import $1 as $2\n{.used: $2.}"
) it's hard to write the corresponding code manually.
Why not use an AST construction macro instead?
which one, or how would it work? if such macro doesn't exist yet i suggest to delay this to future work; this PR is already quite complex and solves many hard to fix long standing bugs (4 filed issues plus other issues I didn't bother writing bugs for).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well this style is simply not acceptable. Since it's a bugfix, why not fix the bug instead of pushing for idioms that are dubious at best. (How do we know "".dup(addQuoted(module))
respects Nim's quoting rules?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use an AST construction macro instead?
done
{.used: symbol}
: fixes lots of issues with UnusedImport, XDeclaredButNotUsed, etc{.used: symbol}
: fixes lots of issues with UnusedImport, XDeclaredButNotUsed, etc; fix #13185, #17511, #17510, #14246
template createModuleAliasImpl(ident): untyped = | ||
createModuleAlias(realModule, nextSymId c.idgen, ident, realModule.info, c.config.options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
realModule.info
would lead to wrong line info in error msgs
@@ -851,6 +851,8 @@ type | |||
TSym* {.acyclic.} = object of TIdObj # Keep in sync with PackedSym | |||
# proc and type instantiations are cached in the generic symbol | |||
case kind*: TSymKind | |||
of skModule: | |||
realModule*: PSym # for `createModuleAlias` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need IC support?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, but untestable beyond the existing ic tests (that still do pass) because of pre-existing issue with ic+export: #17943; new tests can be added in future work.
Also, manually serializing/deserializing the AST/symbols/various compiler structures as in done in packed_ast is IMO the wrong approach, it duplicates logic and adds significant complexity, and on top of that in my experience using ic makes compilation slower that a full recompilation. We should really explore alternatives:
- using a generic serializer/deserializer that works with any types including loopy structures, plus ability to customize in the rare places where it needs to; it's not hard and much more maintaniable
- the compiler server approach we discussed before, which IMO is the best (still need to share my code, what worked, and what didn't yet work); this definitely gives the best performance, almost as fast as
nim secret
despite using c backend
we should probably discuss this elsewhere though, separate topic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well since you bring it up:
using a generic serializer/deserializer that works with any types including loopy structures, plus ability to customize in the rare places where it needs to; it's not hard and much more maintaniable
Well if IC is already too slow, a more generic solution won't make it faster... Furthermore I regard the .rod file format as the future for Nim's tooling and it enforces us to be honest about phase ordering issues (no more unprincipled mutations possible). A compiler server accomplishes nothing comparable. We already have nimsuggest, it eats gigabytes of RAM and occasionally 100% CPU...
The "loopy structures" are harmful and cause many many bugs; don't write tooling to hide them, avoid them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{.used.}
now accepts symbols, e.g.{.used: mymodule.}
or{.used: myFun.}
. This allows using a clean approach instead of hacks/workarounds likediscard mymodule.someSymbol
that also don't always work
Is this needed to fix the bugs you listed? If no then shouldn't it be in a separated PR while this one focuses on the bugs you fixed?
yes, it's needed, at least for fixing #13185, and I don't think this extension to |
I don't think so, it can be fixed without introducing a new feature. IMO the PR needs to be split up into bugfix PR and feature addition PR+RFC. |
I prefer the way i did it in this PR but here you go: #17978 |
…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
…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
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
{.used.}
now accepts symbols, e.g.{.used: mymodule.}
or{.used: myFun.}
. This allows using a clean approach instead of hacks/workarounds likediscard mymodule.someSymbol
that also don't always work--import:mymodule
#13185 (UnusedImport should not be triggered for --import:mymodule
)import foo
vsimport foo as foo2
both callcreateModuleAliasImpl
; this allows keeping module modifications (egmoduleSym.flags
) isolated from the actual imported moduleUnusedImport
warning in presence ofimport foo as bar
#17511, see [1]DuplicateModuleImport
that triggers for eg onimport foo; import foo
import foo as bar
would not show deprecation msg for foo (before PR, onlyimport foo
would show the deprecation msg), eg with{.deprecated: "blah".}
UnusedImport
warnings regression from 1.4.2 to 1.5.1 #17510UnusedImport
warning with re-exported symbols or withtyAlias
(type Foo* = Bar
) #14246nimoutFull
which is critical for this kind of testnote
UnusedImport
warning in presence ofimport foo as bar
#17511 has 2 components A and B, I'm fixing both:A is the
test.nim(1, 17) Warning: imported and not used: 'sequtils' [UnusedImport]
(warning in the imported module referring to:B is the 2nd warning
test2.nim(1, 11) Warning: imported and not used: 'test' [UnusedImport]
which is a regressionthis PR also introduces
genPNode
it simplify writing PNode-based compiler code
see the full runnableExamples in the PR:
future work
(all pre-existing)
{.used: module.}
to fix UnusedImport warning is displayed for every runnable example #11819 in a better, more targeted wayswitch("warningAsError", "UnusedImport")
tonimStrictMode
now that it works more reliably