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

HirId-ification initiative #50928

Closed
zackmdavis opened this issue May 21, 2018 · 30 comments
Closed

HirId-ification initiative #50928

zackmdavis opened this issue May 21, 2018 · 30 comments
Labels
A-HIR Area: The high-level intermediate representation (HIR) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zackmdavis
Copy link
Member

HirIds were introduced in bc259ee (March 2017) as a replacement for ast::NodeIds after HIR lowering; this is said to be good for incremental compilation. The commentary for pull request #43740 (August 2017) says:

In the future the HirId should completely replace the NodeId in HIR nodes. [...] Ideally we convert more and more code from NodeId to HirId in the future so that there are no more NodeIds after HIR lowering anywhere.

The HIR chapter of the rustc development guide similarly says "while [NodeIds] are still in common use, they are being slowly phased out" (emphasis in original).

However, a drawback of the "gradually migrate over to the new way of doing things in the course of addressing other issues" strategy is that it's all too easy for the codebase to remain in a transitional state indefinitely. Thus, perhaps we should have this dedicated issue to track progress towards not using NodeIds after HIR lowering.

@michaelwoerister
Copy link
Member

Thanks, @zackmdavis!

However, a drawback of the "gradually migrate over to the new way of doing things in the course of addressing other issues" strategy is that it's all too easy for the codebase to remain in a transitional state indefinitely.

Yes, that's indeed what has been happening over the last year. We've done enough to make incremental compilation work and then pretty much stopped.

Btw, it might be a good idea to add a custom implementation of Hash for HirId, something like:

impl Hash for HirId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let data: u64 = unsafe { mem::transmute(*self) };
        state.write_u64(data);
    }
}

It would have to be tested if that actually speeds things up.

@cuviper cuviper added C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-HIR Area: The high-level intermediate representation (HIR) labels May 21, 2018
zackmdavis added a commit to zackmdavis/rust that referenced this issue May 28, 2018
Changing the `each_binding` utility method to take the `HirId` of a
binding pattern rather than its `NodeId` seems like a modest first step
in support of the `HirId`ification initiative rust-lang#50928. (The inspiration
for choosing this in particular came from the present author's previous
work on diagnostics issued during liveness analysis, which is the most
greatly affected module in this change.)
bors added a commit that referenced this issue May 28, 2018
…lwoerister

operate on `HirId` instead of `NodeId` in `hir::Pat::each_binding`, and consequences of that

See #50928 for motivation.

Questions—

 * Is #50928 actually a good idea as a plan of record, or is there some reason to keep `NodeId`s?
 * Are the uses of `find_node_for_hir_id` in this initial submission OK (see the FIXME comments)?
 * Can we bikeshed a better method names `struct_span_lint_hir` _&c._? (Coined in analogy to the `struct_span_lint_node` and `NodeId`, but it feels kind of semantically clunky.)

r? @michaelwoerister
@mark-i-m
Copy link
Member

mark-i-m commented Jun 3, 2018

Perhaps add tracking-issue label?

zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 2, 2018
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 2, 2018
zackmdavis added a commit to zackmdavis/rust that referenced this issue Jul 2, 2018
bors added a commit that referenced this issue Jul 2, 2018
HirId-ification, continued

Another incremental step towards the vision of #50928 (previously: #50929).

r? @michaelwoerister
@ljedrz
Copy link
Contributor

ljedrz commented Jan 9, 2019

I'm currently working on this.

@ljedrz ljedrz mentioned this issue Jan 13, 2019
37 tasks
JohnTitor added a commit to JohnTitor/rust that referenced this issue May 30, 2020
…, r=petrochenkov

Remove remaining calls to `as_local_node_id`

Split out from rust-lang#72552

cc rust-lang#50928
@marmeladema
Copy link
Contributor

Once #72777 and #72781 have landed, rustdoc should be free of NodeId.

In order to avoid re-introducing NodeId by mistake, would it be valuable to remove all NodeId related methods in hir map? The only remaining user of NodeId seems to be save_analysis and it can technically use the underlying Definitions object to access NodeIds.

RalfJung added a commit to RalfJung/rust that referenced this issue May 30, 2020
…f-id-from-node-id, r=petrochenkov

rustdoc: remove calls to `local_def_id_from_node_id`

rustdoc calls `local_def_id_from_node_id(CRATE_NODE_ID)` when it can just creates a top level `DefId` using `DefId::local(CRATE_DEF_INDEX)`.

cc rust-lang#50928

r? @petrochenkov
RalfJung added a commit to RalfJung/rust that referenced this issue May 30, 2020
…f-id-from-node-id, r=petrochenkov

rustdoc: remove calls to `local_def_id_from_node_id`

rustdoc calls `local_def_id_from_node_id(CRATE_NODE_ID)` when it can just creates a top level `DefId` using `DefId::local(CRATE_DEF_INDEX)`.

cc rust-lang#50928

r? @petrochenkov
RalfJung added a commit to RalfJung/rust that referenced this issue May 31, 2020
…str-path-error, r=petrochenkov

Use `LocalDefId` instead of `NodeId` in `resolve_str_path_error`

Together with rust-lang#72777 this should remove all uses of `NodeId` in `rustdoc`.

cc rust-lang#50928

r? @petrochenkov
RalfJung added a commit to RalfJung/rust that referenced this issue May 31, 2020
…str-path-error, r=petrochenkov

Use `LocalDefId` instead of `NodeId` in `resolve_str_path_error`

Together with rust-lang#72777 this should remove all uses of `NodeId` in `rustdoc`.

cc rust-lang#50928

r? @petrochenkov
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 4, 2020
…=Xanewok

save_analysis: work on HIR tree instead of AST

In order to reduce the uses of `NodeId`s in the compiler, `save_analysis` crate has been reworked to operate on the HIR tree instead of the AST.

cc rust-lang#50928
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 5, 2020
…pis, r=ecstatic-morse

Remove unsused `NodeId` related APIs in hir map

cc rust-lang#50928

r? @ecstatic-morse
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 5, 2020
…pis, r=ecstatic-morse

Remove unsused `NodeId` related APIs in hir map

cc rust-lang#50928

r? @ecstatic-morse
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 5, 2020
…pis, r=ecstatic-morse

Remove unsused `NodeId` related APIs in hir map

cc rust-lang#50928

r? @ecstatic-morse
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 7, 2020
…def-id, r=petrochenkov

Use `LocalDefId` directly in `Resolver::export_map`

This is to avoid the final conversion from `NodeId` to `HirId`
during call to `(clone|into)_outputs`

This brings down the post-lowering uses of `NodeId` down to 2 calls to convert the `trait_map`.

cc rust-lang#50928

r? @petrochenkov
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Jun 8, 2020
…def-id, r=petrochenkov

Use `LocalDefId` directly in `Resolver::export_map`

This is to avoid the final conversion from `NodeId` to `HirId`
during call to `(clone|into)_outputs`

This brings down the post-lowering uses of `NodeId` down to 2 calls to convert the `trait_map`.

cc rust-lang#50928

r? @petrochenkov
@marmeladema
Copy link
Contributor

marmeladema commented Jun 9, 2020

Small update:

I believe the solely remaining use of NodeIds post hir lowering is the conversion of the trait map:

  • let trait_map = self
    .trait_map
    .into_iter()
    .map(|(k, v)| {
    (
    definitions.node_id_to_hir_id(k),
    v.into_iter()
    .map(|tc| tc.map_import_ids(|id| definitions.node_id_to_hir_id(id)))
    .collect(),
    )
    })
    .collect();
  • trait_map: self
    .trait_map
    .iter()
    .map(|(&k, v)| {
    (
    self.definitions.node_id_to_hir_id(k),
    v.iter()
    .cloned()
    .map(|tc| {
    tc.map_import_ids(|id| self.definitions.node_id_to_hir_id(id))
    })
    .collect(),
    )
    })
    .collect(),

I see two approaches to solve this:

  1. Either keep the trait_map in the Resolver but move the conversion as part of the lowering and store the resulting hir trait_map into the current crate. That is not great because the mapping NodeId/HirId would still be required.

  2. Or build that hir trait_map directly as part of the lowering. I don't know if its easily doable or not yet.

What do you people think about all of this?

Manishearth added a commit to Manishearth/rust that referenced this issue Jun 20, 2020
…=petrochenkov

Pre-compute `LocalDefId` <-> `HirId` mappings and remove `NodeId` <-> `HirId` conversion APIs

cc rust-lang#50928

I don't know who is exactly the best person to review this.

r? @petrochenkov
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 20, 2020
…=petrochenkov

Pre-compute `LocalDefId` <-> `HirId` mappings and remove `NodeId` <-> `HirId` conversion APIs

cc rust-lang#50928

I don't know who is exactly the best person to review this.

r? @petrochenkov
@marmeladema
Copy link
Contributor

Now that #73291 has landed, the table/map to convert NodeId to/from HirId are gone. They have been replaced by direct LocalDefId to/from HirId table/map.

Next step (and possibly final step?) is to move the remaining API's involving NodeId to the Resolver which means that there will be no way of dealing with NodeId once the lowering is done.

Manishearth added a commit to Manishearth/rust that referenced this issue Jun 23, 2020
… r=petrochenkov

Move remaining `NodeId` APIs from `Definitions` to `Resolver`

Implements rust-lang#73291 (comment)

TL;DR: it moves all fields that are only needed during name resolution passes into the `Resolver` and keep the rest in `Definitions`. This effectively enforces that all references to `NodeId`s are gone once HIR lowering is completed.

After this, the only remaining work for rust-lang#50928 should be to adjust the dev guide.

r? @petrochenkov
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 23, 2020
… r=petrochenkov

Move remaining `NodeId` APIs from `Definitions` to `Resolver`

Implements rust-lang#73291 (comment)

TL;DR: it moves all fields that are only needed during name resolution passes into the `Resolver` and keep the rest in `Definitions`. This effectively enforces that all references to `NodeId`s are gone once HIR lowering is completed.

After this, the only remaining work for rust-lang#50928 should be to adjust the dev guide.

r? @petrochenkov
@petrochenkov
Copy link
Contributor

Closing as discussed in https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/.60HirId.60-ification.20initiative/near/201835747

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-HIR Area: The high-level intermediate representation (HIR) C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.