2019-11-20 09:25:02 +00:00
|
|
|
//! Many kinds of items or constructs can have generic parameters: functions,
|
|
|
|
//! structs, impls, traits, etc. This module provides a common HIR for these
|
|
|
|
//! generic parameters. See also the `Generics` type and the `generics_of` query
|
|
|
|
//! in rustc.
|
|
|
|
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::FileId;
|
2019-12-07 17:24:52 +00:00
|
|
|
use either::Either;
|
|
|
|
use hir_expand::{
|
2021-09-25 16:57:57 +00:00
|
|
|
name::{AsName, Name},
|
2021-09-21 13:36:52 +00:00
|
|
|
ExpandResult, HirFileId, InFile,
|
2019-12-07 17:24:52 +00:00
|
|
|
};
|
2021-12-29 13:35:59 +00:00
|
|
|
use la_arena::{Arena, ArenaMap, Idx};
|
2021-09-21 13:36:52 +00:00
|
|
|
use once_cell::unsync::Lazy;
|
|
|
|
use std::ops::DerefMut;
|
2021-12-29 13:35:59 +00:00
|
|
|
use stdx::impl_from;
|
2021-09-27 10:54:24 +00:00
|
|
|
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
|
2019-11-20 09:25:02 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-09-21 13:36:52 +00:00
|
|
|
body::{Expander, LowerCtx},
|
2019-12-07 18:52:09 +00:00
|
|
|
child_by_source::ChildBySource,
|
2019-11-23 11:44:43 +00:00
|
|
|
db::DefDatabase,
|
2019-12-07 18:52:09 +00:00
|
|
|
dyn_map::DynMap,
|
2021-04-05 00:03:37 +00:00
|
|
|
intern::Interned,
|
2019-12-07 18:52:09 +00:00
|
|
|
keys,
|
2020-12-14 16:11:27 +00:00
|
|
|
src::{HasChildSource, HasSource},
|
2020-12-11 12:49:32 +00:00
|
|
|
type_ref::{LifetimeRef, TypeBound, TypeRef},
|
2022-03-09 18:50:24 +00:00
|
|
|
AdtId, ConstParamId, GenericDefId, HasModule, LifetimeParamId, LocalLifetimeParamId,
|
|
|
|
LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
|
2019-11-20 09:25:02 +00:00
|
|
|
};
|
|
|
|
|
2021-01-01 09:06:42 +00:00
|
|
|
/// Data about a generic type parameter (to a function, struct, impl, ...).
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2019-12-07 19:09:53 +00:00
|
|
|
pub struct TypeParamData {
|
2020-01-24 18:35:09 +00:00
|
|
|
pub name: Option<Name>,
|
2021-04-05 00:03:37 +00:00
|
|
|
pub default: Option<Interned<TypeRef>>,
|
2020-01-24 18:35:09 +00:00
|
|
|
pub provenance: TypeParamProvenance,
|
|
|
|
}
|
|
|
|
|
2021-01-01 09:06:42 +00:00
|
|
|
/// Data about a generic lifetime parameter (to a function, struct, impl, ...).
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 12:49:32 +00:00
|
|
|
pub struct LifetimeParamData {
|
|
|
|
pub name: Name,
|
|
|
|
}
|
|
|
|
|
2021-01-01 09:06:42 +00:00
|
|
|
/// Data about a generic const parameter (to a function, struct, impl, ...).
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2021-01-01 09:06:42 +00:00
|
|
|
pub struct ConstParamData {
|
|
|
|
pub name: Name,
|
2021-04-05 00:03:37 +00:00
|
|
|
pub ty: Interned<TypeRef>,
|
2022-07-06 16:43:59 +00:00
|
|
|
pub has_default: bool,
|
2021-01-01 09:06:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
2020-01-24 18:35:09 +00:00
|
|
|
pub enum TypeParamProvenance {
|
|
|
|
TypeParamList,
|
|
|
|
TraitSelf,
|
|
|
|
ArgumentImplTrait,
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 13:35:59 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum TypeOrConstParamData {
|
|
|
|
TypeParamData(TypeParamData),
|
|
|
|
ConstParamData(ConstParamData),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeOrConstParamData {
|
|
|
|
pub fn name(&self) -> Option<&Name> {
|
|
|
|
match self {
|
|
|
|
TypeOrConstParamData::TypeParamData(x) => x.name.as_ref(),
|
|
|
|
TypeOrConstParamData::ConstParamData(x) => Some(&x.name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 16:43:59 +00:00
|
|
|
pub fn has_default(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
TypeOrConstParamData::TypeParamData(x) => x.default.is_some(),
|
|
|
|
TypeOrConstParamData::ConstParamData(x) => x.has_default,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:35:59 +00:00
|
|
|
pub fn type_param(&self) -> Option<&TypeParamData> {
|
|
|
|
match self {
|
2022-03-12 12:04:13 +00:00
|
|
|
TypeOrConstParamData::TypeParamData(x) => Some(x),
|
2021-12-29 13:35:59 +00:00
|
|
|
TypeOrConstParamData::ConstParamData(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:21:35 +00:00
|
|
|
pub fn const_param(&self) -> Option<&ConstParamData> {
|
|
|
|
match self {
|
|
|
|
TypeOrConstParamData::TypeParamData(_) => None,
|
|
|
|
TypeOrConstParamData::ConstParamData(x) => Some(x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:35:59 +00:00
|
|
|
pub fn is_trait_self(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
TypeOrConstParamData::TypeParamData(x) => {
|
|
|
|
x.provenance == TypeParamProvenance::TraitSelf
|
|
|
|
}
|
|
|
|
TypeOrConstParamData::ConstParamData(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_from!(TypeParamData, ConstParamData for TypeOrConstParamData);
|
|
|
|
|
2019-11-20 09:25:02 +00:00
|
|
|
/// Data about the generic parameters of a function, struct, impl, etc.
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Default, Hash)]
|
2019-11-20 09:25:02 +00:00
|
|
|
pub struct GenericParams {
|
2022-03-09 18:50:24 +00:00
|
|
|
pub type_or_consts: Arena<TypeOrConstParamData>,
|
2020-12-11 12:49:32 +00:00
|
|
|
pub lifetimes: Arena<LifetimeParamData>,
|
2019-11-20 09:25:02 +00:00
|
|
|
pub where_predicates: Vec<WherePredicate>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
|
|
|
|
/// where clauses like `where T: Foo + Bar` are turned into multiple of these.
|
|
|
|
/// It might still result in multiple actual predicates though, because of
|
|
|
|
/// associated type bindings like `Iterator<Item = u32>`.
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 12:49:32 +00:00
|
|
|
pub enum WherePredicate {
|
2021-05-24 13:13:23 +00:00
|
|
|
TypeBound {
|
|
|
|
target: WherePredicateTypeTarget,
|
|
|
|
bound: Interned<TypeBound>,
|
|
|
|
},
|
|
|
|
Lifetime {
|
|
|
|
target: LifetimeRef,
|
|
|
|
bound: LifetimeRef,
|
|
|
|
},
|
|
|
|
ForLifetime {
|
|
|
|
lifetimes: Box<[Name]>,
|
|
|
|
target: WherePredicateTypeTarget,
|
|
|
|
bound: Interned<TypeBound>,
|
|
|
|
},
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 01:50:10 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 12:49:32 +00:00
|
|
|
pub enum WherePredicateTypeTarget {
|
2021-04-05 00:03:37 +00:00
|
|
|
TypeRef(Interned<TypeRef>),
|
2020-01-31 14:17:48 +00:00
|
|
|
/// For desugared where predicates that can directly refer to a type param.
|
2021-12-29 13:35:59 +00:00
|
|
|
TypeOrConstParam(LocalTypeOrConstParamId),
|
2020-01-31 14:17:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 09:25:02 +00:00
|
|
|
impl GenericParams {
|
2022-03-09 18:50:24 +00:00
|
|
|
/// Iterator of type_or_consts field
|
2022-12-30 09:00:42 +00:00
|
|
|
pub fn iter(
|
|
|
|
&self,
|
2022-03-09 18:50:24 +00:00
|
|
|
) -> impl DoubleEndedIterator<Item = (Idx<TypeOrConstParamData>, &TypeOrConstParamData)> {
|
|
|
|
self.type_or_consts.iter()
|
2021-12-29 13:35:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 17:33:18 +00:00
|
|
|
pub(crate) fn generic_params_query(
|
2020-03-13 15:05:46 +00:00
|
|
|
db: &dyn DefDatabase,
|
2019-11-20 17:33:18 +00:00
|
|
|
def: GenericDefId,
|
2021-04-05 01:50:10 +00:00
|
|
|
) -> Interned<GenericParams> {
|
2020-08-12 14:32:36 +00:00
|
|
|
let _p = profile::span("generic_params_query");
|
2020-06-22 17:15:54 +00:00
|
|
|
|
2022-06-23 18:08:29 +00:00
|
|
|
macro_rules! id_to_generics {
|
|
|
|
($id:ident) => {{
|
|
|
|
let id = $id.lookup(db).id;
|
|
|
|
let tree = id.item_tree(db);
|
|
|
|
let item = &tree[id.value];
|
|
|
|
item.generic_params.clone()
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:59:01 +00:00
|
|
|
match def {
|
2020-06-22 17:15:54 +00:00
|
|
|
GenericDefId::FunctionId(id) => {
|
2021-09-21 13:36:52 +00:00
|
|
|
let loc = id.lookup(db);
|
|
|
|
let tree = loc.id.item_tree(db);
|
|
|
|
let item = &tree[loc.id.value];
|
|
|
|
|
|
|
|
let mut generic_params = GenericParams::clone(&item.explicit_generic_params);
|
|
|
|
|
|
|
|
let module = loc.container.module(db);
|
|
|
|
let func_data = db.function_data(id);
|
|
|
|
|
|
|
|
// Don't create an `Expander` nor call `loc.source(db)` if not needed since this
|
|
|
|
// causes a reparse after the `ItemTree` has been created.
|
|
|
|
let mut expander = Lazy::new(|| Expander::new(db, loc.source(db).file_id, module));
|
2021-12-20 14:24:37 +00:00
|
|
|
for (_, param) in &func_data.params {
|
2021-09-21 13:36:52 +00:00
|
|
|
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
Interned::new(generic_params)
|
2020-06-22 17:15:54 +00:00
|
|
|
}
|
2022-06-23 18:08:29 +00:00
|
|
|
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics!(id),
|
|
|
|
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics!(id),
|
|
|
|
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics!(id),
|
|
|
|
GenericDefId::TraitId(id) => id_to_generics!(id),
|
|
|
|
GenericDefId::TypeAliasId(id) => id_to_generics!(id),
|
|
|
|
GenericDefId::ImplId(id) => id_to_generics!(id),
|
2021-04-05 01:50:10 +00:00
|
|
|
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
|
|
|
|
Interned::new(GenericParams::default())
|
2020-06-22 17:15:54 +00:00
|
|
|
}
|
2021-06-07 11:59:01 +00:00
|
|
|
}
|
2019-11-20 17:33:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 13:02:08 +00:00
|
|
|
pub(crate) fn fill(&mut self, lower_ctx: &LowerCtx<'_>, node: &dyn HasGenericParams) {
|
2020-07-30 13:36:21 +00:00
|
|
|
if let Some(params) = node.generic_param_list() {
|
2021-09-25 17:06:04 +00:00
|
|
|
self.fill_params(lower_ctx, params)
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
if let Some(where_clause) = node.where_clause() {
|
2020-04-30 10:20:13 +00:00
|
|
|
self.fill_where_predicates(lower_ctx, where_clause);
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 10:24:05 +00:00
|
|
|
pub(crate) fn fill_bounds(
|
2020-04-30 10:20:13 +00:00
|
|
|
&mut self,
|
2022-07-20 13:02:08 +00:00
|
|
|
lower_ctx: &LowerCtx<'_>,
|
2021-09-27 10:54:24 +00:00
|
|
|
node: &dyn ast::HasTypeBounds,
|
2020-12-11 12:49:32 +00:00
|
|
|
target: Either<TypeRef, LifetimeRef>,
|
2020-04-30 10:20:13 +00:00
|
|
|
) {
|
2019-11-20 09:25:02 +00:00
|
|
|
for bound in
|
|
|
|
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
|
|
|
|
{
|
2020-12-17 21:01:42 +00:00
|
|
|
self.add_where_predicate_from_bound(lower_ctx, bound, None, target.clone());
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:02:08 +00:00
|
|
|
fn fill_params(&mut self, lower_ctx: &LowerCtx<'_>, params: ast::GenericParamList) {
|
2021-12-29 13:35:59 +00:00
|
|
|
for type_or_const_param in params.type_or_const_params() {
|
|
|
|
match type_or_const_param {
|
|
|
|
ast::TypeOrConstParam::Type(type_param) => {
|
|
|
|
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
|
|
|
|
// FIXME: Use `Path::from_src`
|
|
|
|
let default = type_param
|
|
|
|
.default_type()
|
|
|
|
.map(|it| Interned::new(TypeRef::from_ast(lower_ctx, it)));
|
|
|
|
let param = TypeParamData {
|
|
|
|
name: Some(name.clone()),
|
|
|
|
default,
|
|
|
|
provenance: TypeParamProvenance::TypeParamList,
|
|
|
|
};
|
2022-03-09 18:50:24 +00:00
|
|
|
self.type_or_consts.alloc(param.into());
|
2021-12-29 13:35:59 +00:00
|
|
|
let type_ref = TypeRef::Path(name.into());
|
|
|
|
self.fill_bounds(lower_ctx, &type_param, Either::Left(type_ref));
|
|
|
|
}
|
|
|
|
ast::TypeOrConstParam::Const(const_param) => {
|
|
|
|
let name = const_param.name().map_or_else(Name::missing, |it| it.as_name());
|
|
|
|
let ty = const_param
|
|
|
|
.ty()
|
|
|
|
.map_or(TypeRef::Error, |it| TypeRef::from_ast(lower_ctx, it));
|
2022-07-06 16:43:59 +00:00
|
|
|
let param = ConstParamData {
|
|
|
|
name,
|
|
|
|
ty: Interned::new(ty),
|
|
|
|
has_default: const_param.default_val().is_some(),
|
|
|
|
};
|
2022-03-09 18:50:24 +00:00
|
|
|
self.type_or_consts.alloc(param.into());
|
2021-12-29 13:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 12:49:32 +00:00
|
|
|
}
|
|
|
|
for lifetime_param in params.lifetime_params() {
|
2020-12-15 18:23:51 +00:00
|
|
|
let name =
|
|
|
|
lifetime_param.lifetime().map_or_else(Name::missing, |lt| Name::new_lifetime(<));
|
2020-12-11 12:49:32 +00:00
|
|
|
let param = LifetimeParamData { name: name.clone() };
|
2021-09-25 17:06:04 +00:00
|
|
|
self.lifetimes.alloc(param);
|
2020-12-11 12:49:32 +00:00
|
|
|
let lifetime_ref = LifetimeRef::new_name(name);
|
2021-06-13 03:54:16 +00:00
|
|
|
self.fill_bounds(lower_ctx, &lifetime_param, Either::Right(lifetime_ref));
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:02:08 +00:00
|
|
|
fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx<'_>, where_clause: ast::WhereClause) {
|
2019-11-20 09:25:02 +00:00
|
|
|
for pred in where_clause.predicates() {
|
2020-12-11 12:49:32 +00:00
|
|
|
let target = if let Some(type_ref) = pred.ty() {
|
|
|
|
Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
|
2020-12-15 18:23:51 +00:00
|
|
|
} else if let Some(lifetime) = pred.lifetime() {
|
|
|
|
Either::Right(LifetimeRef::new(&lifetime))
|
2020-12-11 12:49:32 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
2019-11-20 09:25:02 +00:00
|
|
|
};
|
2020-12-17 21:01:42 +00:00
|
|
|
|
|
|
|
let lifetimes: Option<Box<_>> = pred.generic_param_list().map(|param_list| {
|
|
|
|
// Higher-Ranked Trait Bounds
|
|
|
|
param_list
|
|
|
|
.lifetime_params()
|
|
|
|
.map(|lifetime_param| {
|
|
|
|
lifetime_param
|
|
|
|
.lifetime()
|
|
|
|
.map_or_else(Name::missing, |lt| Name::new_lifetime(<))
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
});
|
2019-11-20 09:25:02 +00:00
|
|
|
for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
|
2020-12-17 21:01:42 +00:00
|
|
|
self.add_where_predicate_from_bound(
|
|
|
|
lower_ctx,
|
|
|
|
bound,
|
|
|
|
lifetimes.as_ref(),
|
|
|
|
target.clone(),
|
|
|
|
);
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 10:20:13 +00:00
|
|
|
fn add_where_predicate_from_bound(
|
|
|
|
&mut self,
|
2022-07-20 13:02:08 +00:00
|
|
|
lower_ctx: &LowerCtx<'_>,
|
2020-04-30 10:20:13 +00:00
|
|
|
bound: ast::TypeBound,
|
2020-12-17 21:01:42 +00:00
|
|
|
hrtb_lifetimes: Option<&Box<[Name]>>,
|
2020-12-11 12:49:32 +00:00
|
|
|
target: Either<TypeRef, LifetimeRef>,
|
2020-04-30 10:20:13 +00:00
|
|
|
) {
|
|
|
|
let bound = TypeBound::from_ast(lower_ctx, bound);
|
2020-12-11 12:49:32 +00:00
|
|
|
let predicate = match (target, bound) {
|
2020-12-17 21:01:42 +00:00
|
|
|
(Either::Left(type_ref), bound) => match hrtb_lifetimes {
|
|
|
|
Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
|
|
|
|
lifetimes: hrtb_lifetimes.clone(),
|
2021-04-05 00:03:37 +00:00
|
|
|
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
|
2021-05-24 13:13:23 +00:00
|
|
|
bound: Interned::new(bound),
|
2020-12-17 21:01:42 +00:00
|
|
|
},
|
|
|
|
None => WherePredicate::TypeBound {
|
2021-04-05 00:03:37 +00:00
|
|
|
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
|
2021-05-24 13:13:23 +00:00
|
|
|
bound: Interned::new(bound),
|
2020-12-17 21:01:42 +00:00
|
|
|
},
|
2020-12-11 12:49:32 +00:00
|
|
|
},
|
|
|
|
(Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
|
|
|
|
WherePredicate::Lifetime { target: lifetime, bound }
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
self.where_predicates.push(predicate);
|
2019-11-20 09:25:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 13:36:52 +00:00
|
|
|
pub(crate) fn fill_implicit_impl_trait_args(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
expander: &mut impl DerefMut<Target = Expander>,
|
|
|
|
type_ref: &TypeRef,
|
|
|
|
) {
|
2020-01-24 18:35:09 +00:00
|
|
|
type_ref.walk(&mut |type_ref| {
|
2020-01-31 14:17:48 +00:00
|
|
|
if let TypeRef::ImplTrait(bounds) = type_ref {
|
2020-01-24 18:35:09 +00:00
|
|
|
let param = TypeParamData {
|
|
|
|
name: None,
|
|
|
|
default: None,
|
|
|
|
provenance: TypeParamProvenance::ArgumentImplTrait,
|
|
|
|
};
|
2022-03-09 18:50:24 +00:00
|
|
|
let param_id = self.type_or_consts.alloc(param.into());
|
2020-01-31 14:17:48 +00:00
|
|
|
for bound in bounds {
|
2020-12-11 12:49:32 +00:00
|
|
|
self.where_predicates.push(WherePredicate::TypeBound {
|
2021-12-29 13:35:59 +00:00
|
|
|
target: WherePredicateTypeTarget::TypeOrConstParam(param_id),
|
2020-02-07 14:13:15 +00:00
|
|
|
bound: bound.clone(),
|
2020-01-31 14:17:48 +00:00
|
|
|
});
|
|
|
|
}
|
2020-01-24 18:35:09 +00:00
|
|
|
}
|
2021-09-21 13:36:52 +00:00
|
|
|
if let TypeRef::Macro(mc) = type_ref {
|
|
|
|
let macro_call = mc.to_node(db.upcast());
|
|
|
|
match expander.enter_expand::<ast::Type>(db, macro_call) {
|
|
|
|
Ok(ExpandResult { value: Some((mark, expanded)), .. }) => {
|
2021-09-29 19:06:37 +00:00
|
|
|
let ctx = LowerCtx::new(db, expander.current_file_id());
|
2021-09-21 13:36:52 +00:00
|
|
|
let type_ref = TypeRef::from_ast(&ctx, expanded);
|
|
|
|
self.fill_implicit_impl_trait_args(db, expander, &type_ref);
|
|
|
|
expander.exit(db, mark);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 18:35:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-05 01:32:43 +00:00
|
|
|
pub(crate) fn shrink_to_fit(&mut self) {
|
2022-03-09 18:50:24 +00:00
|
|
|
let Self { lifetimes, type_or_consts: types, where_predicates } = self;
|
2021-04-05 01:32:43 +00:00
|
|
|
lifetimes.shrink_to_fit();
|
|
|
|
types.shrink_to_fit();
|
|
|
|
where_predicates.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:50:24 +00:00
|
|
|
pub fn find_type_by_name(&self, name: &Name, parent: GenericDefId) -> Option<TypeParamId> {
|
|
|
|
self.type_or_consts.iter().find_map(|(id, p)| {
|
|
|
|
if p.name().as_ref() == Some(&name) && p.type_param().is_some() {
|
|
|
|
Some(TypeParamId::from_unchecked(TypeOrConstParamId { local_id: id, parent }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2022-03-04 09:00:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 18:50:24 +00:00
|
|
|
pub fn find_const_by_name(&self, name: &Name, parent: GenericDefId) -> Option<ConstParamId> {
|
|
|
|
self.type_or_consts.iter().find_map(|(id, p)| {
|
|
|
|
if p.name().as_ref() == Some(&name) && p.const_param().is_some() {
|
|
|
|
Some(ConstParamId::from_unchecked(TypeOrConstParamId { local_id: id, parent }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2019-11-20 17:33:18 +00:00
|
|
|
}
|
2020-01-31 14:17:48 +00:00
|
|
|
|
2021-12-29 13:35:59 +00:00
|
|
|
pub fn find_trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
|
2022-03-09 18:50:24 +00:00
|
|
|
self.type_or_consts.iter().find_map(|(id, p)| {
|
2022-06-23 18:08:29 +00:00
|
|
|
matches!(
|
|
|
|
p,
|
|
|
|
TypeOrConstParamData::TypeParamData(TypeParamData {
|
|
|
|
provenance: TypeParamProvenance::TraitSelf,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(|| id)
|
2020-02-07 14:13:15 +00:00
|
|
|
})
|
2020-01-31 14:17:48 +00:00
|
|
|
}
|
2019-11-20 17:33:18 +00:00
|
|
|
}
|
2020-12-14 16:11:27 +00:00
|
|
|
|
2021-09-25 16:57:11 +00:00
|
|
|
fn file_id_and_params_of(
|
|
|
|
def: GenericDefId,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
) -> (HirFileId, Option<ast::GenericParamList>) {
|
|
|
|
match def {
|
|
|
|
GenericDefId::FunctionId(it) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::AdtId(AdtId::StructId(it)) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::AdtId(AdtId::UnionId(it)) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::AdtId(AdtId::EnumId(it)) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::TraitId(it) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::TypeAliasId(it) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
GenericDefId::ImplId(it) => {
|
|
|
|
let src = it.lookup(db).source(db);
|
|
|
|
(src.file_id, src.value.generic_param_list())
|
|
|
|
}
|
|
|
|
// We won't be using this ID anyway
|
|
|
|
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId(!0).into(), None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:35:59 +00:00
|
|
|
impl HasChildSource<LocalTypeOrConstParamId> for GenericDefId {
|
|
|
|
type Value = Either<ast::TypeOrConstParam, ast::Trait>;
|
2020-12-14 16:11:27 +00:00
|
|
|
fn child_source(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
2021-12-29 13:35:59 +00:00
|
|
|
) -> InFile<ArenaMap<LocalTypeOrConstParamId, Self::Value>> {
|
2021-09-25 16:57:11 +00:00
|
|
|
let generic_params = db.generic_params(*self);
|
2022-03-09 18:50:24 +00:00
|
|
|
let mut idx_iter = generic_params.type_or_consts.iter().map(|(idx, _)| idx);
|
2021-09-25 16:57:11 +00:00
|
|
|
|
|
|
|
let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
|
|
|
|
|
|
|
|
let mut params = ArenaMap::default();
|
|
|
|
|
|
|
|
// For traits the first type index is `Self`, we need to add it before the other params.
|
|
|
|
if let GenericDefId::TraitId(id) = *self {
|
2021-10-10 01:50:51 +00:00
|
|
|
let trait_ref = id.lookup(db).source(db).value;
|
2021-09-25 16:57:11 +00:00
|
|
|
let idx = idx_iter.next().unwrap();
|
2022-08-06 20:48:52 +00:00
|
|
|
params.insert(idx, Either::Right(trait_ref));
|
2021-09-25 16:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(generic_params_list) = generic_params_list {
|
2021-12-29 13:35:59 +00:00
|
|
|
for (idx, ast_param) in idx_iter.zip(generic_params_list.type_or_const_params()) {
|
2021-09-25 16:57:11 +00:00
|
|
|
params.insert(idx, Either::Left(ast_param));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InFile::new(file_id, params)
|
2020-12-14 16:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HasChildSource<LocalLifetimeParamId> for GenericDefId {
|
|
|
|
type Value = ast::LifetimeParam;
|
|
|
|
fn child_source(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
) -> InFile<ArenaMap<LocalLifetimeParamId, Self::Value>> {
|
2021-09-25 16:57:11 +00:00
|
|
|
let generic_params = db.generic_params(*self);
|
|
|
|
let idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
|
|
|
|
|
|
|
|
let (file_id, generic_params_list) = file_id_and_params_of(*self, db);
|
|
|
|
|
|
|
|
let mut params = ArenaMap::default();
|
|
|
|
|
|
|
|
if let Some(generic_params_list) = generic_params_list {
|
|
|
|
for (idx, ast_param) in idx_iter.zip(generic_params_list.lifetime_params()) {
|
|
|
|
params.insert(idx, ast_param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InFile::new(file_id, params)
|
2019-12-07 17:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-07 18:52:09 +00:00
|
|
|
|
|
|
|
impl ChildBySource for GenericDefId {
|
2022-01-08 10:34:58 +00:00
|
|
|
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
|
|
|
|
let (gfile_id, generic_params_list) = file_id_and_params_of(*self, db);
|
|
|
|
if gfile_id != file_id {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:57:11 +00:00
|
|
|
let generic_params = db.generic_params(*self);
|
2022-03-09 18:50:24 +00:00
|
|
|
let mut toc_idx_iter = generic_params.type_or_consts.iter().map(|(idx, _)| idx);
|
2021-09-25 16:57:11 +00:00
|
|
|
let lts_idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
|
2020-12-13 21:13:16 +00:00
|
|
|
|
2021-09-25 16:57:11 +00:00
|
|
|
// For traits the first type index is `Self`, skip it.
|
|
|
|
if let GenericDefId::TraitId(_) = *self {
|
2021-12-29 13:35:59 +00:00
|
|
|
toc_idx_iter.next().unwrap(); // advance_by(1);
|
2020-12-13 21:13:16 +00:00
|
|
|
}
|
2021-09-25 16:57:11 +00:00
|
|
|
|
|
|
|
if let Some(generic_params_list) = generic_params_list {
|
2021-12-29 13:35:59 +00:00
|
|
|
for (local_id, ast_param) in
|
|
|
|
toc_idx_iter.zip(generic_params_list.type_or_const_params())
|
|
|
|
{
|
|
|
|
let id = TypeOrConstParamId { parent: *self, local_id };
|
|
|
|
match ast_param {
|
|
|
|
ast::TypeOrConstParam::Type(a) => res[keys::TYPE_PARAM].insert(a, id),
|
|
|
|
ast::TypeOrConstParam::Const(a) => res[keys::CONST_PARAM].insert(a, id),
|
|
|
|
}
|
2021-09-25 16:57:11 +00:00
|
|
|
}
|
|
|
|
for (local_id, ast_param) in lts_idx_iter.zip(generic_params_list.lifetime_params()) {
|
|
|
|
let id = LifetimeParamId { parent: *self, local_id };
|
2022-01-08 11:13:45 +00:00
|
|
|
res[keys::LIFETIME_PARAM].insert(ast_param, id);
|
2021-09-25 16:57:11 +00:00
|
|
|
}
|
2021-01-01 09:06:42 +00:00
|
|
|
}
|
2019-12-07 18:52:09 +00:00
|
|
|
}
|
|
|
|
}
|