expose hir::Type::type_paramters

Used to get E parameter from `Result<T, E>`
This commit is contained in:
Vladyslav Katasonov 2021-02-13 21:46:21 +03:00
parent 4be260d693
commit 37a8cec638
2 changed files with 17 additions and 24 deletions

View File

@ -1131,8 +1131,11 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti
make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module)))
}
FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
let handler_ty =
result_err_ty(parent_ret_ty, ctx, module).unwrap_or_else(make::ty_unit);
let handler_ty = parent_ret_ty
.type_parameters()
.nth(1)
.map(|ty| make_ty(&ty, ctx, module))
.unwrap_or_else(make::ty_unit);
make::ty_generic(
make::name_ref("Result"),
vec![fun_ty.make_ty(ctx, module), handler_ty],
@ -1161,28 +1164,6 @@ fn make_ret_ty(ctx: &AssistContext, module: hir::Module, fun: &Function) -> Opti
Some(make::ret_type(ret_ty))
}
/// Extract `E` type from `Result<T, E>`
fn result_err_ty(
parent_ret_ty: &hir::Type,
ctx: &AssistContext,
module: hir::Module,
) -> Option<ast::Type> {
// FIXME: use hir to extract argument information
// currently we use `format -> take part -> parse`
let path_ty = match make_ty(&parent_ret_ty, ctx, module) {
ast::Type::PathType(path_ty) => path_ty,
_ => return None,
};
let arg_list = path_ty.path()?.segment()?.generic_arg_list()?;
let err_arg = arg_list.generic_args().nth(1)?;
let type_arg = match err_arg {
ast::GenericArg::TypeArg(type_arg) => type_arg,
_ => return None,
};
type_arg.ty()
}
fn make_body(
ctx: &AssistContext,
old_indent: IndentLevel,

View File

@ -1802,6 +1802,18 @@ impl Type {
None
}
pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
let ty = self.ty.value.strip_references();
let substs = match ty {
Ty::Apply(apply_ty) => &apply_ty.parameters,
Ty::Opaque(opaque_ty) => &opaque_ty.parameters,
_ => return Either::Left(iter::empty()),
};
let iter = substs.iter().map(move |ty| self.derived(ty.clone()));
Either::Right(iter)
}
pub fn iterate_method_candidates<T>(
&self,
db: &dyn HirDatabase,