From 22240eabf8dca333adb1d1976044b6a4304cf959 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Sun, 20 Sep 2026 05:38:34 +0900 Subject: [PATCH] feat: add all_types that exposes implicit type parameters from GenericSubstitution --- crates/hir/src/lib.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 43fc2bae26ee..10aa51adfa9b 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -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, Option)> { let container = match self.def { GenericDefId::ConstId(id) => Some(id.lookup(db).container), GenericDefId::FunctionId(id) => Some(id.lookup(db).container), @@ -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(), @@ -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> { + self.types_impl(db) + .map(|(ty, _)| Type { ty: EarlyBinder::bind(ty), owner: self.owner }) + .collect() + } } /// A single local definition.