diff --git a/src/librustc_ast_lowering/item.rs b/src/librustc_ast_lowering/item.rs index fabc2621a8db3..6806f129da19a 100644 --- a/src/librustc_ast_lowering/item.rs +++ b/src/librustc_ast_lowering/item.rs @@ -223,14 +223,7 @@ 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, i.span), - ident, - attrs, - kind, - vis, - span: i.span, - }) + Some(hir::Item { hir_id: self.lower_node_id(i.id, i.span), ident, attrs, kind, vis }) } fn lower_item_kind( @@ -511,14 +504,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let kind = hir::ItemKind::Use(path, hir::UseKind::Single); let vis = this.rebuild_vis(&vis); - this.insert_item(hir::Item { - hir_id: new_id, - ident, - attrs, - kind, - vis, - span, - }); + this.insert_item(hir::Item { hir_id: new_id, ident, attrs, kind, vis }); }); } @@ -580,14 +566,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let kind = this.lower_use_tree(use_tree, &prefix, id, &mut vis, &mut ident, attrs); - this.insert_item(hir::Item { - hir_id: new_hir_id, - ident, - attrs, - kind, - vis, - span: use_tree.span, - }); + this.insert_item(hir::Item { hir_id: new_hir_id, ident, attrs, kind, vis }); }); } @@ -683,7 +662,6 @@ impl<'hir> LoweringContext<'_, 'hir> { ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"), }, vis: self.lower_visibility(&i.vis, None), - span: i.span, } } @@ -790,7 +768,6 @@ impl<'hir> LoweringContext<'_, 'hir> { attrs: self.lower_attrs(&i.attrs), generics, kind, - span: i.span, } } @@ -876,7 +853,6 @@ impl<'hir> LoweringContext<'_, 'hir> { vis: self.lower_visibility(&i.vis, None), defaultness, kind, - span: i.span, } } diff --git a/src/librustc_ast_lowering/lib.rs b/src/librustc_ast_lowering/lib.rs index 461ecead12261..3a8a161f127e1 100644 --- a/src/librustc_ast_lowering/lib.rs +++ b/src/librustc_ast_lowering/lib.rs @@ -575,7 +575,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.spans.push_owner(Idx::new(self.resolver.definitions().def_index_count() - 1)); hir::Crate { - item: hir::CrateItem { module, attrs, span: c.span }, + item: hir::CrateItem { module, attrs }, exported_macros: self.arena.alloc_from_iter(self.exported_macros), non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs), items: self.items, @@ -1507,7 +1507,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { attrs: Default::default(), kind: opaque_ty_item_kind, vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited), - span: opaque_ty_span, }; // Insert the item into the global item list. This usually happens diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 9d9b53fc4a87c..d1d0d54cd4c79 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -214,10 +214,13 @@ impl CodegenCx<'ll, 'tcx> { let llty = self.layout_of(ty).llvm_type(self); // FIXME: refactor this to work without accessing the HIR let (g, attrs) = match self.tcx.hir().get(id) { - Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => { + Node::Item(&hir::Item { + attrs, hir_id, kind: hir::ItemKind::Static(..), .. + }) => { let sym_str = sym.as_str(); if let Some(g) = self.get_declared_value(&sym_str) { if self.val_ty(g) != self.type_ptr_to(llty) { + let span = self.tcx.hir().span(hir_id); span_bug!(span, "Conflicting types for static"); } } @@ -235,11 +238,12 @@ impl CodegenCx<'ll, 'tcx> { Node::ForeignItem(&hir::ForeignItem { ref attrs, - span, + hir_id, kind: hir::ForeignItemKind::Static(..), .. }) => { let fn_attrs = self.tcx.codegen_fn_attrs(def_id); + let span = self.tcx.hir().span(hir_id); (check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), &**attrs) } diff --git a/src/librustc_codegen_ssa/mono_item.rs b/src/librustc_codegen_ssa/mono_item.rs index 5994ef2be5467..fe74b1769acea 100644 --- a/src/librustc_codegen_ssa/mono_item.rs +++ b/src/librustc_codegen_ssa/mono_item.rs @@ -35,7 +35,8 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> { if let hir::ItemKind::GlobalAsm(ref ga) = item.kind { cx.codegen_global_asm(ga); } else { - span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type") + let span = cx.tcx().hir().span(hir_id); + span_bug!(span, "Mismatch between hir::Item type and MonoItem type") } } MonoItem::Fn(instance) => { diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs index 02f7d63e7e133..dd9dc2b599f94 100644 --- a/src/librustc_hir/hir.rs +++ b/src/librustc_hir/hir.rs @@ -581,7 +581,6 @@ pub struct ModuleItems { pub struct CrateItem<'hir> { pub module: Mod<'hir>, pub attrs: &'hir [Attribute], - pub span: Span, } /// The top-level data structure that stores the entire contents of @@ -1846,7 +1845,6 @@ pub struct TraitItem<'hir> { pub attrs: &'hir [Attribute], pub generics: Generics<'hir>, pub kind: TraitItemKind<'hir>, - pub span: Span, } /// Represents a trait method's body (or just argument names). @@ -1889,7 +1887,6 @@ pub struct ImplItem<'hir> { pub attrs: &'hir [Attribute], pub generics: Generics<'hir>, pub kind: ImplItemKind<'hir>, - pub span: Span, } /// Represents various kinds of content within an `impl`. @@ -2424,7 +2421,6 @@ pub struct Item<'hir> { pub attrs: &'hir [Attribute], pub kind: ItemKind<'hir>, pub vis: Visibility<'hir>, - pub span: Span, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] @@ -2597,7 +2593,6 @@ pub struct ForeignItem<'hir> { pub attrs: &'hir [Attribute], pub kind: ForeignItemKind<'hir>, pub hir_id: HirId, - pub span: Span, pub vis: Visibility<'hir>, } diff --git a/src/librustc_hir/intravisit.rs b/src/librustc_hir/intravisit.rs index 6641c885f0134..2f87f222a4d72 100644 --- a/src/librustc_hir/intravisit.rs +++ b/src/librustc_hir/intravisit.rs @@ -941,16 +941,8 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) { // N.B., deliberately force a compilation error if/when new fields are added. - let ImplItem { - hir_id: _, - ident, - ref vis, - ref defaultness, - attrs, - ref generics, - ref kind, - span: _, - } = *impl_item; + let ImplItem { hir_id: _, ident, ref vis, ref defaultness, attrs, ref generics, ref kind } = + *impl_item; visitor.visit_ident(ident); visitor.visit_vis(vis); diff --git a/src/librustc_hir/stable_hash_impls.rs b/src/librustc_hir/stable_hash_impls.rs index 1d3f44a08993a..264ef5bedadca 100644 --- a/src/librustc_hir/stable_hash_impls.rs +++ b/src/librustc_hir/stable_hash_impls.rs @@ -115,30 +115,21 @@ impl HashStable for VisibilityKind<'_> impl HashStable for TraitItem<'_> { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self; + let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind } = *self; hcx.hash_hir_item_like(|hcx| { ident.name.hash_stable(hcx, hasher); attrs.hash_stable(hcx, hasher); generics.hash_stable(hcx, hasher); kind.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); }); } } impl HashStable for ImplItem<'_> { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - let ImplItem { - hir_id: _, - ident, - ref vis, - defaultness, - ref attrs, - ref generics, - ref kind, - span, - } = *self; + let ImplItem { hir_id: _, ident, ref vis, defaultness, ref attrs, ref generics, ref kind } = + *self; hcx.hash_hir_item_like(|hcx| { ident.name.hash_stable(hcx, hasher); @@ -147,21 +138,19 @@ impl HashStable for ImplItem<'_> { attrs.hash_stable(hcx, hasher); generics.hash_stable(hcx, hasher); kind.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); }); } } impl HashStable for Item<'_> { fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { - let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self; + let Item { ident, ref attrs, hir_id: _, ref kind, ref vis } = *self; hcx.hash_hir_item_like(|hcx| { ident.name.hash_stable(hcx, hasher); attrs.hash_stable(hcx, hasher); kind.hash_stable(hcx, hasher); vis.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); }); } } diff --git a/src/librustc_hir_pretty/lib.rs b/src/librustc_hir_pretty/lib.rs index d84ddf706d0f0..a498635243c98 100644 --- a/src/librustc_hir_pretty/lib.rs +++ b/src/librustc_hir_pretty/lib.rs @@ -466,8 +466,9 @@ impl<'a> State<'a> { } pub fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) { + let item_span = self.span(item.hir_id); self.hardbreak_if_not_bol(); - self.maybe_print_comment(item.span.lo()); + self.maybe_print_comment(item_span.lo()); self.print_outer_attributes(&item.attrs); match item.kind { hir::ForeignItemKind::Fn(ref decl, ref arg_names, ref generics) => { @@ -574,8 +575,9 @@ impl<'a> State<'a> { /// Pretty-print an item pub fn print_item(&mut self, item: &hir::Item<'_>) { + let span = self.span(item.hir_id); self.hardbreak_if_not_bol(); - self.maybe_print_comment(item.span.lo()); + self.maybe_print_comment(span.lo()); self.print_outer_attributes(&item.attrs); self.ann.pre(self, AnnNode::Item(item)); match item.kind { @@ -662,14 +664,14 @@ impl<'a> State<'a> { self.nbsp(); self.bopen(); self.print_mod(_mod, &item.attrs); - self.bclose(item.span); + self.bclose(span); } hir::ItemKind::ForeignMod(ref nmod) => { self.head("extern"); self.word_nbsp(nmod.abi.to_string()); self.bopen(); self.print_foreign_mod(nmod, &item.attrs); - self.bclose(item.span); + self.bclose(span); } hir::ItemKind::GlobalAsm(ref ga) => { self.head(visibility_qualified(&item.vis, "global asm")); @@ -698,15 +700,15 @@ impl<'a> State<'a> { }); } hir::ItemKind::Enum(ref enum_definition, ref params) => { - self.print_enum_def(enum_definition, params, item.ident.name, item.span, &item.vis); + self.print_enum_def(enum_definition, params, item.ident.name, span, &item.vis); } hir::ItemKind::Struct(ref struct_def, ref generics) => { self.head(visibility_qualified(&item.vis, "struct")); - self.print_struct(struct_def, generics, item.ident.name, item.span, true); + self.print_struct(struct_def, generics, item.ident.name, span, true); } hir::ItemKind::Union(ref struct_def, ref generics) => { self.head(visibility_qualified(&item.vis, "union")); - self.print_struct(struct_def, generics, item.ident.name, item.span, true); + self.print_struct(struct_def, generics, item.ident.name, span, true); } hir::ItemKind::Impl { unsafety, @@ -753,7 +755,7 @@ impl<'a> State<'a> { for impl_item in items { self.ann.nested(self, Nested::ImplItem(impl_item.id)); } - self.bclose(item.span); + self.bclose(span); } hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, trait_items) => { self.head(""); @@ -780,7 +782,7 @@ impl<'a> State<'a> { for trait_item in trait_items { self.ann.nested(self, Nested::TraitItem(trait_item.id)); } - self.bclose(item.span); + self.bclose(span); } hir::ItemKind::TraitAlias(ref generics, ref bounds) => { self.head(""); @@ -961,9 +963,10 @@ impl<'a> State<'a> { } pub fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) { + let span = self.span(ti.hir_id); self.ann.pre(self, AnnNode::SubItem(ti.hir_id)); self.hardbreak_if_not_bol(); - self.maybe_print_comment(ti.span.lo()); + self.maybe_print_comment(span.lo()); self.print_outer_attributes(&ti.attrs); match ti.kind { hir::TraitItemKind::Const(ref ty, default) => { @@ -1000,9 +1003,10 @@ impl<'a> State<'a> { } pub fn print_impl_item(&mut self, ii: &hir::ImplItem<'_>) { + let span = self.span(ii.hir_id); self.ann.pre(self, AnnNode::SubItem(ii.hir_id)); self.hardbreak_if_not_bol(); - self.maybe_print_comment(ii.span.lo()); + self.maybe_print_comment(span.lo()); self.print_outer_attributes(&ii.attrs); self.print_defaultness(ii.defaultness); diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index 2ee95174dffe6..87923b1e3092d 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -431,8 +431,9 @@ impl DirtyCleanVisitor<'tcx> { } } - fn check_item(&mut self, item_id: hir::HirId, item_span: Span) { + fn check_item(&mut self, item_id: hir::HirId) { let def_id = self.tcx.hir().local_def_id(item_id); + let item_span = self.tcx.hir().span(item_id); for attr in self.tcx.get_attrs(def_id.to_def_id()).iter() { let assertion = match self.assertion_maybe(item_id, attr) { Some(a) => a, @@ -451,15 +452,15 @@ impl DirtyCleanVisitor<'tcx> { impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - self.check_item(item.hir_id, item.span); + self.check_item(item.hir_id); } fn visit_trait_item(&mut self, item: &hir::TraitItem<'_>) { - self.check_item(item.hir_id, item.span); + self.check_item(item.hir_id); } fn visit_impl_item(&mut self, item: &hir::ImplItem<'_>) { - self.check_item(item.hir_id, item.span); + self.check_item(item.hir_id); } } diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 4cd0c03697b09..a5857984c1b21 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -544,11 +544,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingCopyImplementations { return; } let param_env = ty::ParamEnv::empty(); - if ty.is_copy_modulo_regions(cx.tcx, param_env, item.span) { + let span = cx.tcx.hir().span(item.hir_id); + if ty.is_copy_modulo_regions(cx.tcx, param_env, span) { return; } if can_type_implement_copy(cx.tcx, param_env, ty).is_ok() { - cx.struct_span_lint(MISSING_COPY_IMPLEMENTATIONS, item.span, |lint| { + cx.struct_span_lint(MISSING_COPY_IMPLEMENTATIONS, span, |lint| { lint.build( "type could implement `Copy`; consider adding `impl \ Copy`", @@ -603,7 +604,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDebugImplementations { } if !self.impling_types.as_ref().unwrap().contains(&item.hir_id) { - cx.struct_span_lint(MISSING_DEBUG_IMPLEMENTATIONS, item.span, |lint| { + let span = cx.tcx.hir().span(item.hir_id); + cx.struct_span_lint(MISSING_DEBUG_IMPLEMENTATIONS, span, |lint| { lint.build(&format!( "type does not implement `{}`; consider adding `#[derive(Debug)]` \ or a manual implementation", @@ -797,6 +799,7 @@ declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS, NO_MANGLE_GEN impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { fn check_item(&mut self, cx: &LateContext<'_, '_>, it: &hir::Item<'_>) { + let span = cx.tcx.hir().span(it.hir_id); match it.kind { hir::ItemKind::Fn(.., ref generics, _) => { if let Some(no_mangle_attr) = attr::find_by_name(&it.attrs, sym::no_mangle) { @@ -804,7 +807,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { match param.kind { GenericParamKind::Lifetime { .. } => {} GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => { - cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS, it.span, |lint| { + cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS, span, |lint| { lint.build( "functions generic over types or consts must be mangled", ) @@ -828,7 +831,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { if attr::contains_name(&it.attrs, sym::no_mangle) { // Const items do not refer to a particular location in memory, and therefore // don't have anything to attach a symbol to - cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, it.span, |lint| { + cx.struct_span_lint(NO_MANGLE_CONST_ITEMS, span, |lint| { let msg = "const items should never be `#[no_mangle]`"; let mut err = lint.build(msg); @@ -837,11 +840,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems { .tcx .sess .source_map() - .span_to_snippet(it.span) + .span_to_snippet(span) .map(|snippet| snippet.find("const").unwrap_or(0)) .unwrap_or(0) as u32; // `const` is 5 chars - let const_span = it.span.with_hi(BytePos(it.span.lo().0 + start + 5)); + let const_span = span.with_hi(BytePos(span.lo().0 + start + 5)); err.span_suggestion( const_span, "try a static value", @@ -2215,11 +2218,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ClashingExternDecl { // We want to ensure that we use spans for both decls that include where the // name was defined, whether that was from the link_name attribute or not. - let get_relevant_span = - |fi: &hir::ForeignItem<'_>| match Self::name_of_extern_decl(tcx, fi) { - SymbolName::Normal(_) => fi.span, - SymbolName::Link(_, annot_span) => fi.span.to(annot_span), - }; + let get_relevant_span = |fi: &hir::ForeignItem<'_>| { + let fi_span = cx.tcx.hir().span(fi.hir_id); + match Self::name_of_extern_decl(tcx, fi) { + SymbolName::Normal(_) => fi_span, + SymbolName::Link(_, annot_span) => fi_span.to(annot_span), + } + }; // Finally, emit the diagnostic. tcx.struct_span_lint_hir( CLASHING_EXTERN_DECL, diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index d713397f758f3..da6281298b4f5 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -837,12 +837,13 @@ impl EncodeContext<'tcx> { let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let ast_item = tcx.hir().expect_trait_item(hir_id); + let ast_item_span = tcx.hir().span(hir_id); let trait_item = tcx.associated_item(def_id); let container = match trait_item.defaultness { hir::Defaultness::Default { has_value: true } => AssocContainer::TraitWithDefault, hir::Defaultness::Default { has_value: false } => AssocContainer::TraitRequired, - hir::Defaultness::Final => span_bug!(ast_item.span, "traits cannot have final items"), + hir::Defaultness::Final => span_bug!(ast_item_span, "traits cannot have final items"), }; record!(self.tables.kind[def_id] <- match trait_item.kind { @@ -886,7 +887,7 @@ impl EncodeContext<'tcx> { ty::AssocKind::Type => EntryKind::AssocType(container), }); record!(self.tables.visibility[def_id] <- trait_item.vis); - record!(self.tables.span[def_id] <- ast_item.span); + record!(self.tables.span[def_id] <- ast_item_span); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, ast_item.ident); self.encode_stability(def_id); @@ -926,20 +927,21 @@ impl EncodeContext<'tcx> { let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); let ast_item = self.tcx.hir().expect_impl_item(hir_id); + let ast_item_span = self.tcx.hir().span(hir_id); let impl_item = self.tcx.associated_item(def_id); let container = match impl_item.defaultness { hir::Defaultness::Default { has_value: true } => AssocContainer::ImplDefault, hir::Defaultness::Final => AssocContainer::ImplFinal, hir::Defaultness::Default { has_value: false } => { - span_bug!(ast_item.span, "impl items always have values (currently)") + span_bug!(ast_item_span, "impl items always have values (currently)") } }; record!(self.tables.kind[def_id] <- match impl_item.kind { ty::AssocKind::Const => { if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind { - let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id); + let qualifs = self.tcx.at(ast_item_span).mir_const_qualif(def_id); EntryKind::AssocConst( container, @@ -968,7 +970,7 @@ impl EncodeContext<'tcx> { ty::AssocKind::Type => EntryKind::AssocType(container) }); record!(self.tables.visibility[def_id] <- impl_item.vis); - record!(self.tables.span[def_id] <- ast_item.span); + record!(self.tables.span[def_id] <- ast_item_span); record!(self.tables.attributes[def_id] <- ast_item.attrs); self.encode_ident_span(def_id, impl_item.ident); self.encode_stability(def_id); @@ -1081,12 +1083,13 @@ impl EncodeContext<'tcx> { debug!("EncodeContext::encode_info_for_item({:?})", def_id); self.encode_ident_span(def_id, item.ident); + let item_span = tcx.hir().span(item.hir_id); record!(self.tables.kind[def_id] <- match item.kind { hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic, hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic, hir::ItemKind::Const(_, body_id) => { - let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id); + let qualifs = self.tcx.at(item_span).mir_const_qualif(def_id); EntryKind::Const( qualifs, self.encode_rendered_const_for_body(body_id) @@ -1157,7 +1160,7 @@ impl EncodeContext<'tcx> { let coerce_unsized_info = trait_ref.and_then(|t| { if Some(t.def_id) == self.tcx.lang_items().coerce_unsized_trait() { - Some(self.tcx.at(item.span).coerce_unsized_info(def_id)) + Some(self.tcx.at(item_span).coerce_unsized_info(def_id)) } else { None } @@ -1190,7 +1193,7 @@ impl EncodeContext<'tcx> { }); record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(&item.vis, item.hir_id, tcx)); - record!(self.tables.span[def_id] <- item.span); + record!(self.tables.span[def_id] <- item_span); record!(self.tables.attributes[def_id] <- item.attrs); // FIXME(eddyb) there should be a nicer way to do this. match item.kind { @@ -1530,6 +1533,7 @@ impl EncodeContext<'tcx> { fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) { let tcx = self.tcx; + let nitem_span = tcx.hir().span(nitem.hir_id); debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id); @@ -1552,7 +1556,7 @@ impl EncodeContext<'tcx> { }); record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(&nitem.vis, nitem.hir_id, self.tcx)); - record!(self.tables.span[def_id] <- nitem.span); + record!(self.tables.span[def_id] <- nitem_span); record!(self.tables.attributes[def_id] <- nitem.attrs); self.encode_ident_span(def_id, nitem.ident); self.encode_stability(def_id); diff --git a/src/librustc_middle/hir/map/collector.rs b/src/librustc_middle/hir/map/collector.rs index ba326a6e7505e..f96e42a613a98 100644 --- a/src/librustc_middle/hir/map/collector.rs +++ b/src/librustc_middle/hir/map/collector.rs @@ -143,7 +143,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { }; collector.insert_entry( - krate.item.span, + DUMMY_SP, hir::CRATE_HIR_ID, Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) }, hash, @@ -349,12 +349,12 @@ 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(DUMMY_SP, 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(DUMMY_SP, ctor_hir_id, Node::Ctor(struct_def)); } } intravisit::walk_item(this, i); @@ -363,7 +363,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { } fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) { - self.insert(foreign_item.span, foreign_item.hir_id, Node::ForeignItem(foreign_item)); + self.insert(DUMMY_SP, foreign_item.hir_id, Node::ForeignItem(foreign_item)); self.with_parent(foreign_item.hir_id, |this| { intravisit::walk_foreign_item(this, foreign_item); @@ -381,7 +381,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(DUMMY_SP, ti.hir_id, Node::TraitItem(ti), hash); this.with_parent(ti.hir_id, |this| { intravisit::walk_trait_item(this, ti); @@ -395,7 +395,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(DUMMY_SP, ii.hir_id, Node::ImplItem(ii), hash); this.with_parent(ii.hir_id, |this| { intravisit::walk_impl_item(this, ii); diff --git a/src/librustc_passes/check_attr.rs b/src/librustc_passes/check_attr.rs index 4eb592361656d..4f7d806b3c622 100644 --- a/src/librustc_passes/check_attr.rs +++ b/src/librustc_passes/check_attr.rs @@ -51,11 +51,11 @@ impl CheckAttrVisitor<'tcx> { &self, hir_id: HirId, attrs: &'hir [Attribute], - span: &Span, target: Target, item: Option<&Item<'_>>, ) { let mut is_valid = true; + let span = &self.tcx.hir().span(hir_id); for attr in attrs { is_valid &= if attr.check_name(sym::inline) { self.check_inline(hir_id, attr, span, target) @@ -416,25 +416,25 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx Item<'tcx>) { let target = Target::from_item(item); - self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item)); + self.check_attributes(item.hir_id, item.attrs, target, Some(item)); intravisit::walk_item(self, item) } fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) { let target = Target::from_trait_item(trait_item); - self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None); + self.check_attributes(trait_item.hir_id, &trait_item.attrs, target, None); intravisit::walk_trait_item(self, trait_item) } fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem<'tcx>) { let target = Target::from_foreign_item(f_item); - self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None); + self.check_attributes(f_item.hir_id, &f_item.attrs, target, None); intravisit::walk_foreign_item(self, f_item) } fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { let target = target_from_impl_item(self.tcx, impl_item); - self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None); + self.check_attributes(impl_item.hir_id, &impl_item.attrs, target, None); intravisit::walk_impl_item(self, impl_item) } diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index ba67309379ac0..dfd18382ded2f 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -577,6 +577,7 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { if self.should_warn_about_item(item) { // For most items, we want to highlight its identifier + let span = self.tcx.hir().span(item.hir_id); let span = match item.kind { hir::ItemKind::Fn(..) | hir::ItemKind::Mod(..) @@ -591,13 +592,13 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { // (and thus has a source_callee set). // We should probably annotate ident.span with the macro // context, but that's a larger change. - if item.span.source_callee().is_some() { - self.tcx.sess.source_map().guess_head_span(item.span) + if span.source_callee().is_some() { + self.tcx.sess.source_map().guess_head_span(span) } else { item.ident.span } } - _ => item.span, + _ => span, }; let participle = match item.kind { hir::ItemKind::Struct(..) => "constructed", // Issue #52325 @@ -626,7 +627,8 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { fn visit_foreign_item(&mut self, fi: &'tcx hir::ForeignItem<'tcx>) { if self.should_warn_about_foreign_item(fi) { - self.warn_dead_code(fi.hir_id, fi.span, fi.ident.name, "used"); + let span = self.tcx.hir().span(fi.hir_id); + self.warn_dead_code(fi.hir_id, span, fi.ident.name, "used"); } intravisit::walk_foreign_item(self, fi); } @@ -643,25 +645,22 @@ impl Visitor<'tcx> for DeadVisitor<'tcx> { match impl_item.kind { hir::ImplItemKind::Const(_, body_id) => { if !self.symbol_is_live(impl_item.hir_id) { - self.warn_dead_code( - impl_item.hir_id, - impl_item.span, - impl_item.ident.name, - "used", - ); + let span = self.tcx.hir().span(impl_item.hir_id); + self.warn_dead_code(impl_item.hir_id, span, impl_item.ident.name, "used"); } self.visit_nested_body(body_id) } hir::ImplItemKind::Fn(_, body_id) => { if !self.symbol_is_live(impl_item.hir_id) { - // FIXME(66095): Because impl_item.span is annotated with things + // FIXME(66095): Because impl_item's span is annotated with things // like expansion data, and ident.span isn't, we use the // def_span method if it's part of a macro invocation // (and thus has a source_callee set). // We should probably annotate ident.span with the macro // context, but that's a larger change. - let span = if impl_item.span.source_callee().is_some() { - self.tcx.sess.source_map().guess_head_span(impl_item.span) + let span = self.tcx.hir().span(impl_item.hir_id); + let span = if span.source_callee().is_some() { + self.tcx.sess.source_map().guess_head_span(span) } else { impl_item.ident.span }; diff --git a/src/librustc_passes/entry.rs b/src/librustc_passes/entry.rs index e0ad0ac77476f..3b1b491872a8a 100644 --- a/src/librustc_passes/entry.rs +++ b/src/librustc_passes/entry.rs @@ -3,14 +3,14 @@ use rustc_ast::entry::EntryPointType; use rustc_errors::struct_span_err; use rustc_hir::def_id::{CrateNum, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; -use rustc_hir::{HirId, ImplItem, Item, ItemKind, TraitItem}; +use rustc_hir::{HirId, ImplItem, Item, ItemKind, TraitItem, CRATE_HIR_ID}; use rustc_middle::hir::map::Map; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_session::config::{CrateType, EntryFnType}; use rustc_session::Session; use rustc_span::symbol::sym; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::DUMMY_SP; struct EntryContext<'a, 'tcx> { session: &'a Session, @@ -18,17 +18,17 @@ struct EntryContext<'a, 'tcx> { map: Map<'tcx>, /// The top-level function called `main`. - main_fn: Option<(HirId, Span)>, + main_fn: Option, /// The function that has attribute named `main`. - attr_main_fn: Option<(HirId, Span)>, + attr_main_fn: Option, /// The function that has the attribute 'start' on it. - start_fn: Option<(HirId, Span)>, + start_fn: Option, /// The functions that one might think are `main` but aren't, e.g. /// main functions not defined at the top level. For diagnostics. - non_main_fns: Vec<(HirId, Span)>, + non_main_fns: Vec, } impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { @@ -104,37 +104,41 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { match entry_point_type(item, at_root) { EntryPointType::MainNamed => { if ctxt.main_fn.is_none() { - ctxt.main_fn = Some((item.hir_id, item.span)); + ctxt.main_fn = Some(item.hir_id); } else { - struct_span_err!(ctxt.session, item.span, E0136, "multiple `main` functions") - .emit(); + let span = ctxt.map.span(item.hir_id); + struct_span_err!(ctxt.session, span, E0136, "multiple `main` functions").emit(); } } EntryPointType::OtherMain => { - ctxt.non_main_fns.push((item.hir_id, item.span)); + ctxt.non_main_fns.push(item.hir_id); } EntryPointType::MainAttr => { if ctxt.attr_main_fn.is_none() { - ctxt.attr_main_fn = Some((item.hir_id, item.span)); + ctxt.attr_main_fn = Some(item.hir_id); } else { + let span = ctxt.map.span(item.hir_id); + let other_span = ctxt.map.span(ctxt.attr_main_fn.unwrap()); struct_span_err!( ctxt.session, - item.span, + span, E0137, "multiple functions with a `#[main]` attribute" ) - .span_label(item.span, "additional `#[main]` function") - .span_label(ctxt.attr_main_fn.unwrap().1, "first `#[main]` function") + .span_label(span, "additional `#[main]` function") + .span_label(other_span, "first `#[main]` function") .emit(); } } EntryPointType::Start => { if ctxt.start_fn.is_none() { - ctxt.start_fn = Some((item.hir_id, item.span)); + ctxt.start_fn = Some(item.hir_id); } else { - struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions") - .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here") - .span_label(item.span, "multiple `start` functions") + let span = ctxt.map.span(item.hir_id); + let other_span = ctxt.map.span(ctxt.start_fn.unwrap()); + struct_span_err!(ctxt.session, span, E0138, "multiple `start` functions") + .span_label(other_span, "previous `#[start]` function here") + .span_label(span, "multiple `start` functions") .emit(); } } @@ -146,11 +150,11 @@ fn configure_main( tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>, ) -> Option<(LocalDefId, EntryFnType)> { - if let Some((hir_id, _)) = visitor.start_fn { + if let Some(hir_id) = visitor.start_fn { Some((tcx.hir().local_def_id(hir_id), EntryFnType::Start)) - } else if let Some((hir_id, _)) = visitor.attr_main_fn { + } else if let Some(hir_id) = visitor.attr_main_fn { Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main)) - } else if let Some((hir_id, _)) = visitor.main_fn { + } else if let Some(hir_id) = visitor.main_fn { Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main)) } else { no_main_err(tcx, visitor); @@ -159,7 +163,7 @@ fn configure_main( } fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { - let sp = tcx.hir().krate().item.span; + let sp = tcx.hir().span(CRATE_HIR_ID); if *tcx.sess.parse_sess.reached_eof.borrow() { // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about // the missing `fn main()` then as it might have been hidden inside an unclosed block. @@ -177,7 +181,8 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { ); let filename = &tcx.sess.local_crate_source_file; let note = if !visitor.non_main_fns.is_empty() { - for &(_, span) in &visitor.non_main_fns { + for &hir_id in &visitor.non_main_fns { + let span = tcx.hir().span(hir_id); err.span_note(span, "here is a function named `main`"); } err.note("you have one or more functions named `main` not defined at the crate level"); diff --git a/src/librustc_passes/layout_test.rs b/src/librustc_passes/layout_test.rs index 2419e6965968e..d042c9c03fed3 100644 --- a/src/librustc_passes/layout_test.rs +++ b/src/librustc_passes/layout_test.rs @@ -47,6 +47,7 @@ impl LayoutTest<'tcx> { let tcx = self.tcx; let param_env = self.tcx.param_env(item_def_id); let ty = self.tcx.type_of(item_def_id); + let span = self.tcx.hir().span(item.hir_id); match self.tcx.layout_of(param_env.and(ty)) { Ok(ty_layout) => { // Check out the `#[rustc_layout(..)]` attribute to tell what to dump. @@ -55,24 +56,20 @@ impl LayoutTest<'tcx> { for meta_item in meta_items { match meta_item.name_or_empty() { sym::abi => { - self.tcx.sess.span_err(item.span, &format!("abi: {:?}", ty_layout.abi)); + self.tcx.sess.span_err(span, &format!("abi: {:?}", ty_layout.abi)); } sym::align => { - self.tcx - .sess - .span_err(item.span, &format!("align: {:?}", ty_layout.align)); + self.tcx.sess.span_err(span, &format!("align: {:?}", ty_layout.align)); } sym::size => { - self.tcx - .sess - .span_err(item.span, &format!("size: {:?}", ty_layout.size)); + self.tcx.sess.span_err(span, &format!("size: {:?}", ty_layout.size)); } sym::homogeneous_aggregate => { self.tcx.sess.span_err( - item.span, + span, &format!( "homogeneous_aggregate: {:?}", ty_layout @@ -85,7 +82,7 @@ impl LayoutTest<'tcx> { let normalized_ty = self.tcx.normalize_erasing_regions(param_env.with_reveal_all(), ty); self.tcx.sess.span_err( - item.span, + span, &format!("layout_of({:?}) = {:#?}", normalized_ty, *ty_layout), ); } @@ -101,7 +98,7 @@ impl LayoutTest<'tcx> { } Err(layout_error) => { - self.tcx.sess.span_err(item.span, &format!("layout error: {:?}", layout_error)); + self.tcx.sess.span_err(span, &format!("layout error: {:?}", layout_error)); } } } diff --git a/src/librustc_passes/reachable.rs b/src/librustc_passes/reachable.rs index c9a4428c007aa..8d714e7655ad7 100644 --- a/src/librustc_passes/reachable.rs +++ b/src/librustc_passes/reachable.rs @@ -55,7 +55,7 @@ fn method_might_be_inlined( } match tcx.hir().find(tcx.hir().as_local_hir_id(impl_src)) { Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), - Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), + Some(..) | None => span_bug!(tcx.hir().span(impl_item.hir_id), "impl did is not an item"), } } diff --git a/src/librustc_passes/stability.rs b/src/librustc_passes/stability.rs index e9b4a85d94a3c..dddacc8d23d6c 100644 --- a/src/librustc_passes/stability.rs +++ b/src/librustc_passes/stability.rs @@ -496,10 +496,11 @@ impl Visitor<'tcx> for Checker<'tcx> { } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { + let item_span = self.tcx.hir().span(item.hir_id); match item.kind { hir::ItemKind::ExternCrate(_) => { // compiler-generated `extern crate` items have a dummy span. - if item.span.is_dummy() { + if item_span.is_dummy() { return; } @@ -509,7 +510,7 @@ impl Visitor<'tcx> for Checker<'tcx> { None => return, }; let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; - self.tcx.check_stability(def_id, Some(item.hir_id), item.span); + self.tcx.check_stability(def_id, Some(item.hir_id), item_span); } // For implementations of traits, check the stability of each item @@ -519,6 +520,7 @@ impl Visitor<'tcx> for Checker<'tcx> { if let Res::Def(DefKind::Trait, trait_did) = t.path.res { for impl_item_ref in items { let impl_item = self.tcx.hir().impl_item(impl_item_ref.id); + let impl_item_span = self.tcx.hir().span(impl_item.hir_id); let trait_item_def_id = self .tcx .associated_items(trait_did) @@ -527,7 +529,7 @@ impl Visitor<'tcx> for Checker<'tcx> { .map(|item| item.def_id); if let Some(def_id) = trait_item_def_id { // Pass `None` to skip deprecation warnings. - self.tcx.check_stability(def_id, None, impl_item.span); + self.tcx.check_stability(def_id, None, impl_item_span); } } } @@ -544,7 +546,7 @@ impl Visitor<'tcx> for Checker<'tcx> { feature_err( &self.tcx.sess.parse_sess, sym::untagged_unions, - item.span, + item_span, "unions with `Drop` implementations are unstable", ) .emit(); @@ -554,7 +556,7 @@ impl Visitor<'tcx> for Checker<'tcx> { feature_err( &self.tcx.sess.parse_sess, sym::untagged_unions, - item.span, + item_span, "unions with non-`Copy` fields are unstable", ) .emit(); diff --git a/src/librustc_passes/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs index f2f07b5d4fb26..e5b6bb4fa80da 100644 --- a/src/librustc_passes/weak_lang_items.rs +++ b/src/librustc_passes/weak_lang_items.rs @@ -12,7 +12,6 @@ use rustc_middle::ty::TyCtxt; use rustc_session::config::CrateType; use rustc_span::symbol::sym; use rustc_span::symbol::Symbol; -use rustc_span::Span; struct Context<'a, 'tcx> { tcx: TyCtxt<'tcx>, @@ -72,7 +71,7 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { } impl<'a, 'tcx> Context<'a, 'tcx> { - fn register(&mut self, name: Symbol, span: Span, hir_id: hir::HirId) { + fn register(&mut self, name: Symbol, hir_id: hir::HirId) { if let Some(&item) = WEAK_ITEMS_REFS.get(&name) { if self.items.require(item).is_err() { self.items.missing.push(item); @@ -88,6 +87,7 @@ impl<'a, 'tcx> Context<'a, 'tcx> { } } } else { + let span = self.tcx.hir().span(hir_id); struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name) .emit(); } @@ -103,7 +103,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) { if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) { - self.register(lang_item, i.span, i.hir_id); + self.register(lang_item, i.hir_id); } intravisit::walk_foreign_item(self, i) } diff --git a/src/librustc_plugin_impl/build.rs b/src/librustc_plugin_impl/build.rs index 34522cfe97f35..fef6cad3d08c5 100644 --- a/src/librustc_plugin_impl/build.rs +++ b/src/librustc_plugin_impl/build.rs @@ -7,17 +7,16 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; -use rustc_span::Span; struct RegistrarFinder { - registrars: Vec<(hir::HirId, Span)>, + registrars: Vec, } impl<'v> ItemLikeVisitor<'v> for RegistrarFinder { fn visit_item(&mut self, item: &hir::Item<'_>) { if let hir::ItemKind::Fn(..) = item.kind { if attr::contains_name(&item.attrs, sym::plugin_registrar) { - self.registrars.push((item.hir_id, item.span)); + self.registrars.push(item.hir_id); } } } @@ -41,13 +40,14 @@ fn plugin_registrar_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option { match finder.registrars.len() { 0 => None, 1 => { - let (hir_id, _) = finder.registrars.pop().unwrap(); + let hir_id = finder.registrars.pop().unwrap(); Some(tcx.hir().local_def_id(hir_id).to_def_id()) } _ => { let diagnostic = tcx.sess.diagnostic(); let mut e = diagnostic.struct_err("multiple plugin registration functions found"); - for &(_, span) in &finder.registrars { + for &hir_id in &finder.registrars { + let span = tcx.hir().span(hir_id); e.span_note(span, "one is here"); } e.emit(); diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index 05e69bc50a54b..a2a67b1febf94 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -1197,7 +1197,8 @@ fn compute_object_lifetime_defaults(tcx: TyCtxt<'_>) -> HirIdMap>>() .join(","); - tcx.sess.span_err(item.span, &object_lifetime_default_reprs); + let item_span = tcx.hir().span(item.hir_id); + tcx.sess.span_err(item_span, &object_lifetime_default_reprs); } map.insert(item.hir_id, result); diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 473899939211d..e1c17074b0633 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -796,18 +796,18 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }) .collect::>>()?, ), - Node::Item(&hir::Item { span, kind: hir::ItemKind::Fn(ref sig, ..), .. }) + Node::Item(&hir::Item { hir_id, kind: hir::ItemKind::Fn(ref sig, ..), .. }) | Node::ImplItem(&hir::ImplItem { - span, + hir_id, kind: hir::ImplItemKind::Fn(ref sig, _), .. }) | Node::TraitItem(&hir::TraitItem { - span, + hir_id, kind: hir::TraitItemKind::Fn(ref sig, _), .. }) => ( - sm.guess_head_span(span), + sm.guess_head_span(hir.span(hir_id)), sig.decl .inputs .iter() diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index 595992d01dd2d..54439e6090c18 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -156,8 +156,9 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { _ => {} } + let parent_item_span = tcx.hir().span(parent_id); span_bug!( - parent_item.span, + parent_item_span, "unexpected parent of trait or impl item or item not found: {:?}", parent_item.kind ) @@ -214,7 +215,7 @@ fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { .map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()), ), hir::ItemKind::TraitAlias(..) => &[], - _ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"), + _ => span_bug!(tcx.hir().span(id), "associated_item_def_ids: not impl or trait"), } } diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index 3ec6973a17d56..5b23a7e262724 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -27,8 +27,9 @@ fn equate_intrinsic_type<'tcx>( match it.kind { hir::ForeignItemKind::Fn(..) => {} _ => { - struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function") - .span_label(it.span, "expected a function") + let it_span = tcx.hir().span(it.hir_id); + struct_span_err!(tcx.sess, it_span, E0622, "intrinsic must be a function") + .span_label(it_span, "expected a function") .emit(); return; } @@ -62,7 +63,8 @@ fn equate_intrinsic_type<'tcx>( safety, abi, ))); - let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType); + let it_span = tcx.hir().span(it.hir_id); + let cause = ObligationCause::new(it_span, it.hir_id, ObligationCauseCode::IntrinsicType); require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty); } @@ -117,14 +119,15 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) { | "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)), "fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()), op => { + let it_span = tcx.hir().span(it.hir_id); struct_span_err!( tcx.sess, - it.span, + it_span, E0092, "unrecognized atomic operation function: `{}`", op ) - .span_label(it.span, "unrecognized atomic operation") + .span_label(it_span, "unrecognized atomic operation") .emit(); return; } @@ -350,14 +353,15 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) { "count_code_region" => (0, vec![tcx.types.u32], tcx.mk_unit()), ref other => { + let it_span = tcx.hir().span(it.hir_id); struct_span_err!( tcx.sess, - it.span, + it_span, E0093, "unrecognized intrinsic function: `{}`", *other ) - .span_label(it.span, "unrecognized intrinsic") + .span_label(it_span, "unrecognized intrinsic") .emit(); return; } @@ -427,9 +431,10 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) (2, params, param(1)) } Err(_) => { + let it_span = tcx.hir().span(it.hir_id); struct_span_err!( tcx.sess, - it.span, + it_span, E0439, "invalid `simd_shuffle`, needs length: `{}`", name @@ -440,7 +445,8 @@ pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) }, _ => { let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name); - tcx.sess.struct_span_err(it.span, &msg).emit(); + let it_span = tcx.hir().span(it.hir_id); + tcx.sess.struct_span_err(it_span, &msg).emit(); return; } }; diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 1cdc2dc27707a..73dbcc3a550fc 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -1333,12 +1333,13 @@ impl intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> { // Find a `use` statement. for item_id in module.item_ids { let item = self.tcx.hir().expect_item(item_id.id); + let item_span = self.tcx.hir().span(item.hir_id); match item.kind { hir::ItemKind::Use(..) => { // Don't suggest placing a `use` before the prelude // import or other generated ones. - if !item.span.from_expansion() { - self.span = Some(item.span.shrink_to_lo()); + if !item_span.from_expansion() { + self.span = Some(item_span.shrink_to_lo()); self.found_use = true; return; } @@ -1347,11 +1348,11 @@ impl intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> { hir::ItemKind::ExternCrate(_) => {} // ...but do place them before the first other item. _ => { - if self.span.map_or(true, |span| item.span < span) { - if !item.span.from_expansion() { + if self.span.map_or(true, |span| item_span < span) { + if !item_span.from_expansion() { // Don't insert between attributes and an item. if item.attrs.is_empty() { - self.span = Some(item.span.shrink_to_lo()); + self.span = Some(item_span.shrink_to_lo()); } else { // Find the first attribute on the item. for attr in item.attrs { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 8719cbaaa0276..1715cae7929b4 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1989,6 +1989,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { for item in m.items { let generics = tcx.generics_of(tcx.hir().local_def_id(item.hir_id)); let own_counts = generics.own_counts(); + let item_span = tcx.hir().span(item.hir_id); if generics.params.len() - own_counts.lifetimes != 0 { let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) { (_, 0) => ("type", "types", Some("u32")), @@ -1999,12 +2000,12 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { }; struct_span_err!( tcx.sess, - item.span, + item_span, E0044, "foreign items may not have {} parameters", kinds, ) - .span_label(item.span, &format!("can't have {} parameters", kinds)) + .span_label(item_span, &format!("can't have {} parameters", kinds)) .help( // FIXME: once we start storing spans for type arguments, turn this // into a suggestion. @@ -2019,7 +2020,7 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item<'tcx>) { } if let hir::ForeignItemKind::Fn(ref fn_decl, _, _) = item.kind { - require_c_abi_if_c_variadic(tcx, fn_decl, m.abi, item.span); + require_c_abi_if_c_variadic(tcx, fn_decl, m.abi, item_span); } } } @@ -2076,15 +2077,16 @@ fn report_forbidden_specialization( impl_item: &hir::ImplItem<'_>, parent_impl: DefId, ) { + let impl_item_span = tcx.hir().span(impl_item.hir_id); let mut err = struct_span_err!( tcx.sess, - impl_item.span, + impl_item_span, E0520, "`{}` specializes an item from a parent `impl`, but \ that item is not marked `default`", impl_item.ident ); - err.span_label(impl_item.span, format!("cannot specialize default item `{}`", impl_item.ident)); + err.span_label(impl_item_span, format!("cannot specialize default item `{}`", impl_item.ident)); match tcx.span_of_impl(parent_impl) { Ok(span) => { @@ -2187,7 +2189,7 @@ fn check_impl_items_against_trait<'tcx>( ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {} ty::ImplPolarity::Negative => { if let [first_item_ref, ..] = impl_item_refs { - let first_item_span = tcx.hir().impl_item(first_item_ref.id).span; + let first_item_span = tcx.hir().span(tcx.hir().impl_item(first_item_ref.id).hir_id); struct_span_err!( tcx.sess, first_item_span, @@ -2222,6 +2224,7 @@ fn check_impl_items_against_trait<'tcx>( // Check that impl definition matches trait definition if let Some(ty_trait_item) = ty_trait_item { + let impl_item_span = tcx.hir().span(impl_item.hir_id); match impl_item.kind { hir::ImplItemKind::Const(..) => { // Find associated const definition. @@ -2229,21 +2232,21 @@ fn check_impl_items_against_trait<'tcx>( compare_const_impl( tcx, &ty_impl_item, - impl_item.span, + impl_item_span, &ty_trait_item, impl_trait_ref, ); } else { let mut err = struct_span_err!( tcx.sess, - impl_item.span, + impl_item_span, E0323, "item `{}` is an associated const, \ which doesn't match its trait `{}`", ty_impl_item.ident, impl_trait_ref.print_only_trait_path() ); - err.span_label(impl_item.span, "does not match trait"); + err.span_label(impl_item_span, "does not match trait"); // We can only get the spans from local trait definition // Same for E0324 and E0325 if let Some(trait_span) = tcx.hir().span_if_local(ty_trait_item.def_id) { @@ -2258,7 +2261,7 @@ fn check_impl_items_against_trait<'tcx>( compare_impl_method( tcx, &ty_impl_item, - impl_item.span, + impl_item_span, &ty_trait_item, impl_trait_ref, opt_trait_span, @@ -2266,14 +2269,14 @@ fn check_impl_items_against_trait<'tcx>( } else { let mut err = struct_span_err!( tcx.sess, - impl_item.span, + impl_item_span, E0324, "item `{}` is an associated method, \ which doesn't match its trait `{}`", ty_impl_item.ident, impl_trait_ref.print_only_trait_path() ); - err.span_label(impl_item.span, "does not match trait"); + err.span_label(impl_item_span, "does not match trait"); if let Some(trait_span) = opt_trait_span { err.span_label(trait_span, "item in trait"); } @@ -2286,7 +2289,7 @@ fn check_impl_items_against_trait<'tcx>( compare_ty_impl( tcx, &ty_impl_item, - impl_item.span, + impl_item_span, &ty_trait_item, impl_trait_ref, opt_trait_span, @@ -2294,14 +2297,14 @@ fn check_impl_items_against_trait<'tcx>( } else { let mut err = struct_span_err!( tcx.sess, - impl_item.span, + impl_item_span, E0325, "item `{}` is an associated type, \ which doesn't match its trait `{}`", ty_impl_item.ident, impl_trait_ref.print_only_trait_path() ); - err.span_label(impl_item.span, "does not match trait"); + err.span_label(impl_item_span, "does not match trait"); if let Some(trait_span) = opt_trait_span { err.span_label(trait_span, "item in trait"); } diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 810a8baa146de..f10c95d836db8 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -110,7 +110,8 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { .impl_trait_ref(tcx.hir().local_def_id(item.hir_id)) .map_or(false, |trait_ref| tcx.trait_is_auto(trait_ref.def_id)); if let (hir::Defaultness::Default { .. }, true) = (defaultness, is_auto) { - let sp = of_trait.as_ref().map(|t| t.path.span).unwrap_or(item.span); + let item_span = tcx.hir().span(item.hir_id); + let sp = of_trait.as_ref().map(|t| t.path.span).unwrap_or(item_span); let mut err = tcx.sess.struct_span_err(sp, "impls of auto traits cannot be default"); err.span_labels(defaultness_span, "default because of this"); @@ -186,13 +187,14 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { let hir_id = tcx.hir().as_local_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(hir_id); + let trait_item_span = tcx.hir().span(trait_item.hir_id); let method_sig = match trait_item.kind { hir::TraitItemKind::Fn(ref sig, _) => Some(sig), _ => None, }; check_object_unsafe_self_trait_by_name(tcx, &trait_item); - check_associated_item(tcx, trait_item.hir_id, trait_item.span, method_sig); + check_associated_item(tcx, trait_item.hir_id, trait_item_span, method_sig); } fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool { @@ -260,13 +262,14 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { let hir_id = tcx.hir().as_local_hir_id(def_id); let impl_item = tcx.hir().expect_impl_item(hir_id); + let impl_item_span = tcx.hir().span(impl_item.hir_id); let method_sig = match impl_item.kind { hir::ImplItemKind::Fn(ref sig, _) => Some(sig), _ => None, }; - check_associated_item(tcx, impl_item.hir_id, impl_item.span, method_sig); + check_associated_item(tcx, impl_item.hir_id, impl_item_span, method_sig); } fn check_associated_item( @@ -323,7 +326,8 @@ fn check_associated_item( } fn for_item<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>) -> CheckWfFcxBuilder<'tcx> { - for_id(tcx, item.hir_id, item.span) + let item_span = tcx.hir().span(item.hir_id); + for_id(tcx, item.hir_id, item_span) } fn for_id(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) -> CheckWfFcxBuilder<'_> { @@ -354,6 +358,7 @@ fn check_type_defn<'tcx, F>( ) where F: for<'fcx> FnMut(&FnCtxt<'fcx, 'tcx>) -> Vec>, { + let item_span = tcx.hir().span(item.hir_id); for_item(tcx, item).with_fcx(|fcx, fcx_tcx| { let variants = lookup_fields(fcx); let def_id = fcx.tcx.hir().local_def_id(item.hir_id); @@ -369,7 +374,7 @@ fn check_type_defn<'tcx, F>( if ty.needs_infer() { fcx_tcx .sess - .delay_span_bug(item.span, &format!("inference variables in {:?}", ty)); + .delay_span_bug(item_span, &format!("inference variables in {:?}", ty)); // Just treat unresolved type expression as if it needs drop. true } else { @@ -428,7 +433,7 @@ fn check_type_defn<'tcx, F>( } } - check_where_clauses(tcx, fcx, item.span, def_id.to_def_id(), None); + check_where_clauses(tcx, fcx, item_span, def_id.to_def_id(), None); // No implied bounds in a struct definition. vec![] @@ -455,8 +460,9 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) { } } + let item_span = tcx.hir().span(item.hir_id); for_item(tcx, item).with_fcx(|fcx, _| { - check_where_clauses(tcx, fcx, item.span, trait_def_id.to_def_id(), None); + check_where_clauses(tcx, fcx, item_span, trait_def_id.to_def_id(), None); check_associated_type_defaults(fcx, trait_def_id.to_def_id()); vec![] @@ -561,10 +567,11 @@ fn check_associated_type_defaults(fcx: &FnCtxt<'_, '_>, trait_def_id: DefId) { } fn check_item_fn(tcx: TyCtxt<'_>, item: &hir::Item<'_>) { + let item_span = tcx.hir().span(item.hir_id); for_item(tcx, item).with_fcx(|fcx, tcx| { let def_id = fcx.tcx.hir().local_def_id(item.hir_id); let sig = fcx.tcx.fn_sig(def_id); - let sig = fcx.normalize_associated_types_in(item.span, &sig); + let sig = fcx.normalize_associated_types_in(item_span, &sig); let mut implied_bounds = vec![]; let hir_sig = match &item.kind { ItemKind::Fn(sig, ..) => sig, @@ -620,6 +627,7 @@ fn check_impl<'tcx>( ) { debug!("check_impl: {:?}", item); + let item_span = tcx.hir().span(item.hir_id); for_item(tcx, item).with_fcx(|fcx, tcx| { let item_def_id = fcx.tcx.hir().local_def_id(item.hir_id); @@ -645,7 +653,7 @@ fn check_impl<'tcx>( } None => { let self_ty = fcx.tcx.type_of(item_def_id); - let self_ty = fcx.normalize_associated_types_in(item.span, &self_ty); + let self_ty = fcx.normalize_associated_types_in(item_span, &self_ty); fcx.register_wf_obligation( self_ty.into(), ast_self_ty.span, @@ -654,9 +662,9 @@ fn check_impl<'tcx>( } } - check_where_clauses(tcx, fcx, item.span, item_def_id.to_def_id(), None); + check_where_clauses(tcx, fcx, item_span, item_def_id.to_def_id(), None); - fcx.impl_implied_bounds(item_def_id.to_def_id(), item.span) + fcx.impl_implied_bounds(item_def_id.to_def_id(), item_span) }); } diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index 81daf064bb368..560ea0cdc6b57 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -24,7 +24,8 @@ pub fn check_crate(tcx: TyCtxt<'_>) { impl ItemLikeVisitor<'v> for CheckVisitor<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { - if item.vis.node.is_pub() || item.span.is_dummy() { + let item_span = self.tcx.hir().span(item.hir_id); + if item.vis.node.is_pub() || item_span.is_dummy() { return; } if let hir::ItemKind::Use(ref path, _) = item.kind { @@ -213,9 +214,10 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { if let hir::ItemKind::ExternCrate(orig_name) = item.kind { let extern_crate_def_id = self.tcx.hir().local_def_id(item.hir_id); + let item_span = self.tcx.hir().span(item.hir_id); self.crates_to_lint.push(ExternCrateToLint { def_id: extern_crate_def_id.to_def_id(), - span: item.span, + span: item_span, orig_name, warn_if_unused: !item.ident.as_str().starts_with('_'), }); diff --git a/src/librustc_typeck/coherence/inherent_impls.rs b/src/librustc_typeck/coherence/inherent_impls.rs index 93ee87f6c572e..8b798203eb096 100644 --- a/src/librustc_typeck/coherence/inherent_impls.rs +++ b/src/librustc_typeck/coherence/inherent_impls.rs @@ -50,6 +50,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { }; let def_id = self.tcx.hir().local_def_id(item.hir_id); + let item_span = self.tcx.hir().span(item.hir_id); let self_ty = self.tcx.type_of(def_id); let lang_items = self.tcx.lang_items(); match self_ty.kind { @@ -69,7 +70,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "bool", "bool", - item.span, + item_span, ); } ty::Char => { @@ -79,7 +80,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "char", "char", - item.span, + item_span, ); } ty::Str => { @@ -89,7 +90,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { lang_items.str_alloc_impl(), "str", "str", - item.span, + item_span, ); } ty::Slice(slice_item) if slice_item == self.tcx.types.u8 => { @@ -99,7 +100,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { lang_items.slice_u8_alloc_impl(), "slice_u8", "[u8]", - item.span, + item_span, ); } ty::Slice(_) => { @@ -109,7 +110,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { lang_items.slice_alloc_impl(), "slice", "[T]", - item.span, + item_span, ); } ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Not }) @@ -121,7 +122,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "const_slice_ptr", "*const [T]", - item.span, + item_span, ); } ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Mut }) @@ -133,7 +134,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "mut_slice_ptr", "*mut [T]", - item.span, + item_span, ); } ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Not }) => { @@ -143,7 +144,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "const_ptr", "*const T", - item.span, + item_span, ); } ty::RawPtr(ty::TypeAndMut { ty: _, mutbl: hir::Mutability::Mut }) => { @@ -153,7 +154,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "mut_ptr", "*mut T", - item.span, + item_span, ); } ty::Int(ast::IntTy::I8) => { @@ -163,7 +164,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "i8", "i8", - item.span, + item_span, ); } ty::Int(ast::IntTy::I16) => { @@ -173,7 +174,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "i16", "i16", - item.span, + item_span, ); } ty::Int(ast::IntTy::I32) => { @@ -183,7 +184,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "i32", "i32", - item.span, + item_span, ); } ty::Int(ast::IntTy::I64) => { @@ -193,7 +194,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "i64", "i64", - item.span, + item_span, ); } ty::Int(ast::IntTy::I128) => { @@ -203,7 +204,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "i128", "i128", - item.span, + item_span, ); } ty::Int(ast::IntTy::Isize) => { @@ -213,7 +214,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "isize", "isize", - item.span, + item_span, ); } ty::Uint(ast::UintTy::U8) => { @@ -223,7 +224,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "u8", "u8", - item.span, + item_span, ); } ty::Uint(ast::UintTy::U16) => { @@ -233,7 +234,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "u16", "u16", - item.span, + item_span, ); } ty::Uint(ast::UintTy::U32) => { @@ -243,7 +244,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "u32", "u32", - item.span, + item_span, ); } ty::Uint(ast::UintTy::U64) => { @@ -253,7 +254,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "u64", "u64", - item.span, + item_span, ); } ty::Uint(ast::UintTy::U128) => { @@ -263,7 +264,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "u128", "u128", - item.span, + item_span, ); } ty::Uint(ast::UintTy::Usize) => { @@ -273,7 +274,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { None, "usize", "usize", - item.span, + item_span, ); } ty::Float(ast::FloatTy::F32) => { @@ -283,7 +284,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { lang_items.f32_runtime_impl(), "f32", "f32", - item.span, + item_span, ); } ty::Float(ast::FloatTy::F64) => { @@ -293,7 +294,7 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { lang_items.f64_runtime_impl(), "f64", "f64", - item.span, + item_span, ); } ty::Error(_) => {} @@ -329,14 +330,15 @@ impl InherentCollect<'tcx> { let vec = self.impls_map.inherent_impls.entry(def_id).or_default(); vec.push(impl_def_id.to_def_id()); } else { + let item_span = self.tcx.hir().span(item.hir_id); struct_span_err!( self.tcx.sess, - item.span, + item_span, E0116, "cannot define inherent `impl` for a type outside of the crate \ where the type is defined" ) - .span_label(item.span, "impl for type defined outside of crate.") + .span_label(item_span, "impl for type defined outside of crate.") .note("define and implement a trait or new type instead") .emit(); } diff --git a/src/librustc_typeck/coherence/orphan.rs b/src/librustc_typeck/coherence/orphan.rs index f3d13519d4e82..3da6fee601d48 100644 --- a/src/librustc_typeck/coherence/orphan.rs +++ b/src/librustc_typeck/coherence/orphan.rs @@ -33,8 +33,9 @@ impl ItemLikeVisitor<'v> for OrphanChecker<'tcx> { ); let trait_ref = self.tcx.impl_trait_ref(def_id).unwrap(); let trait_def_id = trait_ref.def_id; + let item_span = self.tcx.hir().span(item.hir_id); let sm = self.tcx.sess.source_map(); - let sp = sm.guess_head_span(item.span); + let sp = sm.guess_head_span(item_span); match traits::orphan_check(self.tcx, def_id.to_def_id()) { Ok(()) => {} Err(traits::OrphanCheckErr::NonLocalInputType(tys)) => { diff --git a/src/librustc_typeck/coherence/unsafety.rs b/src/librustc_typeck/coherence/unsafety.rs index b281092ea631d..9ffc4881bc13f 100644 --- a/src/librustc_typeck/coherence/unsafety.rs +++ b/src/librustc_typeck/coherence/unsafety.rs @@ -25,6 +25,7 @@ impl UnsafetyChecker<'tcx> { polarity: hir::ImplPolarity, ) { let local_did = self.tcx.hir().local_def_id(item.hir_id); + let item_span = self.tcx.hir().span(item.hir_id); if let Some(trait_ref) = self.tcx.impl_trait_ref(local_did) { let trait_def = self.tcx.trait_def(trait_ref.def_id); let unsafe_attr = impl_generics.and_then(|generics| { @@ -34,7 +35,7 @@ impl UnsafetyChecker<'tcx> { (Unsafety::Normal, None, Unsafety::Unsafe, hir::ImplPolarity::Positive) => { struct_span_err!( self.tcx.sess, - item.span, + item_span, E0199, "implementing the trait `{}` is not unsafe", trait_ref.print_only_trait_path() @@ -45,7 +46,7 @@ impl UnsafetyChecker<'tcx> { (Unsafety::Unsafe, _, Unsafety::Normal, hir::ImplPolarity::Positive) => { struct_span_err!( self.tcx.sess, - item.span, + item_span, E0200, "the trait `{}` requires an `unsafe impl` declaration", trait_ref.print_only_trait_path() @@ -61,7 +62,7 @@ impl UnsafetyChecker<'tcx> { ) => { struct_span_err!( self.tcx.sess, - item.span, + item_span, E0569, "requires an `unsafe impl` declaration due to `#[{}]` attribute", attr_name @@ -71,7 +72,7 @@ impl UnsafetyChecker<'tcx> { (_, _, Unsafety::Unsafe, hir::ImplPolarity::Negative(_)) => { // Reported in AST validation - self.tcx.sess.delay_span_bug(item.span, "unsafe negative impl"); + self.tcx.sess.delay_span_bug(item_span, "unsafe negative impl"); } (_, _, Unsafety::Normal, hir::ImplPolarity::Negative(_)) | (Unsafety::Unsafe, _, Unsafety::Unsafe, hir::ImplPolarity::Positive) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index df6254c6aa835..c9d6ca73f6ae2 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -529,8 +529,9 @@ fn type_param_predicates( // Implied `Self: Trait` and supertrait bounds. if param_id == item_hir_id { let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id); + let item_span = tcx.hir().span(item.hir_id); extend = - Some((identity_trait_ref.without_const().to_predicate(tcx), item.span)); + Some((identity_trait_ref.without_const().to_predicate(tcx), item_span)); } generics } @@ -660,14 +661,16 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) { tcx.ensure().predicates_of(def_id); } hir::ItemKind::Trait(..) => { + let it_span = tcx.hir().span(it.hir_id); tcx.ensure().generics_of(def_id); tcx.ensure().trait_def(def_id); - tcx.at(it.span).super_predicates_of(def_id); + tcx.at(it_span).super_predicates_of(def_id); tcx.ensure().predicates_of(def_id); } hir::ItemKind::TraitAlias(..) => { + let it_span = tcx.hir().span(it.hir_id); tcx.ensure().generics_of(def_id); - tcx.at(it.span).super_predicates_of(def_id); + tcx.at(it_span).super_predicates_of(def_id); tcx.ensure().predicates_of(def_id); } hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => { @@ -959,15 +962,16 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi let (generics, bounds) = match item.kind { hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits), hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits), - _ => span_bug!(item.span, "super_predicates invoked on non-trait"), + _ => span_bug!(tcx.hir().span(item.hir_id), "super_predicates invoked on non-trait"), }; let icx = ItemCtxt::new(tcx, trait_def_id); // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`. let self_param_ty = tcx.types.self_param; + let item_span = tcx.hir().span(item.hir_id); let superbounds1 = - AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item.span); + AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item_span); let superbounds1 = superbounds1.predicates(tcx, self_param_ty); @@ -1002,18 +1006,19 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef { let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(hir_id); + let item_span = tcx.hir().span(item.hir_id); let (is_auto, unsafety) = match item.kind { hir::ItemKind::Trait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety), hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal), - _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"), + _ => span_bug!(item_span, "trait_def_of_item invoked on non-trait"), }; let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar); if paren_sugar && !tcx.features().unboxed_closures { tcx.sess .struct_span_err( - item.span, + item_span, "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \ which traits can use parenthetical notation", ) @@ -1564,7 +1569,8 @@ fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { } hir::ItemKind::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => { if is_rustc_reservation { - tcx.sess.span_err(item.span, "reservation impls can't be inherent"); + let item_span = tcx.hir().span(item.hir_id); + tcx.sess.span_err(item_span, "reservation impls can't be inherent"); } ty::ImplPolarity::Positive } @@ -1702,7 +1708,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat let ast_generics = match node { Node::TraitItem(item) => { if let hir::TraitItemKind::Type(bounds, _) = item.kind { - is_trait_associated_type = Some((bounds, item.span)); + is_trait_associated_type = Some((bounds, item.hir_id)); } &item.generics } @@ -1934,11 +1940,11 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat if tcx.features().generic_associated_types { // New behavior: bounds declared on associate type are predicates of that // associated type. Not the default because it needs more testing. - if let Some((bounds, span)) = is_trait_associated_type { + if let Some((bounds, hir_id)) = is_trait_associated_type { let projection_ty = tcx.mk_projection(def_id, InternalSubsts::identity_for_item(tcx, def_id)); - predicates.extend(associated_item_bounds(tcx, def_id, bounds, projection_ty, span)) + predicates.extend(associated_item_bounds(tcx, def_id, bounds, projection_ty, hir_id)) } } else if let Some((self_trait_ref, trait_items)) = is_trait { // Current behavior: bounds declared on associate type are predicates @@ -1991,7 +1997,8 @@ fn trait_associated_item_predicates( // For GATs the substs provided to the mk_projection call below are // wrong. We should emit a feature gate error if we get here so skip // this type. - tcx.sess.delay_span_bug(trait_item.span, "gats used without feature gate"); + tcx.sess + .delay_span_bug(tcx.hir().span(trait_item.hir_id), "gats used without feature gate"); return Vec::new(); } @@ -2000,7 +2007,7 @@ fn trait_associated_item_predicates( self_trait_ref.substs, ); - associated_item_bounds(tcx, def_id, bounds, assoc_ty, trait_item.span) + associated_item_bounds(tcx, def_id, bounds, assoc_ty, trait_item.hir_id) } fn associated_item_bounds( @@ -2008,14 +2015,14 @@ fn associated_item_bounds( def_id: DefId, bounds: &'tcx [hir::GenericBound<'tcx>], projection_ty: Ty<'tcx>, - span: Span, + hir_id: hir::HirId, ) -> Vec<(ty::Predicate<'tcx>, Span)> { let bounds = AstConv::compute_bounds( &ItemCtxt::new(tcx, def_id), projection_ty, bounds, SizedByDefault::Yes, - span, + tcx.hir().span(hir_id), ); let predicates = bounds.predicates(tcx, projection_ty); diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 81b295a0beb08..b5dfaba387ca0 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -48,7 +48,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { .unwrap_or_else(|| icx.to_ty(ty)), TraitItemKind::Type(_, Some(ref ty)) => icx.to_ty(ty), TraitItemKind::Type(_, None) => { - span_bug!(item.span, "associated type missing default"); + let item_span = tcx.hir().span(item.hir_id); + span_bug!(item_span, "associated type missing default"); } }, @@ -66,7 +67,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } ImplItemKind::TyAlias(ref ty) => { if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { - report_assoc_ty_on_inherent_impl(tcx, item.span); + let item_span = tcx.hir().span(item.hir_id); + report_assoc_ty_on_inherent_impl(tcx, item_span); } icx.to_ty(ty) @@ -148,8 +150,9 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { | ItemKind::GlobalAsm(..) | ItemKind::ExternCrate(..) | ItemKind::Use(..) => { + let item_span = tcx.hir().span(item.hir_id); span_bug!( - item.span, + item_span, "compute_type_of_item: unexpected item type: {:?}", item.kind ); diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index 37d383db68ab6..a3b3a00cdc8bb 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -85,7 +85,8 @@ impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> { enforce_impl_params_are_constrained(self.tcx, impl_def_id, items); enforce_impl_items_are_distinct(self.tcx, items); if self.min_specialization { - check_min_specialization(self.tcx, impl_def_id.to_def_id(), item.span); + let item_span = self.tcx.hir().span(item.hir_id); + check_min_specialization(self.tcx, impl_def_id.to_def_id(), item_span); } } } @@ -226,6 +227,7 @@ fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplI let mut seen_value_items = FxHashMap::default(); for impl_item_ref in impl_item_refs { let impl_item = tcx.hir().impl_item(impl_item_ref.id); + let impl_item_span = tcx.hir().span(impl_item.hir_id); let seen_items = match impl_item.kind { hir::ImplItemKind::TyAlias(_) => &mut seen_type_items, _ => &mut seen_value_items, @@ -234,7 +236,7 @@ fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplI Occupied(entry) => { let mut err = struct_span_err!( tcx.sess, - impl_item.span, + impl_item_span, E0201, "duplicate definitions with name `{}`:", impl_item.ident @@ -243,11 +245,11 @@ fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplI *entry.get(), format!("previous definition of `{}` here", impl_item.ident), ); - err.span_label(impl_item.span, "duplicate definition"); + err.span_label(impl_item_span, "duplicate definition"); err.emit(); } Vacant(entry) => { - entry.insert(impl_item.span); + entry.insert(impl_item_span); } } } diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 8d8a1b4d96761..6ec7eb33f62aa 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -183,7 +183,8 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { error = true; } if let hir::IsAsync::Async = sig.header.asyncness { - let span = tcx.sess.source_map().guess_head_span(it.span); + let span = tcx.hir().span(it.hir_id); + let span = tcx.sess.source_map().guess_head_span(span); struct_span_err!( tcx.sess, span, @@ -263,7 +264,8 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { error = true; } if let hir::IsAsync::Async = sig.header.asyncness { - let span = tcx.sess.source_map().guess_head_span(it.span); + let span = tcx.hir().span(it.hir_id); + let span = tcx.sess.source_map().guess_head_span(span); struct_span_err!( tcx.sess, span, diff --git a/src/librustc_typeck/outlives/test.rs b/src/librustc_typeck/outlives/test.rs index abe9319d71c59..86a6eb9b293db 100644 --- a/src/librustc_typeck/outlives/test.rs +++ b/src/librustc_typeck/outlives/test.rs @@ -20,7 +20,8 @@ impl ItemLikeVisitor<'tcx> for OutlivesTest<'tcx> { // attribute and report an error with various results if found. if self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_outlives) { let inferred_outlives_of = self.tcx.inferred_outlives_of(item_def_id); - struct_span_err!(self.tcx.sess, item.span, E0640, "{:?}", inferred_outlives_of).emit(); + let item_span = self.tcx.hir().span(item.hir_id); + struct_span_err!(self.tcx.sess, item_span, E0640, "{:?}", inferred_outlives_of).emit(); } } diff --git a/src/librustc_typeck/variance/test.rs b/src/librustc_typeck/variance/test.rs index 1aab89310c6e8..dbf0a79dcc830 100644 --- a/src/librustc_typeck/variance/test.rs +++ b/src/librustc_typeck/variance/test.rs @@ -20,7 +20,8 @@ impl ItemLikeVisitor<'tcx> for VarianceTest<'tcx> { // attribute and report an error with various results if found. if self.tcx.has_attr(item_def_id.to_def_id(), sym::rustc_variance) { let variances_of = self.tcx.variances_of(item_def_id); - struct_span_err!(self.tcx.sess, item.span, E0208, "{:?}", variances_of).emit(); + let item_span = self.tcx.hir().span(item.hir_id); + struct_span_err!(self.tcx.sess, item_span, E0208, "{:?}", variances_of).emit(); } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index cb13fc2d20b56..4f55506d3293c 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1114,7 +1114,7 @@ impl Clean for hir::TraitItem<'_> { Item { name: Some(self.ident.name.clean(cx)), attrs: self.attrs.clean(cx), - source: self.span.clean(cx), + source: cx.tcx.hir().span(self.hir_id).clean(cx), def_id: local_did.to_def_id(), visibility: Visibility::Inherited, stability: get_stability(cx, local_did.to_def_id()), @@ -1142,7 +1142,7 @@ impl Clean for hir::ImplItem<'_> { let local_did = cx.tcx.hir().local_def_id(self.hir_id); Item { name: Some(self.ident.name.clean(cx)), - source: self.span.clean(cx), + source: cx.tcx.hir().span(self.hir_id).clean(cx), attrs: self.attrs.clean(cx), def_id: local_did.to_def_id(), visibility: self.vis.clean(cx), diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 7469af7f96a44..81deaf9bb3618 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -137,7 +137,7 @@ pub fn run(options: Options) -> Result<(), String> { "".to_string(), &krate.item.attrs, CRATE_HIR_ID, - krate.item.span, + tcx.hir().span(CRATE_HIR_ID), |this| { intravisit::walk_crate(this, krate); }, @@ -980,25 +980,29 @@ impl<'a, 'hir, 'tcx> intravisit::Visitor<'hir> for HirCollector<'a, 'hir, 'tcx> item.ident.to_string() }; - self.visit_testable(name, &item.attrs, item.hir_id, item.span, |this| { + let item_span = self.map.span(item.hir_id); + self.visit_testable(name, &item.attrs, item.hir_id, item_span, |this| { intravisit::walk_item(this, item); }); } fn visit_trait_item(&mut self, item: &'hir hir::TraitItem) { - self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item.span, |this| { + let item_span = self.map.span(item.hir_id); + self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item_span, |this| { intravisit::walk_trait_item(this, item); }); } fn visit_impl_item(&mut self, item: &'hir hir::ImplItem) { - self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item.span, |this| { + let item_span = self.map.span(item.hir_id); + self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item_span, |this| { intravisit::walk_impl_item(this, item); }); } fn visit_foreign_item(&mut self, item: &'hir hir::ForeignItem) { - self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item.span, |this| { + let item_span = self.map.span(item.hir_id); + self.visit_testable(item.ident.to_string(), &item.attrs, item.hir_id, item_span, |this| { intravisit::walk_foreign_item(this, item); }); } diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 357de6ce9715e..fd8959c707db7 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -64,7 +64,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { pub fn visit(mut self, krate: &'tcx hir::Crate) -> Module<'tcx> { let mut module = self.visit_mod_contents( - krate.item.span, + self.cx.tcx.hir().span(hir::CRATE_HIR_ID), krate.item.attrs, &Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public }, hir::CRATE_HIR_ID, @@ -99,7 +99,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, generics, fields: sd.fields(), - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), } } @@ -120,7 +120,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, generics, fields: sd.fields(), - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), } } @@ -149,7 +149,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { generics, attrs: &it.attrs, id: it.hir_id, - whence: it.span, + whence: self.cx.tcx.hir().span(it.hir_id), } } @@ -208,7 +208,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { kind, helpers, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), }); } None => { @@ -218,7 +218,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, decl, name, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), generics, header, body, @@ -401,7 +401,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { path: orig_name.map(|x| x.to_string()), vis: &item.vis, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), }) } hir::ItemKind::Use(_, hir::UseKind::ListStem) => {} @@ -443,12 +443,12 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { attrs: &item.attrs, path, glob: is_glob, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), }); } hir::ItemKind::Mod(ref m) => { om.mods.push(self.visit_mod_contents( - item.span, + self.cx.tcx.hir().span(item.hir_id), &item.attrs, &item.vis, item.hir_id, @@ -475,7 +475,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: ident.name, id: item.hir_id, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.typedefs.push(t); @@ -486,7 +486,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { name: ident.name, id: item.hir_id, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.opaque_tys.push(t); @@ -499,7 +499,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { id: item.hir_id, name: ident.name, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.statics.push(s); @@ -514,7 +514,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { id: item.hir_id, name: ident.name, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.constants.push(s); @@ -531,7 +531,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { bounds, id: item.hir_id, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.traits.push(t); @@ -543,7 +543,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { bounds, id: item.hir_id, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.trait_aliases.push(t); @@ -576,7 +576,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { items, attrs: &item.attrs, id: item.hir_id, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), vis: &item.vis, }; om.impls.push(i); @@ -602,7 +602,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { kind: &item.kind, vis: &item.vis, attrs: &item.attrs, - whence: item.span, + whence: self.cx.tcx.hir().span(item.hir_id), }); } diff --git a/src/tools/clippy/clippy_lints/src/attrs.rs b/src/tools/clippy/clippy_lints/src/attrs.rs index 41f125d48398f..57f1bc7529c9b 100644 --- a/src/tools/clippy/clippy_lints/src/attrs.rs +++ b/src/tools/clippy/clippy_lints/src/attrs.rs @@ -280,7 +280,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { if is_relevant_item(cx, item) { - check_attrs(cx, item.span, item.ident.name, &item.attrs) + check_attrs(cx, cx.tcx.hir().span(item.hir_id), item.ident.name, &item.attrs) } match item.kind { ItemKind::ExternCrate(..) | ItemKind::Use(..) => { @@ -352,13 +352,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Attributes { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem<'_>) { if is_relevant_impl(cx, item) { - check_attrs(cx, item.span, item.ident.name, &item.attrs) + check_attrs(cx, cx.tcx.hir().span(item.hir_id), item.ident.name, &item.attrs) } } fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem<'_>) { if is_relevant_trait(cx, item) { - check_attrs(cx, item.span, item.ident.name, &item.attrs) + check_attrs(cx, cx.tcx.hir().span(item.hir_id), item.ident.name, &item.attrs) } } } diff --git a/src/tools/clippy/clippy_lints/src/copy_iterator.rs b/src/tools/clippy/clippy_lints/src/copy_iterator.rs index d79aa2ef0209e..a3ae85a452210 100644 --- a/src/tools/clippy/clippy_lints/src/copy_iterator.rs +++ b/src/tools/clippy/clippy_lints/src/copy_iterator.rs @@ -44,7 +44,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyIterator { span_lint_and_note( cx, COPY_ITERATOR, - item.span, + cx.tcx.hir().span(item.hir_id), "you are implementing `Iterator` on a `Copy` type", None, "consider implementing `IntoIterator` instead", diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index 719f261d95a6f..59da2c77308b7 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -115,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive { let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id)); let is_automatically_derived = is_automatically_derived(&*item.attrs); - check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived); + check_hash_peq(cx, cx.tcx.hir().span(item.hir_id), trait_ref, ty, is_automatically_derived); if is_automatically_derived { check_unsafe_derive_deserialize(cx, item, trait_ref, ty); @@ -210,12 +210,13 @@ fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait _ => (), } + let item_span = cx.tcx.hir().span(item.hir_id); span_lint_and_note( cx, EXPL_IMPL_CLONE_ON_COPY, - item.span, + item_span, "you are implementing `Clone` explicitly on a `Copy` type", - Some(item.span), + Some(item_span), "consider deriving `Clone` or removing `Copy`", ); } @@ -251,7 +252,7 @@ fn check_unsafe_derive_deserialize<'a, 'tcx>( span_lint_and_help( cx, UNSAFE_DERIVE_DESERIALIZE, - item.span, + cx.tcx.hir().span(item.hir_id), "you are deriving `serde::Deserialize` on a type that has methods using `unsafe`", None, "consider implementing `serde::Deserialize` manually. See https://serde.rs/impl-deserialize.html" diff --git a/src/tools/clippy/clippy_lints/src/doc.rs b/src/tools/clippy/clippy_lints/src/doc.rs index 8d1e91f9adbd6..e530e4d12b176 100644 --- a/src/tools/clippy/clippy_lints/src/doc.rs +++ b/src/tools/clippy/clippy_lints/src/doc.rs @@ -155,10 +155,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown { let headers = check_attrs(cx, &self.valid_idents, &item.attrs); match item.kind { hir::ItemKind::Fn(ref sig, _, body_id) => { + let item_span = cx.tcx.hir().span(item.hir_id); if !(is_entrypoint_fn(cx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id()) - || in_external_macro(cx.tcx.sess, item.span)) + || in_external_macro(cx.tcx.sess, item_span)) { - lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id)); + lint_for_missing_headers(cx, item.hir_id, item_span, sig, headers, Some(body_id)); } }, hir::ItemKind::Impl { @@ -180,19 +181,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DocMarkdown { fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) { let headers = check_attrs(cx, &self.valid_idents, &item.attrs); if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind { - if !in_external_macro(cx.tcx.sess, item.span) { - lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, None); + let item_span = cx.tcx.hir().span(item.hir_id); + if !in_external_macro(cx.tcx.sess, item_span) { + lint_for_missing_headers(cx, item.hir_id, item_span, sig, headers, None); } } } fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'_>) { let headers = check_attrs(cx, &self.valid_idents, &item.attrs); - if self.in_trait_impl || in_external_macro(cx.tcx.sess, item.span) { + let item_span = cx.tcx.hir().span(item.hir_id); + if self.in_trait_impl || in_external_macro(cx.tcx.sess, item_span) { return; } if let hir::ImplItemKind::Fn(ref sig, body_id) = item.kind { - lint_for_missing_headers(cx, item.hir_id, item.span, sig, headers, Some(body_id)); + lint_for_missing_headers(cx, item.hir_id, item_span, sig, headers, Some(body_id)); } } } diff --git a/src/tools/clippy/clippy_lints/src/empty_enum.rs b/src/tools/clippy/clippy_lints/src/empty_enum.rs index 3bfef6f4bed12..ea1969ed9f683 100644 --- a/src/tools/clippy/clippy_lints/src/empty_enum.rs +++ b/src/tools/clippy/clippy_lints/src/empty_enum.rs @@ -48,7 +48,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EmptyEnum { span_lint_and_help( cx, EMPTY_ENUM, - item.span, + cx.tcx.hir().span(item.hir_id), "enum with no variants", None, "consider using the uninhabited type `!` (never type) or a wrapper \ diff --git a/src/tools/clippy/clippy_lints/src/fallible_impl_from.rs b/src/tools/clippy/clippy_lints/src/fallible_impl_from.rs index 92812816461c5..8b0013a565bbe 100644 --- a/src/tools/clippy/clippy_lints/src/fallible_impl_from.rs +++ b/src/tools/clippy/clippy_lints/src/fallible_impl_from.rs @@ -61,7 +61,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FallibleImplFrom { if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_def_id); if match_def_path(cx, impl_trait_ref.def_id, &FROM_TRAIT); then { - lint_impl_body(cx, item.span, impl_items); + lint_impl_body(cx, cx.tcx.hir().span(item.hir_id), impl_items); } } } diff --git a/src/tools/clippy/clippy_lints/src/functions.rs b/src/tools/clippy/clippy_lints/src/functions.rs index d6fedab47b210..1d9d3838ddf51 100644 --- a/src/tools/clippy/clippy_lints/src/functions.rs +++ b/src/tools/clippy/clippy_lints/src/functions.rs @@ -234,9 +234,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item<'_>) { let attr = must_use_attr(&item.attrs); if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind { + let item_span = cx.tcx.hir().span(item.hir_id); if let Some(attr) = attr { - let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); - check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr); + let fn_header_span = item_span.with_hi(sig.decl.output.span().hi()); + check_needless_must_use(cx, &sig.decl, item.hir_id, item_span, fn_header_span, attr); return; } if cx.access_levels.is_exported(item.hir_id) @@ -247,9 +248,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { cx, &sig.decl, cx.tcx.hir().body(*body_id), - item.span, + item_span, item.hir_id, - item.span.with_hi(sig.decl.output.span().hi()), + item_span.with_hi(sig.decl.output.span().hi()), "this function could have a `#[must_use]` attribute", ); } @@ -259,9 +260,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::ImplItem<'_>) { if let hir::ImplItemKind::Fn(ref sig, ref body_id) = item.kind { let attr = must_use_attr(&item.attrs); + let item_span = cx.tcx.hir().span(item.hir_id); if let Some(attr) = attr { - let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); - check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr); + let fn_header_span = item_span.with_hi(sig.decl.output.span().hi()); + check_needless_must_use(cx, &sig.decl, item.hir_id, item_span, fn_header_span, attr); } else if cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) && trait_ref_of_method(cx, item.hir_id).is_none() @@ -270,9 +272,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { cx, &sig.decl, cx.tcx.hir().body(*body_id), - item.span, + item_span, item.hir_id, - item.span.with_hi(sig.decl.output.span().hi()), + item_span.with_hi(sig.decl.output.span().hi()), "this method could have a `#[must_use]` attribute", ); } @@ -281,15 +283,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) { if let hir::TraitItemKind::Fn(ref sig, ref eid) = item.kind { + let item_span = cx.tcx.hir().span(item.hir_id); + // don't lint extern functions decls, it's not their fault if sig.header.abi == Abi::Rust { - self.check_arg_number(cx, &sig.decl, item.span.with_hi(sig.decl.output.span().hi())); + self.check_arg_number(cx, &sig.decl, item_span.with_hi(sig.decl.output.span().hi())); } let attr = must_use_attr(&item.attrs); if let Some(attr) = attr { - let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); - check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr); + let fn_header_span = item_span.with_hi(sig.decl.output.span().hi()); + check_needless_must_use(cx, &sig.decl, item.hir_id, item_span, fn_header_span, attr); } if let hir::TraitFn::Provided(eid) = *eid { let body = cx.tcx.hir().body(eid); @@ -300,9 +304,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions { cx, &sig.decl, body, - item.span, + item_span, item.hir_id, - item.span.with_hi(sig.decl.output.span().hi()), + item_span.with_hi(sig.decl.output.span().hi()), "this method could have a `#[must_use]` attribute", ); } diff --git a/src/tools/clippy/clippy_lints/src/inherent_impl.rs b/src/tools/clippy/clippy_lints/src/inherent_impl.rs index 7e2975ac2ae90..ff087d47df5fd 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_impl.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_impl.rs @@ -48,7 +48,7 @@ pub struct MultipleInherentImpl { impl_lint_pass!(MultipleInherentImpl => [MULTIPLE_INHERENT_IMPL]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MultipleInherentImpl { - fn check_item(&mut self, _: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { + fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { if let ItemKind::Impl { ref generics, of_trait: None, @@ -58,8 +58,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MultipleInherentImpl { // Remember for each inherent implementation encoutered its span and generics // but filter out implementations that have generic params (type or lifetime) // or are derived from a macro - if !in_macro(item.span) && generics.params.is_empty() { - self.impls.insert(item.hir_id.owner.to_def_id(), item.span); + let item_span = cx.tcx.hir().span(item.hir_id); + if !in_macro(item_span) && generics.params.is_empty() { + self.impls.insert(item.hir_id.owner.to_def_id(), item_span); } } } diff --git a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs index 289628a2752af..15352edfc593b 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs @@ -94,7 +94,7 @@ declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_S impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem<'_>) { - if impl_item.span.from_expansion() { + if cx.tcx.hir().span(impl_item.hir_id).from_expansion() { return; } @@ -132,7 +132,7 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) { span_lint_and_help( cx, INHERENT_TO_STRING_SHADOW_DISPLAY, - item.span, + cx.tcx.hir().span(item.hir_id), &format!( "type `{}` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`", self_type.to_string() @@ -144,7 +144,7 @@ fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) { span_lint_and_help( cx, INHERENT_TO_STRING, - item.span, + cx.tcx.hir().span(item.hir_id), &format!( "implementation of inherent method `to_string(&self) -> String` for type `{}`", self_type.to_string() diff --git a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs index c9e12fc535ec0..4ae6d38399d2b 100644 --- a/src/tools/clippy/clippy_lints/src/large_const_arrays.rs +++ b/src/tools/clippy/clippy_lints/src/large_const_arrays.rs @@ -47,8 +47,9 @@ impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeConstArrays { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { + let item_span = cx.tcx.hir().span(item.hir_id); if_chain! { - if !item.span.from_expansion(); + if !item_span.from_expansion(); if let ItemKind::Const(hir_ty, _) = &item.kind; let ty = hir_ty_to_ty(cx.tcx, hir_ty); if let ty::Array(element_type, cst) = ty.kind; @@ -63,12 +64,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeConstArrays { let sugg_span = Span::new( hi_pos - BytePos::from_usize("const".len()), hi_pos, - item.span.ctxt(), + item_span.ctxt(), ); span_lint_and_then( cx, LARGE_CONST_ARRAYS, - item.span, + item_span, "large array defined as const", |diag| { diag.span_suggestion( diff --git a/src/tools/clippy/clippy_lints/src/len_zero.rs b/src/tools/clippy/clippy_lints/src/len_zero.rs index 13e85fda8ffeb..990c479874a02 100644 --- a/src/tools/clippy/clippy_lints/src/len_zero.rs +++ b/src/tools/clippy/clippy_lints/src/len_zero.rs @@ -72,7 +72,7 @@ declare_lint_pass!(LenZero => [LEN_ZERO, LEN_WITHOUT_IS_EMPTY]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LenZero { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { - if item.span.from_expansion() { + if cx.tcx.hir().span(item.hir_id).from_expansion() { return; } @@ -159,7 +159,7 @@ fn check_trait_items(cx: &LateContext<'_, '_>, visited_trait: &Item<'_>, trait_i span_lint( cx, LEN_WITHOUT_IS_EMPTY, - visited_trait.span, + cx.tcx.hir().span(visited_trait.hir_id), &format!( "trait `{}` has a `len` method but no (possibly inherited) `is_empty` method", visited_trait.ident.name @@ -200,7 +200,7 @@ fn check_impl_items(cx: &LateContext<'_, '_>, item: &Item<'_>, impl_items: &[Imp span_lint( cx, LEN_WITHOUT_IS_EMPTY, - item.span, + cx.tcx.hir().span(item.hir_id), &format!( "item `{}` has a public `len` method but {} `is_empty` method", ty, is_empty diff --git a/src/tools/clippy/clippy_lints/src/lifetimes.rs b/src/tools/clippy/clippy_lints/src/lifetimes.rs index f36dea2fc4703..8e1de19fefb8e 100644 --- a/src/tools/clippy/clippy_lints/src/lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/lifetimes.rs @@ -79,7 +79,7 @@ declare_lint_pass!(Lifetimes => [NEEDLESS_LIFETIMES, EXTRA_UNUSED_LIFETIMES]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { if let ItemKind::Fn(ref sig, ref generics, id) = item.kind { - check_fn_inner(cx, &sig.decl, Some(id), generics, item.span, true); + check_fn_inner(cx, &sig.decl, Some(id), generics, cx.tcx.hir().span(item.hir_id), true); } } @@ -91,7 +91,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes { &sig.decl, Some(id), &item.generics, - item.span, + cx.tcx.hir().span(item.hir_id), report_extra_lifetimes, ); } @@ -103,7 +103,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Lifetimes { TraitFn::Required(_) => None, TraitFn::Provided(id) => Some(id), }; - check_fn_inner(cx, &sig.decl, body, &item.generics, item.span, true); + check_fn_inner(cx, &sig.decl, body, &item.generics, cx.tcx.hir().span(item.hir_id), true); } } } diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index 071fbfebd2d06..a102da0b3bfe5 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -1472,7 +1472,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods { } fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem<'_>) { - if in_external_macro(cx.sess(), impl_item.span) { + if in_external_macro(cx.sess(), cx.tcx.hir().span(impl_item.hir_id)) { return; } let name = impl_item.ident.name.as_str(); @@ -1503,7 +1503,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods { out_type.matches(cx, &sig.decl.output) && self_kind.matches(cx, self_ty, first_arg_ty) && fn_header_equals(*fn_header, sig.header) { - span_lint(cx, SHOULD_IMPLEMENT_TRAIT, impl_item.span, &format!( + span_lint(cx, SHOULD_IMPLEMENT_TRAIT, cx.tcx.hir().span(impl_item.hir_id), &format!( "defining a method called `{}` on this type; consider implementing \ the `{}` trait or choosing a less ambiguous name", name, trait_name)); } @@ -1575,7 +1575,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods { span_lint( cx, NEW_RET_NO_SELF, - impl_item.span, + cx.tcx.hir().span(impl_item.hir_id), "methods called `new` usually return `Self`", ); } diff --git a/src/tools/clippy/clippy_lints/src/missing_doc.rs b/src/tools/clippy/clippy_lints/src/missing_doc.rs index 5b979af062fc8..3de3b60fc068c 100644 --- a/src/tools/clippy/clippy_lints/src/missing_doc.rs +++ b/src/tools/clippy/clippy_lints/src/missing_doc.rs @@ -125,7 +125,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { } fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate<'_>) { - self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "crate"); + self.check_missing_docs_attrs(cx, &krate.item.attrs, cx.tcx.hir().span(hir::CRATE_HIR_ID), "crate"); } fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item<'_>) { @@ -158,7 +158,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { | hir::ItemKind::Use(..) => return, }; - self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc); + self.check_missing_docs_attrs(cx, &it.attrs, cx.tcx.hir().span(it.hir_id), desc); } fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem<'_>) { @@ -168,7 +168,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { hir::TraitItemKind::Type(..) => "an associated type", }; - self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc); + self.check_missing_docs_attrs(cx, &trait_item.attrs, cx.tcx.hir().span(trait_item.hir_id), desc); } fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem<'_>) { @@ -188,7 +188,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc { hir::ImplItemKind::Fn(..) => "a method", hir::ImplItemKind::TyAlias(_) => "an associated type", }; - self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc); + self.check_missing_docs_attrs(cx, &impl_item.attrs, cx.tcx.hir().span(impl_item.hir_id), desc); } fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField<'_>) { diff --git a/src/tools/clippy/clippy_lints/src/missing_inline.rs b/src/tools/clippy/clippy_lints/src/missing_inline.rs index 1802470b1841e..32c885c2704fa 100644 --- a/src/tools/clippy/clippy_lints/src/missing_inline.rs +++ b/src/tools/clippy/clippy_lints/src/missing_inline.rs @@ -81,7 +81,7 @@ declare_lint_pass!(MissingInline => [MISSING_INLINE_IN_PUBLIC_ITEMS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item<'_>) { - if rustc_middle::lint::in_external_macro(cx.sess(), it.span) || is_executable(cx) { + if rustc_middle::lint::in_external_macro(cx.sess(), cx.tcx.hir().span(it.hir_id)) || is_executable(cx) { return; } @@ -91,7 +91,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline { match it.kind { hir::ItemKind::Fn(..) => { let desc = "a function"; - check_missing_inline_attrs(cx, &it.attrs, it.span, desc); + check_missing_inline_attrs(cx, &it.attrs, cx.tcx.hir().span(it.hir_id), desc); }, hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, trait_items) => { // note: we need to check if the trait is exported so we can't use @@ -106,7 +106,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline { // an impl is not provided let desc = "a default trait method"; let item = cx.tcx.hir().expect_trait_item(tit.id.hir_id); - check_missing_inline_attrs(cx, &item.attrs, item.span, desc); + check_missing_inline_attrs(cx, &item.attrs, cx.tcx.hir().span(item.hir_id), desc); } }, } @@ -131,7 +131,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem<'_>) { use rustc_middle::ty::{ImplContainer, TraitContainer}; - if rustc_middle::lint::in_external_macro(cx.sess(), impl_item.span) || is_executable(cx) { + if rustc_middle::lint::in_external_macro(cx.sess(), cx.tcx.hir().span(impl_item.hir_id)) || is_executable(cx) { return; } @@ -159,6 +159,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingInline { } } - check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc); + check_missing_inline_attrs(cx, &impl_item.attrs, cx.tcx.hir().span(impl_item.hir_id), desc); } } diff --git a/src/tools/clippy/clippy_lints/src/new_without_default.rs b/src/tools/clippy/clippy_lints/src/new_without_default.rs index dd236535c18ad..92360e61c07a8 100644 --- a/src/tools/clippy/clippy_lints/src/new_without_default.rs +++ b/src/tools/clippy/clippy_lints/src/new_without_default.rs @@ -66,7 +66,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { for assoc_item in items { if let hir::AssocItemKind::Fn { has_self: false } = assoc_item.kind { let impl_item = cx.tcx.hir().impl_item(assoc_item.id); - if in_external_macro(cx.sess(), impl_item.span) { + if in_external_macro(cx.sess(), cx.tcx.hir().span(impl_item.hir_id)) { return; } if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind { @@ -126,7 +126,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { cx, NEW_WITHOUT_DEFAULT, id, - impl_item.span, + cx.tcx.hir().span(impl_item.hir_id), &format!( "you should consider adding a `Default` implementation for `{}`", self_ty @@ -134,7 +134,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault { |diag| { diag.suggest_prepend_item( cx, - item.span, + cx.tcx.hir().span(item.hir_id), "try this", &create_new_without_default_suggest_msg(self_ty), Applicability::MaybeIncorrect, diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs index bb257e5a542d9..ff286d88e52e7 100644 --- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs +++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs @@ -145,7 +145,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx Item<'_>) { if let ItemKind::Const(hir_ty, ..) = &it.kind { let ty = hir_ty_to_ty(cx.tcx, hir_ty); - verify_ty_bound(cx, ty, Source::Item { item: it.span }); + verify_ty_bound(cx, ty, Source::Item { item: cx.tcx.hir().span(it.hir_id) }); } } @@ -157,7 +157,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst { ty, Source::Assoc { ty: hir_ty.span, - item: trait_item.span, + item: cx.tcx.hir().span(trait_item.hir_id), }, ); } @@ -175,7 +175,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst { ty, Source::Assoc { ty: hir_ty.span, - item: impl_item.span, + item: cx.tcx.hir().span(impl_item.hir_id), }, ); } diff --git a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs index 6fc07f91660ee..d77eea3aa6c95 100644 --- a/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs +++ b/src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs @@ -44,7 +44,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantPubCrate { if let VisibilityKind::Crate { .. } = item.vis.node { if !cx.access_levels.is_exported(item.hir_id) { if let Some(false) = self.is_exported.last() { - let span = item.span.with_hi(item.ident.span.hi()); + let span = cx.tcx.hir().span(item.hir_id).with_hi(item.ident.span.hi()); let def_id = cx.tcx.hir().local_def_id(item.hir_id); let descr = cx.tcx.def_kind(def_id).descr(def_id.to_def_id()); span_lint_and_then( diff --git a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs index e9fa3fcd674f6..d21743ad1564a 100644 --- a/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/src/tools/clippy/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -127,7 +127,7 @@ impl_lint_pass!(TriviallyCopyPassByRef => [TRIVIALLY_COPY_PASS_BY_REF]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem<'_>) { - if item.span.from_expansion() { + if cx.tcx.hir().span(item.hir_id).from_expansion() { return; } diff --git a/src/tools/clippy/clippy_lints/src/types.rs b/src/tools/clippy/clippy_lints/src/types.rs index 9afa340149fc1..433cb86d36dfc 100644 --- a/src/tools/clippy/clippy_lints/src/types.rs +++ b/src/tools/clippy/clippy_lints/src/types.rs @@ -2331,6 +2331,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher { return; } + let item_span = cx.tcx.hir().span(item.hir_id); + match item.kind { ItemKind::Impl { ref generics, @@ -2342,15 +2344,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher { vis.visit_ty(ty); for target in &vis.found { - if differing_macro_contexts(item.span, target.span()) { + if differing_macro_contexts(item_span, target.span()) { return; } let generics_suggestion_span = generics.span.substitute_dummy({ - let pos = snippet_opt(cx, item.span.until(target.span())) - .and_then(|snip| Some(item.span.lo() + BytePos(snip.find("impl")? as u32 + 4))); + let pos = snippet_opt(cx, item_span.until(target.span())) + .and_then(|snip| Some(item_span.lo() + BytePos(snip.find("impl")? as u32 + 4))); if let Some(pos) = pos { - Span::new(pos, pos, item.span.data().ctxt) + Span::new(pos, pos, item_span.data().ctxt) } else { return; } @@ -2387,13 +2389,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher { continue; } let generics_suggestion_span = generics.span.substitute_dummy({ - let pos = snippet_opt(cx, item.span.until(cx.tcx.hir().span(body.params[0].pat.hir_id))) + let pos = snippet_opt(cx, item_span.until(cx.tcx.hir().span(body.params[0].pat.hir_id))) .and_then(|snip| { let i = snip.find("fn")?; - Some(item.span.lo() + BytePos((i + (&snip[i..]).find('(')?) as u32)) + Some(item_span.lo() + BytePos((i + (&snip[i..]).find('(')?) as u32)) }) .expect("failed to create span for type parameters"); - Span::new(pos, pos, item.span.data().ctxt) + Span::new(pos, pos, item_span.data().ctxt) }); let mut ctr_vis = ImplicitHasherConstructorVisitor::new(cx, target); diff --git a/src/tools/clippy/clippy_lints/src/unused_self.rs b/src/tools/clippy/clippy_lints/src/unused_self.rs index 01475591a20c9..4d6d56db3d326 100644 --- a/src/tools/clippy/clippy_lints/src/unused_self.rs +++ b/src/tools/clippy/clippy_lints/src/unused_self.rs @@ -41,7 +41,7 @@ declare_lint_pass!(UnusedSelf => [UNUSED_SELF]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf { fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) { - if impl_item.span.from_expansion() { + if cx.tcx.hir().span(impl_item.hir_id).from_expansion() { return; } let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id); diff --git a/src/tools/clippy/clippy_lints/src/use_self.rs b/src/tools/clippy/clippy_lints/src/use_self.rs index f8e1aff33e773..a5d506a39abc6 100644 --- a/src/tools/clippy/clippy_lints/src/use_self.rs +++ b/src/tools/clippy/clippy_lints/src/use_self.rs @@ -159,7 +159,7 @@ fn check_trait_method_impl_decl<'a, 'tcx>( impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UseSelf { fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { - if in_external_macro(cx.sess(), item.span) { + if in_external_macro(cx.sess(), cx.tcx.hir().span(item.hir_id)) { return; } if_chain! { diff --git a/src/tools/clippy/clippy_lints/src/utils/internal_lints.rs b/src/tools/clippy/clippy_lints/src/utils/internal_lints.rs index 89e2bcdd7935d..7cab63e9ed66f 100644 --- a/src/tools/clippy/clippy_lints/src/utils/internal_lints.rs +++ b/src/tools/clippy/clippy_lints/src/utils/internal_lints.rs @@ -257,6 +257,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { return; } + let item_span = cx.tcx.hir().span(item.hir_id); if let hir::ItemKind::Static(ref ty, Mutability::Not, body_id) = item.kind { if is_lint_ref_type(cx, ty) { let expr = &cx.tcx.hir().body(body_id).value; @@ -277,15 +278,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { span_lint( cx, DEFAULT_LINT, - item.span, + item_span, &format!("the lint `{}` has the default lint description", item.ident.name), ); } } - self.declared_lints.insert(item.ident.name, item.span); + self.declared_lints.insert(item.ident.name, item_span); } - } else if is_expn_of(item.span, "impl_lint_pass").is_some() - || is_expn_of(item.span, "declare_lint_pass").is_some() + } else if is_expn_of(item_span, "impl_lint_pass").is_some() + || is_expn_of(item_span, "declare_lint_pass").is_some() { if let hir::ItemKind::Impl { of_trait: None, diff --git a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs index b637253bd0264..7340be49a2889 100644 --- a/src/tools/clippy/clippy_lints/src/wildcard_imports.rs +++ b/src/tools/clippy/clippy_lints/src/wildcard_imports.rs @@ -111,7 +111,7 @@ impl LateLintPass<'_, '_> for WildcardImports { } if_chain! { if let ItemKind::Use(use_path, UseKind::Glob) = &item.kind; - if self.warn_on_all || !self.check_exceptions(item, use_path.segments); + if self.warn_on_all || !self.check_exceptions(cx, item, use_path.segments); let used_imports = cx.tcx.names_imported_by_glob_use(item.hir_id.owner); if !used_imports.is_empty(); // Already handled by `unused_imports` then { @@ -131,9 +131,10 @@ impl LateLintPass<'_, '_> for WildcardImports { // formattings like `use _ :: *;`, we extend it up to, but not including the // `;`. In nested imports, like `use _::{inner::*, _}` there is no `;` and we // can just use the end of the item span - let mut span = use_path.span.with_hi(item.span.hi()); + let item_span = cx.tcx.hir().span(item.hir_id); + let mut span = use_path.span.with_hi(item_span.hi()); if snippet(cx, span, "").ends_with(';') { - span = use_path.span.with_hi(item.span.hi() - BytePos(1)); + span = use_path.span.with_hi(item_span.hi() - BytePos(1)); } ( span, false, @@ -188,8 +189,9 @@ impl LateLintPass<'_, '_> for WildcardImports { } impl WildcardImports { - fn check_exceptions(&self, item: &Item<'_>, segments: &[PathSegment<'_>]) -> bool { - in_macro(item.span) + fn check_exceptions(&self, cx: &LateContext<'_, '_>, item: &Item<'_>, segments: &[PathSegment<'_>]) -> bool { + let item_span = cx.tcx.hir().span(item.hir_id); + in_macro(item_span) || is_prelude_import(segments) || (is_super_only_import(segments) && self.test_modules_deep > 0) }