Skip to content

Commit 77bf251

Browse files
committed
feed def_span in resolver
1 parent f2348fb commit 77bf251

File tree

9 files changed

+140
-137
lines changed

9 files changed

+140
-137
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
804804
/// Intercept all spans entering HIR.
805805
/// Mark a span as relative to the current owning item.
806806
fn lower_span(&self, span: Span) -> Span {
807-
if self.tcx.sess.opts.incremental.is_some() {
808-
span.with_parent(Some(self.current_hir_id_owner.def_id))
809-
} else {
810-
// Do not make spans relative when not using incremental compilation.
811-
span
812-
}
807+
rustc_middle::util::lower_span(self.tcx, span, self.current_hir_id_owner.def_id)
813808
}
814809

815810
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

+22-99
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 {
@@ -847,110 +866,14 @@ impl<'hir> Map<'hir> {
847866
}
848867

849868
pub fn opt_span(self, hir_id: HirId) -> Option<Span> {
850-
fn until_within(outer: Span, end: Span) -> Span {
851-
if let Some(end) = end.find_ancestor_inside(outer) {
852-
outer.with_hi(end.hi())
853-
} else {
854-
outer
855-
}
856-
}
857-
858-
fn named_span(item_span: Span, ident: Ident, generics: Option<&Generics<'_>>) -> Span {
859-
if ident.name != kw::Empty {
860-
let mut span = until_within(item_span, ident.span);
861-
if let Some(g) = generics
862-
&& !g.span.is_dummy()
863-
&& let Some(g_span) = g.span.find_ancestor_inside(item_span)
864-
{
865-
span = span.to(g_span);
866-
}
867-
span
868-
} else {
869-
item_span
870-
}
869+
if let Some(owner) = hir_id.as_owner() {
870+
let span = self.tcx.def_span(owner.def_id);
871+
return Some(span);
871872
}
872873

873874
let span = match self.tcx.opt_hir_node(hir_id)? {
874-
// Function-like.
875-
Node::Item(Item { kind: ItemKind::Fn(sig, ..), span: outer_span, .. })
876-
| Node::TraitItem(TraitItem {
877-
kind: TraitItemKind::Fn(sig, ..),
878-
span: outer_span,
879-
..
880-
})
881-
| Node::ImplItem(ImplItem {
882-
kind: ImplItemKind::Fn(sig, ..), span: outer_span, ..
883-
}) => {
884-
// Ensure that the returned span has the item's SyntaxContext, and not the
885-
// SyntaxContext of the visibility.
886-
sig.span.find_ancestor_in_same_ctxt(*outer_span).unwrap_or(*outer_span)
887-
}
888-
// Impls, including their where clauses.
889-
Node::Item(Item {
890-
kind: ItemKind::Impl(Impl { generics, .. }),
891-
span: outer_span,
892-
..
893-
}) => until_within(*outer_span, generics.where_clause_span),
894-
// Constants and Statics.
895-
Node::Item(Item {
896-
kind: ItemKind::Const(ty, ..) | ItemKind::Static(ty, ..),
897-
span: outer_span,
898-
..
899-
})
900-
| Node::TraitItem(TraitItem {
901-
kind: TraitItemKind::Const(ty, ..),
902-
span: outer_span,
903-
..
904-
})
905-
| Node::ImplItem(ImplItem {
906-
kind: ImplItemKind::Const(ty, ..),
907-
span: outer_span,
908-
..
909-
})
910-
| Node::ForeignItem(ForeignItem {
911-
kind: ForeignItemKind::Static(ty, ..),
912-
span: outer_span,
913-
..
914-
}) => until_within(*outer_span, ty.span),
915-
// With generics and bounds.
916-
Node::Item(Item {
917-
kind: ItemKind::Trait(_, _, generics, bounds, _),
918-
span: outer_span,
919-
..
920-
})
921-
| Node::TraitItem(TraitItem {
922-
kind: TraitItemKind::Type(bounds, _),
923-
generics,
924-
span: outer_span,
925-
..
926-
}) => {
927-
let end = if let Some(b) = bounds.last() { b.span() } else { generics.span };
928-
until_within(*outer_span, end)
929-
}
930-
// Other cases.
931-
Node::Item(item) => match &item.kind {
932-
ItemKind::Use(path, _) => {
933-
// Ensure that the returned span has the item's SyntaxContext, and not the
934-
// SyntaxContext of the path.
935-
path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
936-
}
937-
_ => named_span(item.span, item.ident, item.kind.generics()),
938-
},
939875
Node::Variant(variant) => named_span(variant.span, variant.ident, None),
940-
Node::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics)),
941-
Node::ForeignItem(item) => match item.kind {
942-
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
943-
_ => named_span(item.span, item.ident, None),
944-
},
945876
Node::Ctor(_) => return self.opt_span(self.parent_id(hir_id)),
946-
Node::Expr(Expr {
947-
kind: ExprKind::Closure(Closure { fn_decl_span, .. }),
948-
span,
949-
..
950-
}) => {
951-
// Ensure that the returned span has the item's SyntaxContext.
952-
fn_decl_span.find_ancestor_inside(*span).unwrap_or(*span)
953-
}
954877
_ => self.span_with_body(hir_id),
955878
};
956879
debug_assert_eq!(span.ctxt(), self.span_with_body(hir_id).ctxt());

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
@@ -1441,7 +1441,7 @@ fn collect_used_items<'tcx>(
14411441
// and abort compilation if any of them errors.
14421442
MirUsedCollector {
14431443
tcx,
1444-
body: body,
1444+
body,
14451445
output,
14461446
instance,
14471447
move_size_spans: vec![],

0 commit comments

Comments
 (0)