Remove HirId -> LocalDefId
map from HIR.
This commit is contained in:
parent
3175d03d3b
commit
15d6325747
46 changed files with 321 additions and 382 deletions
|
@ -211,7 +211,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
None => continue,
|
||||
};
|
||||
if !self.span.filter_generated(ident.span) {
|
||||
let id = id_from_hir_id(hir_id, &self.save_ctxt);
|
||||
let id = id_from_hir_id(hir_id);
|
||||
let span = self.span_from_span(ident.span);
|
||||
|
||||
self.dumper.dump_def(
|
||||
|
@ -240,17 +240,17 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
&mut self,
|
||||
sig: &'tcx hir::FnSig<'tcx>,
|
||||
body: Option<hir::BodyId>,
|
||||
def_id: LocalDefId,
|
||||
owner_id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
generics: &'tcx hir::Generics<'tcx>,
|
||||
span: Span,
|
||||
) {
|
||||
debug!("process_method: {:?}:{}", def_id, ident);
|
||||
debug!("process_method: {:?}:{}", owner_id, ident);
|
||||
|
||||
let map = self.tcx.hir();
|
||||
let hir_id = map.local_def_id_to_hir_id(def_id);
|
||||
self.nest_typeck_results(def_id, |v| {
|
||||
if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) {
|
||||
let hir_id: hir::HirId = owner_id.into();
|
||||
self.nest_typeck_results(owner_id.def_id, |v| {
|
||||
if let Some(mut method_data) = v.save_ctxt.get_method_data(owner_id, ident, span) {
|
||||
if let Some(body) = body {
|
||||
v.process_formals(map.body(body).params, &method_data.qualname);
|
||||
}
|
||||
|
@ -258,9 +258,10 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
|
||||
method_data.value =
|
||||
fn_to_string(sig.decl, sig.header, Some(ident.name), generics, &[], None);
|
||||
method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
|
||||
method_data.sig =
|
||||
sig::method_signature(owner_id, ident, generics, sig, &v.save_ctxt);
|
||||
|
||||
v.dumper.dump_def(&access_from!(v.save_ctxt, def_id), method_data);
|
||||
v.dumper.dump_def(&access_from!(v.save_ctxt, owner_id.def_id), method_data);
|
||||
}
|
||||
|
||||
// walk arg and return types
|
||||
|
@ -282,14 +283,11 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
fn process_struct_field_def(
|
||||
&mut self,
|
||||
field: &'tcx hir::FieldDef<'tcx>,
|
||||
parent_id: hir::HirId,
|
||||
parent_id: LocalDefId,
|
||||
) {
|
||||
let field_data = self.save_ctxt.get_field_data(field, parent_id);
|
||||
if let Some(field_data) = field_data {
|
||||
self.dumper.dump_def(
|
||||
&access_from!(self.save_ctxt, self.tcx.hir().local_def_id(field.hir_id)),
|
||||
field_data,
|
||||
);
|
||||
self.dumper.dump_def(&access_from!(self.save_ctxt, field.def_id), field_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,7 +307,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
// Append $id to name to make sure each one is unique.
|
||||
let qualname = format!("{}::{}${}", prefix, name, id);
|
||||
if !self.span.filter_generated(param_ss) {
|
||||
let id = id_from_hir_id(param.hir_id, &self.save_ctxt);
|
||||
let id = id_from_def_id(param.def_id.to_def_id());
|
||||
let span = self.span_from_span(param_ss);
|
||||
|
||||
self.dumper.dump_def(
|
||||
|
@ -387,25 +385,24 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
|
||||
fn process_assoc_const(
|
||||
&mut self,
|
||||
def_id: LocalDefId,
|
||||
owner_id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
typ: &'tcx hir::Ty<'tcx>,
|
||||
expr: Option<&'tcx hir::Expr<'tcx>>,
|
||||
parent_id: DefId,
|
||||
attrs: &'tcx [ast::Attribute],
|
||||
) {
|
||||
let qualname = format!("::{}", self.tcx.def_path_str(def_id.to_def_id()));
|
||||
let qualname = format!("::{}", self.tcx.def_path_str(owner_id.to_def_id()));
|
||||
|
||||
if !self.span.filter_generated(ident.span) {
|
||||
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
|
||||
let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
|
||||
let sig = sig::assoc_const_signature(owner_id, ident.name, typ, expr, &self.save_ctxt);
|
||||
let span = self.span_from_span(ident.span);
|
||||
|
||||
self.dumper.dump_def(
|
||||
&access_from!(self.save_ctxt, def_id),
|
||||
&access_from!(self.save_ctxt, owner_id.def_id),
|
||||
Def {
|
||||
kind: DefKind::Const,
|
||||
id: id_from_hir_id(hir_id, &self.save_ctxt),
|
||||
id: id_from_def_id(owner_id.to_def_id()),
|
||||
span,
|
||||
name: ident.name.to_string(),
|
||||
qualname,
|
||||
|
@ -421,7 +418,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
}
|
||||
|
||||
// walk type and init value
|
||||
self.nest_typeck_results(def_id, |v| {
|
||||
self.nest_typeck_results(owner_id.def_id, |v| {
|
||||
v.visit_ty(typ);
|
||||
if let Some(expr) = expr {
|
||||
v.visit_expr(expr);
|
||||
|
@ -456,8 +453,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
if include_priv_fields {
|
||||
return Some(f.ident.to_string());
|
||||
}
|
||||
let def_id = self.save_ctxt.tcx.hir().local_def_id(f.hir_id);
|
||||
if self.save_ctxt.tcx.visibility(def_id).is_public() {
|
||||
if self.save_ctxt.tcx.visibility(f.def_id).is_public() {
|
||||
Some(f.ident.to_string())
|
||||
} else {
|
||||
None
|
||||
|
@ -466,7 +462,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let value = format!("{} {{ {} }}", name, fields_str);
|
||||
(value, fields.iter().map(|f| id_from_hir_id(f.hir_id, &self.save_ctxt)).collect())
|
||||
(value, fields.iter().map(|f| id_from_def_id(f.def_id.to_def_id())).collect())
|
||||
}
|
||||
_ => (String::new(), vec![]),
|
||||
};
|
||||
|
@ -495,7 +491,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
|
||||
self.nest_typeck_results(item.owner_id.def_id, |v| {
|
||||
for field in def.fields() {
|
||||
v.process_struct_field_def(field, item.hir_id());
|
||||
v.process_struct_field_def(field, item.owner_id.def_id);
|
||||
v.visit_ty(&field.ty);
|
||||
}
|
||||
|
||||
|
@ -529,7 +525,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
|
||||
if !self.span.filter_generated(name_span) {
|
||||
let span = self.span_from_span(name_span);
|
||||
let id = id_from_hir_id(variant.hir_id, &self.save_ctxt);
|
||||
let id = id_from_def_id(variant.def_id.to_def_id());
|
||||
let parent = Some(id_from_def_id(item.owner_id.to_def_id()));
|
||||
let attrs = self.tcx.hir().attrs(variant.hir_id);
|
||||
|
||||
|
@ -567,7 +563,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
}
|
||||
if !self.span.filter_generated(name_span) {
|
||||
let span = self.span_from_span(name_span);
|
||||
let id = id_from_hir_id(variant.hir_id, &self.save_ctxt);
|
||||
let id = id_from_def_id(variant.def_id.to_def_id());
|
||||
let parent = Some(id_from_def_id(item.owner_id.to_def_id()));
|
||||
let attrs = self.tcx.hir().attrs(variant.hir_id);
|
||||
|
||||
|
@ -593,7 +589,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
}
|
||||
|
||||
for field in variant.data.fields() {
|
||||
self.process_struct_field_def(field, variant.hir_id);
|
||||
self.process_struct_field_def(field, variant.def_id);
|
||||
self.visit_ty(field.ty);
|
||||
}
|
||||
}
|
||||
|
@ -883,7 +879,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
// Rust uses the id of the pattern for var lookups, so we'll use it too.
|
||||
if !self.span.filter_generated(ident.span) {
|
||||
let qualname = format!("{}${}", ident, hir_id);
|
||||
let id = id_from_hir_id(hir_id, &self.save_ctxt);
|
||||
let id = id_from_hir_id(hir_id);
|
||||
let span = self.span_from_span(ident.span);
|
||||
|
||||
self.dumper.dump_def(
|
||||
|
@ -983,7 +979,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
let body = body.map(|b| self.tcx.hir().body(b).value);
|
||||
let attrs = self.tcx.hir().attrs(trait_item.hir_id());
|
||||
self.process_assoc_const(
|
||||
trait_item.owner_id.def_id,
|
||||
trait_item.owner_id,
|
||||
trait_item.ident,
|
||||
&ty,
|
||||
body,
|
||||
|
@ -997,7 +993,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
self.process_method(
|
||||
sig,
|
||||
body,
|
||||
trait_item.owner_id.def_id,
|
||||
trait_item.owner_id,
|
||||
trait_item.ident,
|
||||
&trait_item.generics,
|
||||
trait_item.span,
|
||||
|
@ -1028,7 +1024,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
decl_id: None,
|
||||
docs: self.save_ctxt.docs_for_attrs(attrs),
|
||||
sig: sig::assoc_type_signature(
|
||||
trait_item.hir_id(),
|
||||
trait_item.owner_id,
|
||||
trait_item.ident,
|
||||
Some(bounds),
|
||||
default_ty.as_deref(),
|
||||
|
@ -1053,7 +1049,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
let body = self.tcx.hir().body(body);
|
||||
let attrs = self.tcx.hir().attrs(impl_item.hir_id());
|
||||
self.process_assoc_const(
|
||||
impl_item.owner_id.def_id,
|
||||
impl_item.owner_id,
|
||||
impl_item.ident,
|
||||
&ty,
|
||||
Some(&body.value),
|
||||
|
@ -1065,7 +1061,7 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
self.process_method(
|
||||
sig,
|
||||
Some(body),
|
||||
impl_item.owner_id.def_id,
|
||||
impl_item.owner_id,
|
||||
impl_item.ident,
|
||||
&impl_item.generics,
|
||||
impl_item.span,
|
||||
|
@ -1081,18 +1077,16 @@ impl<'tcx> DumpVisitor<'tcx> {
|
|||
}
|
||||
|
||||
pub(crate) fn process_crate(&mut self) {
|
||||
let id = hir::CRATE_HIR_ID;
|
||||
let qualname =
|
||||
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
|
||||
let qualname = format!("::{}", self.tcx.def_path_str(CRATE_DEF_ID.to_def_id()));
|
||||
|
||||
let sm = self.tcx.sess.source_map();
|
||||
let krate_mod = self.tcx.hir().root_module();
|
||||
let filename = sm.span_to_filename(krate_mod.spans.inner_span);
|
||||
let data_id = id_from_hir_id(id, &self.save_ctxt);
|
||||
let data_id = id_from_def_id(CRATE_DEF_ID.to_def_id());
|
||||
let children =
|
||||
krate_mod.item_ids.iter().map(|i| id_from_def_id(i.owner_id.to_def_id())).collect();
|
||||
let span = self.span_from_span(krate_mod.spans.inner_span);
|
||||
let attrs = self.tcx.hir().attrs(id);
|
||||
let attrs = self.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
||||
|
||||
self.dumper.dump_def(
|
||||
&Access { public: true, reachable: true },
|
||||
|
@ -1319,7 +1313,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
|
|||
// output the inferred type here? :shrug:
|
||||
hir::ArrayLen::Infer(..) => {}
|
||||
hir::ArrayLen::Body(anon_const) => self
|
||||
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
|
||||
.nest_typeck_results(anon_const.def_id, |v| {
|
||||
v.visit_expr(&map.body(anon_const.body).value)
|
||||
}),
|
||||
}
|
||||
|
@ -1361,7 +1355,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
|
|||
}
|
||||
}
|
||||
}
|
||||
hir::ExprKind::Closure(&hir::Closure { ref fn_decl, body, .. }) => {
|
||||
hir::ExprKind::Closure(&hir::Closure { ref fn_decl, body, def_id, .. }) => {
|
||||
let id = format!("${}", ex.hir_id);
|
||||
|
||||
// walk arg and return types
|
||||
|
@ -1375,7 +1369,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
|
|||
|
||||
// walk the body
|
||||
let map = self.tcx.hir();
|
||||
self.nest_typeck_results(self.tcx.hir().local_def_id(ex.hir_id), |v| {
|
||||
self.nest_typeck_results(def_id, |v| {
|
||||
let body = map.body(body);
|
||||
v.process_formals(body.params, &id);
|
||||
v.visit_expr(&body.value)
|
||||
|
@ -1389,7 +1383,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
|
|||
// output the inferred type here? :shrug:
|
||||
hir::ArrayLen::Infer(..) => {}
|
||||
hir::ArrayLen::Body(anon_const) => self
|
||||
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
|
||||
.nest_typeck_results(anon_const.def_id, |v| {
|
||||
v.visit_expr(&map.body(anon_const.body).value)
|
||||
}),
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ use rustc_ast::util::comments::beautify_doc_string;
|
|||
use rustc_ast_pretty::pprust::attribute_to_string;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{DefKind as HirDefKind, Res};
|
||||
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
||||
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
|
||||
use rustc_hir::intravisit::{self, Visitor};
|
||||
use rustc_hir::Node;
|
||||
use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string};
|
||||
|
@ -318,7 +318,7 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
qualname,
|
||||
value,
|
||||
parent: None,
|
||||
children: def.variants.iter().map(|v| id_from_hir_id(v.hir_id, self)).collect(),
|
||||
children: def.variants.iter().map(|v| id_from_def_id(v.def_id.to_def_id())).collect(),
|
||||
decl_id: None,
|
||||
docs: self.docs_for_attrs(attrs),
|
||||
sig: sig::item_signature(item, self),
|
||||
|
@ -379,12 +379,11 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_field_data(&self, field: &hir::FieldDef<'_>, scope: hir::HirId) -> Option<Def> {
|
||||
pub fn get_field_data(&self, field: &hir::FieldDef<'_>, scope: LocalDefId) -> Option<Def> {
|
||||
let name = field.ident.to_string();
|
||||
let scope_def_id = self.tcx.hir().local_def_id(scope).to_def_id();
|
||||
let qualname = format!("::{}::{}", self.tcx.def_path_str(scope_def_id), field.ident);
|
||||
let qualname = format!("::{}::{}", self.tcx.def_path_str(scope.to_def_id()), field.ident);
|
||||
filter!(self.span_utils, field.ident.span);
|
||||
let field_def_id = self.tcx.hir().local_def_id(field.hir_id).to_def_id();
|
||||
let field_def_id = field.def_id.to_def_id();
|
||||
let typ = self.tcx.type_of(field_def_id).to_string();
|
||||
|
||||
let id = id_from_def_id(field_def_id);
|
||||
|
@ -398,7 +397,7 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
name,
|
||||
qualname,
|
||||
value: typ,
|
||||
parent: Some(id_from_def_id(scope_def_id)),
|
||||
parent: Some(id_from_def_id(scope.to_def_id())),
|
||||
children: vec![],
|
||||
decl_id: None,
|
||||
docs: self.docs_for_attrs(attrs),
|
||||
|
@ -409,12 +408,11 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
|
||||
// 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.
|
||||
pub fn get_method_data(&self, hir_id: hir::HirId, ident: Ident, span: Span) -> Option<Def> {
|
||||
pub fn get_method_data(&self, owner_id: hir::OwnerId, ident: Ident, span: Span) -> Option<Def> {
|
||||
// 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.
|
||||
let def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
|
||||
let (qualname, parent_scope, decl_id, docs, attributes) =
|
||||
match self.tcx.impl_of_method(def_id) {
|
||||
match self.tcx.impl_of_method(owner_id.to_def_id()) {
|
||||
Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
|
||||
Some(Node::Item(item)) => match item.kind {
|
||||
hir::ItemKind::Impl(hir::Impl { ref self_ty, .. }) => {
|
||||
|
@ -427,8 +425,8 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
let trait_id = self.tcx.trait_id_of_impl(impl_id);
|
||||
let mut docs = String::new();
|
||||
let mut attrs = vec![];
|
||||
if let Some(Node::ImplItem(_)) = hir.find(hir_id) {
|
||||
attrs = self.tcx.hir().attrs(hir_id).to_vec();
|
||||
if let Some(Node::ImplItem(_)) = hir.find(owner_id.into()) {
|
||||
attrs = self.tcx.hir().attrs(owner_id.into()).to_vec();
|
||||
docs = self.docs_for_attrs(&attrs);
|
||||
}
|
||||
|
||||
|
@ -452,29 +450,29 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
_ => {
|
||||
span_bug!(
|
||||
span,
|
||||
"Container {:?} for method {} not an impl?",
|
||||
"Container {:?} for method {:?} not an impl?",
|
||||
impl_id,
|
||||
hir_id
|
||||
owner_id,
|
||||
);
|
||||
}
|
||||
},
|
||||
r => {
|
||||
span_bug!(
|
||||
span,
|
||||
"Container {:?} for method {} is not a node item {:?}",
|
||||
"Container {:?} for method {:?} is not a node item {:?}",
|
||||
impl_id,
|
||||
hir_id,
|
||||
owner_id,
|
||||
r
|
||||
);
|
||||
}
|
||||
},
|
||||
None => match self.tcx.trait_of_item(def_id) {
|
||||
None => match self.tcx.trait_of_item(owner_id.to_def_id()) {
|
||||
Some(def_id) => {
|
||||
let mut docs = String::new();
|
||||
let mut attrs = vec![];
|
||||
|
||||
if let Some(Node::TraitItem(_)) = self.tcx.hir().find(hir_id) {
|
||||
attrs = self.tcx.hir().attrs(hir_id).to_vec();
|
||||
if let Some(Node::TraitItem(_)) = self.tcx.hir().find(owner_id.into()) {
|
||||
attrs = self.tcx.hir().attrs(owner_id.into()).to_vec();
|
||||
docs = self.docs_for_attrs(&attrs);
|
||||
}
|
||||
|
||||
|
@ -487,7 +485,7 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
)
|
||||
}
|
||||
None => {
|
||||
debug!("could not find container for method {} at {:?}", hir_id, span);
|
||||
debug!("could not find container for method {:?} at {:?}", owner_id, span);
|
||||
// This is not necessarily a bug, if there was a compilation error,
|
||||
// the typeck results we need might not exist.
|
||||
return None;
|
||||
|
@ -501,7 +499,7 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
|
||||
Some(Def {
|
||||
kind: DefKind::Method,
|
||||
id: id_from_def_id(def_id),
|
||||
id: id_from_def_id(owner_id.to_def_id()),
|
||||
span: self.span_from_span(ident.span),
|
||||
name: ident.name.to_string(),
|
||||
qualname,
|
||||
|
@ -669,7 +667,7 @@ impl<'tcx> SaveContext<'tcx> {
|
|||
|
||||
match res {
|
||||
Res::Local(id) => {
|
||||
Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_hir_id(id, self) })
|
||||
Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_hir_id(id) })
|
||||
}
|
||||
Res::Def(HirDefKind::Trait, def_id) if fn_type(path_seg) => {
|
||||
Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) })
|
||||
|
@ -1033,18 +1031,15 @@ fn id_from_def_id(id: DefId) -> rls_data::Id {
|
|||
rls_data::Id { krate: id.krate.as_u32(), index: id.index.as_u32() }
|
||||
}
|
||||
|
||||
fn id_from_hir_id(id: hir::HirId, scx: &SaveContext<'_>) -> rls_data::Id {
|
||||
let def_id = scx.tcx.hir().opt_local_def_id(id);
|
||||
def_id.map(|id| id_from_def_id(id.to_def_id())).unwrap_or_else(|| {
|
||||
// Create a *fake* `DefId` out of a `HirId` by combining the owner
|
||||
// `local_def_index` and the `local_id`.
|
||||
// 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.owner.def_id.local_def_index.as_u32() | id.local_id.as_u32().reverse_bits(),
|
||||
}
|
||||
})
|
||||
fn id_from_hir_id(id: hir::HirId) -> rls_data::Id {
|
||||
// Create a *fake* `DefId` out of a `HirId` by combining the owner
|
||||
// `local_def_index` and the `local_id`.
|
||||
// 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.owner.def_id.local_def_index.as_u32() | id.local_id.as_u32().reverse_bits(),
|
||||
}
|
||||
}
|
||||
|
||||
fn null_id() -> rls_data::Id {
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
//
|
||||
// FIXME where clauses need implementing, defs/refs in generics are mostly missing.
|
||||
|
||||
use crate::{id_from_def_id, id_from_hir_id, SaveContext};
|
||||
use crate::{id_from_def_id, SaveContext};
|
||||
|
||||
use rls_data::{SigElement, Signature};
|
||||
|
||||
|
@ -34,6 +34,7 @@ use rustc_hir as hir;
|
|||
use rustc_hir::def::{DefKind, Res};
|
||||
use rustc_hir_pretty::id_to_string;
|
||||
use rustc_hir_pretty::{bounds_to_string, path_segment_to_string, path_to_string, ty_to_string};
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::symbol::{Ident, Symbol};
|
||||
|
||||
pub fn item_signature(item: &hir::Item<'_>, scx: &SaveContext<'_>) -> Option<Signature> {
|
||||
|
@ -71,7 +72,7 @@ pub fn variant_signature(variant: &hir::Variant<'_>, scx: &SaveContext<'_>) -> O
|
|||
}
|
||||
|
||||
pub fn method_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
generics: &hir::Generics<'_>,
|
||||
m: &hir::FnSig<'_>,
|
||||
|
@ -84,7 +85,7 @@ pub fn method_signature(
|
|||
}
|
||||
|
||||
pub fn assoc_const_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Symbol,
|
||||
ty: &hir::Ty<'_>,
|
||||
default: Option<&hir::Expr<'_>>,
|
||||
|
@ -97,7 +98,7 @@ pub fn assoc_const_signature(
|
|||
}
|
||||
|
||||
pub fn assoc_type_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
bounds: Option<hir::GenericBounds<'_>>,
|
||||
default: Option<&hir::Ty<'_>>,
|
||||
|
@ -112,7 +113,8 @@ pub fn assoc_type_signature(
|
|||
type Result = std::result::Result<Signature, &'static str>;
|
||||
|
||||
trait Sig {
|
||||
fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result;
|
||||
type Parent;
|
||||
fn make(&self, offset: usize, id: Option<Self::Parent>, scx: &SaveContext<'_>) -> Result;
|
||||
}
|
||||
|
||||
fn extend_sig(
|
||||
|
@ -148,6 +150,7 @@ fn text_sig(text: String) -> Signature {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::Ty<'hir> {
|
||||
type Parent = hir::HirId;
|
||||
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
let id = Some(self.hir_id);
|
||||
match self.kind {
|
||||
|
@ -326,6 +329,7 @@ impl<'hir> Sig for hir::Ty<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::Item<'hir> {
|
||||
type Parent = hir::HirId;
|
||||
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
let id = Some(self.hir_id());
|
||||
|
||||
|
@ -391,7 +395,7 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
text.push_str("fn ");
|
||||
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
|
||||
sig.text.push('(');
|
||||
for i in decl.inputs {
|
||||
|
@ -441,7 +445,7 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
hir::ItemKind::TyAlias(ref ty, ref generics) => {
|
||||
let text = "type ".to_owned();
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
|
||||
sig.text.push_str(" = ");
|
||||
let ty = ty.make(offset + sig.text.len(), id, scx)?;
|
||||
|
@ -453,21 +457,21 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
hir::ItemKind::Enum(_, ref generics) => {
|
||||
let text = "enum ".to_owned();
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
sig.text.push_str(" {}");
|
||||
Ok(sig)
|
||||
}
|
||||
hir::ItemKind::Struct(_, ref generics) => {
|
||||
let text = "struct ".to_owned();
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
sig.text.push_str(" {}");
|
||||
Ok(sig)
|
||||
}
|
||||
hir::ItemKind::Union(_, ref generics) => {
|
||||
let text = "union ".to_owned();
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
sig.text.push_str(" {}");
|
||||
Ok(sig)
|
||||
}
|
||||
|
@ -483,7 +487,7 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
}
|
||||
text.push_str("trait ");
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
|
||||
if !bounds.is_empty() {
|
||||
sig.text.push_str(": ");
|
||||
|
@ -498,7 +502,7 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
let mut text = String::new();
|
||||
text.push_str("trait ");
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
|
||||
if !bounds.is_empty() {
|
||||
sig.text.push_str(" = ");
|
||||
|
@ -532,7 +536,8 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
text.push_str(" const");
|
||||
}
|
||||
|
||||
let generics_sig = generics.make(offset + text.len(), id, scx)?;
|
||||
let generics_sig =
|
||||
generics.make(offset + text.len(), Some(self.owner_id.def_id), scx)?;
|
||||
text.push_str(&generics_sig.text);
|
||||
|
||||
text.push(' ');
|
||||
|
@ -575,6 +580,7 @@ impl<'hir> Sig for hir::Item<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::Path<'hir> {
|
||||
type Parent = hir::HirId;
|
||||
fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
|
||||
|
||||
|
@ -609,7 +615,8 @@ impl<'hir> Sig for hir::Path<'hir> {
|
|||
|
||||
// This does not cover the where clause, which must be processed separately.
|
||||
impl<'hir> Sig for hir::Generics<'hir> {
|
||||
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
type Parent = LocalDefId;
|
||||
fn make(&self, offset: usize, _parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
|
||||
if self.params.is_empty() {
|
||||
return Ok(text_sig(String::new()));
|
||||
}
|
||||
|
@ -624,7 +631,7 @@ impl<'hir> Sig for hir::Generics<'hir> {
|
|||
}
|
||||
param_text.push_str(param.name.ident().as_str());
|
||||
defs.push(SigElement {
|
||||
id: id_from_hir_id(param.hir_id, scx),
|
||||
id: id_from_def_id(param.def_id.to_def_id()),
|
||||
start: offset + text.len(),
|
||||
end: offset + text.len() + param_text.as_str().len(),
|
||||
});
|
||||
|
@ -646,12 +653,13 @@ impl<'hir> Sig for hir::Generics<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::FieldDef<'hir> {
|
||||
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
type Parent = LocalDefId;
|
||||
fn make(&self, offset: usize, _parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
|
||||
let mut text = String::new();
|
||||
|
||||
text.push_str(&self.ident.to_string());
|
||||
let defs = Some(SigElement {
|
||||
id: id_from_hir_id(self.hir_id, scx),
|
||||
id: id_from_def_id(self.def_id.to_def_id()),
|
||||
start: offset,
|
||||
end: offset + text.len(),
|
||||
});
|
||||
|
@ -666,13 +674,14 @@ impl<'hir> Sig for hir::FieldDef<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::Variant<'hir> {
|
||||
fn make(&self, offset: usize, parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
type Parent = LocalDefId;
|
||||
fn make(&self, offset: usize, parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
|
||||
let mut text = self.ident.to_string();
|
||||
match self.data {
|
||||
hir::VariantData::Struct(fields, r) => {
|
||||
let id = parent_id.ok_or("Missing id for Variant's parent")?;
|
||||
let name_def = SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(id.to_def_id()),
|
||||
start: offset,
|
||||
end: offset + text.len(),
|
||||
};
|
||||
|
@ -693,9 +702,9 @@ impl<'hir> Sig for hir::Variant<'hir> {
|
|||
text.push('}');
|
||||
Ok(Signature { text, defs, refs })
|
||||
}
|
||||
hir::VariantData::Tuple(fields, id, _) => {
|
||||
hir::VariantData::Tuple(fields, _, def_id) => {
|
||||
let name_def = SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(def_id.to_def_id()),
|
||||
start: offset,
|
||||
end: offset + text.len(),
|
||||
};
|
||||
|
@ -703,7 +712,7 @@ impl<'hir> Sig for hir::Variant<'hir> {
|
|||
let mut defs = vec![name_def];
|
||||
let mut refs = vec![];
|
||||
for f in fields {
|
||||
let field_sig = f.make(offset + text.len(), Some(id), scx)?;
|
||||
let field_sig = f.make(offset + text.len(), Some(def_id), scx)?;
|
||||
text.push_str(&field_sig.text);
|
||||
text.push_str(", ");
|
||||
defs.extend(field_sig.defs.into_iter());
|
||||
|
@ -712,9 +721,9 @@ impl<'hir> Sig for hir::Variant<'hir> {
|
|||
text.push(')');
|
||||
Ok(Signature { text, defs, refs })
|
||||
}
|
||||
hir::VariantData::Unit(id, _) => {
|
||||
hir::VariantData::Unit(_, def_id) => {
|
||||
let name_def = SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(def_id.to_def_id()),
|
||||
start: offset,
|
||||
end: offset + text.len(),
|
||||
};
|
||||
|
@ -725,6 +734,7 @@ impl<'hir> Sig for hir::Variant<'hir> {
|
|||
}
|
||||
|
||||
impl<'hir> Sig for hir::ForeignItem<'hir> {
|
||||
type Parent = hir::HirId;
|
||||
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
|
||||
let id = Some(self.hir_id());
|
||||
match self.kind {
|
||||
|
@ -733,7 +743,7 @@ impl<'hir> Sig for hir::ForeignItem<'hir> {
|
|||
text.push_str("fn ");
|
||||
|
||||
let mut sig =
|
||||
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?;
|
||||
name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
|
||||
|
||||
sig.text.push('(');
|
||||
for i in decl.inputs {
|
||||
|
@ -797,25 +807,25 @@ fn name_and_generics(
|
|||
mut text: String,
|
||||
offset: usize,
|
||||
generics: &hir::Generics<'_>,
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
name: Ident,
|
||||
scx: &SaveContext<'_>,
|
||||
) -> Result {
|
||||
let name = name.to_string();
|
||||
let def = SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(id.to_def_id()),
|
||||
start: offset + text.len(),
|
||||
end: offset + text.len() + name.len(),
|
||||
};
|
||||
text.push_str(&name);
|
||||
let generics: Signature = generics.make(offset + text.len(), Some(id), scx)?;
|
||||
let generics: Signature = generics.make(offset + text.len(), Some(id.def_id), scx)?;
|
||||
// FIXME where clause
|
||||
let text = format!("{}{}", text, generics.text);
|
||||
Ok(extend_sig(generics, text, vec![def], vec![]))
|
||||
}
|
||||
|
||||
fn make_assoc_type_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
bounds: Option<hir::GenericBounds<'_>>,
|
||||
default: Option<&hir::Ty<'_>>,
|
||||
|
@ -824,7 +834,7 @@ fn make_assoc_type_signature(
|
|||
let mut text = "type ".to_owned();
|
||||
let name = ident.to_string();
|
||||
let mut defs = vec![SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(id.to_def_id()),
|
||||
start: text.len(),
|
||||
end: text.len() + name.len(),
|
||||
}];
|
||||
|
@ -837,7 +847,7 @@ fn make_assoc_type_signature(
|
|||
}
|
||||
if let Some(default) = default {
|
||||
text.push_str(" = ");
|
||||
let ty_sig = default.make(text.len(), Some(id), scx)?;
|
||||
let ty_sig = default.make(text.len(), Some(id.into()), scx)?;
|
||||
text.push_str(&ty_sig.text);
|
||||
defs.extend(ty_sig.defs.into_iter());
|
||||
refs.extend(ty_sig.refs.into_iter());
|
||||
|
@ -847,7 +857,7 @@ fn make_assoc_type_signature(
|
|||
}
|
||||
|
||||
fn make_assoc_const_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Symbol,
|
||||
ty: &hir::Ty<'_>,
|
||||
default: Option<&hir::Expr<'_>>,
|
||||
|
@ -856,7 +866,7 @@ fn make_assoc_const_signature(
|
|||
let mut text = "const ".to_owned();
|
||||
let name = ident.to_string();
|
||||
let mut defs = vec![SigElement {
|
||||
id: id_from_hir_id(id, scx),
|
||||
id: id_from_def_id(id.to_def_id()),
|
||||
start: text.len(),
|
||||
end: text.len() + name.len(),
|
||||
}];
|
||||
|
@ -864,7 +874,7 @@ fn make_assoc_const_signature(
|
|||
text.push_str(&name);
|
||||
text.push_str(": ");
|
||||
|
||||
let ty_sig = ty.make(text.len(), Some(id), scx)?;
|
||||
let ty_sig = ty.make(text.len(), Some(id.into()), scx)?;
|
||||
text.push_str(&ty_sig.text);
|
||||
defs.extend(ty_sig.defs.into_iter());
|
||||
refs.extend(ty_sig.refs.into_iter());
|
||||
|
@ -878,7 +888,7 @@ fn make_assoc_const_signature(
|
|||
}
|
||||
|
||||
fn make_method_signature(
|
||||
id: hir::HirId,
|
||||
id: hir::OwnerId,
|
||||
ident: Ident,
|
||||
generics: &hir::Generics<'_>,
|
||||
m: &hir::FnSig<'_>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue