Skip to content

Commit 12376e0

Browse files
committed
feed def_span in resolver
1 parent 3166bbe commit 12376e0

File tree

9 files changed

+142
-61
lines changed

9 files changed

+142
-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_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![],

compiler/rustc_resolve/src/def_collector.rs

+87-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast::*;
44
use rustc_expand::expand::AstFragment;
55
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
66
use rustc_hir::def_id::LocalDefId;
7+
use rustc_middle::hir::map::{named_span, until_within};
78
use rustc_span::hygiene::LocalExpnId;
89
use rustc_span::symbol::{kw, sym, Symbol};
910
use rustc_span::Span;
@@ -31,18 +32,20 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
3132
node_id: NodeId,
3233
name: Symbol,
3334
def_kind: DefKind,
35+
def_span: Span,
3436
span: Span,
3537
) -> LocalDefId {
3638
let parent_def = self.parent_def;
3739
debug!(
38-
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?})",
39-
node_id, def_kind, parent_def
40+
"create_def(node_id={:?}, def_kind={:?}, parent_def={:?}, def_span={:?})",
41+
node_id, def_kind, parent_def, def_span
4042
);
4143
self.resolver.create_def(
4244
parent_def,
4345
node_id,
4446
name,
4547
def_kind,
48+
def_span,
4649
self.expansion.to_expn_id(),
4750
span.with_parent(None),
4851
)
@@ -78,7 +81,7 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
7881
self.visit_macro_invoc(field.id);
7982
} else {
8083
let name = field.ident.map_or_else(|| sym::integer(index(self)), |ident| ident.name);
81-
let def = self.create_def(field.id, name, DefKind::Field, field.span);
84+
let def = self.create_def(field.id, name, DefKind::Field, field.span, field.span);
8285
self.with_parent(def, |this| visit::walk_field_def(this, field));
8386
}
8487
}
@@ -91,6 +94,33 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
9194
}
9295
}
9396

97+
fn def_span_for_item(i: &Item) -> Span {
98+
match &i.kind {
99+
ItemKind::ForeignMod(_) => i.span,
100+
ItemKind::GlobalAsm(_) => i.span,
101+
ItemKind::Fn(f) => f.sig.span.find_ancestor_in_same_ctxt(i.span).unwrap_or(i.span),
102+
ItemKind::Static(s) => until_within(i.span, s.ty.span),
103+
ItemKind::Const(c) => until_within(i.span, c.ty.span),
104+
ItemKind::Impl(im) => until_within(i.span, im.generics.where_clause.span),
105+
ItemKind::MacroDef(_) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
106+
ItemKind::Mod(_, _) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
107+
ItemKind::TyAlias(_) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
108+
ItemKind::TraitAlias(_, _) => {
109+
named_span(i.span, i.ident, i.kind.generics().map(|g| g.span))
110+
}
111+
ItemKind::ExternCrate(_) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
112+
ItemKind::Union(_, _) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
113+
ItemKind::Enum(..) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
114+
ItemKind::Struct(..) => named_span(i.span, i.ident, i.kind.generics().map(|g| g.span)),
115+
ItemKind::Trait(t) => {
116+
let end = if let Some(b) = t.bounds.last() { b.span() } else { t.generics.span };
117+
until_within(i.span, end)
118+
}
119+
ItemKind::Use(_) => unreachable!(),
120+
ItemKind::MacCall(_) => unreachable!(),
121+
}
122+
}
123+
94124
impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
95125
fn visit_item(&mut self, i: &'a Item) {
96126
debug!("visit_item: {:?}", i);
@@ -127,7 +157,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
127157
return visit::walk_item(self, i);
128158
}
129159
};
130-
let def_id = self.create_def(i.id, i.ident.name, def_kind, i.span);
160+
let def_id = self.create_def(i.id, i.ident.name, def_kind, def_span_for_item(i), i.span);
131161

132162
if let Some(macro_data) = opt_macro_data {
133163
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> {
143173
ctor_node_id,
144174
kw::Empty,
145175
DefKind::Ctor(CtorOf::Struct, ctor_kind),
176+
this.resolver.tcx.def_span(this.parent_def),
146177
i.span,
147178
);
148179
}
@@ -176,6 +207,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
176207
coroutine_kind.closure_id(),
177208
kw::Empty,
178209
DefKind::Closure,
210+
body.span.find_ancestor_in_same_ctxt(span).unwrap_or(span),
179211
span,
180212
);
181213
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> {
190222
}
191223

192224
fn visit_use_tree(&mut self, use_tree: &'a UseTree, id: NodeId, _nested: bool) {
193-
self.create_def(id, kw::Empty, DefKind::Use, use_tree.span);
225+
let def_span =
226+
use_tree.prefix.span.find_ancestor_in_same_ctxt(use_tree.span).unwrap_or(use_tree.span);
227+
self.create_def(id, kw::Empty, DefKind::Use, def_span, use_tree.span);
194228
visit::walk_use_tree(self, use_tree, id);
195229
}
196230

197231
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
198-
let def_kind = match fi.kind {
199-
ForeignItemKind::Static(_, mt, _) => DefKind::Static(mt),
200-
ForeignItemKind::Fn(_) => DefKind::Fn,
201-
ForeignItemKind::TyAlias(_) => DefKind::ForeignTy,
232+
let (def_kind, def_span) = match &fi.kind {
233+
ForeignItemKind::Static(ty, mt, _) => {
234+
(DefKind::Static(*mt), until_within(fi.span, ty.span))
235+
}
236+
ForeignItemKind::Fn(f) => {
237+
(DefKind::Fn, until_within(fi.span, f.sig.decl.output.span()))
238+
}
239+
ForeignItemKind::TyAlias(_) => {
240+
(DefKind::ForeignTy, named_span(fi.span, fi.ident, None))
241+
}
202242
ForeignItemKind::MacCall(_) => return self.visit_macro_invoc(fi.id),
203243
};
204244

205-
let def = self.create_def(fi.id, fi.ident.name, def_kind, fi.span);
245+
let def = self.create_def(fi.id, fi.ident.name, def_kind, def_span, fi.span);
206246

207247
self.with_parent(def, |this| visit::walk_foreign_item(this, fi));
208248
}
@@ -211,13 +251,20 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
211251
if v.is_placeholder {
212252
return self.visit_macro_invoc(v.id);
213253
}
214-
let def = self.create_def(v.id, v.ident.name, DefKind::Variant, v.span);
254+
let def = self.create_def(
255+
v.id,
256+
v.ident.name,
257+
DefKind::Variant,
258+
named_span(v.span, v.ident, None),
259+
v.span,
260+
);
215261
self.with_parent(def, |this| {
216262
if let Some((ctor_kind, ctor_node_id)) = CtorKind::from_ast(&v.data) {
217263
this.create_def(
218264
ctor_node_id,
219265
kw::Empty,
220266
DefKind::Ctor(CtorOf::Variant, ctor_kind),
267+
this.resolver.tcx.def_span(this.parent_def),
221268
v.span,
222269
);
223270
}
@@ -244,7 +291,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
244291
GenericParamKind::Type { .. } => DefKind::TyParam,
245292
GenericParamKind::Const { .. } => DefKind::ConstParam,
246293
};
247-
self.create_def(param.id, param.ident.name, def_kind, param.ident.span);
294+
self.create_def(param.id, param.ident.name, def_kind, param.span(), param.ident.span);
248295

249296
// impl-Trait can happen inside generic parameters, like
250297
// ```
@@ -258,14 +305,19 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
258305
}
259306

260307
fn visit_assoc_item(&mut self, i: &'a AssocItem, ctxt: visit::AssocCtxt) {
261-
let def_kind = match &i.kind {
262-
AssocItemKind::Fn(..) => DefKind::AssocFn,
263-
AssocItemKind::Const(..) => DefKind::AssocConst,
264-
AssocItemKind::Type(..) => DefKind::AssocTy,
308+
let (def_kind, def_span) = match &i.kind {
309+
AssocItemKind::Fn(f) => {
310+
(DefKind::AssocFn, f.sig.span.find_ancestor_in_same_ctxt(i.span).unwrap_or(i.span))
311+
}
312+
AssocItemKind::Const(c) => (DefKind::AssocConst, until_within(i.span, c.ty.span)),
313+
AssocItemKind::Type(ty) => (DefKind::AssocTy, {
314+
let end = if let Some(b) = ty.bounds.last() { b.span() } else { ty.generics.span };
315+
until_within(i.span, end)
316+
}),
265317
AssocItemKind::MacCall(..) => return self.visit_macro_invoc(i.id),
266318
};
267319

268-
let def = self.create_def(i.id, i.ident.name, def_kind, i.span);
320+
let def = self.create_def(i.id, i.ident.name, def_kind, def_span, i.span);
269321
self.with_parent(def, |this| visit::walk_assoc_item(this, i, ctxt));
270322
}
271323

@@ -277,7 +329,13 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
277329
}
278330

279331
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
280-
let def = self.create_def(constant.id, kw::Empty, DefKind::AnonConst, constant.value.span);
332+
let def = self.create_def(
333+
constant.id,
334+
kw::Empty,
335+
DefKind::AnonConst,
336+
constant.value.span,
337+
constant.value.span,
338+
);
281339
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
282340
}
283341

@@ -287,26 +345,35 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
287345
ExprKind::Closure(ref closure) => {
288346
// Async closures desugar to closures inside of closures, so
289347
// we must create two defs.
290-
let closure_def = self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span);
348+
let def_span =
349+
closure.fn_decl_span.find_ancestor_inside(expr.span).unwrap_or(expr.span);
350+
let closure_def =
351+
self.create_def(expr.id, kw::Empty, DefKind::Closure, def_span, expr.span);
291352
match closure.coroutine_kind {
292353
Some(coroutine_kind) => self.create_def(
293354
coroutine_kind.closure_id(),
294355
kw::Empty,
295356
DefKind::Closure,
357+
closure
358+
.body
359+
.span
360+
.find_ancestor_in_same_ctxt(expr.span)
361+
.unwrap_or(expr.span),
296362
expr.span,
297363
),
298364
None => closure_def,
299365
}
300366
}
301367
ExprKind::Gen(_, _, _) => {
302-
self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span)
368+
self.create_def(expr.id, kw::Empty, DefKind::Closure, expr.span, expr.span)
303369
}
304370
ExprKind::ConstBlock(ref constant) => {
305371
let def = self.create_def(
306372
constant.id,
307373
kw::Empty,
308374
DefKind::InlineConst,
309375
constant.value.span,
376+
constant.value.span,
310377
);
311378
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
312379
return;

0 commit comments

Comments
 (0)