Skip to content

Commit 9d6c95d

Browse files
authored
Rollup merge of rust-lang#139772 - nnethercote:rm-hir-Map, r=Zalathar
Remove `hir::Map` A follow-up to rust-lang#139232. r? `@Zalathar`
2 parents 36df548 + 9734e44 commit 9d6c95d

File tree

8 files changed

+36
-65
lines changed

8 files changed

+36
-65
lines changed

compiler/rustc_hir/src/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! 1. **Shallow visit**: Get a simple callback for every item (or item-like thing) in the HIR.
77
//! - Example: find all items with a `#[foo]` attribute on them.
88
//! - How: Use the `hir_crate_items` or `hir_module_items` query to traverse over item-like ids
9-
//! (ItemId, TraitItemId, etc.) and use tcx.def_kind and `tcx.hir().item*(id)` to filter and
9+
//! (ItemId, TraitItemId, etc.) and use tcx.def_kind and `tcx.hir_item*(id)` to filter and
1010
//! access actual item-like thing, respectively.
1111
//! - Pro: Efficient; just walks the lists of item ids and gives users control whether to access
1212
//! the hir_owners themselves or not.

compiler/rustc_middle/src/hir/map.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! This module used to contain a type called `Map`. That type has since been
2+
//! eliminated, and all its methods are now on `TyCtxt`. But the module name
3+
//! stays as `map` because there isn't an obviously better name for it.
4+
15
use rustc_abi::ExternAbi;
26
use rustc_ast::visit::{VisitorResult, walk_list};
37
use rustc_data_structures::fingerprint::Fingerprint;
@@ -18,16 +22,6 @@ use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
1822
use crate::query::LocalCrate;
1923
use crate::ty::TyCtxt;
2024

21-
// FIXME: the structure was necessary in the past but now it
22-
// only serves as "namespace" for HIR-related methods, and can be
23-
// removed if all the methods are reasonably renamed and moved to tcx
24-
// (https://github.com/rust-lang/rust/pull/118256#issuecomment-1826442834).
25-
#[allow(unused)] // FIXME: temporary
26-
#[derive(Copy, Clone)]
27-
pub struct Map<'hir> {
28-
pub(super) tcx: TyCtxt<'hir>,
29-
}
30-
3125
/// An iterator that walks up the ancestor tree of a given `HirId`.
3226
/// Constructed using `tcx.hir_parent_iter(hir_id)`.
3327
struct ParentHirIterator<'tcx> {
@@ -335,7 +329,7 @@ impl<'tcx> TyCtxt<'tcx> {
335329

336330
/// Returns an iterator of the `DefId`s for all body-owners in this
337331
/// crate. If you would prefer to iterate over the bodies
338-
/// themselves, you can do `self.hir().krate().body_ids.iter()`.
332+
/// themselves, you can do `self.hir_crate(()).body_ids.iter()`.
339333
#[inline]
340334
pub fn hir_body_owners(self) -> impl Iterator<Item = LocalDefId> {
341335
self.hir_crate_items(()).body_owners.iter().copied()

compiler/rustc_middle/src/hir/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ impl ModuleItems {
116116
}
117117

118118
impl<'tcx> TyCtxt<'tcx> {
119-
#[inline(always)]
120-
pub fn hir(self) -> map::Map<'tcx> {
121-
map::Map { tcx: self }
122-
}
123-
124119
pub fn parent_module(self, id: HirId) -> LocalModDefId {
125120
if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod {
126121
LocalModDefId::new_unchecked(id.owner.def_id)

compiler/rustc_middle/src/query/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ rustc_queries! {
161161

162162
/// Represents crate as a whole (as distinct from the top-level crate module).
163163
///
164-
/// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir_crate()`),
165-
/// we will have to assume that any change means that you need to be recompiled.
166-
/// This is because the `hir_crate` query gives you access to all other items.
167-
/// To avoid this fate, do not call `tcx.hir_crate()`; instead,
168-
/// prefer wrappers like [`TyCtxt::hir_visit_all_item_likes_in_crate`].
164+
/// If you call `tcx.hir_crate(())` we will have to assume that any change
165+
/// means that you need to be recompiled. This is because the `hir_crate`
166+
/// query gives you access to all other items. To avoid this fate, do not
167+
/// call `tcx.hir_crate(())`; instead, prefer wrappers like
168+
/// [`TyCtxt::hir_visit_all_item_likes_in_crate`].
169169
query hir_crate(key: ()) -> &'tcx Crate<'tcx> {
170170
arena_cache
171171
eval_always
@@ -197,15 +197,15 @@ rustc_queries! {
197197

198198
/// Gives access to the HIR node's parent for the HIR owner `key`.
199199
///
200-
/// This can be conveniently accessed by methods on `tcx.hir()`.
200+
/// This can be conveniently accessed by `tcx.hir_*` methods.
201201
/// Avoid calling this query directly.
202202
query hir_owner_parent(key: hir::OwnerId) -> hir::HirId {
203203
desc { |tcx| "getting HIR parent of `{}`", tcx.def_path_str(key) }
204204
}
205205

206206
/// Gives access to the HIR nodes and bodies inside `key` if it's a HIR owner.
207207
///
208-
/// This can be conveniently accessed by methods on `tcx.hir()`.
208+
/// This can be conveniently accessed by `tcx.hir_*` methods.
209209
/// Avoid calling this query directly.
210210
query opt_hir_owner_nodes(key: LocalDefId) -> Option<&'tcx hir::OwnerNodes<'tcx>> {
211211
desc { |tcx| "getting HIR owner items in `{}`", tcx.def_path_str(key) }
@@ -214,7 +214,7 @@ rustc_queries! {
214214

215215
/// Gives access to the HIR attributes inside the HIR owner `key`.
216216
///
217-
/// This can be conveniently accessed by methods on `tcx.hir()`.
217+
/// This can be conveniently accessed by `tcx.hir_*` methods.
218218
/// Avoid calling this query directly.
219219
query hir_attr_map(key: hir::OwnerId) -> &'tcx hir::AttributeMap<'tcx> {
220220
desc { |tcx| "getting HIR owner attributes in `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,7 @@ impl<'tcx> TyCtxt<'tcx> {
21472147
return vec![];
21482148
};
21492149

2150-
let mut v = TraitObjectVisitor(vec![], self.hir());
2150+
let mut v = TraitObjectVisitor(vec![]);
21512151
v.visit_ty_unambig(hir_output);
21522152
v.0
21532153
}
@@ -2160,7 +2160,7 @@ impl<'tcx> TyCtxt<'tcx> {
21602160
scope_def_id: LocalDefId,
21612161
) -> Option<(Vec<&'tcx hir::Ty<'tcx>>, Span, Option<Span>)> {
21622162
let hir_id = self.local_def_id_to_hir_id(scope_def_id);
2163-
let mut v = TraitObjectVisitor(vec![], self.hir());
2163+
let mut v = TraitObjectVisitor(vec![]);
21642164
// when the return type is a type alias
21652165
if let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = self.hir_fn_decl_by_hir_id(hir_id)
21662166
&& let hir::TyKind::Path(hir::QPath::Resolved(

compiler/rustc_middle/src/ty/diagnostics.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ pub fn suggest_constraining_type_params<'a>(
571571
}
572572

573573
/// Collect al types that have an implicit `'static` obligation that we could suggest `'_` for.
574-
pub struct TraitObjectVisitor<'tcx>(pub Vec<&'tcx hir::Ty<'tcx>>, pub crate::hir::map::Map<'tcx>);
574+
pub(crate) struct TraitObjectVisitor<'tcx>(pub(crate) Vec<&'tcx hir::Ty<'tcx>>);
575575

576576
impl<'v> hir::intravisit::Visitor<'v> for TraitObjectVisitor<'v> {
577577
fn visit_ty(&mut self, ty: &'v hir::Ty<'v, AmbigArg>) {
@@ -592,18 +592,6 @@ impl<'v> hir::intravisit::Visitor<'v> for TraitObjectVisitor<'v> {
592592
}
593593
}
594594

595-
/// Collect al types that have an implicit `'static` obligation that we could suggest `'_` for.
596-
pub struct StaticLifetimeVisitor<'tcx>(pub Vec<Span>, pub crate::hir::map::Map<'tcx>);
597-
598-
impl<'v> hir::intravisit::Visitor<'v> for StaticLifetimeVisitor<'v> {
599-
fn visit_lifetime(&mut self, lt: &'v hir::Lifetime) {
600-
if let hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Static = lt.res
601-
{
602-
self.0.push(lt.ident.span);
603-
}
604-
}
605-
}
606-
607595
pub struct IsSuggestableVisitor<'tcx> {
608596
tcx: TyCtxt<'tcx>,
609597
infer_suggestable: bool,

src/doc/rustc-dev-guide/src/appendix/glossary.md

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Term | Meaning
3131
<span id="generics">generics</span> | The list of generic parameters defined on an item. There are three kinds of generic parameters: Type, lifetime and const parameters.
3232
<span id="hir">HIR</span> | The _high-level [IR](#ir)_, created by lowering and desugaring the AST. ([see more](../hir.md))
3333
<span id="hir-id">`HirId`</span> | Identifies a particular node in the HIR by combining a def-id with an "intra-definition offset". See [the HIR chapter for more](../hir.md#identifiers-in-the-hir).
34-
<span id="hir-map">HIR map</span> | The HIR map, accessible via `tcx.hir()`, allows you to quickly navigate the HIR and convert between various forms of identifiers.
3534
<span id="ice">ICE</span> | Short for _internal compiler error_, this is when the compiler crashes.
3635
<span id="ich">ICH</span> | Short for _incremental compilation hash_, these are used as fingerprints for things such as HIR and crate metadata, to check if changes have been made. This is useful in incremental compilation to see if part of a crate has changed and should be recompiled.
3736
<span id="infcx">`infcx`</span> | The type inference context (`InferCtxt`). (see `rustc_middle::infer`)

src/doc/rustc-dev-guide/src/hir.md

+19-24
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The HIR uses a bunch of different identifiers that coexist and serve different p
100100
a wrapper around a [`HirId`]. For more info about HIR bodies, please refer to the
101101
[HIR chapter][hir-bodies].
102102

103-
These identifiers can be converted into one another through the [HIR map][map].
103+
These identifiers can be converted into one another through the `TyCtxt`.
104104

105105
[`DefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefId.html
106106
[`LocalDefId`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.LocalDefId.html
@@ -110,30 +110,24 @@ These identifiers can be converted into one another through the [HIR map][map].
110110
[`CrateNum`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.CrateNum.html
111111
[`DefIndex`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def_id/struct.DefIndex.html
112112
[`Body`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Body.html
113-
[hir-map]: ./hir.md#the-hir-map
114113
[hir-bodies]: ./hir.md#hir-bodies
115-
[map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
116114

117-
## The HIR Map
115+
## HIR Operations
118116

119117
Most of the time when you are working with the HIR, you will do so via
120-
the **HIR Map**, accessible in the tcx via [`tcx.hir()`] (and defined in
121-
the [`hir::map`] module). The [HIR map] contains a [number of methods] to
122-
convert between IDs of various kinds and to lookup data associated
123-
with a HIR node.
118+
`TyCtxt`. It contains a number of methods, defined in the `hir::map` module and
119+
mostly prefixed with `hir_`, to convert between IDs of various kinds and to
120+
lookup data associated with a HIR node.
124121

125-
[`tcx.hir()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.hir
126-
[`hir::map`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/index.html
127-
[HIR map]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html
128-
[number of methods]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#methods
122+
[`TyCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html
129123

130124
For example, if you have a [`LocalDefId`], and you would like to convert it
131-
to a [`HirId`], you can use [`tcx.hir().local_def_id_to_hir_id(def_id)`][local_def_id_to_hir_id].
125+
to a [`HirId`], you can use [`tcx.local_def_id_to_hir_id(def_id)`][local_def_id_to_hir_id].
132126
You need a `LocalDefId`, rather than a `DefId`, since only local items have HIR nodes.
133127

134-
[local_def_id_to_hir_id]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.local_def_id_to_hir_id
128+
[local_def_id_to_hir_id]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.local_def_id_to_hir_id
135129

136-
Similarly, you can use [`tcx.hir().find(n)`][find] to lookup the node for a
130+
Similarly, you can use [`tcx.hir_node(n)`][hir_node] to lookup the node for a
137131
[`HirId`]. This returns a `Option<Node<'hir>>`, where [`Node`] is an enum
138132
defined in the map. By matching on this, you can find out what sort of
139133
node the `HirId` referred to and also get a pointer to the data
@@ -142,26 +136,27 @@ that `n` must be some HIR expression, you can do
142136
[`tcx.hir_expect_expr(n)`][expect_expr], which will extract and return the
143137
[`&hir::Expr`][Expr], panicking if `n` is not in fact an expression.
144138

145-
[find]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.find
139+
[hir_node]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.hir_node
146140
[`Node`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.Node.html
147141
[expect_expr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.expect_expr
148142
[Expr]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Expr.html
149143

150-
Finally, you can use the HIR map to find the parents of nodes, via
151-
calls like [`tcx.hir().get_parent(n)`][get_parent].
144+
Finally, you can find the parents of nodes, via
145+
calls like [`tcx.parent_hir_node(n)`][parent_hir_node].
146+
147+
[get_parent_item]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.parent_hir_node
152148

153-
[get_parent]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.get_parent
154149

155150
## HIR Bodies
156151

157152
A [`rustc_hir::Body`] represents some kind of executable code, such as the body
158153
of a function/closure or the definition of a constant. Bodies are
159154
associated with an **owner**, which is typically some kind of item
160155
(e.g. an `fn()` or `const`), but could also be a closure expression
161-
(e.g. `|x, y| x + y`). You can use the HIR map to find the body
162-
associated with a given def-id ([`maybe_body_owned_by`]) or to find
163-
the owner of a body ([`body_owner_def_id`]).
156+
(e.g. `|x, y| x + y`). You can use the `TyCtxt` to find the body
157+
associated with a given def-id ([`hir_maybe_body_owned_by`]) or to find
158+
the owner of a body ([`hir_body_owner_def_id`]).
164159

165160
[`rustc_hir::Body`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.Body.html
166-
[`maybe_body_owned_by`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.maybe_body_owned_by
167-
[`body_owner_def_id`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/hir/map/struct.Map.html#method.body_owner_def_id
161+
[`hir_maybe_body_owned_by`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.hir_maybe_body_owned_by
162+
[`hir_body_owner_def_id`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.hir_body_owner_def_id

0 commit comments

Comments
 (0)