Skip to content

Commit

Permalink
feed def_span in resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Dec 24, 2023
1 parent 3166bbe commit 12376e0
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 61 deletions.
7 changes: 1 addition & 6 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
47 changes: 24 additions & 23 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) -> 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 {
Expand Down Expand Up @@ -847,27 +866,9 @@ impl<'hir> Map<'hir> {
}

pub fn opt_span(self, hir_id: HirId) -> Option<Span> {
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)? {
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_middle/src/util/lower_span.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
Expand Down
107 changes: 87 additions & 20 deletions compiler/rustc_resolve/src/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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,
);
}
Expand Down Expand Up @@ -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));
Expand All @@ -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));
}
Expand All @@ -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,
);
}
Expand All @@ -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
// ```
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -287,26 +345,35 @@ 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(
constant.id,
kw::Empty,
DefKind::InlineConst,
constant.value.span,
constant.value.span,
);
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
return;
Expand Down
Loading

0 comments on commit 12376e0

Please sign in to comment.