Skip to content

Commit 55bf6a4

Browse files
authored
Auto merge of rust-lang#36487 - nrc:save-doc-urls, r=@eddyb
save-analysis: better 'parent' info In particular, this fixes some bugs displaying doc URLs for method calls.
2 parents 3392775 + 48e69e0 commit 55bf6a4

File tree

4 files changed

+69
-43
lines changed

4 files changed

+69
-43
lines changed

src/librustc_save_analysis/data.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub struct FunctionData {
167167
pub scope: NodeId,
168168
pub value: String,
169169
pub visibility: Visibility,
170-
pub parent: Option<NodeId>,
170+
pub parent: Option<DefId>,
171171
pub docs: String,
172172
}
173173

@@ -250,6 +250,7 @@ pub struct MethodData {
250250
pub scope: NodeId,
251251
pub value: String,
252252
pub decl_id: Option<DefId>,
253+
pub parent: Option<DefId>,
253254
pub visibility: Visibility,
254255
pub docs: String,
255256
}
@@ -300,7 +301,7 @@ pub struct StructVariantData {
300301
pub type_value: String,
301302
pub value: String,
302303
pub scope: NodeId,
303-
pub parent: Option<NodeId>,
304+
pub parent: Option<DefId>,
304305
pub docs: String,
305306
}
306307

@@ -326,7 +327,7 @@ pub struct TupleVariantData {
326327
pub type_value: String,
327328
pub value: String,
328329
pub scope: NodeId,
329-
pub parent: Option<NodeId>,
330+
pub parent: Option<DefId>,
330331
pub docs: String,
331332
}
332333

@@ -339,7 +340,7 @@ pub struct TypeDefData {
339340
pub qualname: String,
340341
pub value: String,
341342
pub visibility: Visibility,
342-
pub parent: Option<NodeId>,
343+
pub parent: Option<DefId>,
343344
pub docs: String,
344345
}
345346

@@ -380,7 +381,7 @@ pub struct VariableData {
380381
pub qualname: String,
381382
pub span: Span,
382383
pub scope: NodeId,
383-
pub parent: Option<NodeId>,
384+
pub parent: Option<DefId>,
384385
pub value: String,
385386
pub type_value: String,
386387
pub visibility: Visibility,

src/librustc_save_analysis/dump_visitor.rs

+47-25
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
//! is used for recording the output in a format-agnostic way (see CsvDumper
2828
//! for an example).
2929
30+
use rustc::hir;
3031
use rustc::hir::def::Def;
3132
use rustc::hir::def_id::DefId;
32-
use rustc::hir::map::Node;
33+
use rustc::hir::map::{Node, NodeItem};
3334
use rustc::session::Session;
3435
use rustc::ty::{self, TyCtxt, ImplOrTraitItem, ImplOrTraitItemContainer};
3536

@@ -47,7 +48,7 @@ use syntax_pos::*;
4748
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
4849
use super::data::*;
4950
use super::dump::Dump;
50-
use super::external_data::Lower;
51+
use super::external_data::{Lower, make_def_id};
5152
use super::span_utils::SpanUtils;
5253
use super::recorder;
5354

@@ -271,11 +272,13 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
271272

272273
// looks up anything, not just a type
273274
fn lookup_type_ref(&self, ref_id: NodeId) -> Option<DefId> {
274-
match self.tcx.expect_def(ref_id) {
275-
Def::PrimTy(..) => None,
276-
Def::SelfTy(..) => None,
277-
def => Some(def.def_id()),
278-
}
275+
self.tcx.expect_def_or_none(ref_id).and_then(|def| {
276+
match def {
277+
Def::PrimTy(..) => None,
278+
Def::SelfTy(..) => None,
279+
def => Some(def.def_id()),
280+
}
281+
})
279282
}
280283

281284
fn process_def_kind(&mut self,
@@ -399,20 +402,36 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
399402
if !self.span.filter_generated(Some(method_data.span), span) {
400403
let container =
401404
self.tcx.impl_or_trait_item(self.tcx.map.local_def_id(id)).container();
402-
let decl_id = if let ImplOrTraitItemContainer::ImplContainer(id) = container {
403-
self.tcx.trait_id_of_impl(id).and_then(|id| {
404-
for item in &**self.tcx.trait_items(id) {
405-
if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
406-
if m.name == name {
407-
return Some(m.def_id);
405+
let mut trait_id;
406+
let mut decl_id = None;
407+
match container {
408+
ImplOrTraitItemContainer::ImplContainer(id) => {
409+
trait_id = self.tcx.trait_id_of_impl(id);
410+
411+
match trait_id {
412+
Some(id) => {
413+
for item in &**self.tcx.trait_items(id) {
414+
if let &ImplOrTraitItem::MethodTraitItem(ref m) = item {
415+
if m.name == name {
416+
decl_id = Some(m.def_id);
417+
break;
418+
}
419+
}
420+
}
421+
}
422+
None => {
423+
if let Some(NodeItem(item)) = self.tcx.map.get_if_local(id) {
424+
if let hir::ItemImpl(_, _, _, _, ref ty, _) = item.node {
425+
trait_id = self.lookup_type_ref(ty.id);
426+
}
408427
}
409428
}
410429
}
411-
None
412-
})
413-
} else {
414-
None
415-
};
430+
}
431+
ImplOrTraitItemContainer::TraitContainer(id) => {
432+
trait_id = Some(id);
433+
}
434+
}
416435

417436
self.dumper.method(MethodData {
418437
id: method_data.id,
@@ -422,6 +441,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
422441
qualname: method_data.qualname.clone(),
423442
value: sig_str,
424443
decl_id: decl_id,
444+
parent: trait_id,
425445
visibility: vis,
426446
docs: docs_for_attrs(attrs),
427447
}.lower(self.tcx));
@@ -544,7 +564,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
544564
span: Span,
545565
typ: &ast::Ty,
546566
expr: &ast::Expr,
547-
parent_id: NodeId,
567+
parent_id: DefId,
548568
vis: Visibility,
549569
attrs: &[Attribute]) {
550570
let qualname = format!("::{}", self.tcx.node_path_str(id));
@@ -659,7 +679,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
659679
type_value: enum_data.qualname.clone(),
660680
value: val,
661681
scope: enum_data.scope,
662-
parent: Some(item.id),
682+
parent: Some(make_def_id(item.id, &self.tcx.map)),
663683
docs: docs_for_attrs(&variant.node.attrs),
664684
}.lower(self.tcx));
665685
}
@@ -684,7 +704,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
684704
type_value: enum_data.qualname.clone(),
685705
value: val,
686706
scope: enum_data.scope,
687-
parent: Some(item.id),
707+
parent: Some(make_def_id(item.id, &self.tcx.map)),
688708
docs: docs_for_attrs(&variant.node.attrs),
689709
}.lower(self.tcx));
690710
}
@@ -738,7 +758,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
738758
}
739759
self.process_generic_params(type_parameters, item.span, "", item.id);
740760
for impl_item in impl_items {
741-
self.process_impl_item(impl_item, item.id);
761+
let map = &self.tcx.map;
762+
self.process_impl_item(impl_item, make_def_id(item.id, map));
742763
}
743764
}
744765

@@ -809,7 +830,8 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
809830
// walk generics and methods
810831
self.process_generic_params(generics, item.span, &qualname, item.id);
811832
for method in methods {
812-
self.process_trait_item(method, item.id)
833+
let map = &self.tcx.map;
834+
self.process_trait_item(method, make_def_id(item.id, map))
813835
}
814836
}
815837

@@ -1076,7 +1098,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
10761098
}
10771099
}
10781100

1079-
fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: NodeId) {
1101+
fn process_trait_item(&mut self, trait_item: &ast::TraitItem, trait_id: DefId) {
10801102
self.process_macro_use(trait_item.span, trait_item.id);
10811103
match trait_item.node {
10821104
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
@@ -1104,7 +1126,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11041126
}
11051127
}
11061128

1107-
fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: NodeId) {
1129+
fn process_impl_item(&mut self, impl_item: &ast::ImplItem, impl_id: DefId) {
11081130
self.process_macro_use(impl_item.span, impl_item.id);
11091131
match impl_item.node {
11101132
ast::ImplItemKind::Const(ref ty, ref expr) => {

src/librustc_save_analysis/external_data.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait Lower {
2323
fn lower(self, tcx: TyCtxt) -> Self::Target;
2424
}
2525

26-
fn make_def_id(id: NodeId, map: &Map) -> DefId {
26+
pub fn make_def_id(id: NodeId, map: &Map) -> DefId {
2727
map.opt_local_def_id(id).unwrap_or(null_def_id())
2828
}
2929

@@ -188,7 +188,7 @@ impl Lower for data::FunctionData {
188188
scope: make_def_id(self.scope, &tcx.map),
189189
value: self.value,
190190
visibility: self.visibility,
191-
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
191+
parent: self.parent,
192192
docs: self.docs,
193193
}
194194
}
@@ -353,7 +353,7 @@ impl Lower for data::MethodData {
353353
value: self.value,
354354
decl_id: self.decl_id,
355355
visibility: self.visibility,
356-
parent: Some(make_def_id(self.scope, &tcx.map)),
356+
parent: self.parent,
357357
docs: self.docs,
358358
}
359359
}
@@ -471,7 +471,7 @@ impl Lower for data::StructVariantData {
471471
type_value: self.type_value,
472472
value: self.value,
473473
scope: make_def_id(self.scope, &tcx.map),
474-
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
474+
parent: self.parent,
475475
docs: self.docs,
476476
}
477477
}
@@ -533,7 +533,7 @@ impl Lower for data::TupleVariantData {
533533
type_value: self.type_value,
534534
value: self.value,
535535
scope: make_def_id(self.scope, &tcx.map),
536-
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
536+
parent: self.parent,
537537
docs: self.docs,
538538
}
539539
}
@@ -563,7 +563,7 @@ impl Lower for data::TypeDefData {
563563
qualname: self.qualname,
564564
value: self.value,
565565
visibility: self.visibility,
566-
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
566+
parent: self.parent,
567567
docs: self.docs,
568568
}
569569
}
@@ -668,7 +668,7 @@ impl Lower for data::VariableData {
668668
scope: make_def_id(self.scope, &tcx.map),
669669
value: self.value,
670670
type_value: self.type_value,
671-
parent: self.parent.map(|id| make_def_id(id, &tcx.map)),
671+
parent: self.parent,
672672
visibility: self.visibility,
673673
docs: self.docs,
674674
}

src/librustc_save_analysis/lib.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub use self::csv_dumper::CsvDumper;
6464
pub use self::json_api_dumper::JsonApiDumper;
6565
pub use self::json_dumper::JsonDumper;
6666
pub use self::data::*;
67+
pub use self::external_data::make_def_id;
6768
pub use self::dump::Dump;
6869
pub use self::dump_visitor::DumpVisitor;
6970
use self::span_utils::SpanUtils;
@@ -295,7 +296,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
295296
qualname: qualname,
296297
span: sub_span.unwrap(),
297298
scope: scope,
298-
parent: Some(scope),
299+
parent: Some(make_def_id(scope, &self.tcx.map)),
299300
value: "".to_owned(),
300301
type_value: typ,
301302
visibility: From::from(&field.vis),
@@ -312,20 +313,22 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
312313
name: ast::Name, span: Span) -> Option<FunctionData> {
313314
// The qualname for a method is the trait name or name of the struct in an impl in
314315
// which the method is declared in, followed by the method's name.
315-
let (qualname, vis, docs) = match self.tcx.impl_of_method(self.tcx.map.local_def_id(id)) {
316+
let (qualname, parent_scope, vis, docs) =
317+
match self.tcx.impl_of_method(self.tcx.map.local_def_id(id)) {
316318
Some(impl_id) => match self.tcx.map.get_if_local(impl_id) {
317319
Some(NodeItem(item)) => {
318320
match item.node {
319321
hir::ItemImpl(.., ref ty, _) => {
320322
let mut result = String::from("<");
321323
result.push_str(&rustc::hir::print::ty_to_string(&ty));
322324

323-
if let Some(def_id) = self.tcx.trait_id_of_impl(impl_id) {
325+
let trait_id = self.tcx.trait_id_of_impl(impl_id);
326+
if let Some(def_id) = trait_id {
324327
result.push_str(" as ");
325328
result.push_str(&self.tcx.item_path_str(def_id));
326329
}
327330
result.push_str(">");
328-
(result, From::from(&item.vis), docs_for_attrs(&item.attrs))
331+
(result, trait_id, From::from(&item.vis), docs_for_attrs(&item.attrs))
329332
}
330333
_ => {
331334
span_bug!(span,
@@ -348,6 +351,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
348351
match self.tcx.map.get_if_local(def_id) {
349352
Some(NodeItem(item)) => {
350353
(format!("::{}", self.tcx.item_path_str(def_id)),
354+
Some(def_id),
351355
From::from(&item.vis),
352356
docs_for_attrs(&item.attrs))
353357
}
@@ -381,7 +385,6 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
381385

382386
let sub_span = self.span_utils.sub_span_after_keyword(span, keywords::Fn);
383387
filter!(self.span_utils, sub_span, span, None);
384-
let parent_scope = self.enclosing_scope(id);
385388
Some(FunctionData {
386389
id: id,
387390
name: name.to_string(),
@@ -392,7 +395,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
392395
// FIXME you get better data here by using the visitor.
393396
value: String::new(),
394397
visibility: vis,
395-
parent: Some(parent_scope),
398+
parent: parent_scope,
396399
docs: docs,
397400
})
398401
}

0 commit comments

Comments
 (0)