Skip to content

Commit ba5b911

Browse files
committed
Auto merge of #41593 - achernyak:def_span, r=eddyb
query for def_span Resolves `fn def_span(&self, sess: &Session, def: DefId) -> Span;` of #41417. I had to change the query name to `def_sess_span` since `ty::TyCtxt` already has a method `def_span` implemented. This also will probably have merge conflicts with #41534 but I will resolves those once it's merged and wanted to start a code review on this one now. r? @eddyb
2 parents afa1240 + d3b7af0 commit ba5b911

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

src/librustc/dep_graph/dep_node.rs

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub enum DepNode<D: Clone + Debug> {
148148
// For proj. cache, we just keep a list of all def-ids, since it is
149149
// not a hotspot.
150150
ProjectionCache { def_ids: Vec<D> },
151+
152+
DescribeDef(D),
153+
DefSpan(D),
151154
}
152155

153156
impl<D: Clone + Debug> DepNode<D> {
@@ -253,6 +256,8 @@ impl<D: Clone + Debug> DepNode<D> {
253256
let def_ids: Option<Vec<E>> = def_ids.iter().map(op).collect();
254257
def_ids.map(|d| ProjectionCache { def_ids: d })
255258
}
259+
DescribeDef(ref d) => op(d).map(DescribeDef),
260+
DefSpan(ref d) => op(d).map(DefSpan),
256261
}
257262
}
258263
}

src/librustc/middle/cstore.rs

-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ pub trait CrateStore {
181181
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>;
182182

183183
// item info
184-
fn def_span(&self, sess: &Session, def: DefId) -> Span;
185184
fn stability(&self, def: DefId) -> Option<attr::Stability>;
186185
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
187186
fn visibility(&self, def: DefId) -> ty::Visibility;
@@ -312,7 +311,6 @@ impl CrateStore for DummyCrateStore {
312311
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any>
313312
{ bug!("crate_data_as_rc_any") }
314313
// item info
315-
fn def_span(&self, sess: &Session, def: DefId) -> Span { bug!("def_span") }
316314
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
317315
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
318316
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }

src/librustc/ty/maps.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ impl<'tcx> QueryDescription for queries::describe_def<'tcx> {
285285
}
286286
}
287287

288+
impl<'tcx> QueryDescription for queries::def_span<'tcx> {
289+
fn describe(_: TyCtxt, _: DefId) -> String {
290+
bug!("def_span")
291+
}
292+
}
293+
288294
macro_rules! define_maps {
289295
(<$tcx:tt>
290296
$($(#[$attr:meta])*
@@ -359,8 +365,10 @@ macro_rules! define_maps {
359365
}
360366

361367
// FIXME(eddyb) Get more valid Span's on queries.
362-
if span == DUMMY_SP {
363-
span = key.default_span(tcx);
368+
// def_span guard is necesary to prevent a recursive loop,
369+
// default_span calls def_span query internally.
370+
if span == DUMMY_SP && stringify!($name) != "def_span" {
371+
span = key.default_span(tcx)
364372
}
365373

366374
let _task = tcx.dep_graph.in_task(Self::to_dep_node(&key));
@@ -568,7 +576,8 @@ define_maps! { <'tcx>
568576
[] def_symbol_name: SymbolName(DefId) -> ty::SymbolName,
569577
[] symbol_name: symbol_name_dep_node(ty::Instance<'tcx>) -> ty::SymbolName,
570578

571-
[] describe_def: meta_data_node(DefId) -> Option<Def>
579+
[] describe_def: DescribeDef(DefId) -> Option<Def>,
580+
[] def_span: DefSpan(DefId) -> Span
572581
}
573582

574583
fn coherent_trait_dep_node((_, def_id): (CrateNum, DefId)) -> DepNode<DefId> {
@@ -599,8 +608,4 @@ fn typeck_item_bodies_dep_node(_: CrateNum) -> DepNode<DefId> {
599608

600609
fn const_eval_dep_node((def_id, _): (DefId, &Substs)) -> DepNode<DefId> {
601610
DepNode::ConstEval(def_id)
602-
}
603-
604-
fn meta_data_node(def_id: DefId) -> DepNode<DefId> {
605-
DepNode::MetaData(def_id)
606-
}
611+
}

src/librustc/ty/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -2285,14 +2285,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
22852285
}
22862286
}
22872287

2288-
pub fn def_span(self, def_id: DefId) -> Span {
2289-
if let Some(id) = self.hir.as_local_node_id(def_id) {
2290-
self.hir.span(id)
2291-
} else {
2292-
self.sess.cstore.def_span(&self.sess, def_id)
2293-
}
2294-
}
2295-
22962288
pub fn vis_is_accessible_from(self, vis: Visibility, block: NodeId) -> bool {
22972289
vis.is_accessible_from(self.hir.local_def_id(self.hir.get_module_parent(block)), self)
22982290
}
@@ -2694,12 +2686,17 @@ fn associated_item_def_ids<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26942686
Rc::new(vec)
26952687
}
26962688

2689+
fn def_span<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Span {
2690+
tcx.hir.span_if_local(def_id).unwrap()
2691+
}
2692+
26972693
pub fn provide(providers: &mut ty::maps::Providers) {
26982694
*providers = ty::maps::Providers {
26992695
associated_item,
27002696
associated_item_def_ids,
27012697
adt_sized_constraint,
27022698
adt_dtorck_constraint,
2699+
def_span,
27032700
..*providers
27042701
};
27052702
}

src/librustc_metadata/cstore_impl.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,14 @@ provide! { <'tcx> tcx, def_id, cdata
114114
inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
115115
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
116116
describe_def => { cdata.get_def(def_id.index) }
117+
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
117118
}
118119

119120
impl CrateStore for cstore::CStore {
120121
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Rc<Any> {
121122
self.get_crate_data(krate)
122123
}
123124

124-
fn def_span(&self, sess: &Session, def: DefId) -> Span {
125-
self.dep_graph.read(DepNode::MetaData(def));
126-
self.get_crate_data(def.krate).get_span(def.index, sess)
127-
}
128-
129125
fn stability(&self, def: DefId) -> Option<attr::Stability> {
130126
self.dep_graph.read(DepNode::MetaData(def));
131127
self.get_crate_data(def.krate).get_stability(def.index)

0 commit comments

Comments
 (0)