2019-02-05 14:37:15 +01:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2019-02-10 16:13:30 +09:00
|
|
|
#![feature(nll)]
|
2019-02-07 01:02:00 +09:00
|
|
|
#![deny(rust_2018_idioms)]
|
2019-04-15 13:35:08 +02:00
|
|
|
#![deny(internal)]
|
2019-06-11 12:50:47 +03:00
|
|
|
#![deny(unused_lifetimes)]
|
2016-03-22 18:40:24 +02:00
|
|
|
#![allow(unused_attributes)]
|
2017-05-08 14:36:44 -07:00
|
|
|
|
2018-03-03 06:22:19 +01:00
|
|
|
#![recursion_limit="256"]
|
|
|
|
|
2016-09-07 11:23:49 +12:00
|
|
|
|
2016-05-03 12:54:29 +02:00
|
|
|
mod json_dumper;
|
|
|
|
mod dump_visitor;
|
|
|
|
#[macro_use]
|
2017-06-08 14:45:15 +12:00
|
|
|
mod span_utils;
|
2017-05-30 10:37:11 +12:00
|
|
|
mod sig;
|
2016-05-03 12:54:29 +02:00
|
|
|
|
2016-04-30 00:48:03 +00:00
|
|
|
use rustc::hir;
|
2019-04-20 19:36:05 +03:00
|
|
|
use rustc::hir::def::{CtorOf, Res, DefKind as HirDefKind};
|
2018-08-25 15:56:16 +01:00
|
|
|
use rustc::hir::Node;
|
2017-11-08 10:43:05 +13:00
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
2019-01-07 23:55:55 +01:00
|
|
|
use rustc::middle::privacy::AccessLevels;
|
2018-04-10 23:01:24 +09:00
|
|
|
use rustc::middle::cstore::ExternCrate;
|
2018-09-24 16:28:53 +02:00
|
|
|
use rustc::session::config::{CrateType, Input, OutputType};
|
2018-12-19 12:20:59 +02:00
|
|
|
use rustc::ty::{self, DefIdTree, TyCtxt};
|
2019-02-07 01:02:00 +09:00
|
|
|
use rustc::{bug, span_bug};
|
2018-09-22 23:19:39 +02:00
|
|
|
use rustc_codegen_utils::link::{filename_for_metadata, out_filename};
|
2016-03-22 18:40:24 +02:00
|
|
|
|
2018-02-02 08:29:59 +01:00
|
|
|
use std::cell::Cell;
|
2017-07-18 17:44:19 +12:00
|
|
|
use std::default::Default;
|
2016-03-22 18:40:24 +02:00
|
|
|
use std::env;
|
2016-12-13 09:12:39 -08:00
|
|
|
use std::fs::File;
|
2016-03-22 18:40:24 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-09 12:23:29 +12:00
|
|
|
|
2018-10-11 21:15:18 +13:00
|
|
|
use syntax::ast::{self, Attribute, DUMMY_NODE_ID, NodeId, PatKind};
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::Spanned;
|
2016-09-07 09:27:56 -07:00
|
|
|
use syntax::parse::lexer::comments::strip_doc_comment_decoration;
|
2017-06-08 14:45:15 +12:00
|
|
|
use syntax::print::pprust;
|
2015-05-05 22:03:20 +12:00
|
|
|
use syntax::visit::{self, Visitor};
|
2017-11-08 10:43:05 +13:00
|
|
|
use syntax::print::pprust::{arg_to_string, ty_to_string};
|
2018-08-18 12:14:03 +02:00
|
|
|
use syntax::source_map::MacroAttribute;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::*;
|
2015-05-11 08:35:08 +12:00
|
|
|
|
2017-07-19 11:52:09 +12:00
|
|
|
use json_dumper::JsonDumper;
|
2017-06-08 14:45:15 +12:00
|
|
|
use dump_visitor::DumpVisitor;
|
|
|
|
use span_utils::SpanUtils;
|
|
|
|
|
2018-09-24 16:38:54 +02:00
|
|
|
use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
|
|
|
|
RelationKind, SpanData, Impl, ImplKind};
|
2017-07-18 17:44:19 +12:00
|
|
|
use rls_data::config::Config;
|
2018-09-24 16:38:54 +02:00
|
|
|
|
2019-02-07 01:02:00 +09:00
|
|
|
use log::{debug, error, info};
|
|
|
|
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct SaveContext<'l, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-01-25 16:24:00 -05:00
|
|
|
tables: &'l ty::TypeckTables<'tcx>,
|
2019-01-07 23:55:55 +01:00
|
|
|
access_levels: &'l AccessLevels,
|
2016-04-18 10:30:55 +12:00
|
|
|
span_utils: SpanUtils<'tcx>,
|
2017-07-18 17:44:19 +12:00
|
|
|
config: Config,
|
2018-02-02 08:29:59 +01:00
|
|
|
impl_counter: Cell<u32>,
|
2015-05-05 18:46:09 +12:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Data {
|
|
|
|
RefData(Ref),
|
|
|
|
DefData(Def),
|
2018-02-02 08:29:59 +01:00
|
|
|
RelationData(Relation, Impl),
|
2017-06-08 14:45:15 +12:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'l, 'tcx> SaveContext<'l, 'tcx> {
|
2017-06-08 14:45:15 +12:00
|
|
|
fn span_from_span(&self, span: Span) -> SpanData {
|
2017-11-08 10:43:05 +13:00
|
|
|
use rls_span::{Column, Row};
|
2017-06-08 14:45:15 +12:00
|
|
|
|
2018-08-18 12:14:09 +02:00
|
|
|
let cm = self.tcx.sess.source_map();
|
2017-07-31 23:04:34 +03:00
|
|
|
let start = cm.lookup_char_pos(span.lo());
|
|
|
|
let end = cm.lookup_char_pos(span.hi());
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
SpanData {
|
2018-09-03 14:31:57 +02:00
|
|
|
file_name: start.file.name.to_string().into(),
|
2017-07-31 23:04:34 +03:00
|
|
|
byte_start: span.lo().0,
|
|
|
|
byte_end: span.hi().0,
|
2017-06-08 14:45:15 +12:00
|
|
|
line_start: Row::new_one_indexed(start.line as u32),
|
|
|
|
line_end: Row::new_one_indexed(end.line as u32),
|
|
|
|
column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
|
|
|
|
column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Returns path to the compilation output (e.g., libfoo-12345678.rmeta)
|
2018-09-22 23:19:39 +02:00
|
|
|
pub fn compilation_output(&self, crate_name: &str) -> PathBuf {
|
|
|
|
let sess = &self.tcx.sess;
|
|
|
|
// Save-analysis is emitted per whole session, not per each crate type
|
|
|
|
let crate_type = sess.crate_types.borrow()[0];
|
|
|
|
let outputs = &*self.tcx.output_filenames(LOCAL_CRATE);
|
|
|
|
|
|
|
|
if outputs.outputs.contains_key(&OutputType::Metadata) {
|
|
|
|
filename_for_metadata(sess, crate_name, outputs)
|
|
|
|
} else if outputs.outputs.should_codegen() {
|
|
|
|
out_filename(sess, crate_type, outputs, crate_name)
|
|
|
|
} else {
|
|
|
|
// Otherwise it's only a DepInfo, in which case we return early and
|
|
|
|
// not even reach the analysis stage.
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 18:46:09 +12:00
|
|
|
// List external crates used by the current crate.
|
2017-06-08 14:45:15 +12:00
|
|
|
pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
|
2018-09-03 14:31:57 +02:00
|
|
|
let mut result = Vec::with_capacity(self.tcx.crates().len());
|
2015-05-05 18:46:09 +12:00
|
|
|
|
2017-09-07 08:13:41 -07:00
|
|
|
for &n in self.tcx.crates().iter() {
|
2018-12-01 17:27:12 +01:00
|
|
|
let span = match self.tcx.extern_crate(n.as_def_id()) {
|
|
|
|
Some(&ExternCrate { span, .. }) => span,
|
2016-04-25 20:54:00 +12:00
|
|
|
None => {
|
|
|
|
debug!("Skipping crate {}, no data", n);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2018-08-18 12:14:09 +02:00
|
|
|
let lo_loc = self.span_utils.sess.source_map().lookup_char_pos(span.lo());
|
2017-06-08 14:45:15 +12:00
|
|
|
result.push(ExternalCrateData {
|
2017-12-14 08:09:19 +01:00
|
|
|
// FIXME: change file_name field to PathBuf in rls-data
|
|
|
|
// https://github.com/nrc/rls-data/issues/7
|
2018-09-11 16:39:07 +02:00
|
|
|
file_name: self.span_utils.make_filename_string(&lo_loc.file),
|
2017-10-15 23:27:17 +02:00
|
|
|
num: n.as_u32(),
|
|
|
|
id: GlobalCrateId {
|
|
|
|
name: self.tcx.crate_name(n).to_string(),
|
2017-11-08 10:43:05 +13:00
|
|
|
disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(),
|
2017-10-15 23:27:17 +02:00
|
|
|
},
|
2015-09-28 09:20:49 +13:00
|
|
|
});
|
2015-11-21 21:39:05 +02:00
|
|
|
}
|
2015-05-05 18:46:09 +12:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2015-05-05 19:27:44 +12:00
|
|
|
|
2017-03-08 18:04:30 +13:00
|
|
|
pub fn get_extern_item_data(&self, item: &ast::ForeignItem) -> Option<Data> {
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2017-03-08 18:04:30 +13:00
|
|
|
match item.node {
|
|
|
|
ast::ForeignItemKind::Fn(ref decl, ref generics) => {
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
Some(Data::DefData(Def {
|
2018-08-18 17:16:46 +02:00
|
|
|
kind: DefKind::ForeignFunction,
|
2017-06-08 14:45:15 +12:00
|
|
|
id: id_from_node_id(item.id, self),
|
2018-09-10 12:54:36 +12:00
|
|
|
span: self.span_from_span(item.ident.span),
|
2017-03-08 18:04:30 +13:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
2017-03-08 18:04:30 +13:00
|
|
|
value: make_signature(decl, generics),
|
|
|
|
parent: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-06-05 16:42:39 +12:00
|
|
|
sig: sig::foreign_item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2017-03-08 18:04:30 +13:00
|
|
|
}))
|
|
|
|
}
|
2018-09-10 12:54:36 +12:00
|
|
|
ast::ForeignItemKind::Static(ref ty, _) => {
|
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
2019-02-07 01:02:00 +09:00
|
|
|
let id = id_from_node_id(item.id, self);
|
2018-09-10 12:54:36 +12:00
|
|
|
let span = self.span_from_span(item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
Some(Data::DefData(Def {
|
2018-08-18 17:16:46 +02:00
|
|
|
kind: DefKind::ForeignStatic,
|
2017-06-08 14:45:15 +12:00
|
|
|
id,
|
|
|
|
span,
|
2017-03-08 18:04:30 +13:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
|
|
|
value: ty_to_string(ty),
|
2017-03-08 18:04:30 +13:00
|
|
|
parent: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-06-05 16:42:39 +12:00
|
|
|
sig: sig::foreign_item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2017-03-08 18:04:30 +13:00
|
|
|
}))
|
|
|
|
}
|
2017-09-03 19:53:58 +01:00
|
|
|
// FIXME(plietar): needs a new DefKind in rls-data
|
|
|
|
ast::ForeignItemKind::Ty => None,
|
2018-03-10 18:16:26 -08:00
|
|
|
ast::ForeignItemKind::Macro(..) => None,
|
2017-03-08 18:04:30 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 11:58:09 +13:00
|
|
|
pub fn get_item_data(&self, item: &ast::Item) -> Option<Data> {
|
2015-05-05 19:27:44 +12:00
|
|
|
match item.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
ast::ItemKind::Fn(ref decl, .., ref generics, _) => {
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Data::DefData(Def {
|
|
|
|
kind: DefKind::Function,
|
|
|
|
id: id_from_node_id(item.id, self),
|
2018-09-10 12:54:36 +12:00
|
|
|
span: self.span_from_span(item.ident.span),
|
2016-05-23 10:23:36 +12:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
2016-05-11 13:46:39 -07:00
|
|
|
value: make_signature(decl, generics),
|
2016-08-30 16:00:48 +12:00
|
|
|
parent: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: sig::item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2016-01-22 11:58:09 +13:00
|
|
|
}))
|
2015-05-05 19:27:44 +12:00
|
|
|
}
|
2018-09-10 12:54:36 +12:00
|
|
|
ast::ItemKind::Static(ref typ, ..) => {
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2015-05-11 08:35:08 +12:00
|
|
|
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
let id = id_from_node_id(item.id, self);
|
2018-09-10 12:54:36 +12:00
|
|
|
let span = self.span_from_span(item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
Some(Data::DefData(Def {
|
|
|
|
kind: DefKind::Static,
|
|
|
|
id,
|
|
|
|
span,
|
2015-07-28 18:07:20 +02:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
|
|
|
value: ty_to_string(&typ),
|
2016-08-30 16:00:48 +12:00
|
|
|
parent: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: sig::item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2016-01-22 11:58:09 +13:00
|
|
|
}))
|
2015-05-11 08:35:08 +12:00
|
|
|
}
|
2017-06-08 14:45:15 +12:00
|
|
|
ast::ItemKind::Const(ref typ, _) => {
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
let id = id_from_node_id(item.id, self);
|
2018-09-10 12:54:36 +12:00
|
|
|
let span = self.span_from_span(item.ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
Some(Data::DefData(Def {
|
|
|
|
kind: DefKind::Const,
|
|
|
|
id,
|
|
|
|
span,
|
2015-07-28 18:07:20 +02:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
|
|
|
value: ty_to_string(typ),
|
2016-08-30 16:00:48 +12:00
|
|
|
parent: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: sig::item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2016-01-22 11:58:09 +13:00
|
|
|
}))
|
2015-05-11 08:35:08 +12:00
|
|
|
}
|
2016-02-09 11:36:51 +01:00
|
|
|
ast::ItemKind::Mod(ref m) => {
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2015-05-26 10:35:53 +12:00
|
|
|
|
2018-08-18 12:14:09 +02:00
|
|
|
let cm = self.tcx.sess.source_map();
|
2015-05-26 10:35:53 +12:00
|
|
|
let filename = cm.span_to_filename(m.inner);
|
|
|
|
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2016-11-30 11:50:08 +13:00
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Data::DefData(Def {
|
|
|
|
kind: DefKind::Mod,
|
|
|
|
id: id_from_node_id(item.id, self),
|
2015-07-28 18:07:20 +02:00
|
|
|
name: item.ident.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
2018-09-10 12:54:36 +12:00
|
|
|
span: self.span_from_span(item.ident.span),
|
2017-12-14 08:09:19 +01:00
|
|
|
value: filename.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
parent: None,
|
2017-11-08 10:43:05 +13:00
|
|
|
children: m.items
|
|
|
|
.iter()
|
|
|
|
.map(|i| id_from_node_id(i.id, self))
|
|
|
|
.collect(),
|
2017-06-08 14:45:15 +12:00
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: sig::item_signature(item, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2016-01-22 11:58:09 +13:00
|
|
|
}))
|
2015-09-02 15:37:07 +12:00
|
|
|
}
|
2016-05-11 13:46:39 -07:00
|
|
|
ast::ItemKind::Enum(ref def, _) => {
|
|
|
|
let name = item.ident.to_string();
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(item.id)));
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, item.ident.span);
|
2017-11-08 10:43:05 +13:00
|
|
|
let variants_str = def.variants
|
|
|
|
.iter()
|
2018-03-19 01:21:30 +03:00
|
|
|
.map(|v| v.node.ident.to_string())
|
2017-11-08 10:43:05 +13:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ");
|
2017-06-08 14:45:15 +12:00
|
|
|
let value = format!("{}::{{{}}}", name, variants_str);
|
|
|
|
Some(Data::DefData(Def {
|
|
|
|
kind: DefKind::Enum,
|
|
|
|
id: id_from_node_id(item.id, self),
|
2018-09-10 12:54:36 +12:00
|
|
|
span: self.span_from_span(item.ident.span),
|
2017-06-08 14:45:15 +12:00
|
|
|
name,
|
|
|
|
qualname,
|
|
|
|
value,
|
|
|
|
parent: None,
|
|
|
|
children: def.variants
|
2017-11-08 10:43:05 +13:00
|
|
|
.iter()
|
2019-03-21 23:38:50 +01:00
|
|
|
.map(|v| id_from_node_id(v.node.id, self))
|
2017-11-08 10:43:05 +13:00
|
|
|
.collect(),
|
2017-06-08 14:45:15 +12:00
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&item.attrs),
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: sig::item_signature(item, self),
|
2018-09-03 14:31:57 +02:00
|
|
|
attributes: lower_attributes(item.attrs.clone(), self),
|
2016-01-22 11:58:09 +13:00
|
|
|
}))
|
2015-09-02 15:37:07 +12:00
|
|
|
}
|
2018-02-02 08:29:59 +01:00
|
|
|
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {
|
2017-06-08 14:45:15 +12:00
|
|
|
if let ast::TyKind::Path(None, ref path) = typ.node {
|
2015-06-05 17:50:04 +12:00
|
|
|
// Common case impl for a struct or something basic.
|
2017-06-08 14:45:15 +12:00
|
|
|
if generated_code(path.span) {
|
|
|
|
return None;
|
2015-06-05 17:50:04 +12:00
|
|
|
}
|
2018-09-10 12:54:36 +12:00
|
|
|
let sub_span = path.segments.last().unwrap().ident.span;
|
|
|
|
filter!(self.span_utils, sub_span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
2018-02-02 08:29:59 +01:00
|
|
|
let impl_id = self.next_impl_id();
|
2018-09-10 12:54:36 +12:00
|
|
|
let span = self.span_from_span(sub_span);
|
2018-02-02 08:29:59 +01:00
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
let type_data = self.lookup_ref_id(typ.id);
|
2017-11-08 10:43:05 +13:00
|
|
|
type_data.map(|type_data| {
|
|
|
|
Data::RelationData(Relation {
|
2018-02-02 08:29:59 +01:00
|
|
|
kind: RelationKind::Impl {
|
|
|
|
id: impl_id,
|
|
|
|
},
|
|
|
|
span: span.clone(),
|
2017-11-08 10:43:05 +13:00
|
|
|
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)
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_else(|| null_id()),
|
2018-02-02 08:29:59 +01:00
|
|
|
},
|
|
|
|
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![],
|
2017-11-08 10:43:05 +13:00
|
|
|
})
|
|
|
|
})
|
2017-06-08 14:45:15 +12:00
|
|
|
} else {
|
|
|
|
None
|
2015-06-05 17:50:04 +12:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 10:35:53 +12:00
|
|
|
_ => {
|
|
|
|
// FIXME
|
2016-03-28 23:21:01 +02:00
|
|
|
bug!();
|
2015-05-26 10:35:53 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option<Def> {
|
2016-04-06 11:19:10 +03:00
|
|
|
if let Some(ident) = field.ident {
|
2016-11-22 08:12:02 +13:00
|
|
|
let name = ident.to_string();
|
2018-12-28 10:08:30 +02:00
|
|
|
let qualname = format!("::{}::{}",
|
|
|
|
self.tcx.def_path_str(self.tcx.hir().local_def_id(scope)),
|
|
|
|
ident);
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, ident.span);
|
2018-12-04 13:45:36 +01:00
|
|
|
let def_id = self.tcx.hir().local_def_id(field.id);
|
2017-04-24 15:20:46 +03:00
|
|
|
let typ = self.tcx.type_of(def_id).to_string();
|
2016-11-22 08:12:02 +13:00
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
let id = id_from_node_id(field.id, self);
|
2018-09-10 12:54:36 +12:00
|
|
|
let span = self.span_from_span(ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
|
|
|
|
Some(Def {
|
|
|
|
kind: DefKind::Field,
|
|
|
|
id,
|
|
|
|
span,
|
|
|
|
name,
|
|
|
|
qualname,
|
|
|
|
value: typ,
|
|
|
|
parent: Some(id_from_node_id(scope, self)),
|
|
|
|
children: vec![],
|
|
|
|
decl_id: None,
|
2017-07-18 17:54:49 +12:00
|
|
|
docs: self.docs_for_attrs(&field.attrs),
|
2017-06-05 16:42:39 +12:00
|
|
|
sig: sig::field_signature(field, self),
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(field.attrs.clone(), self),
|
2016-04-02 16:47:53 +03:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2015-06-05 16:03:48 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 12:23:29 +12:00
|
|
|
// FIXME would be nice to take a MethodItem here, but the ast provides both
|
|
|
|
// trait and impl flavours, so the caller must do the disassembly.
|
2018-09-10 12:54:36 +12:00
|
|
|
pub fn get_method_data(&self, id: ast::NodeId, ident: ast::Ident, span: Span) -> Option<Def> {
|
2015-07-09 12:23:29 +12:00
|
|
|
// The qualname for a method is the trait name or name of the struct in an impl in
|
|
|
|
// which the method is declared in, followed by the method's name.
|
2017-06-08 14:45:15 +12:00
|
|
|
let (qualname, parent_scope, decl_id, docs, attributes) =
|
2018-12-04 13:45:36 +01:00
|
|
|
match self.tcx.impl_of_method(self.tcx.hir().local_def_id(id)) {
|
|
|
|
Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Item(item)) => match item.node {
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Impl(.., ref ty, _) => {
|
2018-05-07 11:43:34 +12:00
|
|
|
let mut qualname = String::from("<");
|
2019-02-18 10:59:17 +01:00
|
|
|
qualname.push_str(&self.tcx.hir().hir_to_pretty_string(ty.hir_id));
|
2015-07-09 12:23:29 +12:00
|
|
|
|
2018-07-29 17:40:36 +01:00
|
|
|
let trait_id = self.tcx.trait_id_of_impl(impl_id);
|
2016-11-10 02:06:34 +02:00
|
|
|
let mut decl_id = None;
|
2018-05-07 11:43:34 +12:00
|
|
|
let mut docs = String::new();
|
|
|
|
let mut attrs = vec![];
|
2019-06-24 09:55:11 +02:00
|
|
|
let hir_id = self.tcx.hir().node_to_hir_id(id);
|
|
|
|
if let Some(Node::ImplItem(item)) =
|
|
|
|
self.tcx.hir().find_by_hir_id(hir_id)
|
|
|
|
{
|
2018-05-07 11:43:34 +12:00
|
|
|
docs = self.docs_for_attrs(&item.attrs);
|
|
|
|
attrs = item.attrs.to_vec();
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:17:49 +12:00
|
|
|
if let Some(def_id) = trait_id {
|
2018-05-07 11:43:34 +12:00
|
|
|
// A method in a trait impl.
|
|
|
|
qualname.push_str(" as ");
|
2018-12-19 12:31:35 +02:00
|
|
|
qualname.push_str(&self.tcx.def_path_str(def_id));
|
2017-11-08 10:43:05 +13:00
|
|
|
self.tcx
|
|
|
|
.associated_items(def_id)
|
2018-09-10 12:54:36 +12:00
|
|
|
.find(|item| item.ident.name == ident.name)
|
2016-11-10 02:06:34 +02:00
|
|
|
.map(|item| decl_id = Some(item.def_id));
|
2015-07-09 12:23:29 +12:00
|
|
|
}
|
2018-05-07 11:43:34 +12:00
|
|
|
qualname.push_str(">");
|
|
|
|
|
|
|
|
(qualname, trait_id, decl_id, docs, attrs)
|
2015-07-09 12:23:29 +12:00
|
|
|
}
|
|
|
|
_ => {
|
2017-11-08 10:43:05 +13:00
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"Container {:?} for method {} not an impl?",
|
|
|
|
impl_id,
|
|
|
|
id
|
|
|
|
);
|
2015-09-02 15:37:07 +12:00
|
|
|
}
|
2017-11-08 10:43:05 +13:00
|
|
|
},
|
|
|
|
r => {
|
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"Container {:?} for method {} is not a node item {:?}",
|
|
|
|
impl_id,
|
|
|
|
id,
|
|
|
|
r
|
|
|
|
);
|
2015-07-09 12:23:29 +12:00
|
|
|
}
|
2017-11-08 10:43:05 +13:00
|
|
|
},
|
2018-12-04 13:45:36 +01:00
|
|
|
None => match self.tcx.trait_of_item(self.tcx.hir().local_def_id(id)) {
|
2018-05-07 11:43:34 +12:00
|
|
|
Some(def_id) => {
|
|
|
|
let mut docs = String::new();
|
|
|
|
let mut attrs = vec![];
|
2019-06-24 09:55:11 +02:00
|
|
|
let hir_id = self.tcx.hir().node_to_hir_id(id);
|
2018-05-07 11:43:34 +12:00
|
|
|
|
2019-06-24 09:55:11 +02:00
|
|
|
if let Some(Node::TraitItem(item)) = self.tcx.hir().find_by_hir_id(hir_id) {
|
2018-05-07 11:43:34 +12:00
|
|
|
docs = self.docs_for_attrs(&item.attrs);
|
|
|
|
attrs = item.attrs.to_vec();
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
2018-12-19 12:31:35 +02:00
|
|
|
format!("::{}", self.tcx.def_path_str(def_id)),
|
2017-11-08 10:43:05 +13:00
|
|
|
Some(def_id),
|
|
|
|
None,
|
2018-05-07 11:43:34 +12:00
|
|
|
docs,
|
|
|
|
attrs,
|
|
|
|
)
|
|
|
|
}
|
2017-11-08 10:43:05 +13:00
|
|
|
None => {
|
|
|
|
debug!("Could not find container for method {} at {:?}", id, span);
|
|
|
|
// This is not necessarily a bug, if there was a compilation error,
|
|
|
|
// the tables we need might not exist.
|
|
|
|
return None;
|
2015-07-09 12:23:29 +12:00
|
|
|
}
|
2017-11-08 10:43:05 +13:00
|
|
|
},
|
|
|
|
};
|
2015-07-09 12:23:29 +12:00
|
|
|
|
2018-09-10 12:54:36 +12:00
|
|
|
let qualname = format!("{}::{}", qualname, ident.name);
|
2015-07-09 12:23:29 +12:00
|
|
|
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, ident.span);
|
2016-11-30 11:50:08 +13:00
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Def {
|
|
|
|
kind: DefKind::Method,
|
|
|
|
id: id_from_node_id(id, self),
|
2018-09-10 12:54:36 +12:00
|
|
|
span: self.span_from_span(ident.span),
|
|
|
|
name: ident.name.to_string(),
|
2017-06-08 14:45:15 +12:00
|
|
|
qualname,
|
2016-05-11 13:46:39 -07:00
|
|
|
// FIXME you get better data here by using the visitor.
|
|
|
|
value: String::new(),
|
2017-06-08 14:45:15 +12:00
|
|
|
parent: parent_scope.map(|id| id_from_def_id(id)),
|
|
|
|
children: vec![],
|
|
|
|
decl_id: decl_id.map(|id| id_from_def_id(id)),
|
|
|
|
docs,
|
2017-05-31 15:59:48 +12:00
|
|
|
sig: None,
|
2017-06-08 14:45:15 +12:00
|
|
|
attributes: lower_attributes(attributes, self),
|
2016-01-22 11:58:09 +13:00
|
|
|
})
|
2015-07-09 12:23:29 +12:00
|
|
|
}
|
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
pub fn get_trait_ref_data(&self, trait_ref: &ast::TraitRef) -> Option<Ref> {
|
2016-01-22 11:58:09 +13:00
|
|
|
self.lookup_ref_id(trait_ref.ref_id).and_then(|def_id| {
|
2015-06-15 17:58:10 +12:00
|
|
|
let span = trait_ref.path.span;
|
2017-02-02 16:23:27 +13:00
|
|
|
if generated_code(span) {
|
|
|
|
return None;
|
|
|
|
}
|
2018-09-10 12:54:36 +12:00
|
|
|
let sub_span = trait_ref.path.segments.last().unwrap().ident.span;
|
|
|
|
filter!(self.span_utils, sub_span);
|
|
|
|
let span = self.span_from_span(sub_span);
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Type,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id),
|
2016-01-22 11:58:09 +13:00
|
|
|
})
|
2015-06-05 17:50:04 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-15 10:06:01 +12:00
|
|
|
pub fn get_expr_data(&self, expr: &ast::Expr) -> Option<Data> {
|
2019-06-20 09:55:21 +02:00
|
|
|
let expr_hir_id = self.tcx.hir().node_to_hir_id(expr.id);
|
2019-06-20 10:05:56 +02:00
|
|
|
let hir_node = self.tcx.hir().expect_expr(expr_hir_id);
|
2017-01-06 21:54:24 +02:00
|
|
|
let ty = self.tables.expr_ty_adjusted_opt(&hir_node);
|
2018-08-22 01:35:02 +01:00
|
|
|
if ty.is_none() || ty.unwrap().sty == ty::Error {
|
2016-01-28 13:10:04 +13:00
|
|
|
return None;
|
|
|
|
}
|
2015-05-26 10:35:53 +12:00
|
|
|
match expr.node {
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Field(ref sub_ex, ident) => {
|
2019-06-24 09:55:11 +02:00
|
|
|
let sub_ex_hir_id = self.tcx.hir().node_to_hir_id(sub_ex.id);
|
|
|
|
let hir_node = match self.tcx.hir().find_by_hir_id(sub_ex_hir_id) {
|
2018-08-25 15:56:16 +01:00
|
|
|
Some(Node::Expr(expr)) => expr,
|
2016-05-25 15:24:55 +12:00
|
|
|
_ => {
|
2017-11-08 10:43:05 +13:00
|
|
|
debug!(
|
|
|
|
"Missing or weird node for sub-expression {} in {:?}",
|
|
|
|
sub_ex.id,
|
|
|
|
expr
|
|
|
|
);
|
2016-05-25 15:24:55 +12:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2017-01-06 21:54:24 +02:00
|
|
|
match self.tables.expr_ty_adjusted(&hir_node).sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(def, _) if !def.is_enum() => {
|
2018-04-05 03:20:21 +03:00
|
|
|
let variant = &def.non_enum_variant();
|
|
|
|
let index = self.tcx.find_field_index(ident, variant).unwrap();
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, ident.span);
|
|
|
|
let span = self.span_from_span(ident.span);
|
2017-06-08 14:45:15 +12:00
|
|
|
return Some(Data::RefData(Ref {
|
|
|
|
kind: RefKind::Variable,
|
|
|
|
span,
|
2018-04-05 03:20:21 +03:00
|
|
|
ref_id: id_from_def_id(variant.fields[index].did),
|
2015-08-02 22:52:50 +03:00
|
|
|
}));
|
2015-05-26 10:35:53 +12:00
|
|
|
}
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Tuple(..) => None,
|
2015-06-15 10:06:01 +12:00
|
|
|
_ => {
|
2016-09-06 01:26:02 +03:00
|
|
|
debug!("Expected struct or union type, found {:?}", ty);
|
2015-06-15 10:06:01 +12:00
|
|
|
None
|
|
|
|
}
|
2015-05-26 10:35:53 +12:00
|
|
|
}
|
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
ast::ExprKind::Struct(ref path, ..) => {
|
2017-01-06 21:54:24 +02:00
|
|
|
match self.tables.expr_ty_adjusted(&hir_node).sty {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Adt(def, _) if !def.is_enum() => {
|
2018-09-10 12:54:36 +12:00
|
|
|
let sub_span = path.segments.last().unwrap().ident.span;
|
|
|
|
filter!(self.span_utils, sub_span);
|
|
|
|
let span = self.span_from_span(sub_span);
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Data::RefData(Ref {
|
|
|
|
kind: RefKind::Type,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def.did),
|
2015-06-15 10:06:01 +12:00
|
|
|
}))
|
2015-06-09 09:51:54 +12:00
|
|
|
}
|
|
|
|
_ => {
|
2016-09-06 01:26:02 +03:00
|
|
|
// FIXME ty could legitimately be an enum, but then we will fail
|
2015-06-15 10:06:01 +12:00
|
|
|
// later if we try to look up the fields.
|
2016-09-06 01:26:02 +03:00
|
|
|
debug!("expected struct or union, found {:?}", ty);
|
2015-06-15 10:06:01 +12:00
|
|
|
None
|
2015-06-09 09:51:54 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 15:52:42 +13:00
|
|
|
ast::ExprKind::MethodCall(ref seg, ..) => {
|
2018-12-04 13:45:36 +01:00
|
|
|
let expr_hir_id = self.tcx.hir().definitions().node_to_hir_id(expr.id);
|
2019-03-15 17:42:34 +01:00
|
|
|
let method_id = match self.tables.type_dependent_def_id(expr_hir_id) {
|
|
|
|
Some(id) => id,
|
2017-11-08 10:08:19 +13:00
|
|
|
None => {
|
|
|
|
debug!("Could not resolve method id for {:?}", expr);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
2016-11-10 02:06:34 +02:00
|
|
|
let (def_id, decl_id) = match self.tcx.associated_item(method_id).container {
|
2015-07-07 11:42:43 +12:00
|
|
|
ty::ImplContainer(_) => (Some(method_id), None),
|
2015-09-02 15:37:07 +12:00
|
|
|
ty::TraitContainer(_) => (None, Some(method_id)),
|
2015-07-07 11:42:43 +12:00
|
|
|
};
|
2018-03-19 03:54:56 +03:00
|
|
|
let sub_span = seg.ident.span;
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, sub_span);
|
2017-11-06 15:52:42 +13:00
|
|
|
let span = self.span_from_span(sub_span);
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Data::RefData(Ref {
|
|
|
|
kind: RefKind::Function,
|
|
|
|
span,
|
2017-11-08 10:43:05 +13:00
|
|
|
ref_id: def_id
|
|
|
|
.or(decl_id)
|
|
|
|
.map(|id| id_from_def_id(id))
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_else(|| null_id()),
|
2015-07-07 11:42:43 +12:00
|
|
|
}))
|
|
|
|
}
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Path(_, ref path) => {
|
2017-06-08 14:45:15 +12:00
|
|
|
self.get_path_data(expr.id, path).map(|d| Data::RefData(d))
|
2015-07-08 14:30:18 +12:00
|
|
|
}
|
2015-05-05 19:27:44 +12:00
|
|
|
_ => {
|
2015-05-11 08:35:08 +12:00
|
|
|
// FIXME
|
2016-03-28 23:21:01 +02:00
|
|
|
bug!();
|
2015-05-05 19:27:44 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:57:34 +02:00
|
|
|
pub fn get_path_res(&self, id: NodeId) -> Res {
|
|
|
|
let hir_id = self.tcx.hir().node_to_hir_id(id);
|
2019-06-20 10:39:19 +02:00
|
|
|
match self.tcx.hir().get(hir_id) {
|
2019-04-20 19:36:05 +03:00
|
|
|
Node::TraitRef(tr) => tr.path.res,
|
2016-11-25 13:21:19 +02:00
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Item(&hir::Item {
|
2018-07-11 23:36:06 +08:00
|
|
|
node: hir::ItemKind::Use(ref path, _),
|
2017-11-08 10:43:05 +13:00
|
|
|
..
|
|
|
|
}) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Visibility(&Spanned {
|
2019-04-20 19:36:05 +03:00
|
|
|
node: hir::VisibilityKind::Restricted { ref path, .. }, .. }) => path.res,
|
2016-11-25 13:21:19 +02:00
|
|
|
|
save-analysis: Get path def from parent in case there's no def for the path itself.
This fixes #57462.
The relevant part from the hir type collector is:
```
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(32) => Entry { parent: NodeId(33), dep_node: 4294967040, node: Expr(expr(32: <Foo>::new)) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(48) => Entry { parent: NodeId(32), dep_node: 4294967040, node: Ty(type(Foo)) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(30) => Entry { parent: NodeId(48), dep_node: 4294967040, node: PathSegment(PathSegment { ident: Foo#0, id: Some(NodeId(30)), def: Some(Err), args: None, infer_types: true }) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(31) => Entry { parent: NodeId(32), dep_node: 4294967040, node: PathSegment(PathSegment { ident: new#0, id: Some(NodeId(31)), def: Some(Err), args: None, infer_types: true }) }
```
We have the right ID when looking for NodeId(31) and try with NodeId(32) (which
is the right thing to look for) from get_path_data, but not for the segments
that we write from `write_sub_paths_truncated`.
Basically `process_path` takes an id which is always the parent, and that we
fall back to in `get_path_data()`, so we get the right result for the last path
segment, but not for the other segments that get written to from
`write_sub_paths_truncated`.
I think we can stop passing the explicit id around to `get_path_data` now, will
consider sending that as a followup.
2019-01-09 17:31:16 +01:00
|
|
|
Node::PathSegment(seg) => {
|
2019-04-20 19:36:05 +03:00
|
|
|
match seg.res {
|
|
|
|
Some(res) if res != Res::Err => res,
|
2019-06-24 09:41:29 +02:00
|
|
|
_ => {
|
2019-06-24 09:46:38 +02:00
|
|
|
let parent_node = self.tcx.hir().get_parent_node(hir_id);
|
2019-06-24 09:41:29 +02:00
|
|
|
self.get_path_res(self.tcx.hir().hir_to_node_id(parent_node))
|
|
|
|
},
|
save-analysis: Get path def from parent in case there's no def for the path itself.
This fixes #57462.
The relevant part from the hir type collector is:
```
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(32) => Entry { parent: NodeId(33), dep_node: 4294967040, node: Expr(expr(32: <Foo>::new)) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(48) => Entry { parent: NodeId(32), dep_node: 4294967040, node: Ty(type(Foo)) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(30) => Entry { parent: NodeId(48), dep_node: 4294967040, node: PathSegment(PathSegment { ident: Foo#0, id: Some(NodeId(30)), def: Some(Err), args: None, infer_types: true }) }
DEBUG 2019-01-09T15:42:58Z: rustc::hir::map::collector: hir_map: NodeId(31) => Entry { parent: NodeId(32), dep_node: 4294967040, node: PathSegment(PathSegment { ident: new#0, id: Some(NodeId(31)), def: Some(Err), args: None, infer_types: true }) }
```
We have the right ID when looking for NodeId(31) and try with NodeId(32) (which
is the right thing to look for) from get_path_data, but not for the segments
that we write from `write_sub_paths_truncated`.
Basically `process_path` takes an id which is always the parent, and that we
fall back to in `get_path_data()`, so we get the right result for the last path
segment, but not for the other segments that get written to from
`write_sub_paths_truncated`.
I think we can stop passing the explicit id around to `get_path_data` now, will
consider sending that as a followup.
2019-01-09 17:31:16 +01:00
|
|
|
}
|
2019-02-07 16:03:57 +11:00
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(&hir::Expr {
|
2018-07-11 20:05:29 +08:00
|
|
|
node: hir::ExprKind::Struct(ref qpath, ..),
|
2017-11-08 10:43:05 +13:00
|
|
|
..
|
2019-02-07 16:03:57 +11:00
|
|
|
}) => {
|
2019-04-20 19:36:05 +03:00
|
|
|
self.tables.qpath_res(qpath, hir_id)
|
2019-02-07 16:03:57 +11:00
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Expr(&hir::Expr {
|
2018-07-11 20:05:29 +08:00
|
|
|
node: hir::ExprKind::Path(ref qpath),
|
2017-11-08 10:43:05 +13:00
|
|
|
..
|
|
|
|
}) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Pat(&hir::Pat {
|
2017-11-08 10:43:05 +13:00
|
|
|
node: hir::PatKind::Path(ref qpath),
|
|
|
|
..
|
|
|
|
}) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Pat(&hir::Pat {
|
2017-11-08 10:43:05 +13:00
|
|
|
node: hir::PatKind::Struct(ref qpath, ..),
|
|
|
|
..
|
|
|
|
}) |
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Pat(&hir::Pat {
|
2017-11-08 10:43:05 +13:00
|
|
|
node: hir::PatKind::TupleStruct(ref qpath, ..),
|
|
|
|
..
|
2019-04-11 23:53:59 +02:00
|
|
|
}) |
|
|
|
|
Node::Ty(&hir::Ty {
|
|
|
|
node: hir::TyKind::Path(ref qpath),
|
|
|
|
..
|
2017-11-08 10:43:05 +13:00
|
|
|
}) => {
|
2019-04-20 19:36:05 +03:00
|
|
|
self.tables.qpath_res(qpath, hir_id)
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 15:56:16 +01:00
|
|
|
Node::Binding(&hir::Pat {
|
2017-11-08 10:43:05 +13:00
|
|
|
node: hir::PatKind::Binding(_, canonical_id, ..),
|
|
|
|
..
|
2019-04-20 19:36:05 +03:00
|
|
|
}) => Res::Local(canonical_id),
|
2016-11-25 13:21:19 +02:00
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
_ => Res::Err,
|
2016-11-24 07:50:22 +13:00
|
|
|
}
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2016-11-24 07:50:22 +13:00
|
|
|
|
2018-11-19 16:28:57 +13:00
|
|
|
pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Ref> {
|
|
|
|
path.segments
|
|
|
|
.last()
|
|
|
|
.and_then(|seg| {
|
|
|
|
self.get_path_segment_data(seg)
|
|
|
|
.or_else(|| self.get_path_segment_data_with_id(seg, id))
|
|
|
|
})
|
2018-09-10 15:26:47 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_path_segment_data(&self, path_seg: &ast::PathSegment) -> Option<Ref> {
|
2018-11-19 16:28:57 +13:00
|
|
|
self.get_path_segment_data_with_id(path_seg, path_seg.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path_segment_data_with_id(
|
|
|
|
&self,
|
|
|
|
path_seg: &ast::PathSegment,
|
|
|
|
id: NodeId,
|
|
|
|
) -> Option<Ref> {
|
2017-11-01 17:59:06 +13:00
|
|
|
// Returns true if the path is function type sugar, e.g., `Fn(A) -> B`.
|
2018-09-10 15:26:47 +12:00
|
|
|
fn fn_type(seg: &ast::PathSegment) -> bool {
|
|
|
|
if let Some(ref generic_args) = seg.args {
|
2018-05-16 12:57:45 +01:00
|
|
|
if let ast::GenericArgs::Parenthesized(_) = **generic_args {
|
2017-11-01 17:59:06 +13:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2018-11-19 16:28:57 +13:00
|
|
|
if id == DUMMY_NODE_ID {
|
2018-10-11 21:15:18 +13:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-06-21 08:57:34 +02:00
|
|
|
let res = self.get_path_res(id);
|
2018-09-10 15:26:47 +12:00
|
|
|
let span = path_seg.ident.span;
|
|
|
|
filter!(self.span_utils, span);
|
|
|
|
let span = self.span_from_span(span);
|
2017-11-06 15:52:42 +13:00
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
match res {
|
2019-05-28 18:06:01 +03:00
|
|
|
Res::Local(id) => {
|
2017-04-29 14:39:47 +03:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Variable,
|
|
|
|
span,
|
2019-04-03 09:07:45 +02:00
|
|
|
ref_id: id_from_node_id(self.tcx.hir().hir_to_node_id(id), self),
|
2017-04-29 14:39:47 +03:00
|
|
|
})
|
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Trait, def_id) if fn_type(path_seg) => {
|
2018-09-10 12:54:36 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Type,
|
2018-09-10 15:26:47 +12:00
|
|
|
span,
|
2018-09-10 12:54:36 +12:00
|
|
|
ref_id: id_from_def_id(def_id),
|
2017-11-01 17:59:06 +13:00
|
|
|
})
|
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Struct, def_id) |
|
|
|
|
Res::Def(HirDefKind::Variant, def_id) |
|
|
|
|
Res::Def(HirDefKind::Union, def_id) |
|
|
|
|
Res::Def(HirDefKind::Enum, def_id) |
|
|
|
|
Res::Def(HirDefKind::TyAlias, def_id) |
|
|
|
|
Res::Def(HirDefKind::ForeignTy, def_id) |
|
|
|
|
Res::Def(HirDefKind::TraitAlias, def_id) |
|
2019-05-19 16:26:08 +08:00
|
|
|
Res::Def(HirDefKind::AssocExistential, def_id) |
|
|
|
|
Res::Def(HirDefKind::AssocTy, def_id) |
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Trait, def_id) |
|
|
|
|
Res::Def(HirDefKind::Existential, def_id) |
|
|
|
|
Res::Def(HirDefKind::TyParam, def_id) => {
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Type,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id),
|
|
|
|
})
|
2015-07-08 14:30:18 +12:00
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::ConstParam, def_id) => {
|
2019-02-05 16:51:49 +01:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Variable,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id),
|
|
|
|
})
|
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Ctor(CtorOf::Struct, ..), def_id) => {
|
2017-11-02 11:28:13 +13:00
|
|
|
// This is a reference to a tuple struct where the def_id points
|
|
|
|
// to an invisible constructor function. That is not a very useful
|
|
|
|
// def, so adjust to point to the tuple struct itself.
|
2018-12-19 12:20:59 +02:00
|
|
|
let parent_def_id = self.tcx.parent(def_id).unwrap();
|
2017-11-02 11:28:13 +13:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Type,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(parent_def_id),
|
|
|
|
})
|
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Static, _) |
|
|
|
|
Res::Def(HirDefKind::Const, _) |
|
2019-05-19 16:26:08 +08:00
|
|
|
Res::Def(HirDefKind::AssocConst, _) |
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Ctor(..), _) => {
|
2019-03-21 23:38:50 +01:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Variable,
|
|
|
|
span,
|
2019-04-20 19:36:05 +03:00
|
|
|
ref_id: id_from_def_id(res.def_id()),
|
2019-03-21 23:38:50 +01:00
|
|
|
})
|
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Method, decl_id) => {
|
2015-08-16 09:06:23 -04:00
|
|
|
let def_id = if decl_id.is_local() {
|
2016-11-10 02:06:34 +02:00
|
|
|
let ti = self.tcx.associated_item(decl_id);
|
2017-11-08 10:43:05 +13:00
|
|
|
self.tcx
|
|
|
|
.associated_items(ti.container.id())
|
2018-06-10 22:24:24 +03:00
|
|
|
.find(|item| item.ident.name == ti.ident.name &&
|
|
|
|
item.defaultness.has_value())
|
2016-11-10 02:06:34 +02:00
|
|
|
.map(|item| item.def_id)
|
2015-07-08 14:30:18 +12:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Function,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id.unwrap_or(decl_id)),
|
|
|
|
})
|
2015-09-02 15:37:07 +12:00
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Fn, def_id) => {
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Function,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id),
|
|
|
|
})
|
2015-07-08 14:30:18 +12:00
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::Def(HirDefKind::Mod, def_id) => {
|
2017-06-08 14:45:15 +12:00
|
|
|
Some(Ref {
|
|
|
|
kind: RefKind::Mod,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(def_id),
|
|
|
|
})
|
2015-07-25 17:05:34 +12:00
|
|
|
}
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::PrimTy(..) |
|
|
|
|
Res::SelfTy(..) |
|
|
|
|
Res::Def(HirDefKind::Macro(..), _) |
|
|
|
|
Res::ToolMod |
|
|
|
|
Res::NonMacroAttr(..) |
|
|
|
|
Res::SelfCtor(..) |
|
|
|
|
Res::Err => None,
|
2015-07-08 14:30:18 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
pub fn get_field_ref_data(
|
|
|
|
&self,
|
|
|
|
field_ref: &ast::Field,
|
|
|
|
variant: &ty::VariantDef,
|
|
|
|
) -> Option<Ref> {
|
2018-09-10 12:54:36 +12:00
|
|
|
filter!(self.span_utils, field_ref.ident.span);
|
2018-09-12 14:05:08 +12:00
|
|
|
self.tcx.find_field_index(field_ref.ident, variant).map(|index| {
|
|
|
|
let span = self.span_from_span(field_ref.ident.span);
|
|
|
|
Ref {
|
|
|
|
kind: RefKind::Variable,
|
|
|
|
span,
|
|
|
|
ref_id: id_from_def_id(variant.fields[index].did),
|
|
|
|
}
|
2016-01-22 11:58:09 +13:00
|
|
|
})
|
2015-06-09 09:51:54 +12:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
/// Attempt to return MacroRef for any AST node.
|
2016-01-29 20:22:55 +13:00
|
|
|
///
|
|
|
|
/// For a given piece of AST defined by the supplied Span and NodeId,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// returns `None` if the node is not macro-generated or the span is malformed,
|
2017-06-08 14:45:15 +12:00
|
|
|
/// else uses the expansion callsite and callee to return some MacroRef.
|
|
|
|
pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
|
2016-01-29 20:22:55 +13:00
|
|
|
if !generated_code(span) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Note we take care to use the source callsite/callee, to handle
|
|
|
|
// nested expansions and ensure we only generate data for source-visible
|
|
|
|
// macro uses.
|
2017-03-17 04:04:41 +00:00
|
|
|
let callsite = span.source_callsite();
|
2017-06-08 14:45:15 +12:00
|
|
|
let callsite_span = self.span_from_span(callsite);
|
2017-10-15 23:27:17 +02:00
|
|
|
let callee = span.source_callee()?;
|
2018-06-23 21:41:39 +03:00
|
|
|
let callee_span = callee.def_site?;
|
2016-01-29 20:22:55 +13:00
|
|
|
|
|
|
|
// Ignore attribute macros, their spans are usually mangled
|
|
|
|
if let MacroAttribute(_) = callee.format {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callee is an imported macro from an external crate, need to get
|
|
|
|
// the source span and name from the session, as their spans are localized
|
|
|
|
// when read in, and no longer correspond to the source.
|
2017-11-08 10:43:05 +13:00
|
|
|
if let Some(mac) = self.tcx
|
|
|
|
.sess
|
|
|
|
.imported_macro_spans
|
|
|
|
.borrow()
|
|
|
|
.get(&callee_span)
|
|
|
|
{
|
2016-01-29 20:22:55 +13:00
|
|
|
let &(ref mac_name, mac_span) = mac;
|
2017-06-08 14:45:15 +12:00
|
|
|
let mac_span = self.span_from_span(mac_span);
|
|
|
|
return Some(MacroRef {
|
|
|
|
span: callsite_span,
|
|
|
|
qualname: mac_name.clone(), // FIXME: generate the real qualname
|
|
|
|
callee_span: mac_span,
|
|
|
|
});
|
2016-01-29 20:22:55 +13:00
|
|
|
}
|
|
|
|
|
2017-06-08 14:45:15 +12:00
|
|
|
let callee_span = self.span_from_span(callee_span);
|
|
|
|
Some(MacroRef {
|
|
|
|
span: callsite_span,
|
2018-06-23 21:41:39 +03:00
|
|
|
qualname: callee.format.name().to_string(), // FIXME: generate the real qualname
|
2017-06-08 14:45:15 +12:00
|
|
|
callee_span,
|
2016-01-29 20:22:55 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-05 17:50:04 +12:00
|
|
|
fn lookup_ref_id(&self, ref_id: NodeId) -> Option<DefId> {
|
2019-06-21 08:57:34 +02:00
|
|
|
match self.get_path_res(ref_id) {
|
2019-04-20 19:36:05 +03:00
|
|
|
Res::PrimTy(_) | Res::SelfTy(..) | Res::Err => None,
|
2016-06-03 23:15:00 +03:00
|
|
|
def => Some(def.def_id()),
|
2015-06-05 17:50:04 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 17:54:49 +12:00
|
|
|
fn docs_for_attrs(&self, attrs: &[Attribute]) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
|
|
|
|
for attr in attrs {
|
2019-05-08 13:21:18 +10:00
|
|
|
if attr.check_name(sym::doc) {
|
2017-07-18 17:54:49 +12:00
|
|
|
if let Some(val) = attr.value_str() {
|
|
|
|
if attr.is_sugared_doc {
|
|
|
|
result.push_str(&strip_doc_comment_decoration(&val.as_str()));
|
|
|
|
} else {
|
|
|
|
result.push_str(&val.as_str());
|
|
|
|
}
|
|
|
|
result.push('\n');
|
2018-01-16 13:31:42 -06:00
|
|
|
} else if let Some(meta_list) = attr.meta_item_list() {
|
|
|
|
meta_list.into_iter()
|
2019-05-08 13:21:18 +10:00
|
|
|
.filter(|it| it.check_name(sym::include))
|
2018-01-16 13:31:42 -06:00
|
|
|
.filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
|
|
|
|
.flat_map(|it| it)
|
2019-05-08 13:21:18 +10:00
|
|
|
.filter(|meta| meta.check_name(sym::contents))
|
2018-01-16 13:31:42 -06:00
|
|
|
.filter_map(|meta| meta.value_str())
|
|
|
|
.for_each(|val| {
|
|
|
|
result.push_str(&val.as_str());
|
|
|
|
result.push('\n');
|
|
|
|
});
|
2017-07-18 17:54:49 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.config.full_docs {
|
|
|
|
if let Some(index) = result.find("\n\n") {
|
|
|
|
result.truncate(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2018-02-02 08:29:59 +01:00
|
|
|
|
|
|
|
fn next_impl_id(&self) -> u32 {
|
|
|
|
let next = self.impl_counter.get();
|
|
|
|
self.impl_counter.set(next + 1);
|
|
|
|
next
|
|
|
|
}
|
2015-05-05 18:46:09 +12:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:46:39 -07:00
|
|
|
fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String {
|
2016-09-07 12:37:16 +12:00
|
|
|
let mut sig = "fn ".to_owned();
|
2017-10-16 21:07:26 +02:00
|
|
|
if !generics.params.is_empty() {
|
2016-05-11 13:46:39 -07:00
|
|
|
sig.push('<');
|
2017-11-08 10:43:05 +13:00
|
|
|
sig.push_str(&generics
|
2017-10-16 21:07:26 +02:00
|
|
|
.params
|
2017-11-08 10:43:05 +13:00
|
|
|
.iter()
|
2018-05-27 21:54:10 +01:00
|
|
|
.map(|param| param.ident.to_string())
|
2017-11-08 10:43:05 +13:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "));
|
2016-05-11 13:46:39 -07:00
|
|
|
sig.push_str("> ");
|
|
|
|
}
|
|
|
|
sig.push('(');
|
2017-11-08 10:43:05 +13:00
|
|
|
sig.push_str(&decl.inputs
|
|
|
|
.iter()
|
|
|
|
.map(arg_to_string)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", "));
|
2016-05-11 13:46:39 -07:00
|
|
|
sig.push(')');
|
|
|
|
match decl.output {
|
2016-09-07 12:37:16 +12:00
|
|
|
ast::FunctionRetTy::Default(_) => sig.push_str(" -> ()"),
|
2016-05-11 13:46:39 -07:00
|
|
|
ast::FunctionRetTy::Ty(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
|
|
|
|
}
|
|
|
|
|
|
|
|
sig
|
|
|
|
}
|
|
|
|
|
2017-11-01 17:59:06 +13:00
|
|
|
// An AST visitor for collecting paths (e.g., the names of structs) and formal
|
|
|
|
// variables (idents) from patterns.
|
|
|
|
struct PathCollector<'l> {
|
|
|
|
collected_paths: Vec<(NodeId, &'l ast::Path)>,
|
2018-03-19 03:54:56 +03:00
|
|
|
collected_idents: Vec<(NodeId, ast::Ident, ast::Mutability)>,
|
2015-05-05 22:03:20 +12:00
|
|
|
}
|
|
|
|
|
2017-11-01 17:59:06 +13:00
|
|
|
impl<'l> PathCollector<'l> {
|
|
|
|
fn new() -> PathCollector<'l> {
|
|
|
|
PathCollector {
|
|
|
|
collected_paths: vec![],
|
|
|
|
collected_idents: vec![],
|
|
|
|
}
|
2015-05-05 22:03:20 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 12:33:47 +03:00
|
|
|
impl<'l> Visitor<'l> for PathCollector<'l> {
|
|
|
|
fn visit_pat(&mut self, p: &'l ast::Pat) {
|
2015-05-05 22:03:20 +12:00
|
|
|
match p.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
PatKind::Struct(ref path, ..) => {
|
2017-11-01 17:59:06 +13:00
|
|
|
self.collected_paths.push((p.id, path));
|
2015-05-05 22:03:20 +12:00
|
|
|
}
|
2017-11-08 10:43:05 +13:00
|
|
|
PatKind::TupleStruct(ref path, ..) | PatKind::Path(_, ref path) => {
|
2017-11-01 17:59:06 +13:00
|
|
|
self.collected_paths.push((p.id, path));
|
2015-05-05 22:03:20 +12:00
|
|
|
}
|
2018-03-18 16:47:09 +03:00
|
|
|
PatKind::Ident(bm, ident, _) => {
|
2017-11-08 10:43:05 +13:00
|
|
|
debug!(
|
|
|
|
"PathCollector, visit ident in pat {}: {:?} {:?}",
|
2018-03-18 16:47:09 +03:00
|
|
|
ident,
|
2017-11-08 10:43:05 +13:00
|
|
|
p.span,
|
2018-03-18 16:47:09 +03:00
|
|
|
ident.span
|
2017-11-08 10:43:05 +13:00
|
|
|
);
|
2015-05-05 22:03:20 +12:00
|
|
|
let immut = match bm {
|
|
|
|
// Even if the ref is mut, you can't change the ref, only
|
|
|
|
// the data pointed at, so showing the initialising expression
|
|
|
|
// is still worthwhile.
|
2016-02-09 17:44:47 +01:00
|
|
|
ast::BindingMode::ByRef(_) => ast::Mutability::Immutable,
|
2015-12-18 14:23:01 +01:00
|
|
|
ast::BindingMode::ByValue(mt) => mt,
|
2015-05-05 22:03:20 +12:00
|
|
|
};
|
2017-11-08 10:43:05 +13:00
|
|
|
self.collected_idents
|
2018-03-19 03:54:56 +03:00
|
|
|
.push((p.id, ident, immut));
|
2015-05-05 22:03:20 +12:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
visit::walk_pat(self, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 16:32:49 +13:00
|
|
|
/// Defines what to do with the results of saving the analysis.
|
|
|
|
pub trait SaveHandler {
|
2017-11-08 10:43:05 +13:00
|
|
|
fn save<'l, 'tcx>(
|
|
|
|
&mut self,
|
|
|
|
save_ctxt: SaveContext<'l, 'tcx>,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
cratename: &str,
|
2018-09-24 16:28:53 +02:00
|
|
|
input: &'l Input,
|
2017-11-08 10:43:05 +13:00
|
|
|
);
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
2015-12-22 16:35:02 -05:00
|
|
|
|
2017-03-23 16:32:49 +13:00
|
|
|
/// Dump the save-analysis results to a file.
|
|
|
|
pub struct DumpHandler<'a> {
|
|
|
|
odir: Option<&'a Path>,
|
2017-11-08 10:43:05 +13:00
|
|
|
cratename: String,
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2017-03-23 16:32:49 +13:00
|
|
|
impl<'a> DumpHandler<'a> {
|
2017-07-19 11:52:09 +12:00
|
|
|
pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> {
|
2017-03-23 16:32:49 +13:00
|
|
|
DumpHandler {
|
2017-08-06 22:54:09 -07:00
|
|
|
odir,
|
2017-11-08 10:43:05 +13:00
|
|
|
cratename: cratename.to_owned(),
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
|
|
|
}
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2019-05-22 10:37:33 -07:00
|
|
|
fn output_file(&self, ctx: &SaveContext<'_, '_>) -> (File, PathBuf) {
|
2017-07-18 17:44:19 +12:00
|
|
|
let sess = &ctx.tcx.sess;
|
|
|
|
let file_name = match ctx.config.output_file {
|
|
|
|
Some(ref s) => PathBuf::from(s),
|
|
|
|
None => {
|
|
|
|
let mut root_path = match self.odir {
|
|
|
|
Some(val) => val.join("save-analysis"),
|
|
|
|
None => PathBuf::from("save-analysis-temp"),
|
|
|
|
};
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2017-07-18 17:44:19 +12:00
|
|
|
if let Err(e) = std::fs::create_dir_all(&root_path) {
|
|
|
|
error!("Could not create directory {}: {}", root_path.display(), e);
|
|
|
|
}
|
2017-03-23 16:32:49 +13:00
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
let executable = sess.crate_types
|
|
|
|
.borrow()
|
|
|
|
.iter()
|
2018-07-26 11:13:11 -06:00
|
|
|
.any(|ct| *ct == CrateType::Executable);
|
2017-07-18 17:44:19 +12:00
|
|
|
let mut out_name = if executable {
|
2018-08-23 10:14:52 +02:00
|
|
|
String::new()
|
2017-07-18 17:44:19 +12:00
|
|
|
} else {
|
|
|
|
"lib".to_owned()
|
|
|
|
};
|
|
|
|
out_name.push_str(&self.cratename);
|
|
|
|
out_name.push_str(&sess.opts.cg.extra_filename);
|
2017-07-19 11:52:09 +12:00
|
|
|
out_name.push_str(".json");
|
2017-07-18 17:44:19 +12:00
|
|
|
root_path.push(&out_name);
|
2017-03-23 16:32:49 +13:00
|
|
|
|
2017-07-18 17:44:19 +12:00
|
|
|
root_path
|
|
|
|
}
|
2017-03-23 16:32:49 +13:00
|
|
|
};
|
2017-07-18 17:44:19 +12:00
|
|
|
|
|
|
|
info!("Writing output to {}", file_name.display());
|
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
let output_file = File::create(&file_name).unwrap_or_else(
|
|
|
|
|e| sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)),
|
|
|
|
);
|
2017-07-18 17:44:19 +12:00
|
|
|
|
2019-05-22 10:37:33 -07:00
|
|
|
(output_file, file_name)
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> SaveHandler for DumpHandler<'a> {
|
2017-11-08 10:43:05 +13:00
|
|
|
fn save<'l, 'tcx>(
|
|
|
|
&mut self,
|
|
|
|
save_ctxt: SaveContext<'l, 'tcx>,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
cratename: &str,
|
2018-09-24 16:28:53 +02:00
|
|
|
input: &'l Input,
|
2017-11-08 10:43:05 +13:00
|
|
|
) {
|
2019-05-22 10:37:33 -07:00
|
|
|
let sess = &save_ctxt.tcx.sess;
|
|
|
|
let file_name = {
|
|
|
|
let (mut output, file_name) = self.output_file(&save_ctxt);
|
|
|
|
let mut dumper = JsonDumper::new(&mut output, save_ctxt.config.clone());
|
|
|
|
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2019-05-22 10:37:33 -07:00
|
|
|
visitor.dump_crate_info(cratename, krate);
|
|
|
|
visitor.dump_compilation_options(input, cratename);
|
|
|
|
visit::walk_crate(&mut visitor, krate);
|
|
|
|
|
|
|
|
file_name
|
|
|
|
};
|
|
|
|
|
|
|
|
if sess.opts.debugging_opts.emit_artifact_notifications {
|
|
|
|
sess.parse_sess.span_diagnostic
|
|
|
|
.emit_artifact_notification(&file_name, "save-analysis");
|
|
|
|
}
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2017-03-23 16:32:49 +13:00
|
|
|
/// Call a callback with the results of save-analysis.
|
|
|
|
pub struct CallbackHandler<'b> {
|
2018-07-12 10:26:22 +02:00
|
|
|
pub callback: &'b mut dyn FnMut(&rls_data::Analysis),
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'b> SaveHandler for CallbackHandler<'b> {
|
2017-11-08 10:43:05 +13:00
|
|
|
fn save<'l, 'tcx>(
|
|
|
|
&mut self,
|
|
|
|
save_ctxt: SaveContext<'l, 'tcx>,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
cratename: &str,
|
2018-09-24 16:28:53 +02:00
|
|
|
input: &'l Input,
|
2017-11-08 10:43:05 +13:00
|
|
|
) {
|
2017-03-23 16:32:49 +13:00
|
|
|
// We're using the JsonDumper here because it has the format of the
|
|
|
|
// save-analysis results that we will pass to the callback. IOW, we are
|
|
|
|
// using the JsonDumper to collect the save-analysis results, but not
|
|
|
|
// actually to dump them to a file. This is all a bit convoluted and
|
|
|
|
// there is certainly a simpler design here trying to get out (FIXME).
|
2017-07-19 11:52:09 +12:00
|
|
|
let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
|
|
|
|
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
|
|
|
|
|
|
|
|
visitor.dump_crate_info(cratename, krate);
|
2018-09-24 16:28:53 +02:00
|
|
|
visitor.dump_compilation_options(input, cratename);
|
2017-07-19 11:52:09 +12:00
|
|
|
visit::walk_crate(&mut visitor, krate);
|
2017-03-23 16:32:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:43:05 +13:00
|
|
|
pub fn process_crate<'l, 'tcx, H: SaveHandler>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-11-08 10:43:05 +13:00
|
|
|
krate: &ast::Crate,
|
|
|
|
cratename: &str,
|
2018-09-24 16:28:53 +02:00
|
|
|
input: &'l Input,
|
2017-11-08 10:43:05 +13:00
|
|
|
config: Option<Config>,
|
|
|
|
mut handler: H,
|
|
|
|
) {
|
2017-12-28 06:05:45 +01:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
info!("Dumping crate {}", cratename);
|
2017-03-23 16:32:49 +13:00
|
|
|
|
2019-01-07 23:55:55 +01:00
|
|
|
// Privacy checking requires and is done after type checking; use a
|
|
|
|
// fallback in case the access levels couldn't have been correctly computed.
|
|
|
|
let access_levels = match tcx.sess.compile_status() {
|
|
|
|
Ok(..) => tcx.privacy_access_levels(LOCAL_CRATE),
|
2018-12-01 15:34:04 +01:00
|
|
|
Err(..) => tcx.arena.alloc(AccessLevels::default()),
|
2019-01-07 23:55:55 +01:00
|
|
|
};
|
|
|
|
|
2017-12-28 06:05:45 +01:00
|
|
|
let save_ctxt = SaveContext {
|
|
|
|
tcx,
|
|
|
|
tables: &ty::TypeckTables::empty(None),
|
2019-01-07 23:55:55 +01:00
|
|
|
access_levels: &access_levels,
|
2017-12-28 06:05:45 +01:00
|
|
|
span_utils: SpanUtils::new(&tcx.sess),
|
|
|
|
config: find_config(config),
|
2018-02-02 08:29:59 +01:00
|
|
|
impl_counter: Cell::new(0),
|
2017-12-28 06:05:45 +01:00
|
|
|
};
|
2014-02-05 17:31:33 +13:00
|
|
|
|
2018-09-24 16:28:53 +02:00
|
|
|
handler.save(save_ctxt, krate, cratename, input)
|
2017-12-28 06:05:45 +01:00
|
|
|
})
|
2014-02-05 17:31:33 +13:00
|
|
|
}
|
2015-05-04 22:33:07 +12:00
|
|
|
|
2017-07-18 17:44:19 +12:00
|
|
|
fn find_config(supplied: Option<Config>) -> Config {
|
|
|
|
if let Some(config) = supplied {
|
|
|
|
return config;
|
|
|
|
}
|
2019-04-17 21:34:35 +02:00
|
|
|
|
2017-07-18 17:44:19 +12:00
|
|
|
match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") {
|
|
|
|
None => Config::default(),
|
2019-04-17 21:34:35 +02:00
|
|
|
Some(config) => config.to_str()
|
|
|
|
.ok_or(())
|
|
|
|
.map_err(|_| error!("`RUST_SAVE_ANALYSIS_CONFIG` isn't UTF-8"))
|
|
|
|
.and_then(|cfg| serde_json::from_str(cfg)
|
|
|
|
.map_err(|_| error!("Could not deserialize save-analysis config"))
|
|
|
|
).unwrap_or_default()
|
2017-07-18 17:44:19 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 22:33:07 +12:00
|
|
|
// Utility functions for the module.
|
|
|
|
|
|
|
|
// Helper function to escape quotes in a string
|
|
|
|
fn escape(s: String) -> String {
|
|
|
|
s.replace("\"", "\"\"")
|
|
|
|
}
|
|
|
|
|
2016-01-22 11:58:09 +13:00
|
|
|
// Helper function to determine if a span came from a
|
|
|
|
// macro expansion or syntax extension.
|
2017-05-31 16:13:27 +12:00
|
|
|
fn generated_code(span: Span) -> bool {
|
2018-06-25 01:00:21 +03:00
|
|
|
span.ctxt() != NO_EXPANSION || span.is_dummy()
|
2015-05-04 22:33:07 +12:00
|
|
|
}
|
2017-05-31 16:13:27 +12:00
|
|
|
|
|
|
|
// DefId::index is a newtype and so the JSON serialisation is ugly. Therefore
|
|
|
|
// we use our own Id which is the same, but without the newtype.
|
|
|
|
fn id_from_def_id(id: DefId) -> rls_data::Id {
|
|
|
|
rls_data::Id {
|
|
|
|
krate: id.krate.as_u32(),
|
2019-05-18 13:19:33 +02:00
|
|
|
index: id.index.as_u32(),
|
2017-05-31 16:13:27 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 01:02:00 +09:00
|
|
|
fn id_from_node_id(id: NodeId, scx: &SaveContext<'_, '_>) -> rls_data::Id {
|
2018-12-04 13:45:36 +01:00
|
|
|
let def_id = scx.tcx.hir().opt_local_def_id(id);
|
2017-04-29 14:39:47 +03:00
|
|
|
def_id.map(|id| id_from_def_id(id)).unwrap_or_else(|| {
|
|
|
|
// Create a *fake* `DefId` out of a `NodeId` by subtracting the `NodeId`
|
|
|
|
// out of the maximum u32 value. This will work unless you have *billions*
|
|
|
|
// of definitions in a single crate (very unlikely to actually happen).
|
|
|
|
rls_data::Id {
|
|
|
|
krate: LOCAL_CRATE.as_u32(),
|
|
|
|
index: !id.as_u32(),
|
|
|
|
}
|
|
|
|
})
|
2017-06-08 14:45:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
fn null_id() -> rls_data::Id {
|
|
|
|
rls_data::Id {
|
|
|
|
krate: u32::max_value(),
|
|
|
|
index: u32::max_value(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 01:02:00 +09:00
|
|
|
fn lower_attributes(attrs: Vec<Attribute>, scx: &SaveContext<'_, '_>) -> Vec<rls_data::Attribute> {
|
2017-06-08 14:45:15 +12:00
|
|
|
attrs.into_iter()
|
|
|
|
// Only retain real attributes. Doc comments are lowered separately.
|
2019-05-07 16:03:44 +10:00
|
|
|
.filter(|attr| attr.path != sym::doc)
|
2017-06-08 14:45:15 +12:00
|
|
|
.map(|mut attr| {
|
|
|
|
// Remove the surrounding '#[..]' or '#![..]' of the pretty printed
|
|
|
|
// attribute. First normalize all inner attribute (#![..]) to outer
|
|
|
|
// ones (#[..]), then remove the two leading and the one trailing character.
|
|
|
|
attr.style = ast::AttrStyle::Outer;
|
|
|
|
let value = pprust::attribute_to_string(&attr);
|
|
|
|
// This str slicing works correctly, because the leading and trailing characters
|
|
|
|
// are in the ASCII range and thus exactly one byte each.
|
|
|
|
let value = value[2..value.len()-1].to_string();
|
|
|
|
|
|
|
|
rls_data::Attribute {
|
2017-08-06 22:54:09 -07:00
|
|
|
value,
|
2017-06-08 14:45:15 +12:00
|
|
|
span: scx.span_from_span(attr.span),
|
|
|
|
}
|
|
|
|
}).collect()
|
2017-05-31 16:13:27 +12:00
|
|
|
}
|