Skip to content

Commit 139b02b

Browse files
committed
fix: Support generics in extract_function assist
This change attempts to resolve issue #7637: Extract into Function does not create a generic function with constraints when extracting generic code. In `FunctionBody::analyze_container`, we now traverse the `ancestors` in search of `AnyHasGenericParams`, and attach any `GenericParamList`s and `WhereClause`s we find to the `ContainerInfo`. Later, in `format_function`, we collect all the `GenericParam`s and `WherePred`s from the container, and filter them to keep only types matching `TypeParam`s used within the newly extracted function body or param list. We can then include the new `GenericParamList` and `WhereClause` in the new function definition. This change only impacts `TypeParam`s. `LifetimeParam`s and `ConstParam`s are out of scope for this change.
1 parent 2836dd1 commit 139b02b

File tree

2 files changed

+436
-7
lines changed

2 files changed

+436
-7
lines changed

crates/hir/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3307,6 +3307,15 @@ impl Type {
33073307
let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), to.ty.clone()));
33083308
hir_ty::could_coerce(db, self.env.clone(), &tys)
33093309
}
3310+
3311+
pub fn as_type_param(&self, db: &dyn HirDatabase) -> Option<TypeParam> {
3312+
match self.ty.kind(Interner) {
3313+
TyKind::Placeholder(p) => Some(TypeParam {
3314+
id: TypeParamId::from_unchecked(hir_ty::from_placeholder_idx(db, *p)),
3315+
}),
3316+
_ => None,
3317+
}
3318+
}
33103319
}
33113320

33123321
#[derive(Debug)]

0 commit comments

Comments
 (0)