Skip to content
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

Closed
wants to merge 14 commits into from

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented May 4, 2021

note

import sequtils as su2
export su2

B is the 2nd warning test2.nim(1, 11) Warning: imported and not used: 'test' [UnusedImport] which is a regression

this PR also introduces genPNode

it simplify writing PNode-based compiler code

see the full runnableExamples in the PR:

    let a = [1,2]
    let b = cache.getIdent("foo")
    let node: PNode = genPNode(c, a, b):
      for i in 0..<3:
        let b = @[i, 1]
        echo (a, b, i, "abc")

future work

(all pre-existing)

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)
Copy link
Member Author

@timotheecour timotheecour May 4, 2021

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)

Copy link
Member

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?

Copy link
Member Author

@timotheecour timotheecour May 5, 2021

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).

Copy link
Member

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?)

Copy link
Member Author

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

@timotheecour timotheecour changed the title {.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 May 5, 2021
@timotheecour timotheecour marked this pull request as ready for review May 5, 2021 05:48
template createModuleAliasImpl(ident): untyped =
createModuleAlias(realModule, nextSymId c.idgen, ident, realModule.info, c.config.options)
Copy link
Member Author

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

compiler/importer.nim Outdated Show resolved Hide resolved
@@ -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`
Copy link
Member

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?

Copy link
Member Author

@timotheecour timotheecour May 5, 2021

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

Copy link
Member

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.

Copy link
Collaborator

@alaviss alaviss left a 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 like discard 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?

@timotheecour
Copy link
Member Author

timotheecour commented May 5, 2021

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 {.used.} is controversial.
(and it can also be used to properly fix #11819 in future work)

@timotheecour
Copy link
Member Author

@Clyybber
Copy link
Contributor

Clyybber commented May 6, 2021

yes, it's needed, at least for fixing #13185

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.

@timotheecour
Copy link
Member Author

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

Araq pushed a commit that referenced this pull request Jun 26, 2021
…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
PMunch pushed a commit to PMunch/Nim that referenced this pull request Mar 28, 2022
…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
@stale
Copy link

stale bot commented May 31, 2022

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.

@stale stale bot added the stale Staled PR/issues; remove the label after fixing them label May 31, 2022
@stale stale bot closed this Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Staled PR/issues; remove the label after fixing them
Projects
None yet
4 participants