Skip to content

Commit 00b4019

Browse files
committed
Auto merge of #38529 - nrc:save-sig, r=nikomatsakis
save-analysis: add signature info These 'signatures' for definitions contain enough info for the RLS to create Rustdoc-style info on the fly.
2 parents ce4461f + c24b192 commit 00b4019

File tree

7 files changed

+320
-16
lines changed

7 files changed

+320
-16
lines changed

src/librustc_save_analysis/data.rs

+35
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub struct EnumData {
135135
pub variants: Vec<NodeId>,
136136
pub visibility: Visibility,
137137
pub docs: String,
138+
pub sig: Signature,
138139
}
139140

140141
/// Data for extern crates.
@@ -169,6 +170,7 @@ pub struct FunctionData {
169170
pub visibility: Visibility,
170171
pub parent: Option<DefId>,
171172
pub docs: String,
173+
pub sig: Signature,
172174
}
173175

174176
/// Data about a function call.
@@ -253,6 +255,7 @@ pub struct MethodData {
253255
pub parent: Option<DefId>,
254256
pub visibility: Visibility,
255257
pub docs: String,
258+
pub sig: Signature,
256259
}
257260

258261
/// Data for modules.
@@ -267,6 +270,7 @@ pub struct ModData {
267270
pub items: Vec<NodeId>,
268271
pub visibility: Visibility,
269272
pub docs: String,
273+
pub sig: Signature,
270274
}
271275

272276
/// Data for a reference to a module.
@@ -290,6 +294,7 @@ pub struct StructData {
290294
pub fields: Vec<NodeId>,
291295
pub visibility: Visibility,
292296
pub docs: String,
297+
pub sig: Signature,
293298
}
294299

295300
#[derive(Debug, RustcEncodable)]
@@ -303,6 +308,7 @@ pub struct StructVariantData {
303308
pub scope: NodeId,
304309
pub parent: Option<DefId>,
305310
pub docs: String,
311+
pub sig: Signature,
306312
}
307313

308314
#[derive(Debug, RustcEncodable)]
@@ -316,6 +322,7 @@ pub struct TraitData {
316322
pub items: Vec<NodeId>,
317323
pub visibility: Visibility,
318324
pub docs: String,
325+
pub sig: Signature,
319326
}
320327

321328
#[derive(Debug, RustcEncodable)]
@@ -329,6 +336,7 @@ pub struct TupleVariantData {
329336
pub scope: NodeId,
330337
pub parent: Option<DefId>,
331338
pub docs: String,
339+
pub sig: Signature,
332340
}
333341

334342
/// Data for a typedef.
@@ -342,6 +350,7 @@ pub struct TypeDefData {
342350
pub visibility: Visibility,
343351
pub parent: Option<DefId>,
344352
pub docs: String,
353+
pub sig: Option<Signature>,
345354
}
346355

347356
/// Data for a reference to a type or trait.
@@ -386,6 +395,7 @@ pub struct VariableData {
386395
pub type_value: String,
387396
pub visibility: Visibility,
388397
pub docs: String,
398+
pub sig: Option<Signature>,
389399
}
390400

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

src/librustc_save_analysis/dump_visitor.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
357357
parent: None,
358358
visibility: Visibility::Inherited,
359359
docs: String::new(),
360+
sig: None,
360361
}.lower(self.tcx));
361362
}
362363
}
@@ -429,6 +430,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
429430
parent: trait_id,
430431
visibility: vis,
431432
docs: docs_for_attrs(attrs),
433+
sig: method_data.sig,
432434
}.lower(self.tcx));
433435
}
434436

@@ -500,6 +502,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
500502
visibility: Visibility::Inherited,
501503
parent: None,
502504
docs: String::new(),
505+
sig: None,
503506
}.lower(self.tcx));
504507
}
505508
}
@@ -572,6 +575,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
572575
parent: Some(parent_id),
573576
visibility: vis,
574577
docs: docs_for_attrs(attrs),
578+
sig: None,
575579
}.lower(self.tcx));
576580
}
577581

@@ -615,11 +619,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
615619
fields: fields,
616620
visibility: From::from(&item.vis),
617621
docs: docs_for_attrs(&item.attrs),
622+
sig: self.save_ctxt.sig_base(item),
618623
}.lower(self.tcx));
619624
}
620625

621-
622-
// fields
623626
for field in def.fields() {
624627
self.process_struct_field_def(field, item.id);
625628
self.visit_ty(&field.ty);
@@ -648,6 +651,18 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
648651
qualname.push_str("::");
649652
qualname.push_str(&name);
650653

654+
let text = self.span.signature_string_for_span(variant.span);
655+
let ident_start = text.find(&name).unwrap();
656+
let ident_end = ident_start + name.len();
657+
let sig = Signature {
658+
span: variant.span,
659+
text: text,
660+
ident_start: ident_start,
661+
ident_end: ident_end,
662+
defs: vec![],
663+
refs: vec![],
664+
};
665+
651666
match variant.node.data {
652667
ast::VariantData::Struct(ref fields, _) => {
653668
let sub_span = self.span.span_for_first_ident(variant.span);
@@ -669,6 +684,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
669684
scope: enum_data.scope,
670685
parent: Some(make_def_id(item.id, &self.tcx.map)),
671686
docs: docs_for_attrs(&variant.node.attrs),
687+
sig: sig,
672688
}.lower(self.tcx));
673689
}
674690
}
@@ -694,6 +710,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
694710
scope: enum_data.scope,
695711
parent: Some(make_def_id(item.id, &self.tcx.map)),
696712
docs: docs_for_attrs(&variant.node.attrs),
713+
sig: sig,
697714
}.lower(self.tcx));
698715
}
699716
}
@@ -778,6 +795,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
778795
items: methods.iter().map(|i| i.id).collect(),
779796
visibility: From::from(&item.vis),
780797
docs: docs_for_attrs(&item.attrs),
798+
sig: self.save_ctxt.sig_base(item),
781799
}.lower(self.tcx));
782800
}
783801

@@ -1043,6 +1061,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
10431061
parent: None,
10441062
visibility: Visibility::Inherited,
10451063
docs: String::new(),
1064+
sig: None,
10461065
}.lower(self.tcx));
10471066
}
10481067
}
@@ -1257,10 +1276,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
12571276
Struct(ref def, ref ty_params) => self.process_struct(item, def, ty_params),
12581277
Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
12591278
Impl(..,
1260-
ref ty_params,
1261-
ref trait_ref,
1262-
ref typ,
1263-
ref impl_items) => {
1279+
ref ty_params,
1280+
ref trait_ref,
1281+
ref typ,
1282+
ref impl_items) => {
12641283
self.process_impl(item, ty_params, trait_ref, &typ, impl_items)
12651284
}
12661285
Trait(_, ref generics, ref trait_refs, ref methods) =>
@@ -1283,6 +1302,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
12831302
visibility: From::from(&item.vis),
12841303
parent: None,
12851304
docs: docs_for_attrs(&item.attrs),
1305+
sig: Some(self.save_ctxt.sig_base(item)),
12861306
}.lower(self.tcx));
12871307
}
12881308

@@ -1492,6 +1512,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll,
14921512
parent: None,
14931513
visibility: Visibility::Inherited,
14941514
docs: String::new(),
1515+
sig: None,
14951516
}.lower(self.tcx));
14961517
}
14971518
}

0 commit comments

Comments
 (0)