diff options
Diffstat (limited to 'compiler/rustc_hir/src')
| -rw-r--r-- | compiler/rustc_hir/src/arena.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/def.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 56 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/intravisit.rs | 40 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/itemlikevisit.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/stable_hash_impls.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/target.rs | 2 |
7 files changed, 113 insertions, 22 deletions
diff --git a/compiler/rustc_hir/src/arena.rs b/compiler/rustc_hir/src/arena.rs index 85ab7906d25..c7dc66b70fe 100644 --- a/compiler/rustc_hir/src/arena.rs +++ b/compiler/rustc_hir/src/arena.rs @@ -14,7 +14,7 @@ macro_rules! arena_types { // HIR types [few] hir_krate: rustc_hir::Crate<$tcx>, [] arm: rustc_hir::Arm<$tcx>, - [] asm_operand: rustc_hir::InlineAsmOperand<$tcx>, + [] asm_operand: (rustc_hir::InlineAsmOperand<$tcx>, Span), [] asm_template: rustc_ast::InlineAsmTemplatePiece, [] attribute: rustc_ast::Attribute, [] block: rustc_hir::Block<$tcx>, @@ -29,6 +29,7 @@ macro_rules! arena_types { [] field_pat: rustc_hir::FieldPat<$tcx>, [] fn_decl: rustc_hir::FnDecl<$tcx>, [] foreign_item: rustc_hir::ForeignItem<$tcx>, + [few] foreign_item_ref: rustc_hir::ForeignItemRef<$tcx>, [] impl_item_ref: rustc_hir::ImplItemRef<$tcx>, [few] inline_asm: rustc_hir::InlineAsm<$tcx>, [few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>, diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index 298cfcc254c..4ede9d67b74 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -39,6 +39,9 @@ pub enum NonMacroAttrKind { Tool, /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`). DeriveHelper, + /// Single-segment custom attribute registered by a derive macro + /// but used before that derive macro was expanded (deprecated). + DeriveHelperCompat, /// Single-segment custom attribute registered with `#[register_attr]`. Registered, } @@ -370,7 +373,9 @@ impl NonMacroAttrKind { match self { NonMacroAttrKind::Builtin => "built-in attribute", NonMacroAttrKind::Tool => "tool attribute", - NonMacroAttrKind::DeriveHelper => "derive helper attribute", + NonMacroAttrKind::DeriveHelper | NonMacroAttrKind::DeriveHelperCompat => { + "derive helper attribute" + } NonMacroAttrKind::Registered => "explicitly registered attribute", } } @@ -385,7 +390,9 @@ impl NonMacroAttrKind { /// Users of some attributes cannot mark them as used, so they are considered always used. pub fn is_used(self) -> bool { match self { - NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true, + NonMacroAttrKind::Tool + | NonMacroAttrKind::DeriveHelper + | NonMacroAttrKind::DeriveHelperCompat => true, NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false, } } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 4497c8c0eaa..280e863d474 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -579,6 +579,7 @@ pub struct ModuleItems { pub items: BTreeSet<HirId>, pub trait_items: BTreeSet<TraitItemId>, pub impl_items: BTreeSet<ImplItemId>, + pub foreign_items: BTreeSet<ForeignItemId>, } /// A type representing only the top-level module. @@ -612,6 +613,7 @@ pub struct Crate<'hir> { pub trait_items: BTreeMap<TraitItemId, TraitItem<'hir>>, pub impl_items: BTreeMap<ImplItemId, ImplItem<'hir>>, + pub foreign_items: BTreeMap<ForeignItemId, ForeignItem<'hir>>, pub bodies: BTreeMap<BodyId, Body<'hir>>, pub trait_impls: BTreeMap<DefId, Vec<HirId>>, @@ -644,6 +646,10 @@ impl Crate<'hir> { &self.impl_items[&id] } + pub fn foreign_item(&self, id: ForeignItemId) -> &ForeignItem<'hir> { + &self.foreign_items[&id] + } + pub fn body(&self, id: BodyId) -> &Body<'hir> { &self.bodies[&id] } @@ -673,6 +679,10 @@ impl Crate<'_> { for impl_item in self.impl_items.values() { visitor.visit_impl_item(impl_item); } + + for foreign_item in self.foreign_items.values() { + visitor.visit_foreign_item(foreign_item); + } } /// A parallel version of `visit_all_item_likes`. @@ -695,6 +705,11 @@ impl Crate<'_> { par_for_each_in(&self.impl_items, |(_, impl_item)| { visitor.visit_impl_item(impl_item); }); + }, + { + par_for_each_in(&self.foreign_items, |(_, foreign_item)| { + visitor.visit_foreign_item(foreign_item); + }); } ); } @@ -1840,7 +1855,7 @@ pub struct FnSig<'hir> { } // The bodies for items are stored "out of line", in a separate -// hashmap in the `Crate`. Here we just record the node-id of the item +// hashmap in the `Crate`. Here we just record the hir-id of the item // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)] pub struct TraitItemId { @@ -1884,7 +1899,7 @@ pub enum TraitItemKind<'hir> { } // The bodies for items are stored "out of line", in a separate -// hashmap in the `Crate`. Here we just record the node-id of the item +// hashmap in the `Crate`. Here we just record the hir-id of the item // so it can fetched later. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)] pub struct ImplItemId { @@ -2128,7 +2143,7 @@ impl<'hir> InlineAsmOperand<'hir> { #[derive(Debug, HashStable_Generic)] pub struct InlineAsm<'hir> { pub template: &'hir [InlineAsmTemplatePiece], - pub operands: &'hir [InlineAsmOperand<'hir>], + pub operands: &'hir [(InlineAsmOperand<'hir>, Span)], pub options: InlineAsmOptions, pub line_spans: &'hir [Span], } @@ -2269,12 +2284,6 @@ pub struct Mod<'hir> { pub item_ids: &'hir [ItemId], } -#[derive(Debug, HashStable_Generic)] -pub struct ForeignMod<'hir> { - pub abi: Abi, - pub items: &'hir [ForeignItem<'hir>], -} - #[derive(Encodable, Debug, HashStable_Generic)] pub struct GlobalAsm { pub asm: Symbol, @@ -2392,7 +2401,7 @@ impl StructField<'_> { // Still necessary in couple of places pub fn is_positional(&self) -> bool { let first = self.ident.as_str().as_bytes()[0]; - first >= b'0' && first <= b'9' + (b'0'..=b'9').contains(&first) } } @@ -2432,7 +2441,7 @@ impl VariantData<'hir> { } // The bodies for items are stored "out of line", in a separate -// hashmap in the `Crate`. Here we just record the node-id of the item +// hashmap in the `Crate`. Here we just record the hir-id of the item // so it can fetched later. #[derive(Copy, Clone, Encodable, Debug)] pub struct ItemId { @@ -2521,7 +2530,7 @@ pub enum ItemKind<'hir> { /// A module. Mod(Mod<'hir>), /// An external module, e.g. `extern { .. }`. - ForeignMod(ForeignMod<'hir>), + ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] }, /// Module-level inline assembly (from `global_asm!`). GlobalAsm(&'hir GlobalAsm), /// A type alias, e.g., `type Foo = Bar<u8>`. @@ -2614,6 +2623,29 @@ pub enum AssocItemKind { Type, } +// The bodies for items are stored "out of line", in a separate +// hashmap in the `Crate`. Here we just record the hir-id of the item +// so it can fetched later. +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Debug)] +pub struct ForeignItemId { + pub hir_id: HirId, +} + +/// A reference from a foreign block to one of its items. This +/// contains the item's ID, naturally, but also the item's name and +/// some other high-level details (like whether it is an associated +/// type or method, and whether it is public). This allows other +/// passes to find the impl they want without loading the ID (which +/// means fewer edges in the incremental compilation graph). +#[derive(Debug, HashStable_Generic)] +pub struct ForeignItemRef<'hir> { + pub id: ForeignItemId, + #[stable_hasher(project(name))] + pub ident: Ident, + pub span: Span, + pub vis: Visibility<'hir>, +} + #[derive(Debug, HashStable_Generic)] pub struct ForeignItem<'hir> { #[stable_hasher(project(name))] diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index 35615af0fc7..3c330c5d6c5 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -64,6 +64,10 @@ where fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>) { self.visitor.visit_impl_item(impl_item); } + + fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) { + self.visitor.visit_foreign_item(foreign_item); + } } pub trait IntoVisitor<'hir> { @@ -88,6 +92,10 @@ where fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>) { self.0.into_visitor().visit_impl_item(impl_item); } + + fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>) { + self.0.into_visitor().visit_foreign_item(foreign_item); + } } #[derive(Copy, Clone)] @@ -128,6 +136,7 @@ pub trait Map<'hir> { fn item(&self, id: HirId) -> &'hir Item<'hir>; fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>; fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>; + fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>; } /// An erased version of `Map<'hir>`, using dynamic dispatch. @@ -150,6 +159,9 @@ impl<'hir> Map<'hir> for ErasedMap<'hir> { fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> { self.0.impl_item(id) } + fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> { + self.0.foreign_item(id) + } } /// Specifies what nested things a visitor wants to visit. The most @@ -277,6 +289,14 @@ pub trait Visitor<'v>: Sized { walk_list!(self, visit_impl_item, opt_item); } + /// Like `visit_nested_item()`, but for foreign items. See + /// `visit_nested_item()` for advice on when to override this + /// method. + fn visit_nested_foreign_item(&mut self, id: ForeignItemId) { + let opt_item = self.nested_visit_map().inter().map(|map| map.foreign_item(id)); + walk_list!(self, visit_foreign_item, opt_item); + } + /// Invoked to visit the body of a function, method or closure. Like /// visit_nested_item, does nothing by default unless you override /// `nested_visit_map` to return other than `None`, in which case it will walk @@ -378,6 +398,9 @@ pub trait Visitor<'v>: Sized { fn visit_impl_item(&mut self, ii: &'v ImplItem<'v>) { walk_impl_item(self, ii) } + fn visit_foreign_item_ref(&mut self, ii: &'v ForeignItemRef<'v>) { + walk_foreign_item_ref(self, ii) + } fn visit_impl_item_ref(&mut self, ii: &'v ImplItemRef<'v>) { walk_impl_item_ref(self, ii) } @@ -566,9 +589,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { // `visit_mod()` takes care of visiting the `Item`'s `HirId`. visitor.visit_mod(module, item.span, item.hir_id) } - ItemKind::ForeignMod(ref foreign_module) => { + ItemKind::ForeignMod { abi: _, items } => { visitor.visit_id(item.hir_id); - walk_list!(visitor, visit_foreign_item, foreign_module.items); + walk_list!(visitor, visit_foreign_item_ref, items); } ItemKind::GlobalAsm(_) => { 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 } } +pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>( + visitor: &mut V, + foreign_item_ref: &'v ForeignItemRef<'v>, +) { + // N.B., deliberately force a compilation error if/when new fields are added. + let ForeignItemRef { id, ident, span: _, ref vis } = *foreign_item_ref; + visitor.visit_nested_foreign_item(id); + visitor.visit_ident(ident); + visitor.visit_vis(vis); +} + pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef<'v>) { // N.B., deliberately force a compilation error if/when new fields are added. let ImplItemRef { id, ident, ref kind, span: _, ref vis, ref defaultness } = *impl_item_ref; @@ -1157,7 +1191,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) walk_list!(visitor, visit_expr, optional_expression); } ExprKind::InlineAsm(ref asm) => { - for op in asm.operands { + for (op, _op_sp) in asm.operands { match op { InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } diff --git a/compiler/rustc_hir/src/itemlikevisit.rs b/compiler/rustc_hir/src/itemlikevisit.rs index 369cd49621b..0db562f91a6 100644 --- a/compiler/rustc_hir/src/itemlikevisit.rs +++ b/compiler/rustc_hir/src/itemlikevisit.rs @@ -1,4 +1,4 @@ -use super::{ImplItem, Item, TraitItem}; +use super::{ForeignItem, ImplItem, Item, TraitItem}; /// The "item-like visitor" defines only the top-level methods /// that can be invoked by `Crate::visit_all_item_likes()`. Whether @@ -47,6 +47,7 @@ pub trait ItemLikeVisitor<'hir> { fn visit_item(&mut self, item: &'hir Item<'hir>); fn visit_trait_item(&mut self, trait_item: &'hir TraitItem<'hir>); fn visit_impl_item(&mut self, impl_item: &'hir ImplItem<'hir>); + fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>); } /// A parallel variant of `ItemLikeVisitor`. @@ -54,4 +55,5 @@ pub trait ParItemLikeVisitor<'hir> { fn visit_item(&self, item: &'hir Item<'hir>); fn visit_trait_item(&self, trait_item: &'hir TraitItem<'hir>); fn visit_impl_item(&self, impl_item: &'hir ImplItem<'hir>); + fn visit_foreign_item(&self, foreign_item: &'hir ForeignItem<'hir>); } diff --git a/compiler/rustc_hir/src/stable_hash_impls.rs b/compiler/rustc_hir/src/stable_hash_impls.rs index 1d3f44a0899..439fb88039b 100644 --- a/compiler/rustc_hir/src/stable_hash_impls.rs +++ b/compiler/rustc_hir/src/stable_hash_impls.rs @@ -1,8 +1,8 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use crate::hir::{ - BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty, - VisibilityKind, + BodyId, Expr, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, + Ty, VisibilityKind, }; use crate::hir_id::{HirId, ItemLocalId}; use rustc_span::def_id::{DefPathHash, LocalDefId}; @@ -52,6 +52,15 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId { } } +impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId { + type KeyType = (DefPathHash, ItemLocalId); + + #[inline] + fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { + self.hir_id.to_stable_hash_key(hcx) + } +} + impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { hcx.hash_hir_id(*self, hasher) @@ -77,6 +86,12 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId { } } +impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId { + fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { + hcx.hash_reference_to_item(self.hir_id, hasher) + } +} + impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { hcx.hash_reference_to_item(self.hir_id, hasher) diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_hir/src/target.rs index fd6a312ef3a..b870e4c6ead 100644 --- a/compiler/rustc_hir/src/target.rs +++ b/compiler/rustc_hir/src/target.rs @@ -91,7 +91,7 @@ impl Target { ItemKind::Const(..) => Target::Const, ItemKind::Fn(..) => Target::Fn, ItemKind::Mod(..) => Target::Mod, - ItemKind::ForeignMod(..) => Target::ForeignMod, + ItemKind::ForeignMod { .. } => Target::ForeignMod, ItemKind::GlobalAsm(..) => Target::GlobalAsm, ItemKind::TyAlias(..) => Target::TyAlias, ItemKind::OpaqueTy(..) => Target::OpaqueTy, |
