Skip to content

Commit 7a8e550

Browse files
committed
feed def_span in resolver
1 parent 1dd4db5 commit 7a8e550

File tree

9 files changed

+147
-60
lines changed

9 files changed

+147
-60
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
778778
/// Intercept all spans entering HIR.
779779
/// Mark a span as relative to the current owning item.
780780
fn lower_span(&self, span: Span) -> Span {
781-
if self.tcx.sess.opts.incremental.is_some() {
782-
span.with_parent(Some(self.current_hir_id_owner.def_id))
783-
} else {
784-
// Do not make spans relative when not using incremental compilation.
785-
span
786-
}
781+
rustc_middle::util::lower_span(self.tcx, span, self.current_hir_id_owner.def_id)
787782
}
788783

789784
fn lower_ident(&self, ident: Ident) -> Ident {

compiler/rustc_interface/src/queries.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@ impl<'tcx> Queries<'tcx> {
170170
&pre_configured_attrs,
171171
crate_name,
172172
)));
173+
let span = krate.spans.inner_span;
173174
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
174175
feed.output_filenames(Arc::new(outputs));
175176

176-
let feed = tcx.feed_local_def_id(CRATE_DEF_ID);
177+
let def_id = CRATE_DEF_ID;
178+
let feed = tcx.feed_local_def_id(def_id);
177179
feed.def_kind(DefKind::Mod);
180+
feed.def_span(rustc_middle::util::lower_span(tcx, span, def_id));
178181
});
179182
Ok(qcx)
180183
})

compiler/rustc_middle/src/hir/map/mod.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
1919
use rustc_span::{ErrorGuaranteed, Span};
2020
use rustc_target::spec::abi::Abi;
2121

22+
pub fn until_within(outer: Span, end: Span) -> Span {
23+
if let Some(end) = end.find_ancestor_inside(outer) { outer.with_hi(end.hi()) } else { outer }
24+
}
25+
26+
pub fn named_span(item_span: Span, ident: Ident, generics_span: Option<Span>) -> Span {
27+
if ident.name != kw::Empty {
28+
let mut span = until_within(item_span, ident.span);
29+
if let Some(g) = generics_span
30+
&& !g.is_dummy()
31+
&& let Some(g_span) = g.find_ancestor_inside(item_span)
32+
{
33+
span = span.to(g_span);
34+
}
35+
span
36+
} else {
37+
item_span
38+
}
39+
}
40+
2241
#[inline]
2342
pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> {
2443
match node {
@@ -839,29 +858,10 @@ impl<'hir> Map<'hir> {
839858
}
840859

841860
pub fn opt_span(self, hir_id: HirId) -> Option<Span> {
842-
fn until_within(outer: Span, end: Span) -> Span {
843-
if let Some(end) = end.find_ancestor_inside(outer) {
844-
outer.with_hi(end.hi())
845-
} else {
846-
outer
847-
}
848-
}
849-
850-
fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
851-
if ident.name != kw::Empty {
852-
let mut span = until_within(item_span, ident.span);
853-
if let Some(g) = generics
854-
&& !g.span.is_dummy()
855-
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
856-
{
857-
span = span.to(g_span);
858-
}
859-
span
860-
} else {
861-
item_span
862-
}
861+
if let Some(owner) = hir_id.as_owner() {
862+
let span = self.tcx.def_span(owner.def_id);
863+
return Some(span);
863864
}
864-
865865
let span = match self.find(hir_id)? {
866866
// Function-like.
867867
Node::Item(Item { kind: ItemKind::Fn(sig, ..), span: outer_span, .. })
@@ -926,10 +926,10 @@ impl<'hir> Map<'hir> {
926926
// SyntaxContext of the path.
927927
path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
928928
}
929-
_ => named_span(item.span, item.ident, item.kind.generics()),
929+
_ => named_span(item.span, item.ident, item.kind.generics().map(|g| g.span)),
930930
},
931931
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
932-
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
932+
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics.span)),
933933
Node::ForeignItem(item) => match item.kind {
934934
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
935935
_ => named_span(item.span, item.ident, None),

compiler/rustc_middle/src/hir/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir::def::DefKind;
1414
use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId};
1515
use rustc_hir::*;
1616
use rustc_query_system::ich::StableHashingContext;
17-
use rustc_span::{ErrorGuaranteed, ExpnId, DUMMY_SP};
17+
use rustc_span::{ErrorGuaranteed, ExpnId};
1818

1919
/// Top-level HIR node for current owner. This only contains the node for which
2020
/// `HirId::local_id == 0`, and excludes bodies.
@@ -175,10 +175,6 @@ pub fn provide(providers: &mut Providers) {
175175
providers.hir_attrs = |tcx, id| {
176176
tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs)
177177
};
178-
providers.def_span = |tcx, def_id| {
179-
let hir_id = tcx.local_def_id_to_hir_id(def_id);
180-
tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP)
181-
};
182178
providers.def_ident_span = |tcx, def_id| {
183179
let hir_id = tcx.local_def_id_to_hir_id(def_id);
184180
tcx.hir().opt_ident_span(hir_id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rustc_hir::def_id::LocalDefId;
2+
use rustc_span::Span;
3+
4+
use crate::ty::TyCtxt;
5+
6+
pub fn lower_span(tcx: TyCtxt<'_>, span: Span, parent: LocalDefId) -> Span {
7+
if tcx.sess.opts.incremental.is_some() {
8+
span.with_parent(Some(parent))
9+
} else {
10+
// Do not make spans relative when not using incremental compilation.
11+
span
12+
}
13+
}

compiler/rustc_middle/src/util/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ pub mod bug;
22
pub mod call_kind;
33
pub mod common;
44
pub mod find_self_call;
5+
mod lower_span;
56

67
pub use call_kind::{call_kind, CallDesugaringKind, CallKind};
78
pub use find_self_call::find_self_call;
9+
pub use lower_span::lower_span;
810

911
#[derive(Default, Copy, Clone)]
1012
pub struct Providers {

compiler/rustc_monomorphize/src/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ fn collect_used_items<'tcx>(
14221422
// and abort compilation if any of them errors.
14231423
MirUsedCollector {
14241424
tcx,
1425-
body: body,
1425+
body,
14261426
output,
14271427
instance,
14281428
move_size_spans: vec![],

0 commit comments

Comments
 (0)