Skip to content

Commit 0bb8935

Browse files
committed
Auto merge of #47657 - algesten:save-analysis-impls, r=nrc
Emit data::Impl in save-analysis As discussed on [internals.rust-lang](https://internals.rust-lang.org/t/rustdoc2-rls-analysis-and-the-compiler-help-wanted/6592/5), this PR emits `rls-data::Impl` in the save-analysis. A number of questions are outstanding: - [x] A few `???` around row 356. We need to discuss what goes here, if anything. - [ ] ~~Deriving `id` for impl using hashing. Is this going to clash with rustc defids?~~ - [ ] ~~Deriving `id` for impl using hashing. Is the conversion from 64 bit -> 32 bit problematic?~~ - [x] Need a new rls-data with an `id` field in `Impl` struct. - [ ] ~~Need a new rls-data which `derive` `Hash` for `ImplKind` enum.~~
2 parents 7f2baba + 9a6afa8 commit 0bb8935

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

src/Cargo.lock

+11-1
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
@@ -15,7 +15,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
1515
rustc_typeck = { path = "../librustc_typeck" }
1616
syntax = { path = "../libsyntax" }
1717
syntax_pos = { path = "../libsyntax_pos" }
18-
rls-data = "0.14"
18+
rls-data = "0.15"
1919
rls-span = "0.4"
2020
# FIXME(#40527) should move rustc serialize out of tree
2121
rustc-serialize = "0.3"

src/librustc_save_analysis/dump_visitor.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
770770
impl_items: &'l [ast::ImplItem],
771771
) {
772772
if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
773-
down_cast_data!(impl_data, RelationData, item.span);
774-
self.dumper.dump_relation(impl_data);
773+
if let super::Data::RelationData(rel, imp) = impl_data {
774+
self.dumper.dump_relation(rel);
775+
self.dumper.dump_impl(imp);
776+
} else {
777+
span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
778+
}
775779
}
776780
self.visit_ty(&typ);
777781
if let &Some(ref trait_ref) = trait_ref {

src/librustc_save_analysis/json_dumper.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::io::Write;
1313
use rustc_serialize::json::as_json;
1414

1515
use rls_data::{self, Analysis, CratePreludeData, Def, DefKind, Import, MacroRef, Ref, RefKind,
16-
Relation};
16+
Relation, Impl};
1717
use rls_data::config::Config;
1818
use rls_span::{Column, Row};
1919

@@ -142,4 +142,8 @@ impl<'b, O: DumpOutput + 'b> JsonDumper<O> {
142142
pub fn dump_relation(&mut self, data: Relation) {
143143
self.result.relations.push(data);
144144
}
145+
146+
pub fn dump_impl(&mut self, data: Impl) {
147+
self.result.impls.push(data);
148+
}
145149
}

src/librustc_save_analysis/lib.rs

+36-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use rustc::session::config::CrateType::CrateTypeExecutable;
4545
use rustc::ty::{self, TyCtxt};
4646
use rustc_typeck::hir_ty_to_ty;
4747

48+
use std::cell::Cell;
4849
use std::default::Default;
4950
use std::env;
5051
use std::fs::File;
@@ -65,7 +66,7 @@ use dump_visitor::DumpVisitor;
6566
use span_utils::SpanUtils;
6667

6768
use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
68-
RelationKind, SpanData};
69+
RelationKind, SpanData, Impl, ImplKind};
6970
use rls_data::config::Config;
7071

7172

@@ -75,13 +76,14 @@ pub struct SaveContext<'l, 'tcx: 'l> {
7576
analysis: &'l ty::CrateAnalysis,
7677
span_utils: SpanUtils<'tcx>,
7778
config: Config,
79+
impl_counter: Cell<u32>,
7880
}
7981

8082
#[derive(Debug)]
8183
pub enum Data {
8284
RefData(Ref),
8385
DefData(Def),
84-
RelationData(Relation),
86+
RelationData(Relation, Impl),
8587
}
8688

8789
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
@@ -315,7 +317,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
315317
attributes: lower_attributes(item.attrs.to_owned(), self),
316318
}))
317319
}
318-
ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
320+
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
319321
if let ast::TyKind::Path(None, ref path) = typ.node {
320322
// Common case impl for a struct or something basic.
321323
if generated_code(path.span) {
@@ -324,17 +326,39 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
324326
let sub_span = self.span_utils.sub_span_for_type_name(path.span);
325327
filter!(self.span_utils, sub_span, typ.span, None);
326328

329+
let impl_id = self.next_impl_id();
330+
let span = self.span_from_span(sub_span.unwrap());
331+
327332
let type_data = self.lookup_ref_id(typ.id);
328333
type_data.map(|type_data| {
329334
Data::RelationData(Relation {
330-
kind: RelationKind::Impl,
331-
span: self.span_from_span(sub_span.unwrap()),
335+
kind: RelationKind::Impl {
336+
id: impl_id,
337+
},
338+
span: span.clone(),
332339
from: id_from_def_id(type_data),
333340
to: trait_ref
334341
.as_ref()
335342
.and_then(|t| self.lookup_ref_id(t.ref_id))
336343
.map(id_from_def_id)
337344
.unwrap_or(null_id()),
345+
},
346+
Impl {
347+
id: impl_id,
348+
kind: match *trait_ref {
349+
Some(_) => ImplKind::Direct,
350+
None => ImplKind::Inherent,
351+
},
352+
span: span,
353+
value: String::new(),
354+
parent: None,
355+
children: impls
356+
.iter()
357+
.map(|i| id_from_node_id(i.id, self))
358+
.collect(),
359+
docs: String::new(),
360+
sig: None,
361+
attributes: vec![],
338362
})
339363
})
340364
} else {
@@ -893,6 +917,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
893917

894918
result
895919
}
920+
921+
fn next_impl_id(&self) -> u32 {
922+
let next = self.impl_counter.get();
923+
self.impl_counter.set(next + 1);
924+
next
925+
}
896926
}
897927

898928
fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String {
@@ -1099,6 +1129,7 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
10991129
analysis,
11001130
span_utils: SpanUtils::new(&tcx.sess),
11011131
config: find_config(config),
1132+
impl_counter: Cell::new(0),
11021133
};
11031134

11041135
handler.save(save_ctxt, krate, cratename)

0 commit comments

Comments
 (0)