Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3141,7 +3141,10 @@ impl<'db> GenericSubstitution<'db> {
}
}

pub fn types(&self, db: &'db dyn HirDatabase) -> Vec<(Symbol, Type<'db>)> {
fn types_impl(
&self,
db: &'db dyn HirDatabase,
) -> impl Iterator<Item = (Ty<'db>, Option<Name>)> {
let container = match self.def {
GenericDefId::ConstId(id) => Some(id.lookup(db).container),
GenericDefId::FunctionId(id) => Some(id.lookup(db).container),
Expand All @@ -3168,9 +3171,12 @@ impl<'db> GenericSubstitution<'db> {
TypeOrConstParamData::TypeParamData(param) => Some(param.name.clone()),
TypeOrConstParamData::ConstParamData(_) => None,
});
self.subst
.types()
.zip(container_type_params.into_iter().flatten().chain(type_params))
self.subst.types().zip(container_type_params.into_iter().flatten().chain(type_params))
}

/// Returns type arguments with type parameter names.
pub fn types(&self, db: &'db dyn HirDatabase) -> Vec<(Symbol, Type<'db>)> {
self.types_impl(db)
.filter_map(|(ty, name)| {
Some((
name?.symbol().clone(),
Expand All @@ -3179,6 +3185,18 @@ impl<'db> GenericSubstitution<'db> {
})
.collect()
}

/// Returns all type arguments, including unnamed ones such as implicit `impl Trait` parameters.
///
/// Unlike [`Self::types`], this does not filter out arguments that have no
/// corresponding type parameter name.
// Note: this method is not used by rust-analyzer itself as of writing, but is
// used by external tools, so please do not remove it even if it appears unused.
pub fn all_types(&self, db: &'db dyn HirDatabase) -> Vec<Type<'db>> {
self.types_impl(db)
.map(|(ty, _)| Type { ty: EarlyBinder::bind(ty), owner: self.owner })
.collect()
}
}

/// A single local definition.
Expand Down
Loading