Skip to content

Commit

Permalink
Auto merge of #47657 - algesten:save-analysis-impls, r=nrc
Browse files Browse the repository at this point in the history
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.~~
  • Loading branch information
bors committed Feb 11, 2018
2 parents 7f2baba + 9a6afa8 commit 0bb8935
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/librustc_save_analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
rustc_typeck = { path = "../librustc_typeck" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
rls-data = "0.14"
rls-data = "0.15"
rls-span = "0.4"
# FIXME(#40527) should move rustc serialize out of tree
rustc-serialize = "0.3"
8 changes: 6 additions & 2 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
impl_items: &'l [ast::ImplItem],
) {
if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(impl_data, RelationData, item.span);
self.dumper.dump_relation(impl_data);
if let super::Data::RelationData(rel, imp) = impl_data {
self.dumper.dump_relation(rel);
self.dumper.dump_impl(imp);
} else {
span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
}
}
self.visit_ty(&typ);
if let &Some(ref trait_ref) = trait_ref {
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_save_analysis/json_dumper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::io::Write;
use rustc_serialize::json::as_json;

use rls_data::{self, Analysis, CratePreludeData, Def, DefKind, Import, MacroRef, Ref, RefKind,
Relation};
Relation, Impl};
use rls_data::config::Config;
use rls_span::{Column, Row};

Expand Down Expand Up @@ -142,4 +142,8 @@ impl<'b, O: DumpOutput + 'b> JsonDumper<O> {
pub fn dump_relation(&mut self, data: Relation) {
self.result.relations.push(data);
}

pub fn dump_impl(&mut self, data: Impl) {
self.result.impls.push(data);
}
}
41 changes: 36 additions & 5 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use rustc::session::config::CrateType::CrateTypeExecutable;
use rustc::ty::{self, TyCtxt};
use rustc_typeck::hir_ty_to_ty;

use std::cell::Cell;
use std::default::Default;
use std::env;
use std::fs::File;
Expand All @@ -65,7 +66,7 @@ use dump_visitor::DumpVisitor;
use span_utils::SpanUtils;

use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
RelationKind, SpanData};
RelationKind, SpanData, Impl, ImplKind};
use rls_data::config::Config;


Expand All @@ -75,13 +76,14 @@ pub struct SaveContext<'l, 'tcx: 'l> {
analysis: &'l ty::CrateAnalysis,
span_utils: SpanUtils<'tcx>,
config: Config,
impl_counter: Cell<u32>,
}

#[derive(Debug)]
pub enum Data {
RefData(Ref),
DefData(Def),
RelationData(Relation),
RelationData(Relation, Impl),
}

impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
Expand Down Expand Up @@ -315,7 +317,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
attributes: lower_attributes(item.attrs.to_owned(), self),
}))
}
ast::ItemKind::Impl(.., ref trait_ref, ref typ, _) => {
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
if let ast::TyKind::Path(None, ref path) = typ.node {
// Common case impl for a struct or something basic.
if generated_code(path.span) {
Expand All @@ -324,17 +326,39 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let sub_span = self.span_utils.sub_span_for_type_name(path.span);
filter!(self.span_utils, sub_span, typ.span, None);

let impl_id = self.next_impl_id();
let span = self.span_from_span(sub_span.unwrap());

let type_data = self.lookup_ref_id(typ.id);
type_data.map(|type_data| {
Data::RelationData(Relation {
kind: RelationKind::Impl,
span: self.span_from_span(sub_span.unwrap()),
kind: RelationKind::Impl {
id: impl_id,
},
span: span.clone(),
from: id_from_def_id(type_data),
to: trait_ref
.as_ref()
.and_then(|t| self.lookup_ref_id(t.ref_id))
.map(id_from_def_id)
.unwrap_or(null_id()),
},
Impl {
id: impl_id,
kind: match *trait_ref {
Some(_) => ImplKind::Direct,
None => ImplKind::Inherent,
},
span: span,
value: String::new(),
parent: None,
children: impls
.iter()
.map(|i| id_from_node_id(i.id, self))
.collect(),
docs: String::new(),
sig: None,
attributes: vec![],
})
})
} else {
Expand Down Expand Up @@ -893,6 +917,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {

result
}

fn next_impl_id(&self) -> u32 {
let next = self.impl_counter.get();
self.impl_counter.set(next + 1);
next
}
}

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

handler.save(save_ctxt, krate, cratename)
Expand Down

0 comments on commit 0bb8935

Please sign in to comment.