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

Lower AST node id only once #130259

Merged
merged 3 commits into from
Oct 29, 2024
Merged

Conversation

adwinwhite
Copy link
Contributor

@adwinwhite adwinwhite commented Sep 12, 2024

Fixes #96346.

I basically followed the given instructions except the inline part.

lower_jump_destination can't reuse local existing HirId due to unknown name resolution result so I created an additional mapping for labels.

r? @cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Sep 12, 2024

r? @TaKO8Ki

rustbot has assigned @TaKO8Ki.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 12, 2024
@rustbot rustbot assigned cjgillot and unassigned TaKO8Ki Sep 12, 2024
@adwinwhite
Copy link
Contributor Author

r? @cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Sep 12, 2024

Could not assign reviewer from: cjgillot.
User(s) cjgillot are either the PR author, already assigned, or on vacation, and there are no other candidates.
Use r? to specify someone else to assign.

@rust-log-analyzer

This comment has been minimized.

@adwinwhite adwinwhite marked this pull request as draft September 12, 2024 07:41
@rust-log-analyzer

This comment has been minimized.

@adwinwhite adwinwhite marked this pull request as ready for review September 12, 2024 13:38
@adwinwhite
Copy link
Contributor Author

The test's output changed because I rearranged the order of some lowering to avoid duplicate lowering.

self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
}

pub(super) fn lower_block_with_hir_id(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inlined into its single call site?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I didn't notice there's only one call site.

@@ -502,16 +515,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
let if_expr = self.expr(span, if_kind);
let block = self.block_expr(self.arena.alloc(if_expr));
let span = self.lower_span(span.with_hi(cond.span.hi()));
let opt_label = self.lower_label(opt_label);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the limitation that the recording of label hir id is coupled with lower_label method.
So I have to lower label outside otherwise I need to introduce node_id and hir_id parameters to lower_expr_while_in_loop_scope.

@@ -154,7 +154,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
let mut ident = i.ident;
let vis_span = self.lower_span(i.vis.span);
let hir_id = self.lower_node_id(i.id);
let hir_id =
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use HirId::make_owner.

/// NodeIds of labelled nodes that are lowered inside the current HIR owner.
labelled_node_id_to_local_id: NodeMap<hir::ItemLocalId>,
/// NodeIds of identifier that are lowered inside the current HIR owner.
ident_to_local_id: NodeMap<hir::ItemLocalId>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a single map for both cases?

/// Take care not to call this method if the resulting `HirId` is then not
/// actually used in the HIR, as that would trigger an assertion in the
/// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
/// properly. Calling the method twice with the same `NodeId` is fine though.
/// properly. Calling the method twice with the same `NodeId` is also forbidden.
#[instrument(level = "debug", skip(self), ret)]
fn lower_node_id(&mut self, ast_node_id: NodeId) -> HirId {
assert_ne!(ast_node_id, DUMMY_NODE_ID);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep a check, gated behind #[cfg(debug_assertions)], that we don't call this twice on the same NodeId?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I didn't keep the check to avoid hashing cost. Thanks for reminding me of conditional compilation!

@cjgillot cjgillot 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 Sep 14, 2024
@adwinwhite adwinwhite requested a review from cjgillot September 15, 2024 04:56
@rustbot rustbot 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 Sep 15, 2024
@adwinwhite
Copy link
Contributor Author

@cjgillot May you have another look? There is no hurry of course.

compiler/rustc_ast_lowering/src/lib.rs Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Show resolved Hide resolved
compiler/rustc_ast_lowering/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 268 to 286
// All identifiers resolves to this canonical identifier share its `HirId`.
let binding_id = if canonical_id == p.id {
self.ident_and_label_to_local_id.insert(canonical_id, hir_id.local_id);
hir_id
} else {
hir::HirId {
owner: self.current_hir_id_owner,
local_id: self.ident_and_label_to_local_id[&canonical_id],
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you merge this with the previous match?

@cjgillot
Copy link
Contributor

Sorry for the very slow review.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 24, 2024

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

@cjgillot
Copy link
Contributor

r=me after rebase

@cjgillot cjgillot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 27, 2024
@adwinwhite
Copy link
Contributor Author

@bors r=cjgillot

@bors
Copy link
Contributor

bors commented Oct 28, 2024

@adwinwhite: 🔑 Insufficient privileges: Not in reviewers

@adwinwhite
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot 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 Oct 28, 2024
@cjgillot
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 28, 2024

📌 Commit e3bf50e has been approved by cjgillot

It is now in the queue for this repository.

@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 Oct 28, 2024
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Oct 28, 2024
…jgillot

Lower AST node id only once

Fixes rust-lang#96346.

I basically followed the given instructions except the inline part.

`lower_jump_destination` can't reuse local existing `HirId` due to unknown name resolution result so I created an additional mapping for labels.

r? `@cjgillot`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Oct 28, 2024
…jgillot

Lower AST node id only once

Fixes rust-lang#96346.

I basically followed the given instructions except the inline part.

`lower_jump_destination` can't reuse local existing `HirId` due to unknown name resolution result so I created an additional mapping for labels.

r? ``@cjgillot``
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 28, 2024
…kingjubilee

Rollup of 9 pull requests

Successful merges:

 - rust-lang#130259 (Lower AST node id only once)
 - rust-lang#131441 (Add a new trait `proc_macro::ToTokens`)
 - rust-lang#132247 (stable_mir: Directly use types from rustc_abi)
 - rust-lang#132249 (compiler: Add rustc_abi dependence to the compiler)
 - rust-lang#132255 (Add `LayoutS::is_uninhabited` and use it)
 - rust-lang#132258 ([rustdoc] Unify variant struct fields margins with struct fields)
 - rust-lang#132260 (cg_llvm: Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char`)
 - rust-lang#132261 (refactor: cleaner check to return None)
 - rust-lang#132271 (Updating Fuchsia platform-support documentation)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 29, 2024
…jgillot

Lower AST node id only once

Fixes rust-lang#96346.

I basically followed the given instructions except the inline part.

`lower_jump_destination` can't reuse local existing `HirId` due to unknown name resolution result so I created an additional mapping for labels.

r? ````@cjgillot````
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Oct 29, 2024
…jgillot

Lower AST node id only once

Fixes rust-lang#96346.

I basically followed the given instructions except the inline part.

`lower_jump_destination` can't reuse local existing `HirId` due to unknown name resolution result so I created an additional mapping for labels.

r? `````@cjgillot`````
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 29, 2024
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#130259 (Lower AST node id only once)
 - rust-lang#131441 (Add a new trait `proc_macro::ToTokens`)
 - rust-lang#132247 (stable_mir: Directly use types from rustc_abi)
 - rust-lang#132249 (compiler: Add rustc_abi dependence to the compiler)
 - rust-lang#132255 (Add `LayoutData::is_uninhabited` and use it)
 - rust-lang#132258 ([rustdoc] Unify variant struct fields margins with struct fields)
 - rust-lang#132260 (cg_llvm: Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char`)
 - rust-lang#132261 (refactor: cleaner check to return None)
 - rust-lang#132271 (Updating Fuchsia platform-support documentation)
 - rust-lang#132295 (fixed wast version was released, remove randomization exemption)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 29, 2024
…kingjubilee

Rollup of 9 pull requests

Successful merges:

 - rust-lang#130259 (Lower AST node id only once)
 - rust-lang#131441 (Add a new trait `proc_macro::ToTokens`)
 - rust-lang#132247 (stable_mir: Directly use types from rustc_abi)
 - rust-lang#132249 (compiler: Add rustc_abi dependence to the compiler)
 - rust-lang#132255 (Add `LayoutData::is_uninhabited` and use it)
 - rust-lang#132258 ([rustdoc] Unify variant struct fields margins with struct fields)
 - rust-lang#132260 (cg_llvm: Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char`)
 - rust-lang#132261 (refactor: cleaner check to return None)
 - rust-lang#132271 (Updating Fuchsia platform-support documentation)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a24b377 into rust-lang:master Oct 29, 2024
6 checks passed
@rustbot rustbot added this to the 1.84.0 milestone Oct 29, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 29, 2024
Rollup merge of rust-lang#130259 - adwinwhite:lower-node-id-once, r=cjgillot

Lower AST node id only once

Fixes rust-lang#96346.

I basically followed the given instructions except the inline part.

`lower_jump_destination` can't reuse local existing `HirId` due to unknown name resolution result so I created an additional mapping for labels.

r? ```@cjgillot```
@Kobzol
Copy link
Contributor

Kobzol commented Oct 30, 2024

Just FYI, this PR had a nice effect on the performance of the compiler :) #132277 (comment) Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forbid lowering the same NodeId multiple times
7 participants