Skip to content

Commit c922857

Browse files
committed
Auto merge of #79318 - cjgillot:fitem, r=lcnr
Store HIR ForeignItem in a side table In a similar fashion to Item, ImplItem and TraitItem.
2 parents 361543d + d6b22fa commit c922857

File tree

56 files changed

+400
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+400
-226
lines changed

compiler/rustc_ast_lowering/src/item.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4343
items: BTreeSet::new(),
4444
trait_items: BTreeSet::new(),
4545
impl_items: BTreeSet::new(),
46+
foreign_items: BTreeSet::new(),
4647
},
4748
);
4849

@@ -105,6 +106,18 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
105106

106107
visit::walk_assoc_item(self, item, ctxt);
107108
}
109+
110+
fn visit_foreign_item(&mut self, item: &'a ForeignItem) {
111+
self.lctx.allocate_hir_id_counter(item.id);
112+
self.lctx.with_hir_id_owner(item.id, |lctx| {
113+
let hir_item = lctx.lower_foreign_item(item);
114+
let id = hir::ForeignItemId { hir_id: hir_item.hir_id };
115+
lctx.foreign_items.insert(id, hir_item);
116+
lctx.modules.get_mut(&lctx.current_module).unwrap().foreign_items.insert(id);
117+
});
118+
119+
visit::walk_foreign_item(self, item);
120+
}
108121
}
109122

110123
impl<'hir> LoweringContext<'_, 'hir> {
@@ -304,7 +317,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
304317
})
305318
}
306319
ItemKind::Mod(ref m) => hir::ItemKind::Mod(self.lower_mod(m)),
307-
ItemKind::ForeignMod(ref nm) => hir::ItemKind::ForeignMod(self.lower_foreign_mod(nm)),
320+
ItemKind::ForeignMod(ref fm) => hir::ItemKind::ForeignMod {
321+
abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)),
322+
items: self
323+
.arena
324+
.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item_ref(x))),
325+
},
308326
ItemKind::GlobalAsm(ref ga) => hir::ItemKind::GlobalAsm(self.lower_global_asm(ga)),
309327
ItemKind::TyAlias(_, ref gen, _, Some(ref ty)) => {
310328
// We lower
@@ -704,10 +722,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
704722
}
705723
}
706724

707-
fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod<'hir> {
708-
hir::ForeignMod {
709-
abi: fm.abi.map_or(abi::Abi::C, |abi| self.lower_abi(abi)),
710-
items: self.arena.alloc_from_iter(fm.items.iter().map(|x| self.lower_foreign_item(x))),
725+
fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> {
726+
hir::ForeignItemRef {
727+
id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) },
728+
ident: i.ident,
729+
span: i.span,
730+
vis: self.lower_visibility(&i.vis, Some(i.id)),
711731
}
712732
}
713733

compiler/rustc_ast_lowering/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct LoweringContext<'a, 'hir: 'a> {
101101

102102
trait_items: BTreeMap<hir::TraitItemId, hir::TraitItem<'hir>>,
103103
impl_items: BTreeMap<hir::ImplItemId, hir::ImplItem<'hir>>,
104+
foreign_items: BTreeMap<hir::ForeignItemId, hir::ForeignItem<'hir>>,
104105
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
105106
exported_macros: Vec<hir::MacroDef<'hir>>,
106107
non_exported_macro_attrs: Vec<ast::Attribute>,
@@ -298,6 +299,7 @@ pub fn lower_crate<'a, 'hir>(
298299
items: BTreeMap::new(),
299300
trait_items: BTreeMap::new(),
300301
impl_items: BTreeMap::new(),
302+
foreign_items: BTreeMap::new(),
301303
bodies: BTreeMap::new(),
302304
trait_impls: BTreeMap::new(),
303305
modules: BTreeMap::new(),
@@ -485,6 +487,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
485487
visit::walk_assoc_item(self, item, ctxt);
486488
}
487489

490+
fn visit_foreign_item(&mut self, item: &'tcx ForeignItem) {
491+
self.lctx.allocate_hir_id_counter(item.id);
492+
visit::walk_foreign_item(self, item);
493+
}
494+
488495
fn visit_ty(&mut self, t: &'tcx Ty) {
489496
match t.kind {
490497
// Mirrors the case in visit::walk_ty
@@ -548,6 +555,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
548555
items: self.items,
549556
trait_items: self.trait_items,
550557
impl_items: self.impl_items,
558+
foreign_items: self.foreign_items,
551559
bodies: self.bodies,
552560
body_ids,
553561
trait_impls: self.trait_impls,

compiler/rustc_hir/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ macro_rules! arena_types {
2929
[] field_pat: rustc_hir::FieldPat<$tcx>,
3030
[] fn_decl: rustc_hir::FnDecl<$tcx>,
3131
[] foreign_item: rustc_hir::ForeignItem<$tcx>,
32+
[few] foreign_item_ref: rustc_hir::ForeignItemRef<$tcx>,
3233
[] impl_item_ref: rustc_hir::ImplItemRef<$tcx>,
3334
[few] inline_asm: rustc_hir::InlineAsm<$tcx>,
3435
[few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,

compiler/rustc_hir/src/hir.rs

+42-10
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ pub struct ModuleItems {
579579
pub items: BTreeSet<HirId>,
580580
pub trait_items: BTreeSet<TraitItemId>,
581581
pub impl_items: BTreeSet<ImplItemId>,
582+
pub foreign_items: BTreeSet<ForeignItemId>,
582583
}
583584

584585
/// A type representing only the top-level module.
@@ -612,6 +613,7 @@ pub struct Crate<'hir> {
612613

613614
pub trait_items: BTreeMap<TraitItemId, TraitItem<'hir>>,
614615
pub impl_items: BTreeMap<ImplItemId, ImplItem<'hir>>,
616+
pub foreign_items: BTreeMap<ForeignItemId, ForeignItem<'hir>>,
615617
pub bodies: BTreeMap<BodyId, Body<'hir>>,
616618
pub trait_impls: BTreeMap<DefId, Vec<HirId>>,
617619

@@ -644,6 +646,10 @@ impl Crate<'hir> {
644646
&self.impl_items[&id]
645647
}
646648

649+
pub fn foreign_item(&self, id: ForeignItemId) -> &ForeignItem<'hir> {
650+
&self.foreign_items[&id]
651+
}
652+
647653
pub fn body(&self, id: BodyId) -> &Body<'hir> {
648654
&self.bodies[&id]
649655
}
@@ -673,6 +679,10 @@ impl Crate<'_> {
673679
for impl_item in self.impl_items.values() {
674680
visitor.visit_impl_item(impl_item);
675681
}
682+
683+
for foreign_item in self.foreign_items.values() {
684+
visitor.visit_foreign_item(foreign_item);
685+
}
676686
}
677687

678688
/// A parallel version of `visit_all_item_likes`.
@@ -695,6 +705,11 @@ impl Crate<'_> {
695705
par_for_each_in(&self.impl_items, |(_, impl_item)| {
696706
visitor.visit_impl_item(impl_item);
697707
});
708+
},
709+
{
710+
par_for_each_in(&self.foreign_items, |(_, foreign_item)| {
711+
visitor.visit_foreign_item(foreign_item);
712+
});
698713
}
699714
);
700715
}
@@ -1840,7 +1855,7 @@ pub struct FnSig<'hir> {
18401855
}
18411856

18421857
// The bodies for items are stored "out of line", in a separate
1843-
// hashmap in the `Crate`. Here we just record the node-id of the item
1858+
// hashmap in the `Crate`. Here we just record the hir-id of the item
18441859
// so it can fetched later.
18451860
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
18461861
pub struct TraitItemId {
@@ -1884,7 +1899,7 @@ pub enum TraitItemKind<'hir> {
18841899
}
18851900

18861901
// The bodies for items are stored "out of line", in a separate
1887-
// hashmap in the `Crate`. Here we just record the node-id of the item
1902+
// hashmap in the `Crate`. Here we just record the hir-id of the item
18881903
// so it can fetched later.
18891904
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
18901905
pub struct ImplItemId {
@@ -2269,12 +2284,6 @@ pub struct Mod<'hir> {
22692284
pub item_ids: &'hir [ItemId],
22702285
}
22712286

2272-
#[derive(Debug, HashStable_Generic)]
2273-
pub struct ForeignMod<'hir> {
2274-
pub abi: Abi,
2275-
pub items: &'hir [ForeignItem<'hir>],
2276-
}
2277-
22782287
#[derive(Encodable, Debug, HashStable_Generic)]
22792288
pub struct GlobalAsm {
22802289
pub asm: Symbol,
@@ -2432,7 +2441,7 @@ impl VariantData<'hir> {
24322441
}
24332442

24342443
// The bodies for items are stored "out of line", in a separate
2435-
// hashmap in the `Crate`. Here we just record the node-id of the item
2444+
// hashmap in the `Crate`. Here we just record the hir-id of the item
24362445
// so it can fetched later.
24372446
#[derive(Copy, Clone, Encodable, Debug)]
24382447
pub struct ItemId {
@@ -2521,7 +2530,7 @@ pub enum ItemKind<'hir> {
25212530
/// A module.
25222531
Mod(Mod<'hir>),
25232532
/// An external module, e.g. `extern { .. }`.
2524-
ForeignMod(ForeignMod<'hir>),
2533+
ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] },
25252534
/// Module-level inline assembly (from `global_asm!`).
25262535
GlobalAsm(&'hir GlobalAsm),
25272536
/// A type alias, e.g., `type Foo = Bar<u8>`.
@@ -2614,6 +2623,29 @@ pub enum AssocItemKind {
26142623
Type,
26152624
}
26162625

2626+
// The bodies for items are stored "out of line", in a separate
2627+
// hashmap in the `Crate`. Here we just record the hir-id of the item
2628+
// so it can fetched later.
2629+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)]
2630+
pub struct ForeignItemId {
2631+
pub hir_id: HirId,
2632+
}
2633+
2634+
/// A reference from a foreign block to one of its items. This
2635+
/// contains the item's ID, naturally, but also the item's name and
2636+
/// some other high-level details (like whether it is an associated
2637+
/// type or method, and whether it is public). This allows other
2638+
/// passes to find the impl they want without loading the ID (which
2639+
/// means fewer edges in the incremental compilation graph).
2640+
#[derive(Debug, HashStable_Generic)]
2641+
pub struct ForeignItemRef<'hir> {
2642+
pub id: ForeignItemId,
2643+
#[stable_hasher(project(name))]
2644+
pub ident: Ident,
2645+
pub span: Span,
2646+
pub vis: Visibility<'hir>,
2647+
}
2648+
26172649
#[derive(Debug, HashStable_Generic)]
26182650
pub struct ForeignItem<'hir> {
26192651
#[stable_hasher(project(name))]

compiler/rustc_hir/src/intravisit.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ where
6464
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) {
6565
self.visitor.visit_impl_item(impl_item);
6666
}
67+
68+
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
69+
self.visitor.visit_foreign_item(foreign_item);
70+
}
6771
}
6872

6973
pub trait IntoVisitor<'hir> {
@@ -88,6 +92,10 @@ where
8892
fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) {
8993
self.0.into_visitor().visit_impl_item(impl_item);
9094
}
95+
96+
fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) {
97+
self.0.into_visitor().visit_foreign_item(foreign_item);
98+
}
9199
}
92100

93101
#[derive(Copy, Clone)]
@@ -128,6 +136,7 @@ pub trait Map<'hir> {
128136
fn item(&self, id: HirId) -> &'hir Item<'hir>;
129137
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
130138
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>;
139+
fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>;
131140
}
132141

133142
/// An erased version of `Map<'hir>`, using dynamic dispatch.
@@ -150,6 +159,9 @@ impl<'hir> Map<'hir> for ErasedMap<'hir> {
150159
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
151160
self.0.impl_item(id)
152161
}
162+
fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
163+
self.0.foreign_item(id)
164+
}
153165
}
154166

155167
/// Specifies what nested things a visitor wants to visit. The most
@@ -277,6 +289,14 @@ pub trait Visitor<'v>: Sized {
277289
walk_list!(self, visit_impl_item, opt_item);
278290
}
279291

292+
/// Like `visit_nested_item()`, but for foreign items. See
293+
/// `visit_nested_item()` for advice on when to override this
294+
/// method.
295+
fn visit_nested_foreign_item(&mut self, id: ForeignItemId) {
296+
let opt_item = self.nested_visit_map().inter().map(|map| map.foreign_item(id));
297+
walk_list!(self, visit_foreign_item, opt_item);
298+
}
299+
280300
/// Invoked to visit the body of a function, method or closure. Like
281301
/// visit_nested_item, does nothing by default unless you override
282302
/// `nested_visit_map` to return other than `None`, in which case it will walk
@@ -378,6 +398,9 @@ pub trait Visitor<'v>: Sized {
378398
fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) {
379399
walk_impl_item(self, ii)
380400
}
401+
fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef<'v>) {
402+
walk_foreign_item_ref(self, ii)
403+
}
381404
fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef<'v>) {
382405
walk_impl_item_ref(self, ii)
383406
}
@@ -566,9 +589,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
566589
// `visit_mod()` takes care of visiting the `Item`'s `HirId`.
567590
visitor.visit_mod(module, item.span, item.hir_id)
568591
}
569-
ItemKind::ForeignMod(ref foreign_module) => {
592+
ItemKind::ForeignMod { abi: _, items } => {
570593
visitor.visit_id(item.hir_id);
571-
walk_list!(visitor, visit_foreign_item, foreign_module.items);
594+
walk_list!(visitor, visit_foreign_item_ref, items);
572595
}
573596
ItemKind::GlobalAsm(_) => {
574597
visitor.visit_id(item.hir_id);
@@ -1012,6 +1035,17 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
10121035
}
10131036
}
10141037

1038+
pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
1039+
visitor: &mut V,
1040+
foreign_item_ref: &'v ForeignItemRef<'v>,
1041+
) {
1042+
// N.B., deliberately force a compilation error if/when new fields are added.
1043+
let ForeignItemRef { id, ident, span: _, ref vis } = *foreign_item_ref;
1044+
visitor.visit_nested_foreign_item(id);
1045+
visitor.visit_ident(ident);
1046+
visitor.visit_vis(vis);
1047+
}
1048+
10151049
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef<'v>) {
10161050
// N.B., deliberately force a compilation error if/when new fields are added.
10171051
let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref;

compiler/rustc_hir/src/itemlikevisit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{ImplItem, Item, TraitItem};
1+
use super::{ForeignItem, ImplItem, Item, TraitItem};
22

33
/// The "item-like visitor" defines only the top-level methods
44
/// that can be invoked by `Crate::visit_all_item_likes()`. Whether
@@ -47,11 +47,13 @@ pub trait ItemLikeVisitor<'hir> {
4747
fn visit_item(&mut self, item: &'hir Item<'hir>);
4848
fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>);
4949
fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>);
50+
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>);
5051
}
5152

5253
/// A parallel variant of `ItemLikeVisitor`.
5354
pub trait ParItemLikeVisitor<'hir> {
5455
fn visit_item(&self, item: &'hir Item<'hir>);
5556
fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>);
5657
fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>);
58+
fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>);
5759
}

compiler/rustc_hir/src/stable_hash_impls.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
5-
VisibilityKind,
4+
BodyId, Expr, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId,
5+
Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::{DefPathHash, LocalDefId};
@@ -52,6 +52,15 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
5252
}
5353
}
5454

55+
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
56+
type KeyType = (DefPathHash, ItemLocalId);
57+
58+
#[inline]
59+
fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
60+
self.hir_id.to_stable_hash_key(hcx)
61+
}
62+
}
63+
5564
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
5665
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
5766
hcx.hash_hir_id(*self, hasher)
@@ -77,6 +86,12 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
7786
}
7887
}
7988

89+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
90+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
91+
hcx.hash_reference_to_item(self.hir_id, hasher)
92+
}
93+
}
94+
8095
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
8196
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
8297
hcx.hash_reference_to_item(self.hir_id, hasher)

compiler/rustc_hir/src/target.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Target {
9191
ItemKind::Const(..) => Target::Const,
9292
ItemKind::Fn(..) => Target::Fn,
9393
ItemKind::Mod(..) => Target::Mod,
94-
ItemKind::ForeignMod(..) => Target::ForeignMod,
94+
ItemKind::ForeignMod { .. } => Target::ForeignMod,
9595
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
9696
ItemKind::TyAlias(..) => Target::TyAlias,
9797
ItemKind::OpaqueTy(..) => Target::OpaqueTy,

0 commit comments

Comments
 (0)