From abbe6259e1a4e7bf6e2eccbae0748c0e5c15ebb0 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 28 Nov 2019 23:50:47 +0100 Subject: Handle Attributes in arena. --- src/librustc/arena.rs | 2 +- src/librustc/hir/check_attr.rs | 12 ++++++------ src/librustc/hir/intravisit.rs | 8 ++++---- src/librustc/hir/lowering.rs | 13 ++++++------- src/librustc/hir/lowering/item.rs | 14 +++++++------- src/librustc/hir/map/collector.rs | 2 +- src/librustc/hir/mod.rs | 10 +++++----- src/librustc/middle/stability.rs | 4 ++-- 8 files changed, 32 insertions(+), 33 deletions(-) (limited to 'src/librustc') diff --git a/src/librustc/arena.rs b/src/librustc/arena.rs index d95e5f64f0c..56e089f20ba 100644 --- a/src/librustc/arena.rs +++ b/src/librustc/arena.rs @@ -127,7 +127,7 @@ macro_rules! arena_types { [] attribute: syntax::ast::Attribute, [] global_asm: rustc::hir::GlobalAsm, [] impl_item_ref: rustc::hir::ImplItemRef, - [] macro_def: rustc::hir::MacroDef, + [] macro_def: rustc::hir::MacroDef<$tcx>, [] path: rustc::hir::Path, [] trait_item_ref: rustc::hir::TraitItemRef, [] ty: rustc::hir::Ty, diff --git a/src/librustc/hir/check_attr.rs b/src/librustc/hir/check_attr.rs index d50d60e88c2..becbada225d 100644 --- a/src/librustc/hir/check_attr.rs +++ b/src/librustc/hir/check_attr.rs @@ -4,7 +4,7 @@ //! conflicts between multiple such attributes attached to the same //! item. -use crate::hir::{self, HirId, HirVec, Attribute, Item, ItemKind, TraitItem, TraitItemKind}; +use crate::hir::{self, HirId, Attribute, Item, ItemKind, TraitItem, TraitItemKind}; use crate::hir::DUMMY_HIR_ID; use crate::hir::def_id::DefId; use crate::hir::intravisit::{self, Visitor, NestedVisitorMap}; @@ -158,7 +158,7 @@ impl CheckAttrVisitor<'tcx> { fn check_attributes( &self, hir_id: HirId, - attrs: &HirVec, + attrs: &'hir [Attribute], span: &Span, target: Target, item: Option<&Item<'_>>, @@ -241,7 +241,7 @@ impl CheckAttrVisitor<'tcx> { fn check_track_caller( &self, attr_span: &Span, - attrs: &HirVec, + attrs: &'hir [Attribute], span: &Span, target: Target, ) -> bool { @@ -332,7 +332,7 @@ impl CheckAttrVisitor<'tcx> { /// Checks if the `#[repr]` attributes on `item` are valid. fn check_repr( &self, - attrs: &HirVec, + attrs: &'hir [Attribute], span: &Span, target: Target, item: Option<&Item<'_>>, @@ -477,7 +477,7 @@ impl CheckAttrVisitor<'tcx> { } } - fn check_used(&self, attrs: &HirVec, target: Target) { + fn check_used(&self, attrs: &'hir [Attribute], target: Target) { for attr in attrs { if attr.check_name(sym::used) && target != Target::Static { self.tcx.sess @@ -494,7 +494,7 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx Item<'tcx>) { let target = Target::from_item(item); - self.check_attributes(item.hir_id, &item.attrs, &item.span, target, Some(item)); + self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item)); intravisit::walk_item(self, item) } diff --git a/src/librustc/hir/intravisit.rs b/src/librustc/hir/intravisit.rs index 28c8e58feaf..8fe3c7232b1 100644 --- a/src/librustc/hir/intravisit.rs +++ b/src/librustc/hir/intravisit.rs @@ -367,7 +367,7 @@ pub trait Visitor<'v>: Sized { } fn visit_attribute(&mut self, _attr: &'v Attribute) { } - fn visit_macro_def(&mut self, macro_def: &'v MacroDef) { + fn visit_macro_def(&mut self, macro_def: &'v MacroDef<'v>) { walk_macro_def(self, macro_def) } fn visit_vis(&mut self, vis: &'v Visibility) { @@ -388,10 +388,10 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) { walk_list!(visitor, visit_macro_def, krate.exported_macros); } -pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) { +pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) { visitor.visit_id(macro_def.hir_id); visitor.visit_name(macro_def.span, macro_def.name); - walk_list!(visitor, visit_attribute, ¯o_def.attrs); + walk_list!(visitor, visit_attribute, macro_def.attrs); } pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod, mod_hir_id: HirId) { @@ -554,7 +554,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { walk_list!(visitor, visit_param_bound, bounds); } } - walk_list!(visitor, visit_attribute, &item.attrs); + walk_list!(visitor, visit_attribute, item.attrs); } pub fn walk_use<'v, V: Visitor<'v>>(visitor: &mut V, diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 6ab2bb53e2a..522f9adbaef 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -100,7 +100,7 @@ pub struct LoweringContext<'a, 'hir: 'a> { trait_items: BTreeMap, impl_items: BTreeMap, bodies: BTreeMap, - exported_macros: Vec, + exported_macros: Vec>, non_exported_macro_attrs: Vec, trait_impls: BTreeMap>, @@ -989,15 +989,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } - fn lower_attrs_extendable(&mut self, attrs: &[Attribute]) -> Vec { - attrs - .iter() - .map(|a| self.lower_attr(a)) - .collect() + fn lower_attrs_arena(&mut self, attrs: &[Attribute]) -> &'hir [Attribute] { + self.arena.alloc_from_iter( + attrs.iter().map(|a| self.lower_attr(a)) + ) } fn lower_attrs(&mut self, attrs: &[Attribute]) -> hir::HirVec { - self.lower_attrs_extendable(attrs).into() + attrs.iter().map(|a| self.lower_attr(a)).collect::>().into() } fn lower_attr(&mut self, attr: &Attribute) -> Attribute { diff --git a/src/librustc/hir/lowering/item.rs b/src/librustc/hir/lowering/item.rs index 7e5ac42d13e..0bc08dac151 100644 --- a/src/librustc/hir/lowering/item.rs +++ b/src/librustc/hir/lowering/item.rs @@ -228,7 +228,7 @@ impl LoweringContext<'_, 'hir> { pub fn lower_item(&mut self, i: &Item) -> Option> { let mut ident = i.ident; let mut vis = self.lower_visibility(&i.vis, None); - let attrs = self.lower_attrs(&i.attrs); + let attrs = self.lower_attrs_arena(&i.attrs); if let ItemKind::MacroDef(ref def) = i.kind { if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) { @@ -244,12 +244,12 @@ impl LoweringContext<'_, 'hir> { legacy: def.legacy, }); } else { - self.non_exported_macro_attrs.extend(attrs.into_iter()); + self.non_exported_macro_attrs.extend(attrs.iter().cloned()); } return None; } - let kind = self.lower_item_kind(i.span, i.id, &mut ident, &attrs, &mut vis, &i.kind); + let kind = self.lower_item_kind(i.span, i.id, &mut ident, attrs, &mut vis, &i.kind); Some(hir::Item { hir_id: self.lower_node_id(i.id), @@ -266,7 +266,7 @@ impl LoweringContext<'_, 'hir> { span: Span, id: NodeId, ident: &mut Ident, - attrs: &hir::HirVec, + attrs: &'hir [Attribute], vis: &mut hir::Visibility, i: &ItemKind, ) -> hir::ItemKind<'hir> { @@ -487,7 +487,7 @@ impl LoweringContext<'_, 'hir> { id: NodeId, vis: &mut hir::Visibility, ident: &mut Ident, - attrs: &hir::HirVec, + attrs: &'hir [Attribute], ) -> hir::ItemKind<'hir> { debug!("lower_use_tree(tree={:?})", tree); debug!("lower_use_tree: vis = {:?}", vis); @@ -550,7 +550,7 @@ impl LoweringContext<'_, 'hir> { hir::Item { hir_id: new_id, ident, - attrs: attrs.into_iter().cloned().collect(), + attrs, kind, vis, span, @@ -634,7 +634,7 @@ impl LoweringContext<'_, 'hir> { hir::Item { hir_id: new_hir_id, ident, - attrs: attrs.into_iter().cloned().collect(), + attrs, kind, vis, span: use_tree.span, diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index b56adf25e8a..c460d9ef6cc 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -530,7 +530,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } } - fn visit_macro_def(&mut self, macro_def: &'hir MacroDef) { + fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) { let node_id = self.hir_to_node_id[¯o_def.hir_id]; let def_index = self.definitions.opt_def_index(node_id).unwrap(); diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 4fb9f27c27d..063c0f9b278 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -746,7 +746,7 @@ pub struct Crate<'hir> { pub module: Mod, pub attrs: &'hir [Attribute], pub span: Span, - pub exported_macros: &'hir [MacroDef], + pub exported_macros: &'hir [MacroDef<'hir>], // Attributes from non-exported macros, kept only for collecting the library feature list. pub non_exported_macro_attrs: &'hir [Attribute], @@ -841,10 +841,10 @@ impl Crate<'_> { /// /// Not parsed directly, but created on macro import or `macro_rules!` expansion. #[derive(RustcEncodable, RustcDecodable, Debug, HashStable)] -pub struct MacroDef { +pub struct MacroDef<'hir> { pub name: Name, pub vis: Visibility, - pub attrs: HirVec, + pub attrs: &'hir [Attribute], pub hir_id: HirId, pub span: Span, pub body: TokenStream, @@ -2445,7 +2445,7 @@ pub struct ItemId { pub struct Item<'hir> { pub ident: Ident, pub hir_id: HirId, - pub attrs: HirVec, + pub attrs: &'hir [Attribute], pub kind: ItemKind<'hir>, pub vis: Visibility, pub span: Span, @@ -2804,7 +2804,7 @@ pub enum Node<'hir> { Arm(&'hir Arm), Block(&'hir Block), Local(&'hir Local), - MacroDef(&'hir MacroDef), + MacroDef(&'hir MacroDef<'hir>), /// `Ctor` refers to the constructor of an enum variant or struct. Only tuple or unit variants /// with synthesized constructors. diff --git a/src/librustc/middle/stability.rs b/src/librustc/middle/stability.rs index 6aa84b59338..1c717f0f1f5 100644 --- a/src/librustc/middle/stability.rs +++ b/src/librustc/middle/stability.rs @@ -324,7 +324,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> { }); } - fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) { + fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) { self.annotate(md.hir_id, &md.attrs, md.span, AnnotationKind::Required, |_| {}); } } @@ -397,7 +397,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'a, 'tcx> { intravisit::walk_foreign_item(self, i); } - fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) { + fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) { self.check_missing_stability(md.hir_id, md.span, "macro"); } } -- cgit 1.4.1-3-g733a5