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

Compute a better lint_node_id during expansion #87146

Merged
merged 5 commits into from
Jul 19, 2021

Conversation

Aaron1011
Copy link
Member

When we need to emit a lint at a macro invocation, we currently use the
NodeId of its parent definition (e.g. the enclosing function). This
means that any #[allow] / #[deny] attributes placed 'closer' to the
macro (e.g. on an enclosing block or statement) will have no effect.

This commit computes a better lint_node_id in InvocationCollector.
When we visit/flat_map an AST node, we assign it a NodeId (earlier
than we normally would), and store than NodeId in current
ExpansionData. When we collect a macro invocation, the current
lint_node_id gets cloned along with our ExpansionData, allowing it
to be used if we need to emit a lint later on.

This improves the handling of #[allow] / #[deny] for
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS and some asm!-related lints.
The 'legacy derive helpers' lint retains its current behavior
(I've inlined the now-removed lint_node_id function), since
there isn't an ExpansionData readily available.

@rust-highfive
Copy link
Collaborator

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 15, 2021
@Aaron1011
Copy link
Member Author

r? @petrochenkov

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Aaron1011
Copy link
Member Author

@petrochenkov: This PR is now ready for review

@petrochenkov
Copy link
Contributor

petrochenkov commented Jul 16, 2021

There are about 30 NodeIds in AST, but assign_id! is used 19 times in this PR.
This means only some NodeIds can be assigned to lint_node_id.
I'm not sure which subset of NodeIds is used for linting in this PR, and which subset should be used.

I think it's enough to track IDs that can be immediate parents of macro invocations (with "macro invocations" including uses of macro attributes).

If we are considering attributes, then some interesting situations may arise.
For example, does this example produce a warning?

#[allow(deprecated)]
#[deprecated_macro_attribute_equivalent_to_cfg_false]
struct S;

Apparently it does, because struct S; disappears before getting a meaningful NodeId (the ID is assigned after expanding all macro attributes), so struct S; is not an immediate parent of the macro attribute invocation, the containing module/crate is.
(Anyway, this snippet probably needs to be added as a test case.)

(I'll check which nodes can be immediate parents of macro invocations tomorrow.)

@petrochenkov
Copy link
Contributor

petrochenkov commented Jul 17, 2021

I think it's enough to track IDs that can be immediate parents of macro invocations

Looks like I was wrong, pretty much anything with a NodeId can be an immediate parent of a macro invocation (including stuff like path segments - seg<ty!()>, and excluding only the most basic nodes like lifetimes).

However, it's not necessary to track them all, we only need to track parent nodes (not necessarily immediate) supporting attributes like #[allow], i.e. only nodes listed in Annotatable (that's only 13 assign_id!s), tracking with higher granularity won't have any effect.

compiler/rustc_expand/src/expand.rs Show resolved Hide resolved
compiler/rustc_expand/src/expand.rs Outdated Show resolved Hide resolved
compiler/rustc_expand/src/expand.rs Outdated Show resolved Hide resolved
compiler/rustc_expand/src/expand.rs Outdated Show resolved Hide resolved
compiler/rustc_expand/src/expand.rs Show resolved Hide resolved
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2021
@Aaron1011
Copy link
Member Author

@petrochenkov: I've added the #[allow(deprecated)] test case, and removed the unnecessary assign_id! calls. See my reply to your comment about the handling of Stmt

@Aaron1011 Aaron1011 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 17, 2021
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Jul 17, 2021

📌 Commit cb862d5cac071a21735886dac9878ffb4562ae9f has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2021
@bors
Copy link
Contributor

bors commented Jul 17, 2021

☔ The latest upstream changes (presumably #86676) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 17, 2021
When we need to emit a lint at a macro invocation, we currently use the
`NodeId` of its parent definition (e.g. the enclosing function). This
means that any `#[allow]` / `#[deny]` attributes placed 'closer' to the
macro (e.g. on an enclosing block or statement) will have no effect.

This commit computes a better `lint_node_id` in `InvocationCollector`.
When we visit/flat_map an AST node, we assign it a `NodeId` (earlier
than we normally would), and store than `NodeId` in current
`ExpansionData`. When we collect a macro invocation, the current
`lint_node_id` gets cloned along with our `ExpansionData`, allowing it
to be used if we need to emit a lint later on.

This improves the handling of `#[allow]` / `#[deny]` for
`SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` and some `asm!`-related lints.
The 'legacy derive helpers' lint retains its current behavior
(I've inlined the now-removed `lint_node_id` function), since
there isn't an `ExpansionData` readily available.
@Aaron1011
Copy link
Member Author

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Jul 18, 2021

📌 Commit 1c1c794 has been approved by petrochenkov

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2021
@bors
Copy link
Contributor

bors commented Jul 18, 2021

⌛ Testing commit 1c1c794 with merge 9cc5500d72f84850fbde80fdf5520e2c97c1626d...

@bors
Copy link
Contributor

bors commented Jul 18, 2021

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 18, 2021
@Aaron1011
Copy link
Member Author

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2021
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented Jul 19, 2021

⌛ Testing commit 1c1c794 with merge 0ecff8c...

@bors
Copy link
Contributor

bors commented Jul 19, 2021

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing 0ecff8c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 19, 2021
@bors bors merged commit 0ecff8c into rust-lang:master Jul 19, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants