diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 8e3ed595b38..18ac884c7cb 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -258,10 +258,12 @@ impl<'hir> LoweringContext<'_, 'hir> { ex.span = e.span; } // Merge attributes into the inner expression. - let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect(); - attrs.extend::>(ex.attrs.into()); - self.attrs[ex.hir_id] = &*self.arena.alloc_from_iter(attrs.iter().cloned()); - ex.attrs = attrs.into(); + self.attrs[ex.hir_id] = &*self.arena.alloc_from_iter( + e.attrs + .iter() + .map(|a| self.lower_attr(a)) + .chain(self.attrs[ex.hir_id].iter().cloned()), + ); return ex; } @@ -274,9 +276,8 @@ impl<'hir> LoweringContext<'_, 'hir> { }; let hir_id = self.lower_node_id(e.id); - let attrs = e.attrs.iter().map(|a| self.lower_attr(a)).collect::>(); - self.attrs.push_sparse(hir_id, &*self.arena.alloc_from_iter(attrs.iter().cloned())); - hir::Expr { hir_id, kind, span: e.span, attrs: attrs.into() } + self.lower_attrs(hir_id, &e.attrs); + hir::Expr { hir_id, kind, span: e.span } }) } @@ -684,12 +685,8 @@ impl<'hir> LoweringContext<'_, 'hir> { span, Some(hir::Movability::Static), ); - let generator = hir::Expr { - hir_id: self.lower_node_id(closure_node_id), - kind: generator_kind, - span, - attrs: ThinVec::new(), - }; + let generator = + hir::Expr { hir_id: self.lower_node_id(closure_node_id), kind: generator_kind, span }; // `future::from_generator`: let unstable_span = @@ -843,7 +840,6 @@ impl<'hir> LoweringContext<'_, 'hir> { hir_id: loop_hir_id, kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span), span, - attrs: ThinVec::new(), }); // mut pinned => loop { ... } @@ -1813,12 +1809,8 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::LoopSource::ForLoop, e.span.with_hi(orig_head_span.hi()), ); - let loop_expr = self.arena.alloc(hir::Expr { - hir_id: self.lower_node_id(e.id), - kind, - span: e.span, - attrs: ThinVec::new(), - }); + let loop_expr = + self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: e.span }); // `mut iter => { ... }` let iter_arm = self.arm(iter_pat, loop_expr); @@ -2154,8 +2146,8 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: AttrVec, ) -> hir::Expr<'hir> { let hir_id = self.next_id(); - self.attrs.push_sparse(hir_id, &*self.arena.alloc_from_iter(attrs.iter().cloned())); - hir::Expr { hir_id, kind, span, attrs } + self.lower_attrs(hir_id, &attrs); + hir::Expr { hir_id, kind, span } } fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> { diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 209e845f70f..10ee3f156ed 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -979,7 +979,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ret } - fn lower_attr(&mut self, attr: &Attribute) -> Attribute { + fn lower_attr(&self, attr: &Attribute) -> Attribute { // Note that we explicitly do not walk the path. Since we don't really // lower attributes (we use the AST version) there is nowhere to keep // the `HirId`s. We don't actually need HIR version of attributes anyway. @@ -999,7 +999,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { Attribute { kind, id: attr.id, style: attr.style, span: attr.span } } - fn lower_mac_args(&mut self, args: &MacArgs) -> MacArgs { + fn lower_mac_args(&self, args: &MacArgs) -> MacArgs { match *args { MacArgs::Empty => MacArgs::Empty, MacArgs::Delimited(dspan, delim, ref tokens) => { diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 0f1493542a7..ff1cee1fc20 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -6,7 +6,7 @@ use crate::{itemlikevisit, HirIdVec, LangItem}; use rustc_ast::util::parser::ExprPrecedence; use rustc_ast::{self as ast, CrateSugar, LlvmAsmDialect}; -use rustc_ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy}; +use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy}; pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto}; pub use rustc_ast::{CaptureBy, Movability, Mutability}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; @@ -1446,7 +1446,6 @@ pub struct AnonConst { pub struct Expr<'hir> { pub hir_id: HirId, pub kind: ExprKind<'hir>, - pub attrs: AttrVec, pub span: Span, } @@ -3071,7 +3070,7 @@ impl<'hir> Node<'hir> { #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] mod size_asserts { rustc_data_structures::static_assert_size!(super::Block<'static>, 48); - rustc_data_structures::static_assert_size!(super::Expr<'static>, 72); + rustc_data_structures::static_assert_size!(super::Expr<'static>, 64); rustc_data_structures::static_assert_size!(super::Pat<'static>, 88); rustc_data_structures::static_assert_size!(super::QPath<'static>, 24); rustc_data_structures::static_assert_size!(super::Ty<'static>, 72); diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 43d25608348..4da714aff31 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1334,7 +1334,7 @@ impl<'a> State<'a> { pub fn print_expr(&mut self, expr: &hir::Expr<'_>) { self.maybe_print_comment(expr.span.lo()); - self.print_outer_attributes(&expr.attrs); + self.print_outer_attributes(self.attrs(expr.hir_id)); self.ibox(INDENT_UNIT); self.ann.pre(self, AnnNode::Expr(expr)); match expr.kind { diff --git a/compiler/rustc_middle/src/ich/impls_hir.rs b/compiler/rustc_middle/src/ich/impls_hir.rs index 5ef70a89051..abf56832329 100644 --- a/compiler/rustc_middle/src/ich/impls_hir.rs +++ b/compiler/rustc_middle/src/ich/impls_hir.rs @@ -66,11 +66,10 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { fn hash_hir_expr(&mut self, expr: &hir::Expr<'_>, hasher: &mut StableHasher) { self.while_hashing_hir_bodies(true, |hcx| { - let hir::Expr { hir_id: _, ref span, ref kind, ref attrs } = *expr; + let hir::Expr { hir_id: _, ref span, ref kind } = *expr; span.hash_stable(hcx, hasher); kind.hash_stable(hcx, hasher); - attrs.hash_stable(hcx, hasher); }) } diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs index e8646695e0b..40c0f1f4589 100644 --- a/src/tools/clippy/clippy_lints/src/returns.rs +++ b/src/tools/clippy/clippy_lints/src/returns.rs @@ -177,7 +177,8 @@ fn check_final_expr<'tcx>( // simple return is always "bad" ExprKind::Ret(ref inner) => { // allow `#[cfg(a)] return a; #[cfg(b)] return b;` - if !expr.attrs.iter().any(attr_is_cfg) { + let attrs = cx.tcx.hir().attrs(expr.hir_id); + if !attrs.iter().any(attr_is_cfg) { let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner)); if !borrows { emit_return_lint( diff --git a/src/tools/clippy/clippy_lints/src/utils/inspector.rs b/src/tools/clippy/clippy_lints/src/utils/inspector.rs index 07a79592a4a..9e3973e1d51 100644 --- a/src/tools/clippy/clippy_lints/src/utils/inspector.rs +++ b/src/tools/clippy/clippy_lints/src/utils/inspector.rs @@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector { // fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) { - if !has_attr(cx.sess(), &expr.attrs) { + if !has_attr(cx.sess(), cx.tcx.hir().attrs(expr.hir_id)) { return; } print_expr(cx, expr, 0);