2019-11-26 08:26:08 -06:00
|
|
|
//! Helper functions for working with def, which don't need to be a separate
|
|
|
|
//! query, but can't be computed directly from `*Data` (ie, which need a `db`).
|
2019-11-27 07:25:01 -06:00
|
|
|
use std::sync::Arc;
|
2019-11-26 08:26:08 -06:00
|
|
|
|
2020-02-07 08:13:15 -06:00
|
|
|
use hir_def::generics::WherePredicateTarget;
|
2019-11-26 07:59:24 -06:00
|
|
|
use hir_def::{
|
2019-11-27 07:25:01 -06:00
|
|
|
adt::VariantData,
|
2019-11-26 07:59:24 -06:00
|
|
|
db::DefDatabase,
|
2020-01-25 16:38:33 -06:00
|
|
|
generics::{GenericParams, TypeParamData, TypeParamProvenance},
|
2019-12-13 05:12:36 -06:00
|
|
|
path::Path,
|
2019-11-26 07:59:24 -06:00
|
|
|
resolver::{HasResolver, TypeNs},
|
|
|
|
type_ref::TypeRef,
|
2019-12-20 04:59:50 -06:00
|
|
|
AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId,
|
2019-11-26 07:59:24 -06:00
|
|
|
};
|
2019-12-13 15:01:06 -06:00
|
|
|
use hir_expand::name::{name, Name};
|
2019-11-26 07:59:24 -06:00
|
|
|
|
|
|
|
fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
|
|
|
let resolver = trait_.resolver(db);
|
|
|
|
// returning the iterator directly doesn't easily work because of
|
|
|
|
// lifetime problems, but since there usually shouldn't be more than a
|
|
|
|
// few direct traits this should be fine (we could even use some kind of
|
|
|
|
// SmallVec if performance is a concern)
|
2020-01-31 08:17:48 -06:00
|
|
|
let generic_params = db.generic_params(trait_.into());
|
|
|
|
let trait_self = generic_params.find_trait_self_param();
|
|
|
|
generic_params
|
2019-11-26 07:59:24 -06:00
|
|
|
.where_predicates
|
|
|
|
.iter()
|
2020-01-31 08:17:48 -06:00
|
|
|
.filter_map(|pred| match &pred.target {
|
2020-02-07 08:13:15 -06:00
|
|
|
WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => {
|
|
|
|
pred.bound.as_path()
|
|
|
|
}
|
|
|
|
WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => {
|
|
|
|
pred.bound.as_path()
|
|
|
|
}
|
2019-11-26 07:59:24 -06:00
|
|
|
_ => None,
|
|
|
|
})
|
2019-12-13 05:12:36 -06:00
|
|
|
.filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) {
|
2019-11-26 07:59:24 -06:00
|
|
|
Some(TypeNs::TraitId(t)) => Some(t),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over the whole super trait hierarchy (including the
|
|
|
|
/// trait itself).
|
2019-11-26 09:02:50 -06:00
|
|
|
pub(super) fn all_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> {
|
2019-11-26 07:59:24 -06:00
|
|
|
// we need to take care a bit here to avoid infinite loops in case of cycles
|
|
|
|
// (i.e. if we have `trait A: B; trait B: A;`)
|
|
|
|
let mut result = vec![trait_];
|
|
|
|
let mut i = 0;
|
|
|
|
while i < result.len() {
|
|
|
|
let t = result[i];
|
|
|
|
// yeah this is quadratic, but trait hierarchies should be flat
|
|
|
|
// enough that this doesn't matter
|
|
|
|
for tt in direct_super_traits(db, t) {
|
|
|
|
if !result.contains(&tt) {
|
|
|
|
result.push(tt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2019-11-26 08:42:21 -06:00
|
|
|
|
2020-02-22 06:14:39 -06:00
|
|
|
/// Finds a path from a trait to one of its super traits. Returns an empty
|
2020-02-21 14:49:02 -06:00
|
|
|
/// vector if there is no path.
|
|
|
|
pub(super) fn find_super_trait_path(
|
|
|
|
db: &impl DefDatabase,
|
|
|
|
trait_: TraitId,
|
2020-02-22 06:14:39 -06:00
|
|
|
super_trait: TraitId,
|
2020-02-21 14:49:02 -06:00
|
|
|
) -> Vec<TraitId> {
|
2020-02-22 06:14:39 -06:00
|
|
|
let mut result = Vec::with_capacity(2);
|
|
|
|
result.push(trait_);
|
|
|
|
return if go(db, super_trait, &mut result) { result } else { Vec::new() };
|
2020-02-21 14:49:02 -06:00
|
|
|
|
2020-02-22 06:14:39 -06:00
|
|
|
fn go(db: &impl DefDatabase, super_trait: TraitId, path: &mut Vec<TraitId>) -> bool {
|
|
|
|
let trait_ = *path.last().unwrap();
|
|
|
|
if trait_ == super_trait {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for tt in direct_super_traits(db, trait_) {
|
|
|
|
if path.contains(&tt) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
path.push(tt);
|
|
|
|
if go(db, super_trait, path) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
path.pop();
|
|
|
|
}
|
2020-02-21 14:49:02 -06:00
|
|
|
}
|
2020-02-22 06:14:39 -06:00
|
|
|
false
|
2020-02-21 14:49:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:02:50 -06:00
|
|
|
pub(super) fn associated_type_by_name_including_super_traits(
|
2019-11-26 08:42:21 -06:00
|
|
|
db: &impl DefDatabase,
|
|
|
|
trait_: TraitId,
|
|
|
|
name: &Name,
|
|
|
|
) -> Option<TypeAliasId> {
|
|
|
|
all_super_traits(db, trait_)
|
|
|
|
.into_iter()
|
|
|
|
.find_map(|t| db.trait_data(t).associated_type_by_name(name))
|
|
|
|
}
|
2019-11-27 07:25:01 -06:00
|
|
|
|
|
|
|
pub(super) fn variant_data(db: &impl DefDatabase, var: VariantId) -> Arc<VariantData> {
|
|
|
|
match var {
|
|
|
|
VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
|
|
|
|
VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
|
|
|
|
VariantId::EnumVariantId(it) => {
|
|
|
|
db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 08:46:02 -06:00
|
|
|
|
|
|
|
/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
|
|
|
|
/// The underlying values are cloned if there are other strong references.
|
|
|
|
pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] {
|
|
|
|
if Arc::get_mut(a).is_none() {
|
|
|
|
*a = a.iter().cloned().collect();
|
|
|
|
}
|
|
|
|
Arc::get_mut(a).unwrap()
|
|
|
|
}
|
2019-12-07 04:50:36 -06:00
|
|
|
|
|
|
|
pub(crate) fn generics(db: &impl DefDatabase, def: GenericDefId) -> Generics {
|
|
|
|
let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def)));
|
|
|
|
Generics { def, params: db.generic_params(def), parent_generics }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Generics {
|
|
|
|
def: GenericDefId,
|
|
|
|
pub(crate) params: Arc<GenericParams>,
|
|
|
|
parent_generics: Option<Box<Generics>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Generics {
|
2020-02-07 08:13:15 -06:00
|
|
|
pub(crate) fn iter<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
|
2019-12-07 04:50:36 -06:00
|
|
|
self.parent_generics
|
|
|
|
.as_ref()
|
|
|
|
.into_iter()
|
2020-02-07 08:13:15 -06:00
|
|
|
.flat_map(|it| {
|
|
|
|
it.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))
|
|
|
|
})
|
|
|
|
.chain(
|
|
|
|
self.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.map(move |(local_id, p)| (TypeParamId { parent: self.def, local_id }, p)),
|
|
|
|
)
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 08:13:15 -06:00
|
|
|
pub(crate) fn iter_parent<'a>(
|
|
|
|
&'a self,
|
|
|
|
) -> impl Iterator<Item = (TypeParamId, &'a TypeParamData)> + 'a {
|
|
|
|
self.parent_generics.as_ref().into_iter().flat_map(|it| {
|
|
|
|
it.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.map(move |(local_id, p)| (TypeParamId { parent: it.def, local_id }, p))
|
|
|
|
})
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
|
|
|
|
2019-12-07 06:05:05 -06:00
|
|
|
pub(crate) fn len(&self) -> usize {
|
|
|
|
self.len_split().0
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
2020-01-25 16:38:33 -06:00
|
|
|
|
2019-12-07 06:05:05 -06:00
|
|
|
/// (total, parents, child)
|
|
|
|
pub(crate) fn len_split(&self) -> (usize, usize, usize) {
|
|
|
|
let parent = self.parent_generics.as_ref().map_or(0, |p| p.len());
|
2019-12-07 13:09:53 -06:00
|
|
|
let child = self.params.types.len();
|
2019-12-07 06:05:05 -06:00
|
|
|
(parent + child, parent, child)
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
2020-01-25 16:38:33 -06:00
|
|
|
|
2020-02-07 09:24:09 -06:00
|
|
|
/// (parent total, self param, type param list, impl trait)
|
|
|
|
pub(crate) fn provenance_split(&self) -> (usize, usize, usize, usize) {
|
|
|
|
let parent = self.parent_generics.as_ref().map_or(0, |p| p.len());
|
2020-02-07 08:13:15 -06:00
|
|
|
let self_params = self
|
|
|
|
.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, p)| p.provenance == TypeParamProvenance::TraitSelf)
|
|
|
|
.count();
|
|
|
|
let list_params = self
|
|
|
|
.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, p)| p.provenance == TypeParamProvenance::TypeParamList)
|
|
|
|
.count();
|
|
|
|
let impl_trait_params = self
|
|
|
|
.params
|
|
|
|
.types
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, p)| p.provenance == TypeParamProvenance::ArgumentImplTrait)
|
|
|
|
.count();
|
2020-02-07 09:24:09 -06:00
|
|
|
(parent, self_params, list_params, impl_trait_params)
|
2020-01-25 16:38:33 -06:00
|
|
|
}
|
|
|
|
|
2020-01-31 09:52:43 -06:00
|
|
|
pub(crate) fn param_idx(&self, param: TypeParamId) -> Option<u32> {
|
|
|
|
Some(self.find_param(param)?.0)
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
2020-01-25 16:38:33 -06:00
|
|
|
|
2020-01-31 09:52:43 -06:00
|
|
|
fn find_param(&self, param: TypeParamId) -> Option<(u32, &TypeParamData)> {
|
2019-12-07 04:50:36 -06:00
|
|
|
if param.parent == self.def {
|
|
|
|
let (idx, (_local_id, data)) = self
|
|
|
|
.params
|
2019-12-07 13:09:53 -06:00
|
|
|
.types
|
2019-12-07 04:50:36 -06:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, (idx, _))| *idx == param.local_id)
|
|
|
|
.unwrap();
|
2019-12-07 06:05:05 -06:00
|
|
|
let (_total, parent_len, _child) = self.len_split();
|
2020-01-31 09:52:43 -06:00
|
|
|
Some(((parent_len + idx) as u32, data))
|
|
|
|
} else {
|
|
|
|
self.parent_generics.as_ref().and_then(|g| g.find_param(param))
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<GenericDefId> {
|
|
|
|
let container = match def {
|
|
|
|
GenericDefId::FunctionId(it) => it.lookup(db).container,
|
|
|
|
GenericDefId::TypeAliasId(it) => it.lookup(db).container,
|
|
|
|
GenericDefId::ConstId(it) => it.lookup(db).container,
|
|
|
|
GenericDefId::EnumVariantId(it) => return Some(it.parent.into()),
|
|
|
|
GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match container {
|
2019-12-20 04:59:50 -06:00
|
|
|
AssocContainerId::ImplId(it) => Some(it.into()),
|
|
|
|
AssocContainerId::TraitId(it) => Some(it.into()),
|
2019-12-20 05:07:23 -06:00
|
|
|
AssocContainerId::ContainerId(_) => None,
|
2019-12-07 04:50:36 -06:00
|
|
|
}
|
|
|
|
}
|