diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index e9611c74a6856..0ce11f5f83193 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -170,11 +170,13 @@ impl<'tcx> Queries<'tcx> { &pre_configured_attrs, crate_name, ))); + let span = krate.spans.inner_span; feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs)))); feed.output_filenames(Arc::new(outputs)); let feed = tcx.feed_local_def_id(CRATE_DEF_ID); feed.def_kind(DefKind::Mod); + feed.def_span(span); }); Ok(qcx) }) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index f407d30ac82a3..4eee8829340af 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -19,6 +19,25 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ErrorGuaranteed, Span}; use rustc_target::spec::abi::Abi; +pub fn until_within(outer: Span, end: Span) -> Span { + if let Some(end) = end.find_ancestor_inside(outer) { outer.with_hi(end.hi()) } else { outer } +} + +pub fn named_span(item_span: Span, ident: Ident, generics_span: Option) -> Span { + if ident.name != kw::Empty { + let mut span = until_within(item_span, ident.span); + if let Some(g) = generics_span + && !g.is_dummy() + && let Some(g_span) = g.find_ancestor_inside(item_span) + { + span = span.to(g_span); + } + span + } else { + item_span + } +} + #[inline] pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> { match node { @@ -839,29 +858,6 @@ impl<'hir> Map<'hir> { } pub fn opt_span(self, hir_id: HirId) -> Option { - fn until_within(outer: Span, end: Span) -> Span { - if let Some(end) = end.find_ancestor_inside(outer) { - outer.with_hi(end.hi()) - } else { - outer - } - } - - fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span { - if ident.name != kw::Empty { - let mut span = until_within(item_span, ident.span); - if let Some(g) = generics - && !g.span.is_dummy() - && let Some(g_span) = g.span.find_ancestor_inside(item_span) - { - span = span.to(g_span); - } - span - } else { - item_span - } - } - let span = match self.find(hir_id)? { // Function-like. Node::Item(Item { kind: ItemKind::Fn(sig, ..), span: outer_span, .. }) @@ -926,10 +922,10 @@ impl<'hir> Map<'hir> { // SyntaxContext of the path. path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span) } - _ => named_span(item.span, item.ident, item.kind.generics()), + _ => named_span(item.span, item.ident, item.kind.generics().map(|g| g.span)), }, Node::Variant(variant) => named_span(variant.span, variant.ident, None), - Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)), + Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics.span)), Node::ForeignItem(item) => match item.kind { ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()), _ => named_span(item.span, item.ident, None), diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index a16317a0c670b..c13dddd127957 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -14,7 +14,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; use rustc_hir::*; use rustc_query_system::ich::StableHashingContext; -use rustc_span::{ErrorGuaranteed, ExpnId, DUMMY_SP}; +use rustc_span::{ErrorGuaranteed, ExpnId}; /// Top-level HIR node for current owner. This only contains the node for which /// `HirId::local_id == 0`, and excludes bodies. @@ -175,10 +175,6 @@ pub fn provide(providers: &mut Providers) { providers.hir_attrs = |tcx, id| { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; - providers.def_span = |tcx, def_id| { - let hir_id = tcx.local_def_id_to_hir_id(def_id); - tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP) - }; providers.def_ident_span = |tcx, def_id| { let hir_id = tcx.local_def_id_to_hir_id(def_id); tcx.hir().opt_ident_span(hir_id) diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index b882a038711c3..9096327b62842 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1422,7 +1422,7 @@ fn collect_used_items<'tcx>( // and abort compilation if any of them errors. MirUsedCollector { tcx, - body: body, + body, output, instance, move_size_spans: vec![], diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index 647c92785e10d..0f2fa6aefb8f3 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -4,6 +4,7 @@ use rustc_ast::*; use rustc_expand::expand::AstFragment; use rustc_hir::def::{CtorKind, CtorOf, DefKind}; use rustc_hir::def_id::LocalDefId; +use rustc_middle::hir::map::{named_span, until_within}; use rustc_span::hygiene::LocalExpnId; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; @@ -31,18 +32,20 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { node_id: NodeId, name: Symbol, def_kind: DefKind, + def_span: Span, span: Span, ) -> LocalDefId { let parent_def = self.parent_def; debug!( - "create_def(node_id={:?}, def_kind={:?}, parent_def={:?})", - node_id, def_kind, parent_def + "create_def(node_id={:?}, def_kind={:?}, parent_def={:?}, def_span={:?})", + node_id, def_kind, parent_def, def_span ); self.resolver.create_def( parent_def, node_id, name, def_kind, + def_span, self.expansion.to_expn_id(), span.with_parent(None), ) @@ -78,7 +81,7 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { self.visit_macro_invoc(field.id); } else { let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name); - let def = self.create_def(field.id, name, DefKind::Field, field.span); + let def = self.create_def(field.id, name, DefKind::Field, field.span, field.span); self.with_parent(def, |this| visit::walk_field_def(this, field)); } } @@ -91,6 +94,33 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> { } } +fn def_span_for_item(i: &Item) -> Span { + match &i.kind { + ItemKind::ExternCrate(_) => i.span, + ItemKind::ForeignMod(_) => i.span, + ItemKind::GlobalAsm(_) => i.span, + ItemKind::Fn(f) => f.sig.span.find_ancestor_in_same_ctxt(i.span).unwrap_or(i.span), + ItemKind::Static(s) => until_within(i.span, s.ty.span), + ItemKind::Const(c) => until_within(i.span, c.ty.span), + ItemKind::Impl(im) => until_within(i.span, im.generics.where_clause.span), + ItemKind::MacroDef(_) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::Mod(_, _) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::TyAlias(_) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::TraitAlias(_, _) => { + named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)) + } + ItemKind::Union(_, _) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::Enum(..) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::Struct(..) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)), + ItemKind::Trait(t) => { + let end = if let Some(b) = t.bounds.last() { b.span() } else { t.generics.span }; + until_within(i.span, end) + } + ItemKind::Use(_) => unreachable!(), + ItemKind::MacCall(_) => unreachable!(), + } +} + impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { fn visit_item(&mut self, i: &'a Item) { debug!("visit_item: {:?}", i); @@ -127,7 +157,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { return visit::walk_item(self, i); } }; - let def_id = self.create_def(i.id, i.ident.name, def_kind, i.span); + let def_id = self.create_def(i.id, i.ident.name, def_kind, def_span_for_item(i), i.span); if let Some(macro_data) = opt_macro_data { self.resolver.macro_map.insert(def_id.to_def_id(), macro_data); @@ -143,6 +173,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { ctor_node_id, kw::Empty, DefKind::Ctor(CtorOf::Struct, ctor_kind), + this.resolver.tcx.def_span(this.parent_def), i.span, ); } @@ -171,8 +202,13 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { // then the closure_def will never be used, and we should avoid generating a // def-id for it. if let Some(body) = body { - let closure_def = - self.create_def(closure_id, kw::Empty, DefKind::Closure, span); + let closure_def = self.create_def( + closure_id, + kw::Empty, + DefKind::Closure, + body.span.find_ancestor_in_same_ctxt(span).unwrap_or(span), + span, + ); self.with_parent(closure_def, |this| this.visit_block(body)); } return; @@ -183,19 +219,27 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) { - self.create_def(id, kw::Empty, DefKind::Use, use_tree.span); + let def_span = + use_tree.prefix.span.find_ancestor_in_same_ctxt(use_tree.span).unwrap_or(use_tree.span); + self.create_def(id, kw::Empty, DefKind::Use, def_span, use_tree.span); visit::walk_use_tree(self, use_tree, id); } fn visit_foreign_item(&mut self, fi: &'a ForeignItem) { - let def_kind = match fi.kind { - ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt), - ForeignItemKind::Fn(_) => DefKind::Fn, - ForeignItemKind::TyAlias(_) => DefKind::ForeignTy, + let (def_kind, def_span) = match &fi.kind { + ForeignItemKind::Static(ty, mt, _) => { + (DefKind::Static(*mt), until_within(fi.span, ty.span)) + } + ForeignItemKind::Fn(f) => { + (DefKind::Fn, until_within(fi.span, f.sig.decl.output.span())) + } + ForeignItemKind::TyAlias(_) => { + (DefKind::ForeignTy, named_span(fi.span, fi.ident, None)) + } ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(fi.id), }; - let def = self.create_def(fi.id, fi.ident.name, def_kind, fi.span); + let def = self.create_def(fi.id, fi.ident.name, def_kind, def_span, fi.span); self.with_parent(def, |this| visit::walk_foreign_item(this, fi)); } @@ -204,13 +248,20 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { if v.is_placeholder { return self.visit_macro_invoc(v.id); } - let def = self.create_def(v.id, v.ident.name, DefKind::Variant, v.span); + let def = self.create_def( + v.id, + v.ident.name, + DefKind::Variant, + named_span(v.span, v.ident, None), + v.span, + ); self.with_parent(def, |this| { if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) { this.create_def( ctor_node_id, kw::Empty, DefKind::Ctor(CtorOf::Variant, ctor_kind), + this.resolver.tcx.def_span(this.parent_def), v.span, ); } @@ -237,7 +288,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { GenericParamKind::Type { .. } => DefKind::TyParam, GenericParamKind::Const { .. } => DefKind::ConstParam, }; - self.create_def(param.id, param.ident.name, def_kind, param.ident.span); + self.create_def(param.id, param.ident.name, def_kind, param.span(), param.ident.span); // impl-Trait can happen inside generic parameters, like // ``` @@ -251,14 +302,19 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) { - let def_kind = match &i.kind { - AssocItemKind::Fn(..) => DefKind::AssocFn, - AssocItemKind::Const(..) => DefKind::AssocConst, - AssocItemKind::Type(..) => DefKind::AssocTy, + let (def_kind, def_span) = match &i.kind { + AssocItemKind::Fn(f) => { + (DefKind::AssocFn, f.sig.span.find_ancestor_in_same_ctxt(i.span).unwrap_or(i.span)) + } + AssocItemKind::Const(c) => (DefKind::AssocConst, until_within(i.span, c.ty.span)), + AssocItemKind::Type(ty) => (DefKind::AssocTy, { + let end = if let Some(b) = ty.bounds.last() { b.span() } else { ty.generics.span }; + until_within(i.span, end) + }), AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id), }; - let def = self.create_def(i.id, i.ident.name, def_kind, i.span); + let def = self.create_def(i.id, i.ident.name, def_kind, def_span, i.span); self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt)); } @@ -270,7 +326,13 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { } fn visit_anon_const(&mut self, constant: &'a AnonConst) { - let def = self.create_def(constant.id, kw::Empty, DefKind::AnonConst, constant.value.span); + let def = self.create_def( + constant.id, + kw::Empty, + DefKind::AnonConst, + constant.value.span, + constant.value.span, + ); self.with_parent(def, |this| visit::walk_anon_const(this, constant)); } @@ -280,16 +342,27 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { ExprKind::Closure(ref closure) => { // Async closures desugar to closures inside of closures, so // we must create two defs. - let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span); + let def_span = + closure.fn_decl_span.find_ancestor_inside(expr.span).unwrap_or(expr.span); + let closure_def = + self.create_def(expr.id, kw::Empty, DefKind::Closure, def_span, expr.span); match closure.asyncness { - Async::Yes { closure_id, .. } => { - self.create_def(closure_id, kw::Empty, DefKind::Closure, expr.span) - } + Async::Yes { closure_id, .. } => self.create_def( + closure_id, + kw::Empty, + DefKind::Closure, + closure + .body + .span + .find_ancestor_in_same_ctxt(expr.span) + .unwrap_or(expr.span), + expr.span, + ), Async::No => closure_def, } } ExprKind::Gen(_, _, _) => { - self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span) + self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span, expr.span) } ExprKind::ConstBlock(ref constant) => { let def = self.create_def( @@ -297,6 +370,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { kw::Empty, DefKind::InlineConst, constant.value.span, + constant.value.span, ); self.with_parent(def, |this| visit::walk_anon_const(this, constant)); return; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 70e0eb12c01da..b7c2323fb911d 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1213,6 +1213,7 @@ impl<'tcx> Resolver<'_, 'tcx> { node_id: ast::NodeId, name: Symbol, def_kind: DefKind, + def_span: Span, expn_id: ExpnId, span: Span, ) -> LocalDefId { @@ -1230,6 +1231,7 @@ impl<'tcx> Resolver<'_, 'tcx> { let feed = self.tcx.feed_local_def_id(def_id); feed.def_kind(def_kind); + feed.def_span(def_span); // Create the definition. if expn_id != ExpnId::root() {