Skip to content

Commit 6043a9f

Browse files
committed
feed def_span in resolver
1 parent 3166bbe commit 6043a9f

File tree

10 files changed

+145
-61
lines changed

10 files changed

+145
-61
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_borrowck/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub fn provide(providers: &mut Providers) {
110110
}
111111

112112
fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
113+
let now = std::time::Instant::now();
113114
let (input_body, promoted) = tcx.mir_promoted(def);
114115
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
115116

@@ -134,6 +135,8 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
134135
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
135136
debug!("mir_borrowck done");
136137

138+
tracing::info!("mir_borrowck for {}: {}us", tcx.def_path_str(def), now.elapsed().as_micros());
139+
137140
tcx.arena.alloc(opt_closure_req)
138141
}
139142

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-23
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,27 +866,9 @@ 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)? {
@@ -934,10 +935,10 @@ impl<'hir> Map<'hir> {
934935
// SyntaxContext of the path.
935936
path.span.find_ancestor_in_same_ctxt(item.span).unwrap_or(item.span)
936937
}
937-
_ => named_span(item.span, item.ident, item.kind.generics()),
938+
_ => named_span(item.span, item.ident, item.kind.generics().map(|g| g.span)),
938939
},
939940
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::ImplItem(item) => named_span(item.span, item.ident, Some(item.generics.span)),
941942
Node::ForeignItem(item) => match item.kind {
942943
ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()),
943944
_ => 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
@@ -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)