Skip to content

Commit

Permalink
fix: Support generics in extract_function assist
Browse files Browse the repository at this point in the history
This change attempts to resolve issue rust-lang#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.
  • Loading branch information
DorianListens committed Jul 13, 2022
1 parent 794ecd5 commit e565eec
Show file tree
Hide file tree
Showing 2 changed files with 436 additions and 7 deletions.
9 changes: 9 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3307,6 +3307,15 @@ impl Type {
let tys = hir_ty::replace_errors_with_variables(&(self.ty.clone(), to.ty.clone()));
hir_ty::could_coerce(db, self.env.clone(), &tys)
}

pub fn as_type_param(&self, db: &dyn HirDatabase) -> Option<TypeParam> {
match self.ty.kind(Interner) {
TyKind::Placeholder(p) => Some(TypeParam {
id: TypeParamId::from_unchecked(hir_ty::from_placeholder_idx(db, *p)),
}),
_ => None,
}
}
}

#[derive(Debug)]
Expand Down
Loading

0 comments on commit e565eec

Please sign in to comment.