Skip to content

Commit e7fffa2

Browse files
committed
Auto merge of #42471 - nrc:save-sig-2, r=eddyb
save-analysis: signatures for everything!
2 parents add847a + ffd83fd commit e7fffa2

File tree

11 files changed

+1073
-255
lines changed

11 files changed

+1073
-255
lines changed

src/Cargo.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc_save_analysis/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rustc = { path = "../librustc" }
1414
rustc_typeck = { path = "../librustc_typeck" }
1515
syntax = { path = "../libsyntax" }
1616
syntax_pos = { path = "../libsyntax_pos" }
17-
rls-data = "0.3"
17+
rls-data = "0.6"
1818
rls-span = "0.4"
1919
# FIXME(#40527) should move rustc serialize out of tree
2020
rustc-serialize = "0.3"

src/librustc_save_analysis/data.rs

+8-33
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::hir::def_id::{CrateNum, DefId};
1818
use syntax::ast::{self, Attribute, NodeId};
1919
use syntax_pos::Span;
2020

21-
use rls_data::ExternalCrateData;
21+
use rls_data::{ExternalCrateData, Signature};
2222

2323
pub struct CrateData {
2424
pub name: String,
@@ -129,7 +129,7 @@ pub struct EnumData {
129129
pub variants: Vec<NodeId>,
130130
pub visibility: Visibility,
131131
pub docs: String,
132-
pub sig: Signature,
132+
pub sig: Option<Signature>,
133133
pub attributes: Vec<Attribute>,
134134
}
135135

@@ -165,7 +165,7 @@ pub struct FunctionData {
165165
pub visibility: Visibility,
166166
pub parent: Option<DefId>,
167167
pub docs: String,
168-
pub sig: Signature,
168+
pub sig: Option<Signature>,
169169
pub attributes: Vec<Attribute>,
170170
}
171171

@@ -251,7 +251,7 @@ pub struct MethodData {
251251
pub parent: Option<DefId>,
252252
pub visibility: Visibility,
253253
pub docs: String,
254-
pub sig: Signature,
254+
pub sig: Option<Signature>,
255255
pub attributes: Vec<Attribute>,
256256
}
257257

@@ -292,7 +292,7 @@ pub struct StructData {
292292
pub fields: Vec<NodeId>,
293293
pub visibility: Visibility,
294294
pub docs: String,
295-
pub sig: Signature,
295+
pub sig: Option<Signature>,
296296
pub attributes: Vec<Attribute>,
297297
}
298298

@@ -307,7 +307,7 @@ pub struct StructVariantData {
307307
pub scope: NodeId,
308308
pub parent: Option<DefId>,
309309
pub docs: String,
310-
pub sig: Signature,
310+
pub sig: Option<Signature>,
311311
pub attributes: Vec<Attribute>,
312312
}
313313

@@ -322,7 +322,7 @@ pub struct TraitData {
322322
pub items: Vec<NodeId>,
323323
pub visibility: Visibility,
324324
pub docs: String,
325-
pub sig: Signature,
325+
pub sig: Option<Signature>,
326326
pub attributes: Vec<Attribute>,
327327
}
328328

@@ -337,7 +337,7 @@ pub struct TupleVariantData {
337337
pub scope: NodeId,
338338
pub parent: Option<DefId>,
339339
pub docs: String,
340-
pub sig: Signature,
340+
pub sig: Option<Signature>,
341341
pub attributes: Vec<Attribute>,
342342
}
343343

@@ -419,28 +419,3 @@ pub struct VariableRefData {
419419
pub scope: NodeId,
420420
pub ref_id: DefId,
421421
}
422-
423-
424-
/// Encodes information about the signature of a definition. This should have
425-
/// enough information to create a nice display about a definition without
426-
/// access to the source code.
427-
#[derive(Clone, Debug)]
428-
pub struct Signature {
429-
pub span: Span,
430-
pub text: String,
431-
// These identify the main identifier for the defintion as byte offsets into
432-
// `text`. E.g., of `foo` in `pub fn foo(...)`
433-
pub ident_start: usize,
434-
pub ident_end: usize,
435-
pub defs: Vec<SigElement>,
436-
pub refs: Vec<SigElement>,
437-
}
438-
439-
/// An element of a signature. `start` and `end` are byte offsets into the `text`
440-
/// of the parent `Signature`.
441-
#[derive(Clone, Debug)]
442-
pub struct SigElement {
443-
pub id: DefId,
444-
pub start: usize,
445-
pub end: usize,
446-
}

src/librustc_save_analysis/dump_visitor.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ use syntax::ptr::P;
4848
use syntax::codemap::Spanned;
4949
use syntax_pos::*;
5050

51-
use super::{escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
52-
use super::data::*;
53-
use super::dump::Dump;
54-
use super::external_data::{Lower, make_def_id};
55-
use super::span_utils::SpanUtils;
56-
use super::recorder;
51+
use {escape, generated_code, SaveContext, PathCollector, docs_for_attrs};
52+
use data::*;
53+
use dump::Dump;
54+
use external_data::{Lower, make_def_id};
55+
use recorder;
56+
use span_utils::SpanUtils;
57+
use sig;
5758

5859
use rls_data::ExternalCrateData;
5960

@@ -391,13 +392,13 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
391392
sig: &'l ast::MethodSig,
392393
body: Option<&'l ast::Block>,
393394
id: ast::NodeId,
394-
name: ast::Name,
395+
name: ast::Ident,
395396
vis: Visibility,
396397
attrs: &'l [Attribute],
397398
span: Span) {
398399
debug!("process_method: {}:{}", id, name);
399400

400-
if let Some(method_data) = self.save_ctxt.get_method_data(id, name, span) {
401+
if let Some(method_data) = self.save_ctxt.get_method_data(id, name.name, span) {
401402

402403
let sig_str = ::make_signature(&sig.decl, &sig.generics);
403404
if body.is_some() {
@@ -423,7 +424,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
423424
Some(id) => {
424425
for item in self.tcx.associated_items(id) {
425426
if item.kind == ty::AssociatedKind::Method {
426-
if item.name == name {
427+
if item.name == name.name {
427428
decl_id = Some(item.def_id);
428429
break;
429430
}
@@ -455,7 +456,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
455456
parent: trait_id,
456457
visibility: vis,
457458
docs: docs_for_attrs(attrs),
458-
sig: method_data.sig,
459+
sig: sig::method_signature(id, name, sig, &self.save_ctxt),
459460
attributes: attrs.to_vec(),
460461
}.lower(self.tcx));
461462
}
@@ -580,13 +581,14 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
580581
name: ast::Name,
581582
span: Span,
582583
typ: &'l ast::Ty,
583-
expr: &'l ast::Expr,
584+
expr: Option<&'l ast::Expr>,
584585
parent_id: DefId,
585586
vis: Visibility,
586587
attrs: &'l [Attribute]) {
587588
let qualname = format!("::{}", self.tcx.node_path_str(id));
588589

589590
let sub_span = self.span.sub_span_after_keyword(span, keywords::Const);
591+
let value = expr.map(|e| self.span.snippet(e.span)).unwrap_or(String::new());
590592

591593
if !self.span.filter_generated(sub_span, span) {
592594
self.dumper.variable(VariableData {
@@ -595,20 +597,22 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
595597
id: id,
596598
name: name.to_string(),
597599
qualname: qualname,
598-
value: self.span.snippet(expr.span),
600+
value: value,
599601
type_value: ty_to_string(&typ),
600602
scope: self.cur_scope,
601603
parent: Some(parent_id),
602604
visibility: vis,
603605
docs: docs_for_attrs(attrs),
604-
sig: None,
606+
sig: sig::assoc_const_signature(id, name, typ, expr, &self.save_ctxt),
605607
attributes: attrs.to_vec(),
606608
}.lower(self.tcx));
607609
}
608610

609611
// walk type and init value
610612
self.visit_ty(typ);
611-
self.visit_expr(expr);
613+
if let Some(expr) = expr {
614+
self.visit_expr(expr);
615+
}
612616
}
613617

614618
// FIXME tuple structs should generate tuple-specific data.
@@ -646,7 +650,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
646650
fields: fields,
647651
visibility: From::from(&item.vis),
648652
docs: docs_for_attrs(&item.attrs),
649-
sig: self.save_ctxt.sig_base(item),
653+
sig: sig::item_signature(item, &self.save_ctxt),
650654
attributes: item.attrs.clone(),
651655
}.lower(self.tcx));
652656
}
@@ -679,18 +683,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
679683
qualname.push_str("::");
680684
qualname.push_str(&name);
681685

682-
let text = self.span.signature_string_for_span(variant.span);
683-
let ident_start = text.find(&name).unwrap();
684-
let ident_end = ident_start + name.len();
685-
let sig = Signature {
686-
span: variant.span,
687-
text: text,
688-
ident_start: ident_start,
689-
ident_end: ident_end,
690-
defs: vec![],
691-
refs: vec![],
692-
};
693-
694686
match variant.node.data {
695687
ast::VariantData::Struct(ref fields, _) => {
696688
let sub_span = self.span.span_for_first_ident(variant.span);
@@ -712,7 +704,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
712704
scope: enum_data.scope,
713705
parent: Some(make_def_id(item.id, &self.tcx.hir)),
714706
docs: docs_for_attrs(&variant.node.attrs),
715-
sig: sig,
707+
sig: sig::variant_signature(variant, &self.save_ctxt),
716708
attributes: variant.node.attrs.clone(),
717709
}.lower(self.tcx));
718710
}
@@ -739,7 +731,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
739731
scope: enum_data.scope,
740732
parent: Some(make_def_id(item.id, &self.tcx.hir)),
741733
docs: docs_for_attrs(&variant.node.attrs),
742-
sig: sig,
734+
sig: sig::variant_signature(variant, &self.save_ctxt),
743735
attributes: variant.node.attrs.clone(),
744736
}.lower(self.tcx));
745737
}
@@ -811,7 +803,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
811803
items: methods.iter().map(|i| i.id).collect(),
812804
visibility: From::from(&item.vis),
813805
docs: docs_for_attrs(&item.attrs),
814-
sig: self.save_ctxt.sig_base(item),
806+
sig: sig::item_signature(item, &self.save_ctxt),
815807
attributes: item.attrs.clone(),
816808
}.lower(self.tcx));
817809
}
@@ -1133,12 +1125,12 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11331125
fn process_trait_item(&mut self, trait_item: &'l ast::TraitItem, trait_id: DefId) {
11341126
self.process_macro_use(trait_item.span, trait_item.id);
11351127
match trait_item.node {
1136-
ast::TraitItemKind::Const(ref ty, Some(ref expr)) => {
1128+
ast::TraitItemKind::Const(ref ty, ref expr) => {
11371129
self.process_assoc_const(trait_item.id,
11381130
trait_item.ident.name,
11391131
trait_item.span,
11401132
&ty,
1141-
&expr,
1133+
expr.as_ref().map(|e| &**e),
11421134
trait_id,
11431135
Visibility::Public,
11441136
&trait_item.attrs);
@@ -1147,12 +1139,12 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11471139
self.process_method(sig,
11481140
body.as_ref().map(|x| &**x),
11491141
trait_item.id,
1150-
trait_item.ident.name,
1142+
trait_item.ident,
11511143
Visibility::Public,
11521144
&trait_item.attrs,
11531145
trait_item.span);
11541146
}
1155-
ast::TraitItemKind::Type(ref _bounds, ref default_ty) => {
1147+
ast::TraitItemKind::Type(ref bounds, ref default_ty) => {
11561148
// FIXME do something with _bounds (for type refs)
11571149
let name = trait_item.ident.name.to_string();
11581150
let qualname = format!("::{}", self.tcx.node_path_str(trait_item.id));
@@ -1168,7 +1160,11 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11681160
visibility: Visibility::Public,
11691161
parent: Some(trait_id),
11701162
docs: docs_for_attrs(&trait_item.attrs),
1171-
sig: None,
1163+
sig: sig::assoc_type_signature(trait_item.id,
1164+
trait_item.ident,
1165+
Some(bounds),
1166+
default_ty.as_ref().map(|ty| &**ty),
1167+
&self.save_ctxt),
11721168
attributes: trait_item.attrs.clone(),
11731169
}.lower(self.tcx));
11741170
}
@@ -1177,7 +1173,6 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11771173
self.visit_ty(default_ty)
11781174
}
11791175
}
1180-
ast::TraitItemKind::Const(ref ty, None) => self.visit_ty(ty),
11811176
ast::TraitItemKind::Macro(_) => {}
11821177
}
11831178
}
@@ -1190,7 +1185,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11901185
impl_item.ident.name,
11911186
impl_item.span,
11921187
&ty,
1193-
&expr,
1188+
Some(expr),
11941189
impl_id,
11951190
From::from(&impl_item.vis),
11961191
&impl_item.attrs);
@@ -1199,12 +1194,17 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
11991194
self.process_method(sig,
12001195
Some(body),
12011196
impl_item.id,
1202-
impl_item.ident.name,
1197+
impl_item.ident,
12031198
From::from(&impl_item.vis),
12041199
&impl_item.attrs,
12051200
impl_item.span);
12061201
}
1207-
ast::ImplItemKind::Type(ref ty) => self.visit_ty(ty),
1202+
ast::ImplItemKind::Type(ref ty) => {
1203+
// FIXME uses of the assoc type should ideally point to this
1204+
// 'def' and the name here should be a ref to the def in the
1205+
// trait.
1206+
self.visit_ty(ty)
1207+
}
12081208
ast::ImplItemKind::Macro(_) => {}
12091209
}
12101210
}
@@ -1369,7 +1369,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
13691369
visibility: From::from(&item.vis),
13701370
parent: None,
13711371
docs: docs_for_attrs(&item.attrs),
1372-
sig: Some(self.save_ctxt.sig_base(item)),
1372+
sig: sig::item_signature(item, &self.save_ctxt),
13731373
attributes: item.attrs.clone(),
13741374
}.lower(self.tcx));
13751375
}

0 commit comments

Comments
 (0)