diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 9b1642df11401..35cd0c3fa0894 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -11,7 +11,7 @@ use rustc_hir as hir; use rustc_hir::def::Res; use rustc_session::parse::feature_err; use rustc_span::hygiene::ForLoopLoc; -use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned}; +use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned, DUMMY_SP}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_target::asm; use std::collections::hash_map::Entry; @@ -229,6 +229,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Include parens in span, but only if it is a super-span. if e.span.contains(ex.span) { ex.span = e.span; + self.spans[ex.hir_id] = e.span; } // Merge attributes into the inner expression. let mut attrs: Vec<_> = e.attrs.iter().map(|a| self.lower_attr(a)).collect(); @@ -246,7 +247,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; hir::Expr { - hir_id: self.lower_node_id(e.id), + hir_id: self.lower_node_id(e.id, e.span), kind, span: e.span, attrs: e.attrs.iter().map(|a| self.lower_attr(a)).collect::>().into(), @@ -514,7 +515,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } }); hir::Arm { - hir_id: self.next_id(), + hir_id: self.next_id(arm.span), attrs: self.lower_attrs(&arm.attrs), pat, guard, @@ -548,7 +549,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Resume argument type. We let the compiler infer this to simplify the lowering. It is // fully constrained by `future::from_generator`. - let input_ty = hir::Ty { hir_id: self.next_id(), kind: hir::TyKind::Infer, span }; + let input_ty = hir::Ty { hir_id: self.next_id(span), kind: hir::TyKind::Infer, span }; // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. let decl = self.arena.alloc(hir::FnDecl { @@ -564,7 +565,7 @@ impl<'hir> LoweringContext<'_, 'hir> { Ident::with_dummy_span(sym::_task_context), hir::BindingAnnotation::Mutable, ); - let param = hir::Param { attrs: &[], hir_id: self.next_id(), pat, ty_span: span, span }; + let param = hir::Param { attrs: &[], hir_id: self.next_id(span), pat, ty_span: span, span }; let params = arena_vec![self; param]; let body_id = self.lower_body(move |this| { @@ -586,7 +587,7 @@ impl<'hir> LoweringContext<'_, 'hir> { Some(hir::Movability::Static), ); let generator = hir::Expr { - hir_id: self.lower_node_id(closure_node_id), + hir_id: self.lower_node_id(closure_node_id, span), kind: generator_kind, span, attrs: ThinVec::new(), @@ -683,7 +684,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // `::std::task::Poll::Ready(result) => break result` let loop_node_id = self.resolver.next_node_id(); - let loop_hir_id = self.lower_node_id(loop_node_id); + let loop_hir_id = self.lower_node_id(loop_node_id, span); let ready_arm = { let x_ident = Ident::with_dummy_span(sym::result); let (x_pat, x_pat_hid) = self.pat_ident(span, x_ident); @@ -1008,7 +1009,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let field_pats = self.arena.alloc_from_iter(fields.iter().map(|f| { let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments); hir::FieldPat { - hir_id: self.next_id(), + hir_id: self.next_id(f.span), ident: f.ident, pat, is_shorthand: f.is_shorthand, @@ -1149,7 +1150,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let target_id = match destination { Some((id, _)) => { if let Some(loop_id) = self.resolver.get_label_res(id) { - Ok(self.lower_node_id(loop_id)) + Ok(self.lower_node_id(loop_id, DUMMY_SP)) } else { Err(hir::LoopIdError::UnresolvedLabel) } @@ -1158,7 +1159,7 @@ impl<'hir> LoweringContext<'_, 'hir> { .loop_scopes .last() .cloned() - .map(|id| Ok(self.lower_node_id(id))) + .map(|id| Ok(self.lower_node_id(id, DUMMY_SP))) .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)), }; hir::Destination { label: destination.map(|(_, label)| label), target_id } @@ -1554,7 +1555,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_field(&mut self, f: &Field) -> hir::Field<'hir> { hir::Field { - hir_id: self.next_id(), + hir_id: self.next_id(f.span), ident: f.ident, expr: self.lower_expr(&f.expr), span: f.span, @@ -1619,6 +1620,7 @@ impl<'hir> LoweringContext<'_, 'hir> { None, ); head.span = desugared_span; + self.spans[head.hir_id] = desugared_span; let iter = Ident::with_dummy_span(sym::iter); @@ -1705,7 +1707,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // `[opt_ident]: loop { ... }` let kind = hir::ExprKind::Loop(loop_block, opt_label, hir::LoopSource::ForLoop); let loop_expr = self.arena.alloc(hir::Expr { - hir_id: self.lower_node_id(e.id), + hir_id: self.lower_node_id(e.id, e.span), kind, span: e.span, attrs: ThinVec::new(), @@ -1832,7 +1834,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let thin_attrs = ThinVec::from(attrs); let catch_scope = self.catch_scopes.last().copied(); let ret_expr = if let Some(catch_node) = catch_scope { - let target_id = Ok(self.lower_node_id(catch_node)); + let target_id = Ok(self.lower_node_id(catch_node, DUMMY_SP)); self.arena.alloc(self.expr( try_span, hir::ExprKind::Break( @@ -2005,8 +2007,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } fn expr_unsafe(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Expr<'hir> { - let hir_id = self.next_id(); let span = expr.span; + let hir_id = self.next_id(span); self.expr( span, hir::ExprKind::Block( @@ -2044,16 +2046,16 @@ impl<'hir> LoweringContext<'_, 'hir> { kind: hir::ExprKind<'hir>, attrs: AttrVec, ) -> hir::Expr<'hir> { - hir::Expr { hir_id: self.next_id(), kind, span, attrs } + hir::Expr { hir_id: self.next_id(span), kind, span, attrs } } fn field(&mut self, ident: Ident, expr: &'hir hir::Expr<'hir>, span: Span) -> hir::Field<'hir> { - hir::Field { hir_id: self.next_id(), ident, span, expr, is_shorthand: false } + hir::Field { hir_id: self.next_id(span), ident, span, expr, is_shorthand: false } } fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> { hir::Arm { - hir_id: self.next_id(), + hir_id: self.next_id(expr.span), attrs: &[], pat, guard: None, diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index eef6d38aa0584..8bd964cc3a9e8 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -13,7 +13,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::LocalDefId; use rustc_span::source_map::{respan, DesugaringKind}; use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::Span; +use rustc_span::{Span, DUMMY_SP}; use rustc_target::spec::abi; use smallvec::{smallvec, SmallVec}; @@ -34,8 +34,8 @@ impl ItemLowerer<'_, '_, '_> { } impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { - fn visit_mod(&mut self, m: &'a Mod, _s: Span, _attrs: &[Attribute], n: NodeId) { - let hir_id = self.lctx.lower_node_id(n); + fn visit_mod(&mut self, m: &'a Mod, span: Span, _attrs: &[Attribute], n: NodeId) { + let hir_id = self.lctx.lower_node_id(n, span); self.lctx.modules.insert( hir_id, @@ -230,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> { if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind { if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) { - let hir_id = self.lower_node_id(i.id); + let hir_id = self.lower_node_id(i.id, i.span); let body = P(self.lower_mac_args(body)); self.exported_macros.push(hir::MacroDef { ident, @@ -248,7 +248,14 @@ impl<'hir> LoweringContext<'_, 'hir> { 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), ident, attrs, kind, vis, span: i.span }) + Some(hir::Item { + hir_id: self.lower_node_id(i.id, i.span), + ident, + attrs, + kind, + vis, + span: i.span, + }) } fn lower_item_kind( @@ -357,14 +364,14 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_generics(generics, ImplTraitContext::disallowed()), ), ItemKind::Struct(ref struct_def, ref generics) => { - let struct_def = self.lower_variant_data(struct_def); + let struct_def = self.lower_variant_data(span, struct_def); hir::ItemKind::Struct( struct_def, self.lower_generics(generics, ImplTraitContext::disallowed()), ) } ItemKind::Union(ref vdata, ref generics) => { - let vdata = self.lower_variant_data(vdata); + let vdata = self.lower_variant_data(span, vdata); hir::ItemKind::Union( vdata, self.lower_generics(generics, ImplTraitContext::disallowed()), @@ -395,7 +402,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // method, it will not be considered an in-band // lifetime to be added, but rather a reference to a // parent lifetime. - let lowered_trait_impl_id = self.lower_node_id(id); + let lowered_trait_impl_id = self.lower_node_id(id, DUMMY_SP); let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs( ast_generics, def_id, @@ -537,7 +544,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let span = path.span; self.with_hir_id_owner(new_node_id, |this| { - let new_id = this.lower_node_id(new_node_id); + let new_id = this.lower_node_id(new_node_id, span); let res = this.lower_res(res); let path = this.lower_path_extra(res, &path, ParamMode::Explicit, None); let kind = hir::ItemKind::Use(path, hir::UseKind::Single); @@ -594,7 +601,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Add all the nested `PathListItem`s to the HIR. for &(ref use_tree, id) in trees { - let new_hir_id = self.lower_node_id(id); + let new_hir_id = self.lower_node_id(id, use_tree.span); let mut prefix = prefix.clone(); @@ -663,7 +670,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let segments = self.arena.alloc_from_iter(path.segments.iter().map(|seg| hir::PathSegment { ident: seg.ident, - hir_id: seg.hir_id.map(|_| self.next_id()), + hir_id: seg.hir_id.map(|_| self.next_id(seg.ident.span)), res: seg.res, args: None, infer_args: seg.infer_args, @@ -679,7 +686,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::VisibilityKind::Restricted { ref path, hir_id: _ } => { hir::VisibilityKind::Restricted { path: self.rebuild_use_path(path), - hir_id: self.next_id(), + hir_id: self.next_id(vis.span), } } }; @@ -689,7 +696,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_foreign_item(&mut self, i: &ForeignItem) -> hir::ForeignItem<'hir> { let def_id = self.resolver.local_def_id(i.id); hir::ForeignItem { - hir_id: self.lower_node_id(i.id), + hir_id: self.lower_node_id(i.id, i.span), ident: i.ident, attrs: self.lower_attrs(&i.attrs), kind: match i.kind { @@ -724,7 +731,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef<'hir> { hir::ForeignItemRef { - id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id) }, + id: hir::ForeignItemId { hir_id: self.lower_node_id(i.id, i.span) }, ident: i.ident, span: i.span, vis: self.lower_visibility(&i.vis, Some(i.id)), @@ -738,15 +745,15 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_variant(&mut self, v: &Variant) -> hir::Variant<'hir> { hir::Variant { attrs: self.lower_attrs(&v.attrs), - data: self.lower_variant_data(&v.data), + data: self.lower_variant_data(v.span, &v.data), disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)), - id: self.lower_node_id(v.id), + id: self.lower_node_id(v.id, v.span), ident: v.ident, span: v.span, } } - fn lower_variant_data(&mut self, vdata: &VariantData) -> hir::VariantData<'hir> { + fn lower_variant_data(&mut self, span: Span, vdata: &VariantData) -> hir::VariantData<'hir> { match *vdata { VariantData::Struct(ref fields, recovered) => hir::VariantData::Struct( self.arena @@ -756,9 +763,9 @@ impl<'hir> LoweringContext<'_, 'hir> { VariantData::Tuple(ref fields, id) => hir::VariantData::Tuple( self.arena .alloc_from_iter(fields.iter().enumerate().map(|f| self.lower_struct_field(f))), - self.lower_node_id(id), + self.lower_node_id(id, span), ), - VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id)), + VariantData::Unit(id) => hir::VariantData::Unit(self.lower_node_id(id, span)), } } @@ -777,7 +784,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; hir::StructField { span: f.span, - hir_id: self.lower_node_id(f.id), + hir_id: self.lower_node_id(f.id, f.span), ident: match f.ident { Some(ident) => ident, // FIXME(jseyfried): positional field hygiene. @@ -824,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; hir::TraitItem { - hir_id: self.lower_node_id(i.id), + hir_id: self.lower_node_id(i.id, i.span), ident: i.ident, attrs: self.lower_attrs(&i.attrs), generics, @@ -844,7 +851,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } AssocItemKind::MacCall(..) => unimplemented!(), }; - let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id) }; + let id = hir::TraitItemId { hir_id: self.lower_node_id(i.id, i.span) }; let defaultness = hir::Defaultness::Default { has_value: has_default }; hir::TraitItemRef { id, ident: i.ident, span: i.span, defaultness, kind } } @@ -908,7 +915,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let has_value = true; let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); hir::ImplItem { - hir_id: self.lower_node_id(i.id), + hir_id: self.lower_node_id(i.id, i.span), ident: i.ident, attrs: self.lower_attrs(&i.attrs), generics, @@ -924,7 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let has_value = true; let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); hir::ImplItemRef { - id: hir::ImplItemId { hir_id: self.lower_node_id(i.id) }, + id: hir::ImplItemId { hir_id: self.lower_node_id(i.id, i.span) }, ident: i.ident, span: i.span, vis: self.lower_visibility(&i.vis, Some(i.id)), @@ -956,9 +963,9 @@ impl<'hir> LoweringContext<'_, 'hir> { VisibilityKind::Restricted { ref path, id } => { debug!("lower_visibility: restricted path id = {:?}", id); let lowered_id = if let Some(owner) = explicit_owner { - self.lower_node_id_with_owner(id, owner) + self.lower_node_id_with_owner(id, owner, v.span) } else { - self.lower_node_id(id) + self.lower_node_id(id, v.span) }; let res = self.expect_full_res(id); let res = self.lower_res(res); @@ -1013,7 +1020,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> { hir::Param { attrs: self.lower_attrs(¶m.attrs), - hir_id: self.lower_node_id(param.id), + hir_id: self.lower_node_id(param.id, param.span), pat: self.lower_pat(¶m.pat), ty_span: param.ty.span, span: param.span, @@ -1463,7 +1470,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }), WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => { hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { - hir_id: self.lower_node_id(id), + hir_id: self.lower_node_id(id, span), lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()), rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()), span, diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index f81dc39842cb9..ff91806bf78e3 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -51,7 +51,7 @@ use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res}; use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX}; use rustc_hir::definitions::{DefKey, DefPathData, Definitions}; use rustc_hir::intravisit; -use rustc_hir::{ConstArg, GenericArg, ParamName}; +use rustc_hir::{ConstArg, GenericArg, HirIdVec, ParamName}; use rustc_index::vec::{Idx, IndexVec}; use rustc_session::lint::{builtin::BARE_TRAIT_OBJECTS, BuiltinLintDiagnostics, LintBuffer}; use rustc_session::parse::ParseSess; @@ -59,7 +59,7 @@ use rustc_session::Session; use rustc_span::hygiene::ExpnId; use rustc_span::source_map::{respan, DesugaringKind, ExpnData, ExpnKind}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; -use rustc_span::Span; +use rustc_span::{Span, DUMMY_SP}; use smallvec::{smallvec, SmallVec}; use std::collections::BTreeMap; @@ -110,6 +110,9 @@ struct LoweringContext<'a, 'hir: 'a> { modules: BTreeMap, + /// Collected spans from the AST. + spans: HirIdVec, + generator_kind: Option, /// When inside an `async` context, this is the `HirId` of the @@ -304,6 +307,7 @@ pub fn lower_crate<'a, 'hir>( bodies: BTreeMap::new(), trait_impls: BTreeMap::new(), modules: BTreeMap::new(), + spans: Default::default(), exported_macros: Vec::new(), non_exported_macro_attrs: Vec::new(), catch_scopes: Vec::new(), @@ -556,7 +560,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } - self.lower_node_id(CRATE_NODE_ID); + self.lower_node_id(CRATE_NODE_ID, c.span); debug_assert!(self.node_id_to_hir_id[CRATE_NODE_ID] == Some(hir::CRATE_HIR_ID)); visit::walk_crate(&mut MiscCollector { lctx: &mut self }, c); @@ -590,6 +594,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.resolver.definitions().init_def_id_to_hir_id_mapping(def_id_to_hir_id); + //FIXME(cjgillot) Ideally, each LocalDefId would be a HIR owner. + // In the mean time, allocate the missing empty vectors. + self.spans.push_owner(Idx::new(self.resolver.definitions().def_index_count() - 1)); + hir::Crate { item: hir::CrateItem { module, attrs, span: c.span }, exported_macros: self.arena.alloc_from_iter(self.exported_macros), @@ -604,6 +612,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { modules: self.modules, proc_macros, trait_map, + spans: self.spans, } } @@ -619,7 +628,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Set up the counter if needed. self.item_local_id_counters.entry(owner).or_insert(0); // Always allocate the first `HirId` for the owner itself. - let lowered = self.lower_node_id_with_owner(owner, owner); + let lowered = self.lower_node_id_with_owner(owner, owner, DUMMY_SP); debug_assert_eq!(lowered.local_id.as_u32(), 0); lowered } @@ -627,6 +636,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { fn lower_node_id_generic( &mut self, ast_node_id: NodeId, + span: Span, alloc_hir_id: impl FnOnce(&mut Self) -> hir::HirId, ) -> hir::HirId { assert_ne!(ast_node_id, DUMMY_NODE_ID); @@ -637,15 +647,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.node_id_to_hir_id.resize(min_size, None); } - if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] { + let hir_id = if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] { + if span != DUMMY_SP { + // Some HIR owners are lowered before the traversal of the AST. + // They use DUMMY_SP as a placeholder, to be overwritten here. + #[cfg(debug_assertions)] + if self.spans[existing_hir_id] != DUMMY_SP { + assert_eq!(self.spans[existing_hir_id], span); + } + self.spans[existing_hir_id] = span; + } existing_hir_id } else { // Generate a new `HirId`. let hir_id = alloc_hir_id(self); self.node_id_to_hir_id[ast_node_id] = Some(hir_id); + self.spans.push(hir_id, span); hir_id - } + }; + + hir_id } fn with_hir_id_owner(&mut self, owner: NodeId, f: impl FnOnce(&mut Self) -> T) -> T { @@ -672,8 +694,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// actually used in the HIR, as that would trigger an assertion in the /// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped /// properly. Calling the method twice with the same `NodeId` is fine though. - fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId { - self.lower_node_id_generic(ast_node_id, |this| { + fn lower_node_id(&mut self, ast_node_id: NodeId, span: Span) -> hir::HirId { + self.lower_node_id_generic(ast_node_id, span, |this| { let &mut (owner, ref mut local_id_counter) = this.current_hir_id_owner.last_mut().unwrap(); let local_id = *local_id_counter; @@ -682,8 +704,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }) } - fn lower_node_id_with_owner(&mut self, ast_node_id: NodeId, owner: NodeId) -> hir::HirId { - self.lower_node_id_generic(ast_node_id, |this| { + fn lower_node_id_with_owner( + &mut self, + ast_node_id: NodeId, + owner: NodeId, + span: Span, + ) -> hir::HirId { + self.lower_node_id_generic(ast_node_id, span, |this| { let local_id_counter = this .item_local_id_counters .get_mut(&owner) @@ -705,14 +732,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }) } - fn next_id(&mut self) -> hir::HirId { + fn next_id(&mut self, span: Span) -> hir::HirId { let node_id = self.resolver.next_node_id(); - self.lower_node_id(node_id) + self.lower_node_id(node_id, span) } fn lower_res(&mut self, res: Res) -> Res { res.map_id(|id| { - self.lower_node_id_generic(id, |_| { + self.lower_node_id_generic(id, DUMMY_SP, |_| { panic!("expected `NodeId` to be lowered already for res {:#?}", res); }) }) @@ -834,7 +861,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); hir::GenericParam { - hir_id: self.lower_node_id(node_id), + hir_id: self.lower_node_id(node_id, span), name: hir_name, attrs: &[], bounds: &[], @@ -1159,7 +1186,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }; hir::TypeBinding { - hir_id: self.lower_node_id(constraint.id), + hir_id: self.lower_node_id(constraint.id, constraint.span), ident: constraint.ident, kind, span: constraint.span, @@ -1209,9 +1236,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { tokens: None, }; - let ct = self.with_new_scopes(|this| hir::AnonConst { - hir_id: this.lower_node_id(node_id), - body: this.lower_const_body(path_expr.span, Some(&path_expr)), + let ct = self.lower_anon_const(&AnonConst { + id: node_id, + value: rustc_ast::ptr::P(path_expr), }); return GenericArg::Const(ConstArg { value: ct, span: ty.span }); } @@ -1238,7 +1265,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { param_mode: ParamMode, itctx: ImplTraitContext<'_, 'hir>, ) -> hir::Ty<'hir> { - let id = self.lower_node_id(t.id); + let id = self.lower_node_id(t.id, t.span); let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx); let ty = self.ty_path(id, t.span, qpath); if let hir::TyKind::TraitObject(..) = ty.kind { @@ -1248,7 +1275,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> { - hir::Ty { hir_id: self.next_id(), kind, span } + hir::Ty { hir_id: self.next_id(span), kind, span } } fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> { @@ -1391,7 +1418,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Set the name to `impl Bound1 + Bound2`. let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); in_band_ty_params.push(hir::GenericParam { - hir_id: self.lower_node_id(def_node_id), + hir_id: self.lower_node_id(def_node_id, span), name: ParamName::Plain(ident), pure_wrt_drop: false, attrs: &[], @@ -1446,7 +1473,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } }; - hir::Ty { kind, span: t.span, hir_id: self.lower_node_id(t.id) } + hir::Ty { kind, span: t.span, hir_id: self.lower_node_id(t.id, t.span) } } fn lower_opaque_impl_trait( @@ -1518,7 +1545,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { opaque_ty_span: Span, ) -> hir::HirId { let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item); - let opaque_ty_id = self.lower_node_id(opaque_ty_node_id); + let opaque_ty_id = self.lower_node_id(opaque_ty_node_id, opaque_ty_span); // Generate an `type Foo = impl Trait;` declaration. trace!("registering opaque type with id {:#?}", opaque_ty_id); let opaque_ty_item = hir::Item { @@ -1573,15 +1600,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { intravisit::NestedVisitorMap::None } - fn visit_generic_args(&mut self, span: Span, parameters: &'v hir::GenericArgs<'v>) { + fn visit_generic_args(&mut self, parameters: &'v hir::GenericArgs<'v>) { // Don't collect elided lifetimes used inside of `Fn()` syntax. if parameters.parenthesized { let old_collect_elided_lifetimes = self.collect_elided_lifetimes; self.collect_elided_lifetimes = false; - intravisit::walk_generic_args(self, span, parameters); + intravisit::walk_generic_args(self, parameters); self.collect_elided_lifetimes = old_collect_elided_lifetimes; } else { - intravisit::walk_generic_args(self, span, parameters); + intravisit::walk_generic_args(self, parameters); } } @@ -1654,14 +1681,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.already_defined_lifetimes.insert(name); self.output_lifetimes.push(hir::GenericArg::Lifetime(hir::Lifetime { - hir_id: self.context.next_id(), + hir_id: self.context.next_id(lifetime.span), span: lifetime.span, name, })); let def_node_id = self.context.resolver.next_node_id(); - let hir_id = - self.context.lower_node_id_with_owner(def_node_id, self.opaque_ty_id); + let hir_id = self.context.lower_node_id_with_owner( + def_node_id, + self.opaque_ty_id, + lifetime.span, + ); self.context.resolver.create_def( self.parent, def_node_id, @@ -1745,7 +1775,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let init = l.init.as_ref().map(|e| self.lower_expr(e)); ( hir::Local { - hir_id: self.lower_node_id(l.id), + hir_id: self.lower_node_id(l.id, l.span), ty, pat: self.lower_pat(&l.pat), init, @@ -2041,7 +2071,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { |&(span, hir_name)| { // Input lifetime like `'a` or `'1`: GenericArg::Lifetime(hir::Lifetime { - hir_id: self.next_id(), + hir_id: self.next_id(span), span, name: hir::LifetimeName::Param(hir_name), }) @@ -2050,7 +2080,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { generic_args.extend(lifetime_params[input_lifetimes_count..].iter().map(|&(span, _)| // Output lifetime like `'_`. GenericArg::Lifetime(hir::Lifetime { - hir_id: self.next_id(), + hir_id: self.next_id(span), span, name: hir::LifetimeName::Implicit, }))); @@ -2098,7 +2128,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // ::std::future::Future hir::LangItem::Future, span, - self.next_id(), + self.next_id(span), future_args, ) } @@ -2151,7 +2181,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { span: Span, name: hir::LifetimeName, ) -> hir::Lifetime { - hir::Lifetime { hir_id: self.lower_node_id(id), span, name } + hir::Lifetime { hir_id: self.lower_node_id(id, span), span, name } } fn lower_generic_params_mut<'s>( @@ -2254,7 +2284,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }; hir::GenericParam { - hir_id: self.lower_node_id(param.id), + hir_id: self.lower_node_id(param.id, param.ident.span), name, span: param.ident.span, pure_wrt_drop: self.sess.contains_name(¶m.attrs, sym::may_dangle), @@ -2273,7 +2303,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { hir::QPath::Resolved(None, path) => path, qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath), }; - hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id) } + hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id, path.span) } } fn lower_poly_trait_ref( @@ -2359,7 +2389,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { .flatten(), ); let rules = self.lower_block_check_mode(&b.rules); - let hir_id = self.lower_node_id(b.id); + let hir_id = self.lower_node_id(b.id, b.span); hir::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break } } @@ -2372,9 +2402,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst { - self.with_new_scopes(|this| hir::AnonConst { - hir_id: this.lower_node_id(c.id), - body: this.lower_const_body(c.value.span, Some(&c.value)), + self.with_new_scopes(|this| { + let body = this.lower_const_body(c.value.span, Some(&c.value)); + let span = this.bodies[&body].value.span; + hir::AnonConst { hir_id: this.lower_node_id(c.id, span), body } }) } @@ -2385,13 +2416,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let mut ids: SmallVec<[hir::Stmt<'hir>; 1]> = item_ids .into_iter() .map(|item_id| { - let item_id = hir::ItemId { id: self.lower_node_id(item_id) }; + let item_id = hir::ItemId { id: self.lower_node_id(item_id, DUMMY_SP) }; self.stmt(s.span, hir::StmtKind::Item(item_id)) }) .collect(); ids.push({ hir::Stmt { - hir_id: self.lower_node_id(s.id), + hir_id: self.lower_node_id(s.id, s.span), kind: hir::StmtKind::Local(self.arena.alloc(l)), span: s.span, } @@ -2407,8 +2438,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { .map(|item_id| { let hir_id = id .take() - .map(|id| self.lower_node_id(id)) - .unwrap_or_else(|| self.next_id()); + .map(|id| self.lower_node_id(id, s.span)) + .unwrap_or_else(|| self.next_id(s.span)); hir::Stmt { hir_id, kind: hir::StmtKind::Item(item_id), span: s.span } }) @@ -2419,7 +2450,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { StmtKind::Empty => return smallvec![], StmtKind::MacCall(..) => panic!("shouldn't exist here"), }; - smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id), kind, span: s.span }] + smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id, s.span), kind, span: s.span }] } fn lower_block_check_mode(&mut self, b: &BlockCheckMode) -> hir::BlockCheckMode { @@ -2454,7 +2485,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Helper methods for building HIR. fn stmt(&mut self, span: Span, kind: hir::StmtKind<'hir>) -> hir::Stmt<'hir> { - hir::Stmt { span, kind, hir_id: self.next_id() } + hir::Stmt { span, kind, hir_id: self.next_id(span) } } fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> { @@ -2469,7 +2500,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { pat: &'hir hir::Pat<'hir>, source: hir::LocalSource, ) -> hir::Stmt<'hir> { - let local = hir::Local { attrs, hir_id: self.next_id(), init, pat, source, span, ty: None }; + let local = + hir::Local { attrs, hir_id: self.next_id(span), init, pat, source, span, ty: None }; self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local))) } @@ -2486,7 +2518,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let blk = hir::Block { stmts, expr, - hir_id: self.next_id(), + hir_id: self.next_id(span), rules: hir::BlockCheckMode::DefaultBlock, span, targeted_by_break: false, @@ -2525,7 +2557,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { pat: &'hir hir::Pat<'hir>, ) -> &'hir [hir::FieldPat<'hir>] { let field = hir::FieldPat { - hir_id: self.next_id(), + hir_id: self.next_id(span), ident: Ident::new(sym::integer(0), span), is_shorthand: false, pat, @@ -2554,7 +2586,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ident: Ident, bm: hir::BindingAnnotation, ) -> (&'hir hir::Pat<'hir>, hir::HirId) { - let hir_id = self.next_id(); + let hir_id = self.next_id(span); ( self.arena.alloc(hir::Pat { @@ -2573,7 +2605,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> { self.arena.alloc(hir::Pat { - hir_id: self.next_id(), + hir_id: self.next_id(span), kind, span, default_binding_modes: true, @@ -2582,7 +2614,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> { self.arena.alloc(hir::Pat { - hir_id: self.next_id(), + hir_id: self.next_id(span), kind, span, default_binding_modes: false, @@ -2608,7 +2640,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // The original ID is taken by the `PolyTraitRef`, // so the `Ty` itself needs a different one. - hir_id = self.next_id(); + hir_id = self.next_id(span); hir::TyKind::TraitObject( arena_vec![self; principal], self.elided_dyn_bound(span), @@ -2634,7 +2666,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { AnonymousLifetimeMode::CreateParameter => { let fresh_name = self.collect_fresh_in_band_lifetime(span); hir::Lifetime { - hir_id: self.next_id(), + hir_id: self.next_id(span), span, name: hir::LifetimeName::Param(fresh_name), } @@ -2729,7 +2761,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } let r = hir::Lifetime { - hir_id: self.next_id(), + hir_id: self.next_id(span), span, name: hir::LifetimeName::ImplicitObjectLifetimeDefault, }; @@ -2738,7 +2770,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn new_implicit_lifetime(&mut self, span: Span) -> hir::Lifetime { - hir::Lifetime { hir_id: self.next_id(), span, name: hir::LifetimeName::Implicit } + hir::Lifetime { hir_id: self.next_id(span), span, name: hir::LifetimeName::Implicit } } fn maybe_lint_bare_trait(&mut self, span: Span, id: NodeId, is_global: bool) { diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index e4e7b24d29e52..d0f04f4f28228 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -57,7 +57,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::FieldPat { - hir_id: self.next_id(), + hir_id: self.next_id(f.span), ident: f.ident, pat: self.lower_pat(&f.pat), is_shorthand: f.is_shorthand, @@ -242,7 +242,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { hir::PatKind::Binding( self.lower_binding_mode(binding_mode), - self.lower_node_id(canonical_id), + self.lower_node_id(canonical_id, rustc_span::DUMMY_SP), ident, lower_sub(self), ) @@ -274,7 +274,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// Construct a `Pat` with the `HirId` of `p.id` lowered. fn pat_with_node_id_of(&mut self, p: &Pat, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> { self.arena.alloc(hir::Pat { - hir_id: self.lower_node_id(p.id), + hir_id: self.lower_node_id(p.id, p.span), kind, span: p.span, default_binding_modes: true, diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index 6afed355dc338..20b8ce72add57 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -126,7 +126,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Otherwise, the base path is an implicit `Self` type path, // e.g., `Vec` in `Vec::new` or `::Item` in // `::Item::default`. - let new_id = self.next_id(); + let new_id = self.next_id(p.span); self.arena.alloc(self.ty_path(new_id, p.span, hir::QPath::Resolved(qself, path))) }; @@ -158,7 +158,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } // Wrap the associated extension in another type node. - let new_id = self.next_id(); + let new_id = self.next_id(p.span); ty = self.arena.alloc(self.ty_path(new_id, p.span, qpath)); } @@ -340,9 +340,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let res = self.expect_full_res(segment.id); let id = if let Some(owner) = explicit_owner { - self.lower_node_id_with_owner(segment.id, owner) + self.lower_node_id_with_owner(segment.id, owner, segment.ident.span) } else { - self.lower_node_id(segment.id) + self.lower_node_id(segment.id, segment.ident.span) }; debug!( "lower_path_segment: ident={:?} original-id={:?} new-id={:?}", @@ -426,6 +426,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ) -> hir::TypeBinding<'hir> { let ident = Ident::with_dummy_span(hir::FN_OUTPUT_NAME); let kind = hir::TypeBindingKind::Equality { ty }; - hir::TypeBinding { hir_id: self.next_id(), span, ident, kind } + hir::TypeBinding { hir_id: self.next_id(span), span, ident, kind } } } diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index acd254ae85cb1..c69533023ccf8 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1,6 +1,7 @@ use crate::def::{DefKind, Namespace, Res}; use crate::def_id::DefId; crate use crate::hir_id::HirId; +use crate::hir_id::HirIdVec; use crate::{itemlikevisit, LangItem}; use rustc_ast::util::parser::ExprPrecedence; @@ -641,6 +642,9 @@ pub struct Crate<'hir> { pub proc_macros: Vec, pub trait_map: BTreeMap>, + + /// Collected spans from the AST. + pub spans: HirIdVec, } impl Crate<'hir> { diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index cc8ac4cf5be51..a230bde8be001 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -1,4 +1,5 @@ use crate::def_id::{LocalDefId, CRATE_DEF_INDEX}; +use rustc_index::vec::IndexVec; use std::fmt; /// Uniquely identifies a node in the HIR of the current crate. It is @@ -45,3 +46,45 @@ pub const CRATE_HIR_ID: HirId = HirId { owner: LocalDefId { local_def_index: CRATE_DEF_INDEX }, local_id: ItemLocalId::from_u32(0), }; + +#[derive(Clone, Default, Debug, Encodable, Decodable)] +pub struct HirIdVec { + map: IndexVec>, +} + +impl HirIdVec { + pub fn push_owner(&mut self, id: LocalDefId) { + self.map.ensure_contains_elem(id, IndexVec::new); + } + + pub fn push(&mut self, id: HirId, value: T) { + if id.local_id == ItemLocalId::from_u32(0) { + self.push_owner(id.owner); + } + let submap = &mut self.map[id.owner]; + let _ret_id = submap.push(value); + debug_assert_eq!(_ret_id, id.local_id); + } + + pub fn get(&self, id: HirId) -> Option<&T> { + self.map.get(id.owner)?.get(id.local_id) + } + + pub fn get_owner(&self, id: LocalDefId) -> &IndexVec { + &self.map[id] + } +} + +impl std::ops::Index for HirIdVec { + type Output = T; + + fn index(&self, id: HirId) -> &T { + &self.map[id.owner][id.local_id] + } +} + +impl std::ops::IndexMut for HirIdVec { + fn index_mut(&mut self, id: HirId) -> &mut T { + &mut self.map[id.owner][id.local_id] + } +} diff --git a/compiler/rustc_hir/src/intravisit.rs b/compiler/rustc_hir/src/intravisit.rs index b0e82214cf95f..261c832dd0f37 100644 --- a/compiler/rustc_hir/src/intravisit.rs +++ b/compiler/rustc_hir/src/intravisit.rs @@ -37,7 +37,6 @@ use crate::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor}; use rustc_ast::walk_list; use rustc_ast::{Attribute, Label}; use rustc_span::symbol::{Ident, Symbol}; -use rustc_span::Span; pub struct DeepVisitor<'v, V> { visitor: &'v mut V, @@ -335,13 +334,13 @@ pub trait Visitor<'v>: Sized { fn visit_id(&mut self, _hir_id: HirId) { // Nothing to do. } - fn visit_name(&mut self, _span: Span, _name: Symbol) { + fn visit_name(&mut self, _name: Symbol) { // Nothing to do. } fn visit_ident(&mut self, ident: Ident) { walk_ident(self, ident) } - fn visit_mod(&mut self, m: &'v Mod<'v>, _s: Span, n: HirId) { + fn visit_mod(&mut self, m: &'v Mod<'v>, n: HirId) { walk_mod(self, m, n) } fn visit_foreign_item(&mut self, i: &'v ForeignItem<'v>) { @@ -383,8 +382,8 @@ pub trait Visitor<'v>: Sized { fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) { walk_fn_decl(self, fd) } - fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, s: Span, id: HirId) { - walk_fn(self, fk, fd, b, s, id) + fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, id: HirId) { + walk_fn(self, fk, fd, b, id) } fn visit_use(&mut self, path: &'v Path<'v>, hir_id: HirId) { walk_use(self, path, hir_id) @@ -419,7 +418,6 @@ pub trait Visitor<'v>: Sized { _: Symbol, _: &'v Generics<'v>, _parent_id: HirId, - _: Span, ) { walk_struct_def(self, s) } @@ -431,7 +429,6 @@ pub trait Visitor<'v>: Sized { enum_definition: &'v EnumDef<'v>, generics: &'v Generics<'v>, item_id: HirId, - _: Span, ) { walk_enum_def(self, enum_definition, generics, item_id) } @@ -451,17 +448,17 @@ pub trait Visitor<'v>: Sized { fn visit_lifetime(&mut self, lifetime: &'v Lifetime) { walk_lifetime(self, lifetime) } - fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId, span: Span) { - walk_qpath(self, qpath, id, span) + fn visit_qpath(&mut self, qpath: &'v QPath<'v>, id: HirId) { + walk_qpath(self, qpath, id) } fn visit_path(&mut self, path: &'v Path<'v>, _id: HirId) { walk_path(self, path) } - fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment<'v>) { - walk_path_segment(self, path_span, path_segment) + fn visit_path_segment(&mut self, path_segment: &'v PathSegment<'v>) { + walk_path_segment(self, path_segment) } - fn visit_generic_args(&mut self, path_span: Span, generic_args: &'v GenericArgs<'v>) { - walk_generic_args(self, path_span, generic_args) + fn visit_generic_args(&mut self, generic_args: &'v GenericArgs<'v>) { + walk_generic_args(self, generic_args) } fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding<'v>) { walk_assoc_type_binding(self, type_binding) @@ -483,7 +480,7 @@ pub trait Visitor<'v>: Sized { /// Walks the contents of a crate. See also `Crate::visit_all_items`. pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) { - visitor.visit_mod(&krate.item.module, krate.item.span, CRATE_HIR_ID); + visitor.visit_mod(&krate.item.module, CRATE_HIR_ID); walk_list!(visitor, visit_attribute, krate.item.attrs); walk_list!(visitor, visit_macro_def, krate.exported_macros); } @@ -517,7 +514,7 @@ pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local<'v>) { } pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) { - visitor.visit_name(ident.span, ident.name); + visitor.visit_name(ident.name); } pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) { @@ -567,7 +564,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { ItemKind::ExternCrate(orig_name) => { visitor.visit_id(item.hir_id); if let Some(orig_name) = orig_name { - visitor.visit_name(item.span, orig_name); + visitor.visit_name(orig_name); } } ItemKind::Use(ref path, _) => { @@ -582,12 +579,11 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { FnKind::ItemFn(item.ident, generics, sig.header, &item.vis, &item.attrs), &sig.decl, body_id, - item.span, item.hir_id, ), ItemKind::Mod(ref module) => { // `visit_mod()` takes care of visiting the `Item`'s `HirId`. - visitor.visit_mod(module, item.span, item.hir_id) + visitor.visit_mod(module, item.hir_id) } ItemKind::ForeignMod { abi: _, items } => { visitor.visit_id(item.hir_id); @@ -609,7 +605,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { ItemKind::Enum(ref enum_definition, ref generics) => { visitor.visit_generics(generics); // `visit_enum_def()` takes care of visiting the `Item`'s `HirId`. - visitor.visit_enum_def(enum_definition, generics, item.hir_id, item.span) + visitor.visit_enum_def(enum_definition, generics, item.hir_id) } ItemKind::Impl { unsafety: _, @@ -632,13 +628,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) { | ItemKind::Union(ref struct_definition, ref generics) => { visitor.visit_generics(generics); visitor.visit_id(item.hir_id); - visitor.visit_variant_data( - struct_definition, - item.ident.name, - generics, - item.hir_id, - item.span, - ); + visitor.visit_variant_data(struct_definition, item.ident.name, generics, item.hir_id); } ItemKind::Trait(.., ref generics, bounds, trait_item_refs) => { visitor.visit_id(item.hir_id); @@ -678,13 +668,7 @@ pub fn walk_variant<'v, V: Visitor<'v>>( ) { visitor.visit_ident(variant.ident); visitor.visit_id(variant.id); - visitor.visit_variant_data( - &variant.data, - variant.ident.name, - generics, - parent_item_id, - variant.span, - ); + visitor.visit_variant_data(&variant.data, variant.ident.name, generics, parent_item_id); walk_list!(visitor, visit_anon_const, &variant.disr_expr); walk_list!(visitor, visit_attribute, variant.attrs); } @@ -708,7 +692,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) { visitor.visit_fn_decl(&function_declaration.decl); } TyKind::Path(ref qpath) => { - visitor.visit_qpath(qpath, typ.hir_id, typ.span); + visitor.visit_qpath(qpath, typ.hir_id); } TyKind::OpaqueDef(item_id, lifetimes) => { visitor.visit_nested_item(item_id); @@ -729,12 +713,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) { } } -pub fn walk_qpath<'v, V: Visitor<'v>>( - visitor: &mut V, - qpath: &'v QPath<'v>, - id: HirId, - span: Span, -) { +pub fn walk_qpath<'v, V: Visitor<'v>>(visitor: &mut V, qpath: &'v QPath<'v>, id: HirId) { match *qpath { QPath::Resolved(ref maybe_qself, ref path) => { walk_list!(visitor, visit_ty, maybe_qself); @@ -742,7 +721,7 @@ pub fn walk_qpath<'v, V: Visitor<'v>>( } QPath::TypeRelative(ref qself, ref segment) => { visitor.visit_ty(qself); - visitor.visit_path_segment(span, segment); + visitor.visit_path_segment(segment); } QPath::LangItem(..) => {} } @@ -750,27 +729,19 @@ pub fn walk_qpath<'v, V: Visitor<'v>>( pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path<'v>) { for segment in path.segments { - visitor.visit_path_segment(path.span, segment); + visitor.visit_path_segment(segment); } } -pub fn walk_path_segment<'v, V: Visitor<'v>>( - visitor: &mut V, - path_span: Span, - segment: &'v PathSegment<'v>, -) { +pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V, segment: &'v PathSegment<'v>) { visitor.visit_ident(segment.ident); walk_list!(visitor, visit_id, segment.hir_id); if let Some(ref args) = segment.args { - visitor.visit_generic_args(path_span, args); + visitor.visit_generic_args(args); } } -pub fn walk_generic_args<'v, V: Visitor<'v>>( - visitor: &mut V, - _path_span: Span, - generic_args: &'v GenericArgs<'v>, -) { +pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V, generic_args: &'v GenericArgs<'v>) { walk_list!(visitor, visit_generic_arg, generic_args.args); walk_list!(visitor, visit_assoc_type_binding, generic_args.bindings); } @@ -795,14 +766,14 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat<'v>) { visitor.visit_id(pattern.hir_id); match pattern.kind { PatKind::TupleStruct(ref qpath, children, _) => { - visitor.visit_qpath(qpath, pattern.hir_id, pattern.span); + visitor.visit_qpath(qpath, pattern.hir_id); walk_list!(visitor, visit_pat, children); } PatKind::Path(ref qpath) => { - visitor.visit_qpath(qpath, pattern.hir_id, pattern.span); + visitor.visit_qpath(qpath, pattern.hir_id); } PatKind::Struct(ref qpath, fields, _) => { - visitor.visit_qpath(qpath, pattern.hir_id, pattern.span); + visitor.visit_qpath(qpath, pattern.hir_id); for field in fields { visitor.visit_id(field.hir_id); visitor.visit_ident(field.ident); @@ -859,9 +830,9 @@ pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericB GenericBound::Trait(ref typ, modifier) => { visitor.visit_poly_trait_ref(typ, modifier); } - GenericBound::LangItemTrait(_, span, hir_id, args) => { + GenericBound::LangItemTrait(_, _, hir_id, args) => { visitor.visit_id(hir_id); - visitor.visit_generic_args(span, args); + visitor.visit_generic_args(args); } GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime), } @@ -948,7 +919,6 @@ pub fn walk_fn<'v, V: Visitor<'v>>( function_kind: FnKind<'v>, function_declaration: &'v FnDecl<'v>, body_id: BodyId, - _span: Span, id: HirId, ) { visitor.visit_id(id); @@ -979,7 +949,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai FnKind::Method(trait_item.ident, sig, None, &trait_item.attrs), &sig.decl, body_id, - trait_item.span, trait_item.hir_id, ); } @@ -1029,7 +998,6 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis), &impl_item.attrs), &sig.decl, body_id, - impl_item.span, impl_item.hir_id, ); } @@ -1113,7 +1081,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) visitor.visit_anon_const(count) } ExprKind::Struct(ref qpath, fields, ref optional_base) => { - visitor.visit_qpath(qpath, expression.hir_id, expression.span); + visitor.visit_qpath(qpath, expression.hir_id); for field in fields { visitor.visit_id(field.hir_id); visitor.visit_ident(field.ident); @@ -1129,7 +1097,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) walk_list!(visitor, visit_expr, arguments); } ExprKind::MethodCall(ref segment, _, arguments, _) => { - visitor.visit_path_segment(expression.span, segment); + visitor.visit_path_segment(segment); walk_list!(visitor, visit_expr, arguments); } ExprKind::Binary(_, ref left_expression, ref right_expression) => { @@ -1159,7 +1127,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) FnKind::Closure(&expression.attrs), function_declaration, body, - expression.span, expression.hir_id, ), ExprKind::Block(ref block, ref opt_label) => { @@ -1183,7 +1150,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) visitor.visit_expr(index_expression) } ExprKind::Path(ref qpath) => { - visitor.visit_qpath(qpath, expression.hir_id, expression.span); + visitor.visit_qpath(qpath, expression.hir_id); } ExprKind::Break(ref destination, ref opt_expr) => { walk_list!(visitor, visit_label, &destination.label); diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 2420f82c0418d..db0f7f86a97f2 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -694,9 +694,7 @@ impl IndexVec { pub fn convert_index_type(self) -> IndexVec { IndexVec { raw: self.raw, _marker: PhantomData } } -} -impl IndexVec { /// Grows the index vector so that it contains an entry for /// `elem`; if that is already true, then has no /// effect. Otherwise, inserts new values as needed by invoking @@ -709,11 +707,6 @@ impl IndexVec { } } - #[inline] - pub fn resize(&mut self, new_len: usize, value: T) { - self.raw.resize(new_len, value) - } - #[inline] pub fn resize_to_elem(&mut self, elem: I, fill_value: impl FnMut() -> T) { let min_new_len = elem.index() + 1; @@ -721,6 +714,13 @@ impl IndexVec { } } +impl IndexVec { + #[inline] + pub fn resize(&mut self, new_len: usize, value: T) { + self.raw.resize(new_len, value) + } +} + impl IndexVec { #[inline] pub fn binary_search(&self, value: &T) -> Result { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 374bd6d0d791f..0a45e9dac8155 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1378,36 +1378,42 @@ impl TypeAliasBounds { } } - fn suggest_changing_assoc_types(ty: &hir::Ty<'_>, err: &mut DiagnosticBuilder<'_>) { + fn suggest_changing_assoc_types( + tcx: TyCtxt<'_>, + ty: &hir::Ty<'_>, + err: &mut DiagnosticBuilder<'_>, + ) { // Access to associates types should use `::Assoc`, which does not need a // bound. Let's see if this type does that. // We use a HIR visitor to walk the type. use rustc_hir::intravisit::{self, Visitor}; - struct WalkAssocTypes<'a, 'db> { + struct WalkAssocTypes<'a, 'db, 'tcx> { err: &'a mut DiagnosticBuilder<'db>, + tcx: TyCtxt<'tcx>, } - impl<'a, 'db, 'v> Visitor<'v> for WalkAssocTypes<'a, 'db> { + impl<'a, 'db, 'tcx, 'v> Visitor<'v> for WalkAssocTypes<'a, 'db, 'tcx> { type Map = intravisit::ErasedMap<'v>; fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap { intravisit::NestedVisitorMap::None } - fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId, span: Span) { + fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId) { if TypeAliasBounds::is_type_variable_assoc(qpath) { + let span = self.tcx.hir().span(id); self.err.span_help( span, "use fully disambiguated paths (i.e., `::Assoc`) to refer to \ associated types in type aliases", ); } - intravisit::walk_qpath(self, qpath, id, span) + intravisit::walk_qpath(self, qpath, id) } } // Let's go for a walk! - let mut visitor = WalkAssocTypes { err }; + let mut visitor = WalkAssocTypes { err, tcx }; visitor.visit_ty(ty); } } @@ -1443,7 +1449,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds { Applicability::MachineApplicable, ); if !suggested_changing_assoc_types { - TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err); + TypeAliasBounds::suggest_changing_assoc_types(cx.tcx, ty, &mut err); suggested_changing_assoc_types = true; } err.emit(); @@ -1468,7 +1474,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds { and should be removed"; err.multipart_suggestion(&msg, suggestion, Applicability::MachineApplicable); if !suggested_changing_assoc_types { - TypeAliasBounds::suggest_changing_assoc_types(ty, &mut err); + TypeAliasBounds::suggest_changing_assoc_types(cx.tcx, ty, &mut err); suggested_changing_assoc_types = true; } err.emit(); diff --git a/compiler/rustc_lint/src/late.rs b/compiler/rustc_lint/src/late.rs index 015e109871182..154386d05604c 100644 --- a/compiler/rustc_lint/src/late.rs +++ b/compiler/rustc_lint/src/late.rs @@ -26,7 +26,6 @@ use rustc_middle::hir::map::Map; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint::LintPass; use rustc_span::symbol::Symbol; -use rustc_span::Span; use std::any::Any; use std::cell::Cell; @@ -76,10 +75,10 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> { self.context.param_env = old_param_env; } - fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { - lint_callback!(self, check_mod, m, s, n); + fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, n: hir::HirId) { + lint_callback!(self, check_mod, m, n); hir_visit::walk_mod(self, m, n); - lint_callback!(self, check_mod_post, m, s, n); + lint_callback!(self, check_mod_post, m, n); } fn enter_attrs(&mut self, attrs: &'tcx [ast::Attribute]) { @@ -189,7 +188,6 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas fk: hir_visit::FnKind<'tcx>, decl: &'tcx hir::FnDecl<'tcx>, body_id: hir::BodyId, - span: Span, id: hir::HirId, ) { // Wrap in typeck results here, not just in visit_nested_body, @@ -197,9 +195,9 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas let old_enclosing_body = self.context.enclosing_body.replace(body_id); let old_cached_typeck_results = self.context.cached_typeck_results.take(); let body = self.context.tcx.hir().body(body_id); - lint_callback!(self, check_fn, fk, decl, body, span, id); - hir_visit::walk_fn(self, fk, decl, body_id, span, id); - lint_callback!(self, check_fn_post, fk, decl, body, span, id); + lint_callback!(self, check_fn, fk, decl, body, id); + hir_visit::walk_fn(self, fk, decl, body_id, id); + lint_callback!(self, check_fn_post, fk, decl, body, id); self.context.enclosing_body = old_enclosing_body; self.context.cached_typeck_results.set(old_cached_typeck_results); } @@ -210,7 +208,6 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas _: Symbol, _: &'tcx hir::Generics<'tcx>, _: hir::HirId, - _: Span, ) { lint_callback!(self, check_struct_def, s); hir_visit::walk_struct_def(self, s); @@ -242,13 +239,9 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas hir_visit::walk_ty(self, t); } - fn visit_name(&mut self, sp: Span, name: Symbol) { - lint_callback!(self, check_name, sp, name); - } - - fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) { + fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, n: hir::HirId) { if !self.context.only_module { - self.process_mod(m, s, n); + self.process_mod(m, n); } } @@ -387,8 +380,8 @@ fn late_lint_mod_pass<'tcx, T: LateLintPass<'tcx>>( let mut cx = LateContextAndPass { context, pass }; - let (module, span, hir_id) = tcx.hir().get_module(module_def_id); - cx.process_mod(module, span, hir_id); + let (module, hir_id) = tcx.hir().get_module(module_def_id); + cx.process_mod(module, hir_id); // Visit the crate attributes if hir_id == hir::CRATE_HIR_ID { diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 2336b52619ab8..de3bae81215e4 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -72,7 +72,7 @@ use rustc_session::lint::builtin::{ EXPLICIT_OUTLIVES_REQUIREMENTS, INVALID_CODEBLOCK_ATTRIBUTES, INVALID_HTML_TAGS, MISSING_DOC_CODE_EXAMPLES, NON_AUTOLINKS, PRIVATE_DOC_TESTS, }; -use rustc_span::symbol::{Ident, Symbol}; +use rustc_span::symbol::Ident; use rustc_span::Span; use array_into_iter::ArrayIntoIter; diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index ebd6190dc74cc..06e7e006f8e3d 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -8,7 +8,7 @@ use rustc_hir::intravisit::FnKind; use rustc_hir::{GenericParamKind, PatKind}; use rustc_middle::ty; use rustc_span::symbol::sym; -use rustc_span::{symbol::Ident, BytePos, Span}; +use rustc_span::{symbol::Ident, BytePos}; use rustc_target::spec::abi::Abi; #[derive(PartialEq)] @@ -308,13 +308,7 @@ impl NonSnakeCase { } impl<'tcx> LateLintPass<'tcx> for NonSnakeCase { - fn check_mod( - &mut self, - cx: &LateContext<'_>, - _: &'tcx hir::Mod<'tcx>, - _: Span, - id: hir::HirId, - ) { + fn check_mod(&mut self, cx: &LateContext<'_>, _: &'tcx hir::Mod<'tcx>, id: hir::HirId) { if id != hir::CRATE_HIR_ID { return; } @@ -372,7 +366,6 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase { fk: FnKind<'_>, _: &hir::FnDecl<'_>, _: &hir::Body<'_>, - _: Span, id: hir::HirId, ) { match &fk { diff --git a/compiler/rustc_lint/src/passes.rs b/compiler/rustc_lint/src/passes.rs index 828f283d2a95a..db7c8271edf0b 100644 --- a/compiler/rustc_lint/src/passes.rs +++ b/compiler/rustc_lint/src/passes.rs @@ -5,7 +5,7 @@ use rustc_data_structures::sync; use rustc_hir as hir; use rustc_session::lint::builtin::HardwiredLints; use rustc_session::lint::LintPass; -use rustc_span::symbol::{Ident, Symbol}; +use rustc_span::symbol::Ident; use rustc_span::Span; #[macro_export] @@ -15,11 +15,10 @@ macro_rules! late_lint_methods { fn check_param(a: &$hir hir::Param<$hir>); fn check_body(a: &$hir hir::Body<$hir>); fn check_body_post(a: &$hir hir::Body<$hir>); - fn check_name(a: Span, b: Symbol); fn check_crate(a: &$hir hir::Crate<$hir>); fn check_crate_post(a: &$hir hir::Crate<$hir>); - fn check_mod(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId); - fn check_mod_post(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId); + fn check_mod(a: &$hir hir::Mod<$hir>, c: hir::HirId); + fn check_mod_post(a: &$hir hir::Mod<$hir>, c: hir::HirId); fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>); fn check_foreign_item_post(a: &$hir hir::ForeignItem<$hir>); fn check_item(a: &$hir hir::Item<$hir>); @@ -42,13 +41,11 @@ macro_rules! late_lint_methods { a: rustc_hir::intravisit::FnKind<$hir>, b: &$hir hir::FnDecl<$hir>, c: &$hir hir::Body<$hir>, - d: Span, e: hir::HirId); fn check_fn_post( a: rustc_hir::intravisit::FnKind<$hir>, b: &$hir hir::FnDecl<$hir>, c: &$hir hir::Body<$hir>, - d: Span, e: hir::HirId ); fn check_trait_item(a: &$hir hir::TraitItem<$hir>); diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index 9ad9d53cd0db3..8c489320efd3b 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1266,7 +1266,6 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions { kind: hir::intravisit::FnKind<'tcx>, decl: &'tcx hir::FnDecl<'_>, _: &'tcx hir::Body<'_>, - _: Span, hir_id: hir::HirId, ) { use hir::intravisit::FnKind; diff --git a/compiler/rustc_middle/src/hir/map/blocks.rs b/compiler/rustc_middle/src/hir/map/blocks.rs index 9d392c7b26bf7..14e6528c30656 100644 --- a/compiler/rustc_middle/src/hir/map/blocks.rs +++ b/compiler/rustc_middle/src/hir/map/blocks.rs @@ -17,7 +17,6 @@ use rustc_hir as hir; use rustc_hir::intravisit::FnKind; use rustc_hir::{Expr, FnDecl, Node}; use rustc_span::symbol::Ident; -use rustc_span::Span; /// An FnLikeNode is a Node that is like a fn, in that it has a decl /// and a body (as well as a NodeId, a span, etc). @@ -104,7 +103,6 @@ struct ItemFnParts<'a> { generics: &'a hir::Generics<'a>, body: hir::BodyId, id: hir::HirId, - span: Span, attrs: &'a [Attribute], } @@ -114,19 +112,12 @@ struct ClosureParts<'a> { decl: &'a FnDecl<'a>, body: hir::BodyId, id: hir::HirId, - span: Span, attrs: &'a [Attribute], } impl<'a> ClosureParts<'a> { - fn new( - d: &'a FnDecl<'a>, - b: hir::BodyId, - id: hir::HirId, - s: Span, - attrs: &'a [Attribute], - ) -> Self { - ClosureParts { decl: d, body: b, id, span: s, attrs } + fn new(d: &'a FnDecl<'a>, b: hir::BodyId, id: hir::HirId, attrs: &'a [Attribute]) -> Self { + ClosureParts { decl: d, body: b, id, attrs } } } @@ -146,7 +137,7 @@ impl<'a> FnLikeNode<'a> { pub fn body(self) -> hir::BodyId { self.handle( |i: ItemFnParts<'a>| i.body, - |_, _, _: &'a hir::FnSig<'a>, _, body: hir::BodyId, _, _| body, + |_, _, _: &'a hir::FnSig<'a>, _, body: hir::BodyId, _| body, |c: ClosureParts<'a>| c.body, ) } @@ -154,23 +145,15 @@ impl<'a> FnLikeNode<'a> { pub fn decl(self) -> &'a FnDecl<'a> { self.handle( |i: ItemFnParts<'a>| &*i.decl, - |_, _, sig: &'a hir::FnSig<'a>, _, _, _, _| &sig.decl, + |_, _, sig: &'a hir::FnSig<'a>, _, _, _| &sig.decl, |c: ClosureParts<'a>| c.decl, ) } - pub fn span(self) -> Span { - self.handle( - |i: ItemFnParts<'_>| i.span, - |_, _, _: &'a hir::FnSig<'a>, _, _, span, _| span, - |c: ClosureParts<'_>| c.span, - ) - } - pub fn id(self) -> hir::HirId { self.handle( |i: ItemFnParts<'_>| i.id, - |id, _, _: &'a hir::FnSig<'a>, _, _, _, _| id, + |id, _, _: &'a hir::FnSig<'a>, _, _, _| id, |c: ClosureParts<'_>| c.id, ) } @@ -192,7 +175,7 @@ impl<'a> FnLikeNode<'a> { FnKind::ItemFn(p.ident, p.generics, p.header, p.vis, p.attrs) }; let closure = |c: ClosureParts<'a>| FnKind::Closure(c.attrs); - let method = |_, ident: Ident, sig: &'a hir::FnSig<'a>, vis, _, _, attrs| { + let method = |_, ident: Ident, sig: &'a hir::FnSig<'a>, vis, _, attrs| { FnKind::Method(ident, sig, vis, attrs) }; self.handle(item, method, closure) @@ -207,7 +190,6 @@ impl<'a> FnLikeNode<'a> { &'a hir::FnSig<'a>, Option<&'a hir::Visibility<'a>>, hir::BodyId, - Span, &'a [Attribute], ) -> A, C: FnOnce(ClosureParts<'a>) -> A, @@ -220,7 +202,6 @@ impl<'a> FnLikeNode<'a> { decl: &sig.decl, body: block, vis: &i.vis, - span: i.span, attrs: &i.attrs, header: sig.header, generics, @@ -229,19 +210,19 @@ impl<'a> FnLikeNode<'a> { }, Node::TraitItem(ti) => match ti.kind { hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => { - method(ti.hir_id, ti.ident, sig, None, body, ti.span, &ti.attrs) + method(ti.hir_id, ti.ident, sig, None, body, &ti.attrs) } _ => bug!("trait method FnLikeNode that is not fn-like"), }, Node::ImplItem(ii) => match ii.kind { hir::ImplItemKind::Fn(ref sig, body) => { - method(ii.hir_id, ii.ident, sig, Some(&ii.vis), body, ii.span, &ii.attrs) + method(ii.hir_id, ii.ident, sig, Some(&ii.vis), body, &ii.attrs) } _ => bug!("impl method FnLikeNode that is not fn-like"), }, Node::Expr(e) => match e.kind { hir::ExprKind::Closure(_, ref decl, block, _fn_decl_span, _gen) => { - closure(ClosureParts::new(&decl, block, e.hir_id, e.span, &e.attrs)) + closure(ClosureParts::new(&decl, block, e.hir_id, &e.attrs)) } _ => bug!("expr FnLikeNode that is not fn-like"), }, diff --git a/compiler/rustc_middle/src/hir/map/collector.rs b/compiler/rustc_middle/src/hir/map/collector.rs index 872fcb0f581d0..12acc6f5f46a6 100644 --- a/compiler/rustc_middle/src/hir/map/collector.rs +++ b/compiler/rustc_middle/src/hir/map/collector.rs @@ -16,7 +16,7 @@ use rustc_hir::*; use rustc_index::vec::{Idx, IndexVec}; use rustc_session::{CrateDisambiguator, Session}; use rustc_span::source_map::SourceMap; -use rustc_span::{Span, Symbol, DUMMY_SP}; +use rustc_span::Symbol; use std::iter::repeat; @@ -119,6 +119,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { modules: _, proc_macros: _, trait_map: _, + spans: _, } = *krate; hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes) @@ -233,11 +234,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { } } - fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) { - self.insert_with_hash(span, hir_id, node, Fingerprint::ZERO) + fn insert(&mut self, hir_id: HirId, node: Node<'hir>) { + self.insert_with_hash(hir_id, node, Fingerprint::ZERO) } - fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) { + fn insert_with_hash(&mut self, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) { let entry = Entry { parent: self.parent_node, node }; // Make sure that the DepNode of some node coincides with the HirId @@ -249,6 +250,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { None => format!("{:?}", node), }; + let span = self.krate.spans[hir_id]; span_bug!( span, "inconsistent DepNode at `{:?}` for `{}`: \ @@ -330,7 +332,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_param(&mut self, param: &'hir Param<'hir>) { let node = Node::Param(param); - self.insert(param.pat.span, param.hir_id, node); + self.insert(param.hir_id, node); self.with_parent(param.hir_id, |this| { intravisit::walk_param(this, param); }); @@ -343,12 +345,13 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.definitions.opt_hir_id_to_local_def_id(i.hir_id).unwrap() ); self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| { - this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash); + this.insert_with_hash(i.hir_id, Node::Item(i), hash); + this.with_parent(i.hir_id, |this| { if let ItemKind::Struct(ref struct_def, _) = i.kind { // If this is a tuple or unit-like struct, register the constructor. if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { - this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def)); + this.insert(ctor_hir_id, Node::Ctor(struct_def)); } } intravisit::walk_item(this, i); @@ -362,7 +365,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.definitions.opt_hir_id_to_local_def_id(fi.hir_id).unwrap() ); self.with_dep_node_owner(fi.hir_id.owner, fi, |this, hash| { - this.insert_with_hash(fi.span, fi.hir_id, Node::ForeignItem(fi), hash); + this.insert_with_hash(fi.hir_id, Node::ForeignItem(fi), hash); this.with_parent(fi.hir_id, |this| { intravisit::walk_foreign_item(this, fi); @@ -381,14 +384,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.definitions.opt_hir_id_to_local_def_id(param.hir_id).unwrap() ); self.with_dep_node_owner(param.hir_id.owner, param, |this, hash| { - this.insert_with_hash(param.span, param.hir_id, Node::GenericParam(param), hash); + this.insert_with_hash(param.hir_id, Node::GenericParam(param), hash); this.with_parent(param.hir_id, |this| { intravisit::walk_generic_param(this, param); }); }); } else { - self.insert(param.span, param.hir_id, Node::GenericParam(param)); + self.insert(param.hir_id, Node::GenericParam(param)); intravisit::walk_generic_param(self, param); } } @@ -399,7 +402,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.definitions.opt_hir_id_to_local_def_id(ti.hir_id).unwrap() ); self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| { - this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash); + this.insert_with_hash(ti.hir_id, Node::TraitItem(ti), hash); this.with_parent(ti.hir_id, |this| { intravisit::walk_trait_item(this, ti); @@ -413,7 +416,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { self.definitions.opt_hir_id_to_local_def_id(ii.hir_id).unwrap() ); self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| { - this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash); + this.insert_with_hash(ii.hir_id, Node::ImplItem(ii), hash); this.with_parent(ii.hir_id, |this| { intravisit::walk_impl_item(this, ii); @@ -424,7 +427,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_pat(&mut self, pat: &'hir Pat<'hir>) { let node = if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) }; - self.insert(pat.span, pat.hir_id, node); + self.insert(pat.hir_id, node); self.with_parent(pat.hir_id, |this| { intravisit::walk_pat(this, pat); @@ -434,7 +437,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_arm(&mut self, arm: &'hir Arm<'hir>) { let node = Node::Arm(arm); - self.insert(arm.span, arm.hir_id, node); + self.insert(arm.hir_id, node); self.with_parent(arm.hir_id, |this| { intravisit::walk_arm(this, arm); @@ -442,7 +445,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_anon_const(&mut self, constant: &'hir AnonConst) { - self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant)); + self.insert(constant.hir_id, Node::AnonConst(constant)); self.with_parent(constant.hir_id, |this| { intravisit::walk_anon_const(this, constant); @@ -450,7 +453,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_expr(&mut self, expr: &'hir Expr<'hir>) { - self.insert(expr.span, expr.hir_id, Node::Expr(expr)); + self.insert(expr.hir_id, Node::Expr(expr)); self.with_parent(expr.hir_id, |this| { intravisit::walk_expr(this, expr); @@ -458,22 +461,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) { - self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt)); + self.insert(stmt.hir_id, Node::Stmt(stmt)); self.with_parent(stmt.hir_id, |this| { intravisit::walk_stmt(this, stmt); }); } - fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) { + fn visit_path_segment(&mut self, path_segment: &'hir PathSegment<'hir>) { if let Some(hir_id) = path_segment.hir_id { - self.insert(path_span, hir_id, Node::PathSegment(path_segment)); + self.insert(hir_id, Node::PathSegment(path_segment)); } - intravisit::walk_path_segment(self, path_span, path_segment); + intravisit::walk_path_segment(self, path_segment); } fn visit_ty(&mut self, ty: &'hir Ty<'hir>) { - self.insert(ty.span, ty.hir_id, Node::Ty(ty)); + self.insert(ty.hir_id, Node::Ty(ty)); self.with_parent(ty.hir_id, |this| { intravisit::walk_ty(this, ty); @@ -481,7 +484,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) { - self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr)); + self.insert(tr.hir_ref_id, Node::TraitRef(tr)); self.with_parent(tr.hir_ref_id, |this| { intravisit::walk_trait_ref(this, tr); @@ -493,34 +496,33 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl<'hir>, b: BodyId, - s: Span, id: HirId, ) { assert_eq!(self.parent_node, id); - intravisit::walk_fn(self, fk, fd, b, s, id); + intravisit::walk_fn(self, fk, fd, b, id); } fn visit_block(&mut self, block: &'hir Block<'hir>) { - self.insert(block.span, block.hir_id, Node::Block(block)); + self.insert(block.hir_id, Node::Block(block)); self.with_parent(block.hir_id, |this| { intravisit::walk_block(this, block); }); } fn visit_local(&mut self, l: &'hir Local<'hir>) { - self.insert(l.span, l.hir_id, Node::Local(l)); + self.insert(l.hir_id, Node::Local(l)); self.with_parent(l.hir_id, |this| intravisit::walk_local(this, l)) } fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) { - self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime)); + self.insert(lifetime.hir_id, Node::Lifetime(lifetime)); } fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) { match visibility.node { VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {} VisibilityKind::Restricted { hir_id, .. } => { - self.insert(visibility.span, hir_id, Node::Visibility(visibility)); + self.insert(hir_id, Node::Visibility(visibility)); self.with_parent(hir_id, |this| { intravisit::walk_vis(this, visibility); }); @@ -538,29 +540,24 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { }); self.with_parent(parent, |this| { this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| { - this.insert_with_hash( - macro_def.span, - macro_def.hir_id, - Node::MacroDef(macro_def), - hash, - ); + this.insert_with_hash(macro_def.hir_id, Node::MacroDef(macro_def), hash); }) }); } fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) { - self.insert(v.span, v.id, Node::Variant(v)); + self.insert(v.id, Node::Variant(v)); self.with_parent(v.id, |this| { // Register the constructor of this variant. if let Some(ctor_hir_id) = v.data.ctor_hir_id() { - this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data)); + this.insert(ctor_hir_id, Node::Ctor(&v.data)); } intravisit::walk_variant(this, v, g, item_id); }); } fn visit_struct_field(&mut self, field: &'hir StructField<'hir>) { - self.insert(field.span, field.hir_id, Node::Field(field)); + self.insert(field.hir_id, Node::Field(field)); self.with_parent(field.hir_id, |this| { intravisit::walk_struct_field(this, field); }); diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c2e99224d8b36..a0f5b19777046 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -451,11 +451,11 @@ impl<'hir> Map<'hir> { } } - pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { + pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, HirId) { let hir_id = self.local_def_id_to_hir_id(module); match self.get_entry(hir_id).node { - Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id), - Node::Crate(item) => (&item.module, item.span, hir_id), + Node::Item(&Item { kind: ItemKind::Mod(ref m), .. }) => (m, hir_id), + Node::Crate(item) => (&item.module, hir_id), node => panic!("not a module: {:?}", node), } } @@ -853,61 +853,30 @@ impl<'hir> Map<'hir> { /// This is used by `tcx.get_span` pub fn span(&self, hir_id: HirId) -> Span { match self.find_entry(hir_id).map(|entry| entry.node) { - Some(Node::Param(param)) => param.span, - Some(Node::Item(item)) => match &item.kind { - ItemKind::Fn(sig, _, _) => sig.span, - _ => item.span, - }, - Some(Node::ForeignItem(foreign_item)) => foreign_item.span, - Some(Node::TraitItem(trait_item)) => match &trait_item.kind { - TraitItemKind::Fn(sig, _) => sig.span, - _ => trait_item.span, - }, - Some(Node::ImplItem(impl_item)) => match &impl_item.kind { - ImplItemKind::Fn(sig, _) => sig.span, - _ => impl_item.span, - }, - Some(Node::Variant(variant)) => variant.span, - Some(Node::Field(field)) => field.span, - Some(Node::AnonConst(constant)) => self.body(constant.body).value.span, - Some(Node::Expr(expr)) => expr.span, - Some(Node::Stmt(stmt)) => stmt.span, - Some(Node::PathSegment(seg)) => seg.ident.span, - Some(Node::Ty(ty)) => ty.span, - Some(Node::TraitRef(tr)) => tr.path.span, - Some(Node::Binding(pat)) => pat.span, - Some(Node::Pat(pat)) => pat.span, - Some(Node::Arm(arm)) => arm.span, - Some(Node::Block(block)) => block.span, - Some(Node::Ctor(..)) => match self.find(self.get_parent_node(hir_id)) { - Some(Node::Item(item)) => item.span, - Some(Node::Variant(variant)) => variant.span, - _ => unreachable!(), - }, - Some(Node::Lifetime(lifetime)) => lifetime.span, - Some(Node::GenericParam(param)) => param.span, - Some(Node::Visibility(&Spanned { - node: VisibilityKind::Restricted { ref path, .. }, - .. - })) => path.span, - Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v), - Some(Node::Local(local)) => local.span, - Some(Node::MacroDef(macro_def)) => macro_def.span, - Some(Node::Crate(item)) => item.span, - None => bug!("hir::map::Map::span: id not in map: {:?}", hir_id), - } + Some(Node::Item(item)) => { + if let ItemKind::Fn(sig, _, _) = &item.kind { + return sig.span; + } + } + Some(Node::TraitItem(item)) => { + if let TraitItemKind::Fn(sig, _) = &item.kind { + return sig.span; + } + } + Some(Node::ImplItem(item)) => { + if let ImplItemKind::Fn(sig, _) = &item.kind { + return sig.span; + } + } + _ => {} + }; + self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id] } /// Like `hir.span()`, but includes the body of function items /// (instead of just the function header) pub fn span_with_body(&self, hir_id: HirId) -> Span { - match self.find_entry(hir_id).map(|entry| entry.node) { - Some(Node::TraitItem(item)) => item.span, - Some(Node::ImplItem(impl_item)) => impl_item.span, - Some(Node::Item(item)) => item.span, - Some(_) => self.span(hir_id), - _ => bug!("hir::map::Map::span_with_body: id not in map: {:?}", hir_id), - } + self.tcx.hir_owner_spans(hir_id.owner)[hir_id.local_id] } pub fn span_if_local(&self, id: DefId) -> Option { diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index ae3b30217cc4a..080a87584e5e6 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -77,6 +77,7 @@ pub fn provide(providers: &mut Providers) { }; providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref(); + providers.hir_owner_spans = |tcx, id| tcx.hir_crate(LOCAL_CRATE).spans.get_owner(id); providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); let hir_id = hir.local_def_id_to_hir_id(id.expect_local()); diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 1e836d0a84253..a3fb77005bc92 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -89,6 +89,15 @@ rustc_queries! { desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) } } + // Gives access to the HIR spans inside the HIR owner `key`. + // + // This can be conveniently accessed by methods on `tcx.hir()`. + // Avoid calling this query directly. + query hir_owner_spans(key: LocalDefId) -> &'tcx IndexVec { + eval_always + desc { |tcx| "HIR owner spans in `{}`", tcx.def_path_str(key.to_def_id()) } + } + /// Computes the `DefId` of the corresponding const parameter in case the `key` is a /// const argument and returns `None` otherwise. /// diff --git a/compiler/rustc_mir/src/transform/mod.rs b/compiler/rustc_mir/src/transform/mod.rs index 7f3b421cf76f6..a311b000c386a 100644 --- a/compiler/rustc_mir/src/transform/mod.rs +++ b/compiler/rustc_mir/src/transform/mod.rs @@ -10,7 +10,7 @@ use rustc_middle::mir::visit::Visitor as _; use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPhase, Promoted}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeFoldable}; -use rustc_span::{Span, Symbol}; +use rustc_span::Symbol; use std::borrow::Cow; pub mod add_call_guards; @@ -117,7 +117,6 @@ fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> FxHashSet { _: Symbol, _: &'tcx hir::Generics<'tcx>, _: hir::HirId, - _: Span, ) { if let hir::VariantData::Tuple(_, hir_id) = *v { self.set.insert(self.tcx.hir().local_def_id(hir_id)); diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index c4fb0cf5b28dc..dad1f950ac2bd 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -241,7 +241,6 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> { _: Symbol, _: &hir::Generics<'_>, _: hir::HirId, - _: rustc_span::Span, ) { let has_repr_c = self.repr_has_repr_c; let inherited_pub_visibility = self.inherited_pub_visibility; diff --git a/compiler/rustc_passes/src/hir_stats.rs b/compiler/rustc_passes/src/hir_stats.rs index 1d02c9aa6375d..0f14f5f3c63ec 100644 --- a/compiler/rustc_passes/src/hir_stats.rs +++ b/compiler/rustc_passes/src/hir_stats.rs @@ -124,7 +124,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_item(self, i) } - fn visit_mod(&mut self, m: &'v hir::Mod<'v>, _s: Span, n: hir::HirId) { + fn visit_mod(&mut self, m: &'v hir::Mod<'v>, n: hir::HirId) { self.record("Mod", Id::None, m); hir_visit::walk_mod(self, m, n) } @@ -174,11 +174,10 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { fk: hir_visit::FnKind<'v>, fd: &'v hir::FnDecl<'v>, b: hir::BodyId, - s: Span, id: hir::HirId, ) { self.record("FnDecl", Id::None, fd); - hir_visit::walk_fn(self, fk, fd, b, s, id) + hir_visit::walk_fn(self, fk, fd, b, id) } fn visit_where_predicate(&mut self, predicate: &'v hir::WherePredicate<'v>) { @@ -221,9 +220,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_lifetime(self, lifetime) } - fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId, span: Span) { + fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId) { self.record("QPath", Id::None, qpath); - hir_visit::walk_qpath(self, qpath, id, span) + hir_visit::walk_qpath(self, qpath, id) } fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) { @@ -231,9 +230,9 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { hir_visit::walk_path(self, path) } - fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v hir::PathSegment<'v>) { + fn visit_path_segment(&mut self, path_segment: &'v hir::PathSegment<'v>) { self.record("PathSegment", Id::None, path_segment); - hir_visit::walk_path_segment(self, path_span, path_segment) + hir_visit::walk_path_segment(self, path_segment) } fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding<'v>) { diff --git a/compiler/rustc_passes/src/naked_functions.rs b/compiler/rustc_passes/src/naked_functions.rs index 788f1df328cc4..2de57fdd23d59 100644 --- a/compiler/rustc_passes/src/naked_functions.rs +++ b/compiler/rustc_passes/src/naked_functions.rs @@ -39,7 +39,6 @@ impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> { fk: FnKind<'v>, _fd: &'tcx hir::FnDecl<'tcx>, body_id: hir::BodyId, - span: Span, hir_id: HirId, ) { let ident_span; @@ -68,7 +67,7 @@ impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> { check_abi(self.tcx, hir_id, fn_header.abi, ident_span); check_no_patterns(self.tcx, body.params); check_no_parameters_use(self.tcx, body); - check_asm(self.tcx, hir_id, body, span); + check_asm(self.tcx, hir_id, body); } } } @@ -146,12 +145,13 @@ impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> { } /// Checks that function body contains a single inline assembly block. -fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, hir_id: HirId, body: &'tcx hir::Body<'tcx>, fn_span: Span) { +fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, hir_id: HirId, body: &'tcx hir::Body<'tcx>) { let mut this = CheckInlineAssembly { tcx, items: Vec::new() }; this.visit_body(body); if let [(ItemKind::Asm, _)] = this.items[..] { // Ok. } else { + let fn_span = tcx.hir().span_with_body(hir_id); tcx.struct_span_lint_hir(UNSUPPORTED_NAKED_FUNCTIONS, hir_id, fn_span, |lint| { let mut diag = lint.build("naked functions must contain a single asm block"); let mut has_asm = false; diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 1bcfdf0faf66a..7de878f5859dc 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -809,7 +809,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { self.prev_level = orig_level; } - fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, _sp: Span, id: hir::HirId) { + fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, id: hir::HirId) { // This code is here instead of in visit_item so that the // crate module gets processed as well. if self.prev_level.is_some() { @@ -991,7 +991,7 @@ impl<'tcx> Visitor<'tcx> for NamePrivacyVisitor<'tcx> { NestedVisitorMap::All(self.tcx.hir()) } - fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _s: Span, _n: hir::HirId) { + fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _n: hir::HirId) { // Don't visit nested modules, since we run a separate visitor walk // for each module in `privacy_access_levels` } @@ -1120,7 +1120,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { NestedVisitorMap::All(self.tcx.hir()) } - fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _s: Span, _n: hir::HirId) { + fn visit_mod(&mut self, _m: &'tcx hir::Mod<'tcx>, _n: hir::HirId) { // Don't visit nested modules, since we run a separate visitor walk // for each module in `privacy_access_levels` } @@ -1224,7 +1224,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { // we prohibit access to private statics from other crates, this allows to give // more code internal visibility at link time. (Access to private functions // is already prohibited by type privacy for function types.) - fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId, span: Span) { + fn visit_qpath(&mut self, qpath: &'tcx hir::QPath<'tcx>, id: hir::HirId) { let def = match qpath { hir::QPath::Resolved(_, path) => match path.res { Res::Def(kind, def_id) => Some((kind, def_id)), @@ -1257,6 +1257,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { Some(name) => format!("{} `{}` is private", kind, name), None => format!("{} is private", kind), }; + let span = self.tcx.hir().span(id); sess.struct_span_err(span, &msg) .span_label(span, &format!("private {}", kind)) .emit(); @@ -1264,7 +1265,7 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { } } - intravisit::walk_qpath(self, qpath, id, span); + intravisit::walk_qpath(self, qpath, id); } // Check types of patterns. @@ -2052,7 +2053,8 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility { fn check_mod_privacy(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { // Check privacy of names not checked in previous compilation stages. let mut visitor = NamePrivacyVisitor { tcx, maybe_typeck_results: None, current_item: None }; - let (module, span, hir_id) = tcx.hir().get_module(module_def_id); + let (module, hir_id) = tcx.hir().get_module(module_def_id); + let span = tcx.hir().span(hir_id); intravisit::walk_mod(&mut visitor, module, hir_id); diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index d5ba6f3b53b21..348eaf1dafa7d 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -2866,7 +2866,7 @@ fn insert_late_bound_lifetimes( // is, those would be potentially inputs to // projections if let Some(last_segment) = path.segments.last() { - self.visit_path_segment(path.span, last_segment); + self.visit_path_segment(last_segment); } } diff --git a/compiler/rustc_save_analysis/src/dump_visitor.rs b/compiler/rustc_save_analysis/src/dump_visitor.rs index 617a28ed51938..775188816f5fa 100644 --- a/compiler/rustc_save_analysis/src/dump_visitor.rs +++ b/compiler/rustc_save_analysis/src/dump_visitor.rs @@ -128,7 +128,7 @@ impl<'tcx> DumpVisitor<'tcx> { self.save_ctxt.lookup_def_id(ref_id) } - pub fn dump_crate_info(&mut self, name: &str, krate: &hir::Crate<'_>) { + pub fn dump_crate_info(&mut self, name: &str, _krate: &hir::Crate<'_>) { let source_file = self.tcx.sess.local_crate_source_file.as_ref(); let crate_root = source_file.map(|source_file| { let source_file = Path::new(source_file); @@ -151,7 +151,7 @@ impl<'tcx> DumpVisitor<'tcx> { }, crate_root: crate_root.unwrap_or_else(|| "".to_owned()), external_crates: self.save_ctxt.get_external_crates(), - span: self.span_from_span(krate.item.span), + span: self.span_from_span(self.tcx.hir().span(hir::CRATE_HIR_ID)), }; self.dumper.crate_prelude(data); @@ -376,7 +376,7 @@ impl<'tcx> DumpVisitor<'tcx> { self.nest_typeck_results(map.local_def_id(item.hir_id), |v| { let body = map.body(body); if let Some(fn_data) = v.save_ctxt.get_item_data(item) { - down_cast_data!(fn_data, DefData, item.span); + down_cast_data!(fn_data, DefData, v.tcx.hir().span(item.hir_id)); v.process_formals(body.params, &fn_data.qualname); v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id); @@ -403,7 +403,7 @@ impl<'tcx> DumpVisitor<'tcx> { ) { self.nest_typeck_results(self.tcx.hir().local_def_id(item.hir_id), |v| { if let Some(var_data) = v.save_ctxt.get_item_data(item) { - down_cast_data!(var_data, DefData, item.span); + down_cast_data!(var_data, DefData, v.tcx.hir().span(item.hir_id)); v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id), var_data); } v.visit_ty(&typ); @@ -463,7 +463,7 @@ impl<'tcx> DumpVisitor<'tcx> { def: &'tcx hir::VariantData<'tcx>, ty_params: &'tcx hir::Generics<'tcx>, ) { - debug!("process_struct {:?} {:?}", item, item.span); + debug!("process_struct {:?} {:?}", item, self.tcx.hir().span(item.hir_id)); let name = item.ident.to_string(); let qualname = format!( "::{}", @@ -539,7 +539,7 @@ impl<'tcx> DumpVisitor<'tcx> { None => return, Some(data) => data, }; - down_cast_data!(enum_data, DefData, item.span); + down_cast_data!(enum_data, DefData, self.tcx.hir().span(item.hir_id)); let access = access_from!(self.save_ctxt, item, item.hir_id); @@ -640,12 +640,13 @@ impl<'tcx> DumpVisitor<'tcx> { impl_items: &'tcx [hir::ImplItemRef<'tcx>], ) { if let Some(impl_data) = self.save_ctxt.get_item_data(item) { - if !self.span.filter_generated(item.span) { + let item_span = self.tcx.hir().span(item.hir_id); + if !self.span.filter_generated(item_span) { if let super::Data::RelationData(rel, imp) = impl_data { self.dumper.dump_relation(rel); self.dumper.dump_impl(imp); } else { - span_bug!(item.span, "unexpected data kind: {:?}", impl_data); + span_bug!(item_span, "unexpected data kind: {:?}", impl_data); } } } @@ -756,7 +757,7 @@ impl<'tcx> DumpVisitor<'tcx> { // `item` is the module in question, represented as an( item. fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) { if let Some(mod_data) = self.save_ctxt.get_item_data(item) { - down_cast_data!(mod_data, DefData, item.span); + down_cast_data!(mod_data, DefData, self.tcx.hir().span(item.hir_id)); self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.hir_id), mod_data); } } @@ -822,8 +823,8 @@ impl<'tcx> DumpVisitor<'tcx> { if let hir::QPath::Resolved(_, path) = path { self.write_sub_paths_truncated(path); } - down_cast_data!(struct_lit_data, RefData, ex.span); - if !generated_code(ex.span) { + down_cast_data!(struct_lit_data, RefData, self.tcx.hir().span(ex.hir_id)); + if !generated_code(self.tcx.hir().span(ex.hir_id)) { self.dumper.dump_ref(struct_lit_data); } @@ -847,10 +848,11 @@ impl<'tcx> DumpVisitor<'tcx> { seg: &'tcx hir::PathSegment<'tcx>, args: &'tcx [hir::Expr<'tcx>], ) { - debug!("process_method_call {:?} {:?}", ex, ex.span); + let ex_span = self.tcx.hir().span(ex.hir_id); + debug!("process_method_call {:?} {:?}", ex, ex_span); if let Some(mcd) = self.save_ctxt.get_expr_data(ex) { - down_cast_data!(mcd, RefData, ex.span); - if !generated_code(ex.span) { + down_cast_data!(mcd, RefData, ex_span); + if !generated_code(ex_span) { self.dumper.dump_ref(mcd); } } @@ -974,7 +976,9 @@ impl<'tcx> DumpVisitor<'tcx> { /// If the span is not macro-generated, do nothing, else use callee and /// callsite spans to record macro definition and use data, using the /// mac_uses and mac_defs sets to prevent multiples. - fn process_macro_use(&mut self, _span: Span) { + fn process_macro_use(&mut self, _hir_id: hir::HirId) { + //let span = self.tcx.hir().span(_hir_id); + // // FIXME if we're not dumping the defs (see below), there is no point // dumping refs either. // let source_span = span.source_callsite(); @@ -1011,8 +1015,8 @@ impl<'tcx> DumpVisitor<'tcx> { } fn process_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>, trait_id: DefId) { - self.process_macro_use(trait_item.span); - let vis_span = trait_item.span.shrink_to_lo(); + self.process_macro_use(trait_item.hir_id); + let vis_span = self.tcx.hir().span(trait_item.hir_id).shrink_to_lo(); match trait_item.kind { hir::TraitItemKind::Const(ref ty, body) => { let body = body.map(|b| &self.tcx.hir().body(b).value); @@ -1038,7 +1042,7 @@ impl<'tcx> DumpVisitor<'tcx> { trait_item.ident, &trait_item.generics, &respan, - trait_item.span, + self.tcx.hir().span(trait_item.hir_id), ); } hir::TraitItemKind::Type(ref bounds, ref default_ty) => { @@ -1062,7 +1066,7 @@ impl<'tcx> DumpVisitor<'tcx> { span, name, qualname, - value: self.span.snippet(trait_item.span), + value: self.span.snippet(self.tcx.hir().span(trait_item.hir_id)), parent: Some(id_from_def_id(trait_id)), children: vec![], decl_id: None, @@ -1090,7 +1094,7 @@ impl<'tcx> DumpVisitor<'tcx> { } fn process_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>, impl_id: DefId) { - self.process_macro_use(impl_item.span); + self.process_macro_use(impl_item.hir_id); match impl_item.kind { hir::ImplItemKind::Const(ref ty, body) => { let body = self.tcx.hir().body(body); @@ -1112,7 +1116,7 @@ impl<'tcx> DumpVisitor<'tcx> { impl_item.ident, &impl_item.generics, &impl_item.vis, - impl_item.span, + self.tcx.hir().span(impl_item.hir_id), ); } hir::ImplItemKind::TyAlias(ref ty) => { @@ -1130,7 +1134,9 @@ impl<'tcx> DumpVisitor<'tcx> { format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id())); let sm = self.tcx.sess.source_map(); - let filename = sm.span_to_filename(krate.item.span); + let span = self.tcx.hir().span(hir::CRATE_HIR_ID); + let filename = sm.span_to_filename(span); + let span = self.span_from_span(span); let data_id = id_from_hir_id(id, &self.save_ctxt); let children = krate .item @@ -1139,7 +1145,6 @@ impl<'tcx> DumpVisitor<'tcx> { .iter() .map(|i| id_from_hir_id(i.id, &self.save_ctxt)) .collect(); - let span = self.span_from_span(krate.item.span); self.dumper.dump_def( &Access { public: true, reachable: true }, @@ -1181,7 +1186,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - self.process_macro_use(item.span); + self.process_macro_use(item.hir_id); match item.kind { hir::ItemKind::Use(path, hir::UseKind::Single) => { let sub_span = path.segments.last().unwrap().ident.span; @@ -1219,8 +1224,9 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { // Otherwise it's a span with wrong macro expansion info, which // we don't want to track anyway, since it's probably macro-internal `use` - if let Some(sub_span) = self.span.sub_span_of_star(item.span) { - if !self.span.filter_generated(item.span) { + let item_span = self.tcx.hir().span(item.hir_id); + if let Some(sub_span) = self.span.sub_span_of_star(item_span) { + if !self.span.filter_generated(item_span) { let access = access_from!(self.save_ctxt, item, item.hir_id); let span = self.span_from_span(sub_span); let parent = self @@ -1361,10 +1367,10 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) { - self.process_macro_use(t.span); + self.process_macro_use(t.hir_id); match t.kind { hir::TyKind::Path(ref path) => { - if generated_code(t.span) { + if generated_code(self.tcx.hir().span(t.hir_id)) { return; } @@ -1381,7 +1387,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { if let hir::QPath::Resolved(_, path) = path { self.write_sub_paths_truncated(path); } - intravisit::walk_qpath(self, path, t.hir_id, t.span); + intravisit::walk_qpath(self, path, t.hir_id); } hir::TyKind::Array(ref ty, ref anon_const) => { self.visit_ty(ty); @@ -1402,7 +1408,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) { debug!("visit_expr {:?}", ex.kind); - self.process_macro_use(ex.span); + self.process_macro_use(ex.hir_id); match ex.kind { hir::ExprKind::Struct(ref path, ref fields, ref rest) => { let hir_expr = self.save_ctxt.tcx.hir().expect_expr(ex.hir_id); @@ -1423,8 +1429,9 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { self.visit_expr(&sub_ex); if let Some(field_data) = self.save_ctxt.get_expr_data(ex) { - down_cast_data!(field_data, RefData, ex.span); - if !generated_code(ex.span) { + let ex_span = self.tcx.hir().span(ex.hir_id); + down_cast_data!(field_data, RefData, ex_span); + if !generated_code(ex_span) { self.dumper.dump_ref(field_data); } } @@ -1463,7 +1470,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) { - self.process_macro_use(p.span); + self.process_macro_use(p.hir_id); self.process_pat(p); } @@ -1475,17 +1482,17 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { self.visit_expr(&arm.body); } - fn visit_qpath(&mut self, path: &'tcx hir::QPath<'tcx>, id: hir::HirId, _: Span) { + fn visit_qpath(&mut self, path: &'tcx hir::QPath<'tcx>, id: hir::HirId) { self.process_path(id, path); } fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) { - self.process_macro_use(s.span); + self.process_macro_use(s.hir_id); intravisit::walk_stmt(self, s) } fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) { - self.process_macro_use(l.span); + self.process_macro_use(l.hir_id); self.process_var_decl(&l.pat); // Just walk the initialiser and type (don't want to walk the pattern again). @@ -1499,7 +1506,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { match item.kind { hir::ForeignItemKind::Fn(decl, _, ref generics) => { if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) { - down_cast_data!(fn_data, DefData, item.span); + down_cast_data!(fn_data, DefData, self.tcx.hir().span(item.hir_id)); self.process_generic_params(generics, &fn_data.qualname, item.hir_id); self.dumper.dump_def(&access, fn_data); @@ -1515,7 +1522,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } hir::ForeignItemKind::Static(ref ty, _) => { if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) { - down_cast_data!(var_data, DefData, item.span); + down_cast_data!(var_data, DefData, self.tcx.hir().span(item.hir_id)); self.dumper.dump_def(&access, var_data); } @@ -1523,7 +1530,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> { } hir::ForeignItemKind::Type => { if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) { - down_cast_data!(var_data, DefData, item.span); + down_cast_data!(var_data, DefData, self.tcx.hir().span(item.hir_id)); self.dumper.dump_def(&access, var_data); } } diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index 056c0b3d9d513..bbd319630eb5b 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -301,8 +301,13 @@ impl<'tcx> SaveContext<'tcx> { let name = item.ident.to_string(); let qualname = format!("::{}", self.tcx.def_path_str(def_id)); filter!(self.span_utils, item.ident.span); - let value = - enum_def_to_string(def, generics, item.ident.name, item.span, &item.vis); + let value = enum_def_to_string( + def, + generics, + item.ident.name, + self.tcx.hir().span(item.hir_id), + &item.vis, + ); Some(Data::DefData(Def { kind: DefKind::Enum, id: id_from_def_id(def_id), @@ -892,7 +897,9 @@ impl<'l> Visitor<'l> for PathCollector<'l> { hir::PatKind::Binding(bm, _, ident, _) => { debug!( "PathCollector, visit ident in pat {}: {:?} {:?}", - ident, p.span, ident.span + ident, + self.tcx.hir().span(p.hir_id), + ident.span ); let immut = match bm { // Even if the ref is mut, you can't change the ref, only diff --git a/compiler/rustc_typeck/src/check/gather_locals.rs b/compiler/rustc_typeck/src/check/gather_locals.rs index 825ebc19fa6da..fc420dc2c6495 100644 --- a/compiler/rustc_typeck/src/check/gather_locals.rs +++ b/compiler/rustc_typeck/src/check/gather_locals.rs @@ -133,7 +133,6 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> { _: intravisit::FnKind<'tcx>, _: &'tcx hir::FnDecl<'tcx>, _: hir::BodyId, - _: Span, _: hir::HirId, ) { } diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index 5cfd78ebeacad..12e09f1f07fe7 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1425,7 +1425,7 @@ impl UsePlacementFinder<'tcx> { } impl intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> { - fn visit_mod(&mut self, module: &'tcx hir::Mod<'tcx>, _: Span, hir_id: hir::HirId) { + fn visit_mod(&mut self, module: &'tcx hir::Mod<'tcx>, hir_id: hir::HirId) { if self.span.is_some() { return; } diff --git a/compiler/rustc_typeck/src/check/regionck.rs b/compiler/rustc_typeck/src/check/regionck.rs index 88e8dd3cb129a..b14b29d51e588 100644 --- a/compiler/rustc_typeck/src/check/regionck.rs +++ b/compiler/rustc_typeck/src/check/regionck.rs @@ -350,7 +350,6 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionCtxt<'a, 'tcx> { fk: intravisit::FnKind<'tcx>, _: &'tcx hir::FnDecl<'tcx>, body_id: hir::BodyId, - span: Span, hir_id: hir::HirId, ) { assert!( @@ -365,6 +364,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionCtxt<'a, 'tcx> { let env_snapshot = self.outlives_environment.push_snapshot_pre_closure(); let body = self.tcx.hir().body(body_id); + let span = self.tcx.hir().span(hir_id); self.visit_fn_body(hir_id, body, span); // Restore state from previous function. diff --git a/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs b/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs index 4e9d4d342734c..82207ac5a3af4 100644 --- a/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs +++ b/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs @@ -40,7 +40,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass { _: intravisit::FnKind<'tcx>, _: &'tcx hir::FnDecl, _: &'tcx hir::Body, - span: source_map::Span, id: hir::HirId, ) { let item = match cx.tcx.hir().get(id) { @@ -51,6 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass { let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr"); if !item.attrs.iter().any(allowed) { cx.lint(MISSING_ALLOWED_ATTR, |lint| { + let span = cx.tcx.hir().span(id); lint.build("Missing 'allowed_attr' attribute").set_span(span).emit() }); } diff --git a/src/tools/clippy/clippy_lints/src/booleans.rs b/src/tools/clippy/clippy_lints/src/booleans.rs index 90bb0bd555f27..1f798ec9b3c51 100644 --- a/src/tools/clippy/clippy_lints/src/booleans.rs +++ b/src/tools/clippy/clippy_lints/src/booleans.rs @@ -10,7 +10,6 @@ use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::map::Map; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::source_map::Span; use rustc_span::sym; declare_clippy_lint! { @@ -63,7 +62,6 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool { _: FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - _: Span, _: HirId, ) { NonminimalBoolVisitor { cx }.visit_body(body) diff --git a/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs b/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs index b1bc2ec29e16e..357f697356e0f 100644 --- a/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs +++ b/src/tools/clippy/clippy_lints/src/cognitive_complexity.rs @@ -119,11 +119,11 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, hir_id: HirId, ) { let def_id = cx.tcx.hir().local_def_id(hir_id); if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) { + let span = cx.tcx.hir().span(hir_id); self.check(cx, kind, decl, body, span); } } diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index c75efc6e99f89..d786db464f9a9 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -382,7 +382,7 @@ struct UnsafeVisitor<'a, 'tcx> { impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> { type Map = Map<'tcx>; - fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, span: Span, id: HirId) { + fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, id: HirId) { if self.has_unsafe { return; } @@ -395,7 +395,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> { } } - walk_fn(self, kind, decl, body_id, span, id); + walk_fn(self, kind, decl, body_id, id); } fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { diff --git a/src/tools/clippy/clippy_lints/src/escape.rs b/src/tools/clippy/clippy_lints/src/escape.rs index d2dcb3e5c4644..b3389be3761ac 100644 --- a/src/tools/clippy/clippy_lints/src/escape.rs +++ b/src/tools/clippy/clippy_lints/src/escape.rs @@ -4,7 +4,6 @@ use rustc_infer::infer::TyCtxtInferExt; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::source_map::Span; use rustc_target::abi::LayoutOf; use rustc_target::spec::abi::Abi; use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId}; @@ -63,7 +62,6 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal { fn_kind: intravisit::FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - _: Span, hir_id: HirId, ) { if let Some(header) = fn_kind.header() { diff --git a/src/tools/clippy/clippy_lints/src/functions.rs b/src/tools/clippy/clippy_lints/src/functions.rs index fd93548b55c6d..fc049df8da6b3 100644 --- a/src/tools/clippy/clippy_lints/src/functions.rs +++ b/src/tools/clippy/clippy_lints/src/functions.rs @@ -247,7 +247,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions { kind: intravisit::FnKind<'tcx>, decl: &'tcx hir::FnDecl<'_>, body: &'tcx hir::Body<'_>, - span: Span, hir_id: hir::HirId, ) { let unsafety = match kind { @@ -256,6 +255,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions { intravisit::FnKind::Closure(_) => return, }; + let span = cx.tcx.hir().span_with_body(hir_id); + // don't warn for implementations, it's not their fault if !is_trait_impl_item(cx, hir_id) { // don't lint extern functions decls, it's not their fault either diff --git a/src/tools/clippy/clippy_lints/src/future_not_send.rs b/src/tools/clippy/clippy_lints/src/future_not_send.rs index f9697afe40525..907924fcc799b 100644 --- a/src/tools/clippy/clippy_lints/src/future_not_send.rs +++ b/src/tools/clippy/clippy_lints/src/future_not_send.rs @@ -6,7 +6,7 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::subst::Subst; use rustc_middle::ty::{Opaque, PredicateAtom::Trait}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::{sym, Span}; +use rustc_span::sym; use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt; use rustc_trait_selection::traits::{self, FulfillmentError, TraitEngine}; @@ -55,7 +55,6 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'tcx>, _: &'tcx Body<'tcx>, - _: Span, hir_id: HirId, ) { if let FnKind::Closure(_) = kind { diff --git a/src/tools/clippy/clippy_lints/src/implicit_return.rs b/src/tools/clippy/clippy_lints/src/implicit_return.rs index 03e95c9e27f6a..47797640ee7b8 100644 --- a/src/tools/clippy/clippy_lints/src/implicit_return.rs +++ b/src/tools/clippy/clippy_lints/src/implicit_return.rs @@ -123,8 +123,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitReturn { _: FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, - _: HirId, + hir_id: HirId, ) { let def_id = cx.tcx.hir().body_owner_def_id(body.id()); @@ -134,6 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitReturn { } let mir = cx.tcx.optimized_mir(def_id.to_def_id()); + let span = cx.tcx.hir().span(hir_id); // checking return type through MIR, HIR is not able to determine inferred closure return types // make sure it's not a macro diff --git a/src/tools/clippy/clippy_lints/src/manual_async_fn.rs b/src/tools/clippy/clippy_lints/src/manual_async_fn.rs index 29439e52c48e1..1c7df9e6959b6 100644 --- a/src/tools/clippy/clippy_lints/src/manual_async_fn.rs +++ b/src/tools/clippy/clippy_lints/src/manual_async_fn.rs @@ -9,7 +9,6 @@ use rustc_hir::{ }; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::Span; declare_clippy_lint! { /// **What it does:** It checks for manual implementations of `async` functions. @@ -43,8 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, - _: HirId, + hir_id: HirId, ) { if_chain! { if let Some(header) = kind.header(); @@ -59,6 +57,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn { if block.stmts.is_empty(); if let Some(closure_body) = desugared_async_block(cx, block); then { + let span = cx.tcx.hir().span(hir_id); let header_span = span.with_hi(ret_ty.span.hi()); span_lint_and_then( diff --git a/src/tools/clippy/clippy_lints/src/misc.rs b/src/tools/clippy/clippy_lints/src/misc.rs index 0512d74c7b1c8..85949f712f5c9 100644 --- a/src/tools/clippy/clippy_lints/src/misc.rs +++ b/src/tools/clippy/clippy_lints/src/misc.rs @@ -275,13 +275,13 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints { k: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, - _: HirId, + hir_id: HirId, ) { if let FnKind::Closure(_) = k { // Does not apply to closures return; } + let span = cx.tcx.hir().span(hir_id); if in_external_macro(cx.tcx.sess, span) { return; } diff --git a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs index 6ebeaced62a33..d1102542dd1d9 100644 --- a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs +++ b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs @@ -9,7 +9,6 @@ use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_semver::RustcVersion; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::Span; use rustc_typeck::hir_ty_to_ty; const MISSING_CONST_FOR_FN_MSRV: RustcVersion = RustcVersion::new(1, 37, 0); @@ -94,7 +93,6 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { kind: FnKind<'_>, _: &FnDecl<'_>, _: &Body<'_>, - span: Span, hir_id: HirId, ) { if !meets_msrv(self.msrv.as_ref(), &MISSING_CONST_FOR_FN_MSRV) { @@ -102,6 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { } let def_id = cx.tcx.hir().local_def_id(hir_id); + let span = cx.tcx.hir().span_with_body(hir_id); if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) { return; diff --git a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs index 5043b7b1fc3c1..97e430cff5a76 100644 --- a/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs +++ b/src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs @@ -71,9 +71,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, hir_id: HirId, ) { + let span = cx.tcx.hir().span(hir_id); if span.from_expansion() { return; } diff --git a/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs b/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs index 37e2b50def17a..0e04845c5b580 100644 --- a/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs +++ b/src/tools/clippy/clippy_lints/src/panic_in_result_fn.rs @@ -40,12 +40,12 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn { fn_kind: FnKind<'tcx>, _: &'tcx hir::FnDecl<'tcx>, body: &'tcx hir::Body<'tcx>, - span: Span, hir_id: hir::HirId, ) { if !matches!(fn_kind, FnKind::Closure(_)) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) { + let span = cx.tcx.hir().span_with_body(hir_id); lint_impl_body(cx, span, body); } } diff --git a/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs b/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs index 6a17d654ac943..737ee91b299c4 100644 --- a/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs +++ b/src/tools/clippy/clippy_lints/src/pass_by_ref_or_value.rs @@ -216,9 +216,9 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, _body: &'tcx Body<'_>, - span: Span, hir_id: HirId, ) { + let span = cx.tcx.hir().span(hir_id); if span.from_expansion() { return; } diff --git a/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs b/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs index 5539331d0460b..97e2e47c21124 100644 --- a/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs +++ b/src/tools/clippy/clippy_lints/src/pattern_type_mismatch.rs @@ -129,7 +129,6 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch { _: intravisit::FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - _: Span, hir_id: HirId, ) { if let Some(fn_sig) = cx.typeck_results().liberated_fn_sigs().get(hir_id) { diff --git a/src/tools/clippy/clippy_lints/src/redundant_clone.rs b/src/tools/clippy/clippy_lints/src/redundant_clone.rs index 06adbb523d706..bcfb249d07d0d 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_clone.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_clone.rs @@ -16,7 +16,7 @@ use rustc_middle::mir::{ use rustc_middle::ty::{self, fold::TypeVisitor, Ty}; use rustc_mir::dataflow::{Analysis, AnalysisDomain, GenKill, GenKillAnalysis, ResultsCursor}; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::source_map::{BytePos, Span}; +use rustc_span::source_map::BytePos; use rustc_span::sym; use std::convert::TryFrom; use std::ops::ControlFlow; @@ -75,7 +75,6 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone { _: FnKind<'tcx>, _: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - _: Span, _: HirId, ) { let def_id = cx.tcx.hir().body_owner_def_id(body.id()); diff --git a/src/tools/clippy/clippy_lints/src/returns.rs b/src/tools/clippy/clippy_lints/src/returns.rs index 7f4913a02cbd3..b827bbb0a5fd9 100644 --- a/src/tools/clippy/clippy_lints/src/returns.rs +++ b/src/tools/clippy/clippy_lints/src/returns.rs @@ -127,7 +127,6 @@ impl<'tcx> LateLintPass<'tcx> for Return { kind: FnKind<'tcx>, _: &'tcx FnDecl<'tcx>, body: &'tcx Body<'tcx>, - _: Span, _: HirId, ) { match kind { diff --git a/src/tools/clippy/clippy_lints/src/shadow.rs b/src/tools/clippy/clippy_lints/src/shadow.rs index f839659267825..85ae99d9896dc 100644 --- a/src/tools/clippy/clippy_lints/src/shadow.rs +++ b/src/tools/clippy/clippy_lints/src/shadow.rs @@ -104,7 +104,6 @@ impl<'tcx> LateLintPass<'tcx> for Shadow { _: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - _: Span, _: HirId, ) { if in_external_macro(cx.sess(), body.value.span) { diff --git a/src/tools/clippy/clippy_lints/src/types.rs b/src/tools/clippy/clippy_lints/src/types.rs index fd74783335d57..787cb562d3a8a 100644 --- a/src/tools/clippy/clippy_lints/src/types.rs +++ b/src/tools/clippy/clippy_lints/src/types.rs @@ -255,7 +255,7 @@ pub struct Types { impl_lint_pass!(Types => [BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER]); impl<'tcx> LateLintPass<'tcx> for Types { - fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) { + fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, id: HirId) { // Skip trait implementations; see issue #605. if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id)) { if let ItemKind::Impl { of_trait: Some(_), .. } = item.kind { @@ -1902,7 +1902,6 @@ impl<'tcx> LateLintPass<'tcx> for TypeComplexity { _: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, _: &'tcx Body<'_>, - _: Span, _: HirId, ) { self.check_fndecl(cx, decl); diff --git a/src/tools/clippy/clippy_lints/src/unnecessary_wraps.rs b/src/tools/clippy/clippy_lints/src/unnecessary_wraps.rs index 5b9a80f92db69..c5461832766ed 100644 --- a/src/tools/clippy/clippy_lints/src/unnecessary_wraps.rs +++ b/src/tools/clippy/clippy_lints/src/unnecessary_wraps.rs @@ -10,7 +10,6 @@ use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::subst::GenericArgKind; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::symbol::sym; -use rustc_span::Span; declare_clippy_lint! { /// **What it does:** Checks for private functions that only return `Ok` or `Some`. @@ -61,7 +60,6 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps { fn_kind: FnKind<'tcx>, fn_decl: &FnDecl<'tcx>, body: &Body<'tcx>, - span: Span, hir_id: HirId, ) { match fn_kind { @@ -113,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps { span_lint_and_then( cx, UNNECESSARY_WRAPS, - span, + cx.tcx.hir().span_with_body(hir_id), format!( "this function's return value is unnecessarily wrapped by `{}`", return_type diff --git a/src/tools/clippy/clippy_lints/src/unwrap.rs b/src/tools/clippy/clippy_lints/src/unwrap.rs index f4a77e54dd149..3d6780ee6a9f2 100644 --- a/src/tools/clippy/clippy_lints/src/unwrap.rs +++ b/src/tools/clippy/clippy_lints/src/unwrap.rs @@ -10,7 +10,6 @@ use rustc_middle::hir::map::Map; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::Ty; use rustc_session::{declare_lint_pass, declare_tool_lint}; -use rustc_span::source_map::Span; use rustc_span::sym; declare_clippy_lint! { @@ -217,9 +216,9 @@ impl<'tcx> LateLintPass<'tcx> for Unwrap { kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body: &'tcx Body<'_>, - span: Span, fn_id: HirId, ) { + let span = cx.tcx.hir().span(fn_id); if span.from_expansion() { return; } @@ -229,6 +228,6 @@ impl<'tcx> LateLintPass<'tcx> for Unwrap { unwrappables: Vec::new(), }; - walk_fn(&mut v, kind, decl, body.id(), span, fn_id); + walk_fn(&mut v, kind, decl, body.id(), fn_id); } } diff --git a/src/tools/clippy/clippy_lints/src/utils/mod.rs b/src/tools/clippy/clippy_lints/src/utils/mod.rs index 1c68e837c4ab9..8589246bb2857 100644 --- a/src/tools/clippy/clippy_lints/src/utils/mod.rs +++ b/src/tools/clippy/clippy_lints/src/utils/mod.rs @@ -556,7 +556,7 @@ struct ContainsName { impl<'tcx> Visitor<'tcx> for ContainsName { type Map = Map<'tcx>; - fn visit_name(&mut self, _: Span, name: Symbol) { + fn visit_name(&mut self, name: Symbol) { if self.name == name { self.result = true; }