diff options
Diffstat (limited to 'compiler/rustc_hir/src')
| -rw-r--r-- | compiler/rustc_hir/src/def_path_hash_map.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/definitions.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir.rs | 223 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/hir/tests.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/intravisit.rs | 114 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/lang_items.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/stable_hash_impls.rs | 14 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/target.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_hir/src/tests.rs | 2 |
9 files changed, 245 insertions, 130 deletions
diff --git a/compiler/rustc_hir/src/def_path_hash_map.rs b/compiler/rustc_hir/src/def_path_hash_map.rs index 9a6dee1e511..35c6e57b877 100644 --- a/compiler/rustc_hir/src/def_path_hash_map.rs +++ b/compiler/rustc_hir/src/def_path_hash_map.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::stable_hasher::Hash64; +use rustc_hashes::Hash64; use rustc_span::def_id::DefIndex; #[derive(Clone, Default)] diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index dc527240f74..c4c309e77e1 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -7,8 +7,9 @@ use std::fmt::{self, Write}; use std::hash::Hash; -use rustc_data_structures::stable_hasher::{Hash64, StableHasher}; +use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::unord::UnordMap; +use rustc_hashes::Hash64; use rustc_index::IndexVec; use rustc_macros::{Decodable, Encodable}; use rustc_span::{Symbol, kw, sym}; @@ -92,7 +93,7 @@ impl DefPathTable { pub fn enumerated_keys_and_path_hashes( &self, - ) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator + '_ { + ) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator { self.index_to_key .iter_enumerated() .map(move |(index, key)| (index, key, self.def_path_hash(index))) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index eafc60f9d72..f0eaec55dbd 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,18 +1,20 @@ +// ignore-tidy-filelength use std::fmt; use rustc_abi::ExternAbi; -// ignore-tidy-filelength use rustc_ast::attr::AttributeExt; use rustc_ast::token::CommentKind; use rustc_ast::util::parser::{AssocOp, ExprPrecedence}; use rustc_ast::{ - self as ast, AttrId, AttrStyle, DelimArgs, FloatTy, InlineAsmOptions, InlineAsmTemplatePiece, - IntTy, Label, LitIntType, LitKind, MetaItemInner, MetaItemLit, TraitObjectSyntax, UintTy, + self as ast, FloatTy, InlineAsmOptions, InlineAsmTemplatePiece, IntTy, Label, LitIntType, + LitKind, TraitObjectSyntax, UintTy, UnsafeBinderCastKind, }; pub use rustc_ast::{ - BinOp, BinOpKind, BindingMode, BorrowKind, BoundConstness, BoundPolarity, ByRef, CaptureBy, - ImplPolarity, IsAuto, Movability, Mutability, UnOp, UnsafeBinderCastKind, + AttrId, AttrStyle, BinOp, BinOpKind, BindingMode, BorrowKind, BoundConstness, BoundPolarity, + ByRef, CaptureBy, DelimArgs, ImplPolarity, IsAuto, MetaItemInner, MetaItemLit, Movability, + Mutability, UnOp, }; +use rustc_attr_data_structures::AttributeKind; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::sorted_map::SortedMap; use rustc_data_structures::tagged_ptr::TaggedRef; @@ -1009,174 +1011,248 @@ pub enum AttrArgs { }, } -#[derive(Clone, Debug, Encodable, Decodable)] -pub enum AttrKind { - /// A normal attribute. - Normal(Box<AttrItem>), - - /// A doc comment (e.g. `/// ...`, `//! ...`, `/** ... */`, `/*! ... */`). - /// Doc attributes (e.g. `#[doc="..."]`) are represented with the `Normal` - /// variant (which is much less compact and thus more expensive). - DocComment(CommentKind, Symbol), -} - #[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)] pub struct AttrPath { pub segments: Box<[Ident]>, pub span: Span, } +impl AttrPath { + pub fn from_ast(path: &ast::Path) -> Self { + AttrPath { + segments: path.segments.iter().map(|i| i.ident).collect::<Vec<_>>().into_boxed_slice(), + span: path.span, + } + } +} + +impl fmt::Display for AttrPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.segments.iter().map(|i| i.to_string()).collect::<Vec<_>>().join("::")) + } +} + #[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)] pub struct AttrItem { - pub unsafety: Safety, // Not lowered to hir::Path because we have no NodeId to resolve to. pub path: AttrPath, pub args: AttrArgs, -} - -#[derive(Clone, Debug, Encodable, Decodable)] -pub struct Attribute { - pub kind: AttrKind, - pub id: AttrId, + pub id: HashIgnoredAttrId, /// Denotes if the attribute decorates the following construct (outer) /// or the construct this attribute is contained within (inner). pub style: AttrStyle, + /// Span of the entire attribute pub span: Span, } +/// The derived implementation of [`HashStable_Generic`] on [`Attribute`]s shouldn't hash +/// [`AttrId`]s. By wrapping them in this, we make sure we never do. +#[derive(Copy, Debug, Encodable, Decodable, Clone)] +pub struct HashIgnoredAttrId { + pub attr_id: AttrId, +} + +#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)] +pub enum Attribute { + /// A parsed built-in attribute. + /// + /// Each attribute has a span connected to it. However, you must be somewhat careful using it. + /// That's because sometimes we merge multiple attributes together, like when an item has + /// multiple `repr` attributes. In this case the span might not be very useful. + Parsed(AttributeKind), + + /// An attribute that could not be parsed, out of a token-like representation. + /// This is the case for custom tool attributes. + Unparsed(Box<AttrItem>), +} + impl Attribute { pub fn get_normal_item(&self) -> &AttrItem { - match &self.kind { - AttrKind::Normal(normal) => &normal, - AttrKind::DocComment(..) => panic!("unexpected doc comment"), + match &self { + Attribute::Unparsed(normal) => &normal, + _ => panic!("unexpected parsed attribute"), } } pub fn unwrap_normal_item(self) -> AttrItem { - match self.kind { - AttrKind::Normal(normal) => *normal, - AttrKind::DocComment(..) => panic!("unexpected doc comment"), + match self { + Attribute::Unparsed(normal) => *normal, + _ => panic!("unexpected parsed attribute"), } } pub fn value_lit(&self) -> Option<&MetaItemLit> { - match &self.kind { - AttrKind::Normal(box AttrItem { args: AttrArgs::Eq { expr, .. }, .. }) => Some(expr), + match &self { + Attribute::Unparsed(n) => match n.as_ref() { + AttrItem { args: AttrArgs::Eq { eq_span: _, expr }, .. } => Some(expr), + _ => None, + }, _ => None, } } } impl AttributeExt for Attribute { + #[inline] fn id(&self) -> AttrId { - self.id + match &self { + Attribute::Unparsed(u) => u.id.attr_id, + _ => panic!(), + } } + #[inline] fn meta_item_list(&self) -> Option<ThinVec<ast::MetaItemInner>> { - match &self.kind { - AttrKind::Normal(box AttrItem { args: AttrArgs::Delimited(d), .. }) => { - ast::MetaItemKind::list_from_tokens(d.tokens.clone()) - } + match &self { + Attribute::Unparsed(n) => match n.as_ref() { + AttrItem { args: AttrArgs::Delimited(d), .. } => { + ast::MetaItemKind::list_from_tokens(d.tokens.clone()) + } + _ => None, + }, _ => None, } } + #[inline] fn value_str(&self) -> Option<Symbol> { self.value_lit().and_then(|x| x.value_str()) } + #[inline] fn value_span(&self) -> Option<Span> { self.value_lit().map(|i| i.span) } /// For a single-segment attribute, returns its name; otherwise, returns `None`. + #[inline] fn ident(&self) -> Option<Ident> { - match &self.kind { - AttrKind::Normal(box AttrItem { - path: AttrPath { segments: box [ident], .. }, .. - }) => Some(*ident), + match &self { + Attribute::Unparsed(n) => { + if let [ident] = n.path.segments.as_ref() { + Some(*ident) + } else { + None + } + } _ => None, } } + #[inline] fn path_matches(&self, name: &[Symbol]) -> bool { - match &self.kind { - AttrKind::Normal(n) => n.path.segments.iter().map(|segment| &segment.name).eq(name), - AttrKind::DocComment(..) => false, + match &self { + Attribute::Unparsed(n) => { + n.path.segments.len() == name.len() + && n.path.segments.iter().zip(name).all(|(s, n)| s.name == *n) + } + _ => false, } } + #[inline] fn is_doc_comment(&self) -> bool { - matches!(self.kind, AttrKind::DocComment(..)) + matches!(self, Attribute::Parsed(AttributeKind::DocComment { .. })) } + #[inline] fn span(&self) -> Span { - self.span + match &self { + Attribute::Unparsed(u) => u.span, + // FIXME: should not be needed anymore when all attrs are parsed + Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span, + Attribute::Parsed(AttributeKind::DocComment { span, .. }) => *span, + a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"), + } } + #[inline] fn is_word(&self) -> bool { - matches!(self.kind, AttrKind::Normal(box AttrItem { args: AttrArgs::Empty, .. })) + match &self { + Attribute::Unparsed(n) => { + matches!(n.args, AttrArgs::Empty) + } + _ => false, + } } + #[inline] fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> { - match &self.kind { - AttrKind::Normal(n) => Some(n.path.segments.iter().copied().collect()), - AttrKind::DocComment(..) => None, + match &self { + Attribute::Unparsed(n) => Some(n.path.segments.iter().copied().collect()), + _ => None, } } + #[inline] fn doc_str(&self) -> Option<Symbol> { - match &self.kind { - AttrKind::DocComment(.., data) => Some(*data), - AttrKind::Normal(_) if self.has_name(sym::doc) => self.value_str(), + match &self { + Attribute::Parsed(AttributeKind::DocComment { comment, .. }) => Some(*comment), + Attribute::Unparsed(_) if self.has_name(sym::doc) => self.value_str(), _ => None, } } + #[inline] fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> { - match &self.kind { - AttrKind::DocComment(kind, data) => Some((*data, *kind)), - AttrKind::Normal(_) if self.name_or_empty() == sym::doc => { + match &self { + Attribute::Parsed(AttributeKind::DocComment { kind, comment, .. }) => { + Some((*comment, *kind)) + } + Attribute::Unparsed(_) if self.name_or_empty() == sym::doc => { self.value_str().map(|s| (s, CommentKind::Line)) } _ => None, } } + #[inline] fn style(&self) -> AttrStyle { - self.style + match &self { + Attribute::Unparsed(u) => u.style, + Attribute::Parsed(AttributeKind::DocComment { style, .. }) => *style, + _ => panic!(), + } } } // FIXME(fn_delegation): use function delegation instead of manually forwarding impl Attribute { + #[inline] pub fn id(&self) -> AttrId { AttributeExt::id(self) } + #[inline] pub fn name_or_empty(&self) -> Symbol { AttributeExt::name_or_empty(self) } + #[inline] pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> { AttributeExt::meta_item_list(self) } + #[inline] pub fn value_str(&self) -> Option<Symbol> { AttributeExt::value_str(self) } + #[inline] pub fn value_span(&self) -> Option<Span> { AttributeExt::value_span(self) } + #[inline] pub fn ident(&self) -> Option<Ident> { AttributeExt::ident(self) } + #[inline] pub fn path_matches(&self, name: &[Symbol]) -> bool { AttributeExt::path_matches(self, name) } + #[inline] pub fn is_doc_comment(&self) -> bool { AttributeExt::is_doc_comment(self) } @@ -1186,34 +1262,42 @@ impl Attribute { AttributeExt::has_name(self, name) } + #[inline] pub fn span(&self) -> Span { AttributeExt::span(self) } + #[inline] pub fn is_word(&self) -> bool { AttributeExt::is_word(self) } + #[inline] pub fn path(&self) -> SmallVec<[Symbol; 1]> { AttributeExt::path(self) } + #[inline] pub fn ident_path(&self) -> Option<SmallVec<[Ident; 1]>> { AttributeExt::ident_path(self) } + #[inline] pub fn doc_str(&self) -> Option<Symbol> { AttributeExt::doc_str(self) } + #[inline] pub fn is_proc_macro_attr(&self) -> bool { AttributeExt::is_proc_macro_attr(self) } + #[inline] pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> { AttributeExt::doc_str_and_comment_kind(self) } + #[inline] pub fn style(&self) -> AttrStyle { AttributeExt::style(self) } @@ -1913,13 +1997,18 @@ pub enum BodyOwnerKind { /// Initializer of a `static` item. Static(Mutability), + + /// Fake body for a global asm to store its const-like value types. + GlobalAsm, } impl BodyOwnerKind { pub fn is_fn_or_closure(self) -> bool { match self { BodyOwnerKind::Fn | BodyOwnerKind::Closure => true, - BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(_) => false, + BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(_) | BodyOwnerKind::GlobalAsm => { + false + } } } } @@ -3420,7 +3509,7 @@ pub enum InlineAsmOperand<'hir> { anon_const: &'hir AnonConst, }, SymFn { - anon_const: &'hir AnonConst, + expr: &'hir Expr<'hir>, }, SymStatic { path: QPath<'hir>, @@ -3848,7 +3937,7 @@ impl<'hir> Item<'hir> { expect_foreign_mod, (ExternAbi, &'hir [ForeignItemRef]), ItemKind::ForeignMod { abi, items }, (*abi, items); - expect_global_asm, &'hir InlineAsm<'hir>, ItemKind::GlobalAsm(asm), asm; + expect_global_asm, &'hir InlineAsm<'hir>, ItemKind::GlobalAsm { asm, .. }, asm; expect_ty_alias, (&'hir Ty<'hir>, &'hir Generics<'hir>), ItemKind::TyAlias(ty, generics), (ty, generics); @@ -4015,7 +4104,15 @@ pub enum ItemKind<'hir> { /// An external module, e.g. `extern { .. }`. ForeignMod { abi: ExternAbi, items: &'hir [ForeignItemRef] }, /// Module-level inline assembly (from `global_asm!`). - GlobalAsm(&'hir InlineAsm<'hir>), + GlobalAsm { + asm: &'hir InlineAsm<'hir>, + /// A fake body which stores typeck results for the global asm's sym_fn + /// operands, which are represented as path expressions. This body contains + /// a single [`ExprKind::InlineAsm`] which points to the asm in the field + /// above, and which is typechecked like a inline asm expr just for the + /// typeck results. + fake_body: BodyId, + }, /// A type alias, e.g., `type Foo = Bar<u8>`. TyAlias(&'hir Ty<'hir>, &'hir Generics<'hir>), /// An enum definition, e.g., `enum Foo<A, B> {C<A>, D<B>}`. @@ -4081,7 +4178,7 @@ impl ItemKind<'_> { ItemKind::Macro(..) => "macro", ItemKind::Mod(..) => "module", ItemKind::ForeignMod { .. } => "extern block", - ItemKind::GlobalAsm(..) => "global asm item", + ItemKind::GlobalAsm { .. } => "global asm item", ItemKind::TyAlias(..) => "type alias", ItemKind::Enum(..) => "enum", ItemKind::Struct(..) => "struct", @@ -4540,6 +4637,10 @@ impl<'hir> Node<'hir> { .. }) => Some((owner_id.def_id, *body)), + Node::Item(Item { + owner_id, kind: ItemKind::GlobalAsm { asm: _, fake_body }, .. + }) => Some((owner_id.def_id, *fake_body)), + Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => { Some((*def_id, *body)) } diff --git a/compiler/rustc_hir/src/hir/tests.rs b/compiler/rustc_hir/src/hir/tests.rs index 300d4435530..f75b9662132 100644 --- a/compiler/rustc_hir/src/hir/tests.rs +++ b/compiler/rustc_hir/src/hir/tests.rs @@ -9,11 +9,11 @@ macro_rules! define_tests { let unambig = $kind::$variant::<'_, ()> { $($init)* }; let unambig_to_ambig = unsafe { std::mem::transmute::<_, $kind<'_, AmbigArg>>(unambig) }; - assert!(matches!(&unambig_to_ambig, $kind::$variant { $($init)* })); + assert!(matches!(&unambig_to_ambig, &$kind::$variant { $($init)* })); let ambig_to_unambig = unsafe { std::mem::transmute::<_, $kind<'_, ()>>(unambig_to_ambig) }; - assert!(matches!(&ambig_to_unambig, $kind::$variant { $($init)* })); + assert!(matches!(&ambig_to_unambig, &$kind::$variant { $($init)* })); } )*}; } diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index f632041dba9..d5fa7ec366b 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -18,8 +18,8 @@ //! within one another. //! - Example: Examine each expression to look for its type and do some check or other. //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to -//! `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use -//! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your +//! `nested_filter::OnlyBodies` (and implement `maybe_tcx`), and use +//! `tcx.hir_visit_all_item_likes_in_crate(&mut visitor)`. Within your //! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke //! `intravisit::walk_expr()` to keep walking the subparts). //! - Pro: Visitor methods for any kind of HIR node, not just item-like things. @@ -30,8 +30,8 @@ //! - Example: Lifetime resolution, which wants to bring lifetimes declared on the //! impl into scope while visiting the impl-items, and then back out again. //! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to -//! `nested_filter::All` (and implement `nested_visit_map`). Walk your crate with -//! `tcx.hir().walk_toplevel_module(visitor)` invoked on `tcx.hir().krate()`. +//! `nested_filter::All` (and implement `maybe_tcx`). Walk your crate with +//! `tcx.hir_walk_toplevel_module(visitor)`. //! - Pro: Visitor methods for any kind of HIR node, not just item-like things. //! - Pro: Preserves nesting information //! - Con: Does not integrate well into dependency tracking. @@ -106,45 +106,43 @@ impl<'a> FnKind<'a> { } } -/// An abstract representation of the HIR `rustc_middle::hir::map::Map`. -pub trait Map<'hir> { +/// HIR things retrievable from `TyCtxt`, avoiding an explicit dependence on +/// `TyCtxt`. The only impls are for `!` (where these functions are never +/// called) and `TyCtxt` (in `rustc_middle`). +pub trait HirTyCtxt<'hir> { /// Retrieves the `Node` corresponding to `id`. fn hir_node(&self, hir_id: HirId) -> Node<'hir>; - fn hir_node_by_def_id(&self, def_id: LocalDefId) -> Node<'hir>; - fn body(&self, id: BodyId) -> &'hir Body<'hir>; - fn item(&self, id: ItemId) -> &'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>; + fn hir_body(&self, id: BodyId) -> &'hir Body<'hir>; + fn hir_item(&self, id: ItemId) -> &'hir Item<'hir>; + fn hir_trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>; + fn hir_impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir>; + fn hir_foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir>; } -// Used when no map is actually available, forcing manual implementation of nested visitors. -impl<'hir> Map<'hir> for ! { +// Used when no tcx is actually available, forcing manual implementation of nested visitors. +impl<'hir> HirTyCtxt<'hir> for ! { fn hir_node(&self, _: HirId) -> Node<'hir> { - *self; + unreachable!(); } - fn hir_node_by_def_id(&self, _: LocalDefId) -> Node<'hir> { - *self; + fn hir_body(&self, _: BodyId) -> &'hir Body<'hir> { + unreachable!(); } - fn body(&self, _: BodyId) -> &'hir Body<'hir> { - *self; + fn hir_item(&self, _: ItemId) -> &'hir Item<'hir> { + unreachable!(); } - fn item(&self, _: ItemId) -> &'hir Item<'hir> { - *self; + fn hir_trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> { + unreachable!(); } - fn trait_item(&self, _: TraitItemId) -> &'hir TraitItem<'hir> { - *self; + fn hir_impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> { + unreachable!(); } - fn impl_item(&self, _: ImplItemId) -> &'hir ImplItem<'hir> { - *self; - } - fn foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> { - *self; + fn hir_foreign_item(&self, _: ForeignItemId) -> &'hir ForeignItem<'hir> { + unreachable!(); } } pub mod nested_filter { - use super::Map; + use super::HirTyCtxt; /// Specifies what nested things a visitor wants to visit. By "nested /// things", we are referring to bits of HIR that are not directly embedded @@ -159,7 +157,7 @@ pub mod nested_filter { /// See the comments at [`rustc_hir::intravisit`] for more details on the overall /// visit strategy. pub trait NestedFilter<'hir> { - type Map: Map<'hir>; + type MaybeTyCtxt: HirTyCtxt<'hir>; /// Whether the visitor visits nested "item-like" things. /// E.g., item, impl-item. @@ -175,10 +173,10 @@ pub mod nested_filter { /// /// Use this if you are only walking some particular kind of tree /// (i.e., a type, or fn signature) and you don't want to thread a - /// HIR map around. + /// `tcx` around. pub struct None(()); impl NestedFilter<'_> for None { - type Map = !; + type MaybeTyCtxt = !; const INTER: bool = false; const INTRA: bool = false; } @@ -195,7 +193,7 @@ use nested_filter::NestedFilter; /// (this is why the module is called `intravisit`, to distinguish it /// from the AST's `visit` module, which acts differently). If you /// simply want to visit all items in the crate in some order, you -/// should call `tcx.hir().visit_all_item_likes_in_crate`. Otherwise, see the comment +/// should call `tcx.hir_visit_all_item_likes_in_crate`. Otherwise, see the comment /// on `visit_nested_item` for details on how to visit nested items. /// /// If you want to ensure that your code handles every variant @@ -203,18 +201,18 @@ use nested_filter::NestedFilter; /// to monitor future changes to `Visitor` in case a new method with a /// new default implementation gets introduced.) pub trait Visitor<'v>: Sized { - // this type should not be overridden, it exists for convenient usage as `Self::Map` - type Map: Map<'v> = <Self::NestedFilter as NestedFilter<'v>>::Map; + // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`. + type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt; /////////////////////////////////////////////////////////////////////////// // Nested items. /// Override this type to control which nested HIR are visited; see /// [`NestedFilter`] for details. If you override this type, you - /// must also override [`nested_visit_map`](Self::nested_visit_map). + /// must also override [`maybe_tcx`](Self::maybe_tcx). /// /// **If for some reason you want the nested behavior, but don't - /// have a `Map` at your disposal:** then override the + /// have a `tcx` at your disposal:** then override the /// `visit_nested_XXX` methods. If a new `visit_nested_XXX` variant is /// added in the future, it will cause a panic which can be detected /// and fixed appropriately. @@ -226,9 +224,9 @@ pub trait Visitor<'v>: Sized { /// If `type NestedFilter` is set to visit nested items, this method /// must also be overridden to provide a map to retrieve nested items. - fn nested_visit_map(&mut self) -> Self::Map { + fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt { panic!( - "nested_visit_map must be implemented or consider using \ + "maybe_tcx must be implemented or consider using \ `type NestedFilter = nested_filter::None` (the default)" ); } @@ -240,10 +238,10 @@ pub trait Visitor<'v>: Sized { /// "deep" visit patterns described at /// [`rustc_hir::intravisit`]. The only reason to override /// this method is if you want a nested pattern but cannot supply a - /// [`Map`]; see `nested_visit_map` for advice. + /// `TyCtxt`; see `maybe_tcx` for advice. fn visit_nested_item(&mut self, id: ItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().item(id); + let item = self.maybe_tcx().hir_item(id); try_visit!(self.visit_item(item)); } Self::Result::output() @@ -254,7 +252,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_trait_item(&mut self, id: TraitItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().trait_item(id); + let item = self.maybe_tcx().hir_trait_item(id); try_visit!(self.visit_trait_item(item)); } Self::Result::output() @@ -265,7 +263,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_impl_item(&mut self, id: ImplItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().impl_item(id); + let item = self.maybe_tcx().hir_impl_item(id); try_visit!(self.visit_impl_item(item)); } Self::Result::output() @@ -276,7 +274,7 @@ pub trait Visitor<'v>: Sized { /// method. fn visit_nested_foreign_item(&mut self, id: ForeignItemId) -> Self::Result { if Self::NestedFilter::INTER { - let item = self.nested_visit_map().foreign_item(id); + let item = self.maybe_tcx().hir_foreign_item(id); try_visit!(self.visit_foreign_item(item)); } Self::Result::output() @@ -287,7 +285,7 @@ pub trait Visitor<'v>: Sized { /// `Self::NestedFilter`. fn visit_nested_body(&mut self, id: BodyId) -> Self::Result { if Self::NestedFilter::INTRA { - let body = self.nested_visit_map().body(id); + let body = self.maybe_tcx().hir_body(id); try_visit!(self.visit_body(body)); } Self::Result::output() @@ -575,9 +573,13 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: try_visit!(visitor.visit_id(item.hir_id())); walk_list!(visitor, visit_foreign_item_ref, items); } - ItemKind::GlobalAsm(asm) => { + ItemKind::GlobalAsm { asm: _, fake_body } => { try_visit!(visitor.visit_id(item.hir_id())); - try_visit!(visitor.visit_inline_asm(asm, item.hir_id())); + // Visit the fake body, which contains the asm statement. + // Therefore we should not visit the asm statement again + // outside of the body, or some visitors won't have their + // typeck results set correctly. + try_visit!(visitor.visit_nested_body(fake_body)); } ItemKind::TyAlias(ref ty, ref generics) => { try_visit!(visitor.visit_id(item.hir_id())); @@ -595,9 +597,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V:: defaultness: _, polarity: _, defaultness_span: _, - ref generics, - ref of_trait, - ref self_ty, + generics, + of_trait, + self_ty, items, }) => { try_visit!(visitor.visit_id(item.hir_id())); @@ -1047,7 +1049,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>( } GenericParamKind::Const { ref ty, ref default, synthetic: _ } => { try_visit!(visitor.visit_ty_unambig(ty)); - if let Some(ref default) = default { + if let Some(default) = default { try_visit!(visitor.visit_const_param_default(param.hir_id, default)); } } @@ -1403,8 +1405,8 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>( try_visit!(visitor.visit_generic_args(constraint.gen_args)); match constraint.kind { AssocItemConstraintKind::Equality { ref term } => match term { - Term::Ty(ref ty) => try_visit!(visitor.visit_ty_unambig(ty)), - Term::Const(ref c) => try_visit!(visitor.visit_const_arg_unambig(c)), + Term::Ty(ty) => try_visit!(visitor.visit_ty_unambig(ty)), + Term::Const(c) => try_visit!(visitor.visit_const_arg_unambig(c)), }, AssocItemConstraintKind::Bound { bounds } => { walk_list!(visitor, visit_param_bound, bounds) @@ -1444,10 +1446,12 @@ pub fn walk_inline_asm<'v, V: Visitor<'v>>( try_visit!(visitor.visit_expr(in_expr)); visit_opt!(visitor, visit_expr, out_expr); } - InlineAsmOperand::Const { anon_const, .. } - | InlineAsmOperand::SymFn { anon_const, .. } => { + InlineAsmOperand::Const { anon_const, .. } => { try_visit!(visitor.visit_anon_const(anon_const)); } + InlineAsmOperand::SymFn { expr, .. } => { + try_visit!(visitor.visit_expr(expr)); + } InlineAsmOperand::SymStatic { path, .. } => { try_visit!(visitor.visit_qpath(path, id, *op_sp)); } diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index b6689c95c4b..f5626937ec4 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -60,7 +60,7 @@ impl LanguageItems { self.reverse_items.get(&def_id).copied() } - pub fn iter(&self) -> impl Iterator<Item = (LangItem, DefId)> + '_ { + pub fn iter(&self) -> impl Iterator<Item = (LangItem, DefId)> { self.items .iter() .enumerate() @@ -351,6 +351,7 @@ language_item_table! { PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1); ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::None; + BikeshedGuaranteedNoDrop, sym::bikeshed_guaranteed_no_drop, bikeshed_guaranteed_no_drop, Target::Trait, GenericRequirement::Exact(0); MaybeUninit, sym::maybe_uninit, maybe_uninit, Target::Union, GenericRequirement::None; @@ -370,6 +371,8 @@ language_item_table! { PointerLike, sym::pointer_like, pointer_like, Target::Trait, GenericRequirement::Exact(0); + CoercePointeeValidated, sym::coerce_pointee_validated, coerce_pointee_validated_trait, Target::Trait, GenericRequirement::Exact(0); + ConstParamTy, sym::const_param_ty, const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0); UnsizedConstParamTy, sym::unsized_const_param_ty, unsized_const_param_ty_trait, Target::Trait, GenericRequirement::Exact(0); @@ -429,9 +432,13 @@ language_item_table! { ContractCheckRequires, sym::contract_check_requires, contract_check_requires_fn, Target::Fn, GenericRequirement::None; } +/// The requirement imposed on the generics of a lang item pub enum GenericRequirement { + /// No restriction on the generics None, + /// A minimum number of generics that is demanded on a lang item Minimum(usize), + /// The number of generics must match precisely as stipulated Exact(usize), } diff --git a/compiler/rustc_hir/src/stable_hash_impls.rs b/compiler/rustc_hir/src/stable_hash_impls.rs index db0d0fcf3b9..2709a826549 100644 --- a/compiler/rustc_hir/src/stable_hash_impls.rs +++ b/compiler/rustc_hir/src/stable_hash_impls.rs @@ -1,9 +1,9 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_span::def_id::DefPathHash; +use crate::HashIgnoredAttrId; use crate::hir::{ - Attribute, AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, - TraitItemId, + AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, }; use crate::hir_id::{HirId, ItemLocalId}; @@ -11,9 +11,11 @@ use crate::hir_id::{HirId, ItemLocalId}; /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in `rustc_middle`. pub trait HashStableContext: - rustc_ast::HashStableContext + rustc_target::HashStableContext + rustc_attr_data_structures::HashStableContext + + rustc_ast::HashStableContext + + rustc_abi::HashStableContext { - fn hash_attr(&mut self, _: &Attribute, hasher: &mut StableHasher); + fn hash_attr_id(&mut self, id: &HashIgnoredAttrId, hasher: &mut StableHasher); } impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId { @@ -116,8 +118,8 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> { } } -impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Attribute { +impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - hcx.hash_attr(self, hasher) + hcx.hash_attr_id(self, hasher) } } diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_hir/src/target.rs index f3e8f059c9e..70e95a84e68 100644 --- a/compiler/rustc_hir/src/target.rs +++ b/compiler/rustc_hir/src/target.rs @@ -110,7 +110,7 @@ impl Target { ItemKind::Macro(..) => Target::MacroDef, ItemKind::Mod(..) => Target::Mod, ItemKind::ForeignMod { .. } => Target::ForeignMod, - ItemKind::GlobalAsm(..) => Target::GlobalAsm, + ItemKind::GlobalAsm { .. } => Target::GlobalAsm, ItemKind::TyAlias(..) => Target::TyAlias, ItemKind::Enum(..) => Target::Enum, ItemKind::Struct(..) => Target::Struct, diff --git a/compiler/rustc_hir/src/tests.rs b/compiler/rustc_hir/src/tests.rs index e0e63d183c6..0837444ffdb 100644 --- a/compiler/rustc_hir/src/tests.rs +++ b/compiler/rustc_hir/src/tests.rs @@ -1,6 +1,6 @@ #![allow(rustc::symbol_intern_string_literal)] -use rustc_data_structures::stable_hasher::Hash64; +use rustc_hashes::Hash64; use rustc_span::def_id::{DefPathHash, StableCrateId}; use rustc_span::edition::Edition; use rustc_span::{Symbol, create_session_globals_then}; |
