Skip to content

Commit 986bb53

Browse files
authored
Rollup merge of #34298 - nrc:save-parent, r=eddyb
save-analysis: some tweaks
2 parents 18f2871 + a835d74 commit 986bb53

File tree

5 files changed

+127
-35
lines changed

5 files changed

+127
-35
lines changed

src/librustc_save_analysis/data.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub struct EnumData {
102102
pub qualname: String,
103103
pub span: Span,
104104
pub scope: NodeId,
105+
pub variants: Vec<NodeId>,
106+
105107
}
106108

107109
/// Data for extern crates.
@@ -212,6 +214,7 @@ pub struct MethodData {
212214
pub span: Span,
213215
pub scope: NodeId,
214216
pub value: String,
217+
pub decl_id: Option<DefId>,
215218
}
216219

217220
/// Data for modules.
@@ -223,6 +226,7 @@ pub struct ModData {
223226
pub span: Span,
224227
pub scope: NodeId,
225228
pub filename: String,
229+
pub items: Vec<NodeId>,
226230
}
227231

228232
/// Data for a reference to a module.
@@ -242,7 +246,8 @@ pub struct StructData {
242246
pub ctor_id: NodeId,
243247
pub qualname: String,
244248
pub scope: NodeId,
245-
pub value: String
249+
pub value: String,
250+
pub fields: Vec<NodeId>,
246251
}
247252

248253
#[derive(Debug, RustcEncodable)]
@@ -263,7 +268,8 @@ pub struct TraitData {
263268
pub name: String,
264269
pub qualname: String,
265270
pub scope: NodeId,
266-
pub value: String
271+
pub value: String,
272+
pub items: Vec<NodeId>,
267273
}
268274

269275
#[derive(Debug, RustcEncodable)]
@@ -317,6 +323,7 @@ pub struct UseGlobData {
317323
#[derive(Debug, RustcEncodable)]
318324
pub struct VariableData {
319325
pub id: NodeId,
326+
pub kind: VariableKind,
320327
pub name: String,
321328
pub qualname: String,
322329
pub span: Span,
@@ -325,6 +332,14 @@ pub struct VariableData {
325332
pub type_value: String,
326333
}
327334

335+
#[derive(Debug, RustcEncodable)]
336+
pub enum VariableKind {
337+
Static,
338+
Const,
339+
Local,
340+
Field,
341+
}
342+
328343
/// Data for the use of some item (e.g., the use of a local variable, which
329344
/// will refer to that variables declaration (by ref_id)).
330345
#[derive(Debug, RustcEncodable)]

src/librustc_save_analysis/dump_visitor.rs

+48-23
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::*;
@@ -356,6 +356,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
356356
if !self.span.filter_generated(sub_span, p.span) {
357357
self.dumper.variable(VariableData {
358358
id: id,
359+
kind: VariableKind::Local,
359360
span: sub_span.expect("No span found for variable"),
360361
name: path_to_string(p),
361362
qualname: format!("{}::{}", qualname, path_to_string(p)),
@@ -380,24 +381,42 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
380381

381382
let sig_str = ::make_signature(&sig.decl, &sig.generics);
382383
if body.is_some() {
383-
if !self.span.filter_generated(Some(method_data.span), span) {
384-
let mut data = method_data.clone();
385-
data.value = sig_str;
386-
self.dumper.function(data.lower(self.tcx));
387-
}
388384
self.process_formals(&sig.decl.inputs, &method_data.qualname);
389-
} else {
390-
if !self.span.filter_generated(Some(method_data.span), span) {
391-
self.dumper.method(MethodData {
392-
id: method_data.id,
393-
name: method_data.name,
394-
span: method_data.span,
395-
scope: method_data.scope,
396-
qualname: method_data.qualname.clone(),
397-
value: sig_str,
398-
}.lower(self.tcx));
399-
}
400385
}
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+
401420
self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
402421
}
403422

@@ -519,6 +538,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
519538
if !self.span.filter_generated(sub_span, span) {
520539
self.dumper.variable(VariableData {
521540
span: sub_span.expect("No span found for variable"),
541+
kind: VariableKind::Const,
522542
id: id,
523543
name: name.to_string(),
524544
qualname: qualname,
@@ -542,17 +562,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
542562
let qualname = format!("::{}", self.tcx.node_path_str(item.id));
543563

544564
let sub_span = self.span.sub_span_after_keyword(item.span, keywords::Struct);
545-
let val = if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) =
546-
item.node {
565+
let (val, fields) =
566+
if let ast::ItemKind::Struct(ast::VariantData::Struct(ref fields, _), _) = item.node
567+
{
547568
let fields_str = fields.iter()
548569
.enumerate()
549570
.map(|(i, f)| f.ident.map(|i| i.to_string())
550571
.unwrap_or(i.to_string()))
551572
.collect::<Vec<_>>()
552573
.join(", ");
553-
format!("{} {{ {} }}", name, fields_str)
574+
(format!("{} {{ {} }}", name, fields_str), fields.iter().map(|f| f.id).collect())
554575
} else {
555-
String::new()
576+
(String::new(), vec![])
556577
};
557578

558579
if !self.span.filter_generated(sub_span, item.span) {
@@ -563,7 +584,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
563584
ctor_id: def.id(),
564585
qualname: qualname.clone(),
565586
scope: self.cur_scope,
566-
value: val
587+
value: val,
588+
fields: fields,
567589
}.lower(self.tcx));
568590
}
569591

@@ -718,7 +740,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
718740
name: name,
719741
qualname: qualname.clone(),
720742
scope: self.cur_scope,
721-
value: val
743+
value: val,
744+
items: methods.iter().map(|i| i.id).collect(),
722745
}.lower(self.tcx));
723746
}
724747

@@ -958,6 +981,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
958981
if !self.span.filter_generated(sub_span, p.span) {
959982
self.dumper.variable(VariableData {
960983
span: sub_span.expect("No span found for variable"),
984+
kind: VariableKind::Local,
961985
id: id,
962986
name: path_to_string(p),
963987
qualname: format!("{}${}", path_to_string(p), id),
@@ -1366,6 +1390,7 @@ impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx,
13661390
if !self.span.filter_generated(Some(p.span), p.span) {
13671391
self.dumper.variable(VariableData {
13681392
span: p.span,
1393+
kind: VariableKind::Local,
13691394
id: id,
13701395
name: path_to_string(p),
13711396
qualname: format!("{}${}", path_to_string(p), id),

src/librustc_save_analysis/external_data.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc::ty::TyCtxt;
1414
use syntax::ast::{CrateNum, NodeId};
1515
use syntax::codemap::{Span, CodeMap};
1616

17-
use super::data;
17+
use data;
1818

1919
// FIXME: this should be pub(crate), but the current snapshot doesn't allow it yet
2020
pub trait Lower {
@@ -90,6 +90,7 @@ pub struct EnumData {
9090
pub qualname: String,
9191
pub span: SpanData,
9292
pub scope: DefId,
93+
pub variants: Vec<DefId>
9394
}
9495

9596
impl Lower for data::EnumData {
@@ -103,6 +104,7 @@ impl Lower for data::EnumData {
103104
qualname: self.qualname,
104105
span: SpanData::from_span(self.span, tcx.sess.codemap()),
105106
scope: make_def_id(self.scope, &tcx.map),
107+
variants: self.variants.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
106108
}
107109
}
108110
}
@@ -319,6 +321,7 @@ pub struct MethodData {
319321
pub span: SpanData,
320322
pub scope: DefId,
321323
pub value: String,
324+
pub decl_id: Option<DefId>,
322325
}
323326

324327
impl Lower for data::MethodData {
@@ -332,6 +335,7 @@ impl Lower for data::MethodData {
332335
id: make_def_id(self.id, &tcx.map),
333336
qualname: self.qualname,
334337
value: self.value,
338+
decl_id: self.decl_id,
335339
}
336340
}
337341
}
@@ -345,6 +349,7 @@ pub struct ModData {
345349
pub span: SpanData,
346350
pub scope: DefId,
347351
pub filename: String,
352+
pub items: Vec<DefId>,
348353
}
349354

350355
impl Lower for data::ModData {
@@ -358,6 +363,7 @@ impl Lower for data::ModData {
358363
span: SpanData::from_span(self.span, tcx.sess.codemap()),
359364
scope: make_def_id(self.scope, &tcx.map),
360365
filename: self.filename,
366+
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
361367
}
362368
}
363369
}
@@ -392,7 +398,8 @@ pub struct StructData {
392398
pub ctor_id: DefId,
393399
pub qualname: String,
394400
pub scope: DefId,
395-
pub value: String
401+
pub value: String,
402+
pub fields: Vec<DefId>,
396403
}
397404

398405
impl Lower for data::StructData {
@@ -406,7 +413,8 @@ impl Lower for data::StructData {
406413
ctor_id: make_def_id(self.ctor_id, &tcx.map),
407414
qualname: self.qualname,
408415
scope: make_def_id(self.scope, &tcx.map),
409-
value: self.value
416+
value: self.value,
417+
fields: self.fields.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
410418
}
411419
}
412420
}
@@ -445,7 +453,8 @@ pub struct TraitData {
445453
pub id: DefId,
446454
pub qualname: String,
447455
pub scope: DefId,
448-
pub value: String
456+
pub value: String,
457+
pub items: Vec<DefId>,
449458
}
450459

451460
impl Lower for data::TraitData {
@@ -459,6 +468,7 @@ impl Lower for data::TraitData {
459468
qualname: self.qualname,
460469
scope: make_def_id(self.scope, &tcx.map),
461470
value: self.value,
471+
items: self.items.into_iter().map(|id| make_def_id(id, &tcx.map)).collect(),
462472
}
463473
}
464474
}
@@ -585,6 +595,7 @@ impl Lower for data::UseGlobData {
585595
pub struct VariableData {
586596
pub id: DefId,
587597
pub name: String,
598+
pub kind: data::VariableKind,
588599
pub qualname: String,
589600
pub span: SpanData,
590601
pub scope: DefId,
@@ -598,6 +609,7 @@ impl Lower for data::VariableData {
598609
fn lower(self, tcx: TyCtxt) -> VariableData {
599610
VariableData {
600611
id: make_def_id(self.id, &tcx.map),
612+
kind: self.kind,
601613
name: self.name,
602614
qualname: self.qualname,
603615
span: SpanData::from_span(self.span, tcx.sess.codemap()),

0 commit comments

Comments
 (0)