Skip to content

Commit a835d74

Browse files
committed
save-analysis: add a decl_id for methods
This is non-null if the method is in a (non-inherent) impl and in that case will be the id for the method declaration in the implemented trait.
1 parent c28374e commit a835d74

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

src/librustc_save_analysis/data.rs

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ pub struct MethodData {
214214
pub span: Span,
215215
pub scope: NodeId,
216216
pub value: String,
217+
pub decl_id: Option<DefId>,
217218
}
218219

219220
/// Data for modules.

src/librustc_save_analysis/dump_visitor.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use rustc::hir::def::Def;
3131
use rustc::hir::def_id::DefId;
3232
use rustc::session::Session;
33-
use rustc::ty::{self, TyCtxt};
33+
use rustc::ty::{self, TyCtxt, ImplOrTraitItem, ImplOrTraitItemContainer};
3434

3535
use std::collections::HashSet;
3636
use std::hash::*;
@@ -381,24 +381,42 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
381381

382382
let sig_str = ::make_signature(&sig.decl, &sig.generics);
383383
if body.is_some() {
384-
if !self.span.filter_generated(Some(method_data.span), span) {
385-
let mut data = method_data.clone();
386-
data.value = sig_str;
387-
self.dumper.function(data.lower(self.tcx));
388-
}
389384
self.process_formals(&sig.decl.inputs, &method_data.qualname);
390-
} else {
391-
if !self.span.filter_generated(Some(method_data.span), span) {
392-
self.dumper.method(MethodData {
393-
id: method_data.id,
394-
name: method_data.name,
395-
span: method_data.span,
396-
scope: method_data.scope,
397-
qualname: method_data.qualname.clone(),
398-
value: sig_str,
399-
}.lower(self.tcx));
400-
}
401385
}
386+
387+
// If the method is defined in an impl, then try and find the corresponding
388+
// method decl in a trait, and if there is one, make a decl_id for it. This
389+
// requires looking up the impl, then the trait, then searching for a method
390+
// with the right name.
391+
if !self.span.filter_generated(Some(method_data.span), span) {
392+
let container =
393+
self.tcx.impl_or_trait_item(self.tcx.map.local_def_id(id)).container();
394+
let decl_id = if let ImplOrTraitItemContainer::ImplContainer(id) = container {
395+
self.tcx.trait_id_of_impl(id).and_then(|id| {
396+
for item in &**self.tcx.trait_items(id) {
397+
if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
398+
if m.name == name {
399+
return Some(m.def_id);
400+
}
401+
}
402+
}
403+
None
404+
})
405+
} else {
406+
None
407+
};
408+
409+
self.dumper.method(MethodData {
410+
id: method_data.id,
411+
name: method_data.name,
412+
span: method_data.span,
413+
scope: method_data.scope,
414+
qualname: method_data.qualname.clone(),
415+
value: sig_str,
416+
decl_id: decl_id,
417+
}.lower(self.tcx));
418+
}
419+
402420
self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
403421
}
404422

src/librustc_save_analysis/external_data.rs

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ pub struct MethodData {
321321
pub span: SpanData,
322322
pub scope: DefId,
323323
pub value: String,
324+
pub decl_id: Option<DefId>,
324325
}
325326

326327
impl Lower for data::MethodData {
@@ -334,6 +335,7 @@ impl Lower for data::MethodData {
334335
id: make_def_id(self.id, &tcx.map),
335336
qualname: self.qualname,
336337
value: self.value,
338+
decl_id: self.decl_id,
337339
}
338340
}
339341
}

src/librustc_save_analysis/json_dumper.rs

+12
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct Def {
182182
qualname: String,
183183
value: String,
184184
children: Vec<Id>,
185+
decl_id: Option<Id>,
185186
}
186187

187188
#[derive(Debug, RustcEncodable)]
@@ -221,6 +222,7 @@ impl From<EnumData> for Def {
221222
qualname: data.qualname,
222223
value: data.value,
223224
children: data.variants.into_iter().map(|id| From::from(id)).collect(),
225+
decl_id: None,
224226
}
225227
}
226228
}
@@ -235,6 +237,7 @@ impl From<TupleVariantData> for Def {
235237
qualname: data.qualname,
236238
value: data.value,
237239
children: vec![],
240+
decl_id: None,
238241
}
239242
}
240243
}
@@ -248,6 +251,7 @@ impl From<StructVariantData> for Def {
248251
qualname: data.qualname,
249252
value: data.value,
250253
children: vec![],
254+
decl_id: None,
251255
}
252256
}
253257
}
@@ -261,6 +265,7 @@ impl From<StructData> for Def {
261265
qualname: data.qualname,
262266
value: data.value,
263267
children: data.fields.into_iter().map(|id| From::from(id)).collect(),
268+
decl_id: None,
264269
}
265270
}
266271
}
@@ -274,6 +279,7 @@ impl From<TraitData> for Def {
274279
qualname: data.qualname,
275280
value: data.value,
276281
children: data.items.into_iter().map(|id| From::from(id)).collect(),
282+
decl_id: None,
277283
}
278284
}
279285
}
@@ -287,6 +293,7 @@ impl From<FunctionData> for Def {
287293
qualname: data.qualname,
288294
value: data.value,
289295
children: vec![],
296+
decl_id: None,
290297
}
291298
}
292299
}
@@ -300,6 +307,7 @@ impl From<MethodData> for Def {
300307
qualname: data.qualname,
301308
value: data.value,
302309
children: vec![],
310+
decl_id: data.decl_id.map(|id| From::from(id)),
303311
}
304312
}
305313
}
@@ -313,6 +321,7 @@ impl From<MacroData> for Def {
313321
qualname: data.qualname,
314322
value: String::new(),
315323
children: vec![],
324+
decl_id: None,
316325
}
317326
}
318327
}
@@ -326,6 +335,7 @@ impl From<ModData> for Def {
326335
qualname: data.qualname,
327336
value: data.filename,
328337
children: data.items.into_iter().map(|id| From::from(id)).collect(),
338+
decl_id: None,
329339
}
330340
}
331341
}
@@ -339,6 +349,7 @@ impl From<TypeDefData> for Def {
339349
qualname: data.qualname,
340350
value: data.value,
341351
children: vec![],
352+
decl_id: None,
342353
}
343354
}
344355
}
@@ -357,6 +368,7 @@ impl From<VariableData> for Def {
357368
qualname: data.qualname,
358369
value: data.value,
359370
children: vec![],
371+
decl_id: None,
360372
}
361373
}
362374
}

0 commit comments

Comments
 (0)