2019-11-20 03:25:02 -06: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.
|
|
|
|
|
2019-12-07 11:24:52 -06:00
|
|
|
use either::Either;
|
|
|
|
use hir_expand::{
|
2021-09-25 11:57:57 -05:00
|
|
|
name::{AsName, Name},
|
2024-02-10 05:09:12 -06:00
|
|
|
ExpandResult,
|
2019-12-07 11:24:52 -06:00
|
|
|
};
|
2023-01-09 12:29:28 -06:00
|
|
|
use intern::Interned;
|
2024-02-10 05:09:12 -06:00
|
|
|
use la_arena::{Arena, Idx};
|
2021-09-21 08:36:52 -05:00
|
|
|
use once_cell::unsync::Lazy;
|
2021-12-29 07:35:59 -06:00
|
|
|
use stdx::impl_from;
|
2021-09-27 05:54:24 -05:00
|
|
|
use syntax::ast::{self, HasGenericParams, HasName, HasTypeBounds};
|
2023-05-02 09:12:22 -05:00
|
|
|
use triomphe::Arc;
|
2019-11-20 03:25:02 -06:00
|
|
|
|
|
|
|
use crate::{
|
2019-11-23 05:44:43 -06:00
|
|
|
db::DefDatabase,
|
2023-04-17 10:31:39 -05:00
|
|
|
expander::Expander,
|
2024-02-10 05:40:23 -06:00
|
|
|
item_tree::{GenericsItemTreeNode, ItemTree},
|
2023-04-17 10:31:39 -05:00
|
|
|
lower::LowerCtx,
|
2023-05-11 01:52:13 -05:00
|
|
|
nameres::{DefMap, MacroSubNs},
|
2023-06-30 08:31:04 -05:00
|
|
|
type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef},
|
2024-03-06 05:09:24 -06:00
|
|
|
AdtId, ConstParamId, GenericDefId, HasModule, ItemTreeLoc, LifetimeParamId,
|
|
|
|
LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
|
2019-11-20 03:25:02 -06:00
|
|
|
};
|
|
|
|
|
2021-01-01 03:06:42 -06:00
|
|
|
/// Data about a generic type parameter (to a function, struct, impl, ...).
|
2021-04-04 20:50:10 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2019-12-07 13:09:53 -06:00
|
|
|
pub struct TypeParamData {
|
2020-01-24 12:35:09 -06:00
|
|
|
pub name: Option<Name>,
|
2021-04-04 19:03:37 -05:00
|
|
|
pub default: Option<Interned<TypeRef>>,
|
2020-01-24 12:35:09 -06:00
|
|
|
pub provenance: TypeParamProvenance,
|
|
|
|
}
|
|
|
|
|
2021-01-01 03:06:42 -06:00
|
|
|
/// Data about a generic lifetime parameter (to a function, struct, impl, ...).
|
2021-04-04 20:50:10 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 06:49:32 -06:00
|
|
|
pub struct LifetimeParamData {
|
|
|
|
pub name: Name,
|
|
|
|
}
|
|
|
|
|
2021-01-01 03:06:42 -06:00
|
|
|
/// Data about a generic const parameter (to a function, struct, impl, ...).
|
2021-04-04 20:50:10 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2021-01-01 03:06:42 -06:00
|
|
|
pub struct ConstParamData {
|
|
|
|
pub name: Name,
|
2021-04-04 19:03:37 -05:00
|
|
|
pub ty: Interned<TypeRef>,
|
2023-06-30 08:31:04 -05:00
|
|
|
pub default: Option<ConstRef>,
|
2021-01-01 03:06:42 -06:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:50:10 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
2020-01-24 12:35:09 -06:00
|
|
|
pub enum TypeParamProvenance {
|
|
|
|
TypeParamList,
|
|
|
|
TraitSelf,
|
|
|
|
ArgumentImplTrait,
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum TypeOrConstParamData {
|
|
|
|
TypeParamData(TypeParamData),
|
|
|
|
ConstParamData(ConstParamData),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeOrConstParamData {
|
|
|
|
pub fn name(&self) -> Option<&Name> {
|
|
|
|
match self {
|
2023-07-06 09:03:17 -05:00
|
|
|
TypeOrConstParamData::TypeParamData(it) => it.name.as_ref(),
|
|
|
|
TypeOrConstParamData::ConstParamData(it) => Some(&it.name),
|
2021-12-29 07:35:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-06 11:43:59 -05:00
|
|
|
pub fn has_default(&self) -> bool {
|
|
|
|
match self {
|
2023-07-06 09:03:17 -05:00
|
|
|
TypeOrConstParamData::TypeParamData(it) => it.default.is_some(),
|
2023-06-30 08:31:04 -05:00
|
|
|
TypeOrConstParamData::ConstParamData(it) => it.default.is_some(),
|
2022-07-06 11:43:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
pub fn type_param(&self) -> Option<&TypeParamData> {
|
|
|
|
match self {
|
2023-07-06 09:03:17 -05:00
|
|
|
TypeOrConstParamData::TypeParamData(it) => Some(it),
|
2021-12-29 07:35:59 -06:00
|
|
|
TypeOrConstParamData::ConstParamData(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:21:35 -06:00
|
|
|
pub fn const_param(&self) -> Option<&ConstParamData> {
|
|
|
|
match self {
|
|
|
|
TypeOrConstParamData::TypeParamData(_) => None,
|
2023-07-06 09:03:17 -05:00
|
|
|
TypeOrConstParamData::ConstParamData(it) => Some(it),
|
2022-03-08 10:21:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
pub fn is_trait_self(&self) -> bool {
|
|
|
|
match self {
|
2023-07-06 09:03:17 -05:00
|
|
|
TypeOrConstParamData::TypeParamData(it) => {
|
|
|
|
it.provenance == TypeParamProvenance::TraitSelf
|
2021-12-29 07:35:59 -06:00
|
|
|
}
|
|
|
|
TypeOrConstParamData::ConstParamData(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_from!(TypeParamData, ConstParamData for TypeOrConstParamData);
|
|
|
|
|
2024-03-08 13:38:35 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
|
|
|
pub enum GenericParamData {
|
|
|
|
TypeParamData(TypeParamData),
|
|
|
|
ConstParamData(ConstParamData),
|
|
|
|
LifetimeParamData(LifetimeParamData),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParamData {
|
|
|
|
pub fn name(&self) -> Option<&Name> {
|
|
|
|
match self {
|
|
|
|
GenericParamData::TypeParamData(it) => it.name.as_ref(),
|
|
|
|
GenericParamData::ConstParamData(it) => Some(&it.name),
|
|
|
|
GenericParamData::LifetimeParamData(it) => Some(&it.name),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_param(&self) -> Option<&TypeParamData> {
|
|
|
|
match self {
|
|
|
|
GenericParamData::TypeParamData(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn const_param(&self) -> Option<&ConstParamData> {
|
|
|
|
match self {
|
|
|
|
GenericParamData::ConstParamData(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lifetime_param(&self) -> Option<&LifetimeParamData> {
|
|
|
|
match self {
|
|
|
|
GenericParamData::LifetimeParamData(it) => Some(it),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_from!(TypeParamData, ConstParamData, LifetimeParamData for GenericParamData);
|
|
|
|
|
2024-03-19 02:27:18 -05:00
|
|
|
pub enum GenericParamDataRef<'a> {
|
|
|
|
TypeParamData(&'a TypeParamData),
|
|
|
|
ConstParamData(&'a ConstParamData),
|
|
|
|
LifetimeParamData(&'a LifetimeParamData),
|
|
|
|
}
|
|
|
|
|
2019-11-20 03:25:02 -06:00
|
|
|
/// Data about the generic parameters of a function, struct, impl, etc.
|
2024-01-09 13:43:17 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2019-11-20 03:25:02 -06:00
|
|
|
pub struct GenericParams {
|
2022-03-09 12:50:24 -06:00
|
|
|
pub type_or_consts: Arena<TypeOrConstParamData>,
|
2020-12-11 06:49:32 -06:00
|
|
|
pub lifetimes: Arena<LifetimeParamData>,
|
2024-01-09 13:43:17 -06:00
|
|
|
pub where_predicates: Box<[WherePredicate]>,
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-04 20:50:10 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 06:49:32 -06:00
|
|
|
pub enum WherePredicate {
|
2021-05-24 08:13:23 -05:00
|
|
|
TypeBound {
|
|
|
|
target: WherePredicateTypeTarget,
|
|
|
|
bound: Interned<TypeBound>,
|
|
|
|
},
|
|
|
|
Lifetime {
|
|
|
|
target: LifetimeRef,
|
|
|
|
bound: LifetimeRef,
|
|
|
|
},
|
|
|
|
ForLifetime {
|
|
|
|
lifetimes: Box<[Name]>,
|
|
|
|
target: WherePredicateTypeTarget,
|
|
|
|
bound: Interned<TypeBound>,
|
|
|
|
},
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
|
2021-04-04 20:50:10 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
|
2020-12-11 06:49:32 -06:00
|
|
|
pub enum WherePredicateTypeTarget {
|
2021-04-04 19:03:37 -05:00
|
|
|
TypeRef(Interned<TypeRef>),
|
2020-01-31 08:17:48 -06:00
|
|
|
/// For desugared where predicates that can directly refer to a type param.
|
2021-12-29 07:35:59 -06:00
|
|
|
TypeOrConstParam(LocalTypeOrConstParamId),
|
2020-01-31 08:17:48 -06:00
|
|
|
}
|
|
|
|
|
2024-01-09 13:43:17 -06:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub(crate) struct GenericParamsCollector {
|
2024-01-09 14:30:01 -06:00
|
|
|
pub(crate) type_or_consts: Arena<TypeOrConstParamData>,
|
|
|
|
lifetimes: Arena<LifetimeParamData>,
|
|
|
|
where_predicates: Vec<WherePredicate>,
|
2024-01-09 13:43:17 -06:00
|
|
|
}
|
2019-11-20 11:33:18 -06:00
|
|
|
|
2024-01-09 13:43:17 -06:00
|
|
|
impl GenericParamsCollector {
|
2023-08-02 08:19:56 -05:00
|
|
|
pub(crate) fn fill(
|
|
|
|
&mut self,
|
|
|
|
lower_ctx: &LowerCtx<'_>,
|
|
|
|
node: &dyn HasGenericParams,
|
2023-09-29 05:37:57 -05:00
|
|
|
add_param_attrs: impl FnMut(
|
|
|
|
Either<Idx<TypeOrConstParamData>, Idx<LifetimeParamData>>,
|
|
|
|
ast::GenericParam,
|
|
|
|
),
|
2023-08-02 08:19:56 -05:00
|
|
|
) {
|
2020-07-30 08:36:21 -05:00
|
|
|
if let Some(params) = node.generic_param_list() {
|
2023-08-02 08:19:56 -05:00
|
|
|
self.fill_params(lower_ctx, params, add_param_attrs)
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
if let Some(where_clause) = node.where_clause() {
|
2020-04-30 05:20:13 -05:00
|
|
|
self.fill_where_predicates(lower_ctx, where_clause);
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 05:24:05 -05:00
|
|
|
pub(crate) fn fill_bounds(
|
2020-04-30 05:20:13 -05:00
|
|
|
&mut self,
|
2022-07-20 08:02:08 -05:00
|
|
|
lower_ctx: &LowerCtx<'_>,
|
2023-03-03 09:24:05 -06:00
|
|
|
type_bounds: Option<ast::TypeBoundList>,
|
2020-12-11 06:49:32 -06:00
|
|
|
target: Either<TypeRef, LifetimeRef>,
|
2020-04-30 05:20:13 -05:00
|
|
|
) {
|
2023-03-03 09:24:05 -06:00
|
|
|
for bound in type_bounds.iter().flat_map(|type_bound_list| type_bound_list.bounds()) {
|
2020-12-17 15:01:42 -06:00
|
|
|
self.add_where_predicate_from_bound(lower_ctx, bound, None, target.clone());
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 08:19:56 -05:00
|
|
|
fn fill_params(
|
|
|
|
&mut self,
|
|
|
|
lower_ctx: &LowerCtx<'_>,
|
|
|
|
params: ast::GenericParamList,
|
2023-09-29 05:37:57 -05:00
|
|
|
mut add_param_attrs: impl FnMut(
|
|
|
|
Either<Idx<TypeOrConstParamData>, Idx<LifetimeParamData>>,
|
|
|
|
ast::GenericParam,
|
|
|
|
),
|
2023-08-02 08:19:56 -05:00
|
|
|
) {
|
2021-12-29 07:35:59 -06: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,
|
|
|
|
};
|
2023-08-02 08:19:56 -05:00
|
|
|
let idx = self.type_or_consts.alloc(param.into());
|
2021-12-29 07:35:59 -06:00
|
|
|
let type_ref = TypeRef::Path(name.into());
|
2023-03-03 09:24:05 -06:00
|
|
|
self.fill_bounds(
|
|
|
|
lower_ctx,
|
|
|
|
type_param.type_bound_list(),
|
|
|
|
Either::Left(type_ref),
|
|
|
|
);
|
2023-09-29 05:37:57 -05:00
|
|
|
add_param_attrs(Either::Left(idx), ast::GenericParam::TypeParam(type_param));
|
2021-12-29 07:35:59 -06:00
|
|
|
}
|
|
|
|
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 11:43:59 -05:00
|
|
|
let param = ConstParamData {
|
|
|
|
name,
|
|
|
|
ty: Interned::new(ty),
|
2023-08-08 13:16:28 -05:00
|
|
|
default: ConstRef::from_const_param(lower_ctx, &const_param),
|
2022-07-06 11:43:59 -05:00
|
|
|
};
|
2023-08-02 08:19:56 -05:00
|
|
|
let idx = self.type_or_consts.alloc(param.into());
|
2023-09-29 05:37:57 -05:00
|
|
|
add_param_attrs(Either::Left(idx), ast::GenericParam::ConstParam(const_param));
|
2021-12-29 07:35:59 -06:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 06:49:32 -06:00
|
|
|
}
|
|
|
|
for lifetime_param in params.lifetime_params() {
|
2020-12-15 12:23:51 -06:00
|
|
|
let name =
|
|
|
|
lifetime_param.lifetime().map_or_else(Name::missing, |lt| Name::new_lifetime(<));
|
2020-12-11 06:49:32 -06:00
|
|
|
let param = LifetimeParamData { name: name.clone() };
|
2023-08-02 08:19:56 -05:00
|
|
|
let idx = self.lifetimes.alloc(param);
|
2020-12-11 06:49:32 -06:00
|
|
|
let lifetime_ref = LifetimeRef::new_name(name);
|
2023-03-03 09:24:05 -06:00
|
|
|
self.fill_bounds(
|
|
|
|
lower_ctx,
|
|
|
|
lifetime_param.type_bound_list(),
|
|
|
|
Either::Right(lifetime_ref),
|
|
|
|
);
|
2023-09-29 05:37:57 -05:00
|
|
|
add_param_attrs(Either::Right(idx), ast::GenericParam::LifetimeParam(lifetime_param));
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 08:02:08 -05:00
|
|
|
fn fill_where_predicates(&mut self, lower_ctx: &LowerCtx<'_>, where_clause: ast::WhereClause) {
|
2019-11-20 03:25:02 -06:00
|
|
|
for pred in where_clause.predicates() {
|
2020-12-11 06:49:32 -06:00
|
|
|
let target = if let Some(type_ref) = pred.ty() {
|
|
|
|
Either::Left(TypeRef::from_ast(lower_ctx, type_ref))
|
2020-12-15 12:23:51 -06:00
|
|
|
} else if let Some(lifetime) = pred.lifetime() {
|
|
|
|
Either::Right(LifetimeRef::new(&lifetime))
|
2020-12-11 06:49:32 -06:00
|
|
|
} else {
|
|
|
|
continue;
|
2019-11-20 03:25:02 -06:00
|
|
|
};
|
2020-12-17 15:01:42 -06: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 03:25:02 -06:00
|
|
|
for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
|
2020-12-17 15:01:42 -06:00
|
|
|
self.add_where_predicate_from_bound(
|
|
|
|
lower_ctx,
|
|
|
|
bound,
|
2024-02-09 08:56:47 -06:00
|
|
|
lifetimes.as_deref(),
|
2020-12-17 15:01:42 -06:00
|
|
|
target.clone(),
|
|
|
|
);
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 05:20:13 -05:00
|
|
|
fn add_where_predicate_from_bound(
|
|
|
|
&mut self,
|
2022-07-20 08:02:08 -05:00
|
|
|
lower_ctx: &LowerCtx<'_>,
|
2020-04-30 05:20:13 -05:00
|
|
|
bound: ast::TypeBound,
|
2024-02-09 08:56:47 -06:00
|
|
|
hrtb_lifetimes: Option<&[Name]>,
|
2020-12-11 06:49:32 -06:00
|
|
|
target: Either<TypeRef, LifetimeRef>,
|
2020-04-30 05:20:13 -05:00
|
|
|
) {
|
|
|
|
let bound = TypeBound::from_ast(lower_ctx, bound);
|
2020-12-11 06:49:32 -06:00
|
|
|
let predicate = match (target, bound) {
|
2020-12-17 15:01:42 -06:00
|
|
|
(Either::Left(type_ref), bound) => match hrtb_lifetimes {
|
|
|
|
Some(hrtb_lifetimes) => WherePredicate::ForLifetime {
|
2024-02-09 08:56:47 -06:00
|
|
|
lifetimes: hrtb_lifetimes.to_vec().into_boxed_slice(),
|
2021-04-04 19:03:37 -05:00
|
|
|
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
|
2021-05-24 08:13:23 -05:00
|
|
|
bound: Interned::new(bound),
|
2020-12-17 15:01:42 -06:00
|
|
|
},
|
|
|
|
None => WherePredicate::TypeBound {
|
2021-04-04 19:03:37 -05:00
|
|
|
target: WherePredicateTypeTarget::TypeRef(Interned::new(type_ref)),
|
2021-05-24 08:13:23 -05:00
|
|
|
bound: Interned::new(bound),
|
2020-12-17 15:01:42 -06:00
|
|
|
},
|
2020-12-11 06:49:32 -06:00
|
|
|
},
|
|
|
|
(Either::Right(lifetime), TypeBound::Lifetime(bound)) => {
|
|
|
|
WherePredicate::Lifetime { target: lifetime, bound }
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
self.where_predicates.push(predicate);
|
2019-11-20 03:25:02 -06:00
|
|
|
}
|
|
|
|
|
2021-09-21 08:36:52 -05:00
|
|
|
pub(crate) fn fill_implicit_impl_trait_args(
|
|
|
|
&mut self,
|
|
|
|
db: &dyn DefDatabase,
|
2023-04-17 10:31:39 -05:00
|
|
|
exp: &mut Lazy<(Arc<DefMap>, Expander), impl FnOnce() -> (Arc<DefMap>, Expander)>,
|
2021-09-21 08:36:52 -05:00
|
|
|
type_ref: &TypeRef,
|
|
|
|
) {
|
2020-01-24 12:35:09 -06:00
|
|
|
type_ref.walk(&mut |type_ref| {
|
2020-01-31 08:17:48 -06:00
|
|
|
if let TypeRef::ImplTrait(bounds) = type_ref {
|
2020-01-24 12:35:09 -06:00
|
|
|
let param = TypeParamData {
|
|
|
|
name: None,
|
|
|
|
default: None,
|
|
|
|
provenance: TypeParamProvenance::ArgumentImplTrait,
|
|
|
|
};
|
2022-03-09 12:50:24 -06:00
|
|
|
let param_id = self.type_or_consts.alloc(param.into());
|
2020-01-31 08:17:48 -06:00
|
|
|
for bound in bounds {
|
2020-12-11 06:49:32 -06:00
|
|
|
self.where_predicates.push(WherePredicate::TypeBound {
|
2021-12-29 07:35:59 -06:00
|
|
|
target: WherePredicateTypeTarget::TypeOrConstParam(param_id),
|
2020-02-07 08:13:15 -06:00
|
|
|
bound: bound.clone(),
|
2020-01-31 08:17:48 -06:00
|
|
|
});
|
|
|
|
}
|
2020-01-24 12:35:09 -06:00
|
|
|
}
|
2021-09-21 08:36:52 -05:00
|
|
|
if let TypeRef::Macro(mc) = type_ref {
|
|
|
|
let macro_call = mc.to_node(db.upcast());
|
2023-04-17 10:31:39 -05:00
|
|
|
let (def_map, expander) = &mut **exp;
|
|
|
|
|
|
|
|
let module = expander.module.local_id;
|
|
|
|
let resolver = |path| {
|
|
|
|
def_map
|
|
|
|
.resolve_path(
|
|
|
|
db,
|
|
|
|
module,
|
|
|
|
&path,
|
|
|
|
crate::item_scope::BuiltinShadowMode::Other,
|
2023-05-11 01:52:13 -05:00
|
|
|
Some(MacroSubNs::Bang),
|
2023-04-17 10:31:39 -05:00
|
|
|
)
|
|
|
|
.0
|
|
|
|
.take_macros()
|
|
|
|
};
|
|
|
|
if let Ok(ExpandResult { value: Some((mark, expanded)), .. }) =
|
|
|
|
expander.enter_expand(db, macro_call, resolver)
|
|
|
|
{
|
|
|
|
let ctx = expander.ctx(db);
|
|
|
|
let type_ref = TypeRef::from_ast(&ctx, expanded.tree());
|
|
|
|
self.fill_implicit_impl_trait_args(db, &mut *exp, &type_ref);
|
2023-12-02 06:03:46 -06:00
|
|
|
exp.1.exit(mark);
|
2021-09-21 08:36:52 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-24 12:35:09 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:43:17 -06:00
|
|
|
pub(crate) fn finish(self) -> GenericParams {
|
|
|
|
let Self { mut lifetimes, mut type_or_consts, where_predicates } = self;
|
2021-04-04 20:32:43 -05:00
|
|
|
lifetimes.shrink_to_fit();
|
2024-01-09 13:43:17 -06:00
|
|
|
type_or_consts.shrink_to_fit();
|
|
|
|
GenericParams {
|
|
|
|
type_or_consts,
|
|
|
|
lifetimes,
|
|
|
|
where_predicates: where_predicates.into_boxed_slice(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParams {
|
2024-03-29 07:56:46 -05:00
|
|
|
/// Number of Generic parameters (type_or_consts + lifetimes)
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.type_or_consts.len() + self.lifetimes.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:43:17 -06:00
|
|
|
/// Iterator of type_or_consts field
|
|
|
|
pub fn iter(
|
|
|
|
&self,
|
|
|
|
) -> impl DoubleEndedIterator<Item = (Idx<TypeOrConstParamData>, &TypeOrConstParamData)> {
|
|
|
|
self.type_or_consts.iter()
|
|
|
|
}
|
|
|
|
|
2024-03-06 05:09:24 -06:00
|
|
|
/// Iterator of lifetimes field
|
|
|
|
pub fn iter_lt(
|
|
|
|
&self,
|
|
|
|
) -> impl DoubleEndedIterator<Item = (Idx<LifetimeParamData>, &LifetimeParamData)> {
|
|
|
|
self.lifetimes.iter()
|
|
|
|
}
|
|
|
|
|
2024-01-09 13:43:17 -06:00
|
|
|
pub(crate) fn generic_params_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
def: GenericDefId,
|
|
|
|
) -> Interned<GenericParams> {
|
2024-01-17 20:27:38 -06:00
|
|
|
let _p = tracing::span!(tracing::Level::INFO, "generic_params_query").entered();
|
2024-01-09 13:43:17 -06:00
|
|
|
|
|
|
|
let krate = def.module(db).krate;
|
|
|
|
let cfg_options = db.crate_graph();
|
|
|
|
let cfg_options = &cfg_options[krate].cfg_options;
|
|
|
|
|
|
|
|
// Returns the generic parameters that are enabled under the current `#[cfg]` options
|
|
|
|
let enabled_params = |params: &Interned<GenericParams>, item_tree: &ItemTree| {
|
|
|
|
let enabled = |param| item_tree.attrs(db, krate, param).is_cfg_enabled(cfg_options);
|
|
|
|
|
|
|
|
// In the common case, no parameters will by disabled by `#[cfg]` attributes.
|
|
|
|
// Therefore, make a first pass to check if all parameters are enabled and, if so,
|
|
|
|
// clone the `Interned<GenericParams>` instead of recreating an identical copy.
|
|
|
|
let all_type_or_consts_enabled =
|
|
|
|
params.type_or_consts.iter().all(|(idx, _)| enabled(idx.into()));
|
|
|
|
let all_lifetimes_enabled = params.lifetimes.iter().all(|(idx, _)| enabled(idx.into()));
|
|
|
|
|
|
|
|
if all_type_or_consts_enabled && all_lifetimes_enabled {
|
|
|
|
params.clone()
|
|
|
|
} else {
|
|
|
|
Interned::new(GenericParams {
|
|
|
|
type_or_consts: all_type_or_consts_enabled
|
|
|
|
.then(|| params.type_or_consts.clone())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
params
|
|
|
|
.type_or_consts
|
|
|
|
.iter()
|
2024-01-19 07:54:07 -06:00
|
|
|
.filter(|(idx, _)| enabled((*idx).into()))
|
|
|
|
.map(|(_, param)| param.clone())
|
2024-01-09 13:43:17 -06:00
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
lifetimes: all_lifetimes_enabled
|
|
|
|
.then(|| params.lifetimes.clone())
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
params
|
|
|
|
.lifetimes
|
|
|
|
.iter()
|
2024-01-19 07:54:07 -06:00
|
|
|
.filter(|(idx, _)| enabled((*idx).into()))
|
|
|
|
.map(|(_, param)| param.clone())
|
2024-01-09 13:43:17 -06:00
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
where_predicates: params.where_predicates.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2024-02-10 05:40:23 -06:00
|
|
|
fn id_to_generics<Id: GenericsItemTreeNode>(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
id: impl for<'db> Lookup<
|
|
|
|
Database<'db> = dyn DefDatabase + 'db,
|
|
|
|
Data = impl ItemTreeLoc<Id = Id>,
|
|
|
|
>,
|
|
|
|
enabled_params: impl Fn(&Interned<GenericParams>, &ItemTree) -> Interned<GenericParams>,
|
|
|
|
) -> Interned<GenericParams> {
|
|
|
|
let id = id.lookup(db).item_tree_id();
|
|
|
|
let tree = id.item_tree(db);
|
|
|
|
let item = &tree[id.value];
|
|
|
|
enabled_params(item.generic_params(), &tree)
|
2024-01-09 13:43:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
match def {
|
|
|
|
GenericDefId::FunctionId(id) => {
|
|
|
|
let loc = id.lookup(db);
|
|
|
|
let tree = loc.id.item_tree(db);
|
|
|
|
let item = &tree[loc.id.value];
|
|
|
|
|
|
|
|
let enabled_params = enabled_params(&item.explicit_generic_params, &tree);
|
|
|
|
|
|
|
|
let module = loc.container.module(db);
|
|
|
|
let func_data = db.function_data(id);
|
|
|
|
if func_data.params.is_empty() {
|
|
|
|
enabled_params
|
|
|
|
} else {
|
|
|
|
let mut generic_params = GenericParamsCollector {
|
|
|
|
type_or_consts: enabled_params.type_or_consts.clone(),
|
|
|
|
lifetimes: enabled_params.lifetimes.clone(),
|
|
|
|
where_predicates: enabled_params.where_predicates.clone().into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't create an `Expander` if not needed since this
|
|
|
|
// could cause a reparse after the `ItemTree` has been created due to the spanmap.
|
|
|
|
let mut expander = Lazy::new(|| {
|
|
|
|
(module.def_map(db), Expander::new(db, loc.id.file_id(), module))
|
|
|
|
});
|
|
|
|
for param in func_data.params.iter() {
|
|
|
|
generic_params.fill_implicit_impl_trait_args(db, &mut expander, param);
|
|
|
|
}
|
|
|
|
Interned::new(generic_params.finish())
|
|
|
|
}
|
|
|
|
}
|
2024-02-10 05:40:23 -06:00
|
|
|
GenericDefId::AdtId(AdtId::StructId(id)) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::AdtId(AdtId::EnumId(id)) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::AdtId(AdtId::UnionId(id)) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::TraitId(id) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::TraitAliasId(id) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::TypeAliasId(id) => id_to_generics(db, id, enabled_params),
|
|
|
|
GenericDefId::ImplId(id) => id_to_generics(db, id, enabled_params),
|
2024-01-09 13:43:17 -06:00
|
|
|
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
|
|
|
|
Interned::new(GenericParams {
|
|
|
|
type_or_consts: Default::default(),
|
|
|
|
lifetimes: Default::default(),
|
|
|
|
where_predicates: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 20:32:43 -05:00
|
|
|
}
|
|
|
|
|
2022-03-09 12:50:24 -06: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 03:00:53 -06:00
|
|
|
}
|
|
|
|
|
2022-03-09 12:50:24 -06: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 11:33:18 -06:00
|
|
|
}
|
2020-01-31 08:17:48 -06:00
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
pub fn find_trait_self_param(&self) -> Option<LocalTypeOrConstParamId> {
|
2022-03-09 12:50:24 -06:00
|
|
|
self.type_or_consts.iter().find_map(|(id, p)| {
|
2022-06-23 13:08:29 -05:00
|
|
|
matches!(
|
|
|
|
p,
|
|
|
|
TypeOrConstParamData::TypeParamData(TypeParamData {
|
|
|
|
provenance: TypeParamProvenance::TraitSelf,
|
|
|
|
..
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(|| id)
|
2020-02-07 08:13:15 -06:00
|
|
|
})
|
2020-01-31 08:17:48 -06:00
|
|
|
}
|
2024-03-06 05:09:24 -06:00
|
|
|
|
|
|
|
pub fn find_lifetime_by_name(
|
|
|
|
&self,
|
|
|
|
name: &Name,
|
|
|
|
parent: GenericDefId,
|
|
|
|
) -> Option<LifetimeParamId> {
|
|
|
|
self.lifetimes.iter().find_map(|(id, p)| {
|
|
|
|
if &p.name == name {
|
|
|
|
Some(LifetimeParamId { local_id: id, parent })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-11-20 11:33:18 -06:00
|
|
|
}
|