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 6, 2023
1 parent 1dd4db5 commit 9376503
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 60 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 @@ -778,12 +778,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
48 changes: 24 additions & 24 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 @@ -839,29 +858,10 @@ 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.find(hir_id)? {
// Function-like.
Node::Item(Item { kind: ItemKind::Fn(sig, ..), span: outer_span, .. })
Expand Down Expand Up @@ -926,10 +926,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_span::Span;
use rustc_hir::def_id::LocalDefId;

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 @@ -1422,7 +1422,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
Loading

0 comments on commit 9376503

Please sign in to comment.