diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index e35d7d62cad48..7d7a87f08bd51 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -804,12 +804,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// Intercept all spans entering HIR. /// Mark a span as relative to the current owning item. fn lower_span(&self, span: Span) -> Span { - if self.tcx.sess.opts.incremental.is_some() { - span.with_parent(Some(self.current_hir_id_owner.def_id)) - } else { - // Do not make spans relative when not using incremental compilation. - span - } + rustc_middle::util::lower_span(self.tcx, span, self.current_hir_id_owner.def_id) } fn lower_ident(&self, ident: Ident) -> Ident { diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 8a553b95e8e95..9466e5f0428b3 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -170,11 +170,14 @@ 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); + let def_id = CRATE_DEF_ID; + let feed = tcx.feed_local_def_id(def_id); feed.def_kind(DefKind::Mod); + feed.def_span(rustc_middle::util::lower_span(tcx, span, def_id)); }); Ok(qcx) }) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 81f34c7b8b0a9..890e1f3f81b27 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 { @@ -847,27 +866,9 @@ 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 - } + if let Some(owner) = hir_id.as_owner() { + let span = self.tcx.def_span(owner.def_id); + return Some(span); } let span = match self.tcx.opt_hir_node(hir_id)? { @@ -934,10 +935,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 af99c7d55c37b..8cad423ccb852 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_middle/src/util/lower_span.rs b/compiler/rustc_middle/src/util/lower_span.rs new file mode 100644 index 0000000000000..32b17b428d6e6 --- /dev/null +++ b/compiler/rustc_middle/src/util/lower_span.rs @@ -0,0 +1,13 @@ +use rustc_hir::def_id::LocalDefId; +use rustc_span::Span; + +use crate::ty::TyCtxt; + +pub fn lower_span(tcx: TyCtxt<'_>, span: Span, parent: LocalDefId) -> Span { + if tcx.sess.opts.incremental.is_some() { + span.with_parent(Some(parent)) + } else { + // Do not make spans relative when not using incremental compilation. + span + } +} diff --git a/compiler/rustc_middle/src/util/mod.rs b/compiler/rustc_middle/src/util/mod.rs index 8c95988477d9c..ac922cc577e6e 100644 --- a/compiler/rustc_middle/src/util/mod.rs +++ b/compiler/rustc_middle/src/util/mod.rs @@ -2,9 +2,11 @@ pub mod bug; pub mod call_kind; pub mod common; pub mod find_self_call; +mod lower_span; pub use call_kind::{call_kind, CallDesugaringKind, CallKind}; pub use find_self_call::find_self_call; +pub use lower_span::lower_span; #[derive(Default, Copy, Clone)] pub struct Providers { diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index a68bfcd06d505..ffb91fa9c114a 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1441,7 +1441,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 02553d5007155..5872e399afb89 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::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::ExternCrate(_) => 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, ); } @@ -176,6 +207,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> { coroutine_kind.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)); @@ -190,19 +222,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)); } @@ -211,13 +251,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, ); } @@ -244,7 +291,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 // ``` @@ -258,14 +305,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)); } @@ -277,7 +329,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)); } @@ -287,19 +345,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.coroutine_kind { Some(coroutine_kind) => self.create_def( coroutine_kind.closure_id(), kw::Empty, DefKind::Closure, + closure + .body + .span + .find_ancestor_in_same_ctxt(expr.span) + .unwrap_or(expr.span), expr.span, ), None => 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( @@ -307,6 +373,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 a14f3d494fb4d..2477e908148c5 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1212,6 +1212,7 @@ impl<'tcx> Resolver<'_, 'tcx> { node_id: ast::NodeId, name: Symbol, def_kind: DefKind, + def_span: Span, expn_id: ExpnId, span: Span, ) -> LocalDefId { @@ -1227,16 +1228,19 @@ impl<'tcx> Resolver<'_, 'tcx> { // FIXME: remove `def_span` body, pass in the right spans here and call `tcx.at().create_def()` let def_id = self.tcx.create_def(parent, name, def_kind); - // Create the definition. - if expn_id != ExpnId::root() { - self.expn_that_defined.insert(def_id, expn_id); - } - // A relative span's parent must be an absolute span. debug_assert_eq!(span.data_untracked().parent, None); let _id = self.tcx.untracked().source_span.push(span); debug_assert_eq!(_id, def_id); + let feed = self.tcx.feed_local_def_id(def_id); + feed.def_span(rustc_middle::util::lower_span(self.tcx, def_span, def_id)); + + // Create the definition. + if expn_id != ExpnId::root() { + self.expn_that_defined.insert(def_id, expn_id); + } + // Some things for which we allocate `LocalDefId`s don't correspond to // anything in the AST, so they don't have a `NodeId`. For these cases // we don't need a mapping from `NodeId` to `LocalDefId`.