mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
expose hir::Type::type_paramters
Used to get E parameter from `Result<T, E>`
This commit is contained in:
parent
4be260d693
commit
37a8cec638
2 changed files with 17 additions and 24 deletions
|
@ -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)))
|
make::ty_generic(make::name_ref("Option"), iter::once(fun_ty.make_ty(ctx, module)))
|
||||||
}
|
}
|
||||||
FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
|
FlowHandler::Try { kind: TryKind::Result { ty: parent_ret_ty } } => {
|
||||||
let handler_ty =
|
let handler_ty = parent_ret_ty
|
||||||
result_err_ty(parent_ret_ty, ctx, module).unwrap_or_else(make::ty_unit);
|
.type_parameters()
|
||||||
|
.nth(1)
|
||||||
|
.map(|ty| make_ty(&ty, ctx, module))
|
||||||
|
.unwrap_or_else(make::ty_unit);
|
||||||
make::ty_generic(
|
make::ty_generic(
|
||||||
make::name_ref("Result"),
|
make::name_ref("Result"),
|
||||||
vec![fun_ty.make_ty(ctx, module), handler_ty],
|
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))
|
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(
|
fn make_body(
|
||||||
ctx: &AssistContext,
|
ctx: &AssistContext,
|
||||||
old_indent: IndentLevel,
|
old_indent: IndentLevel,
|
||||||
|
|
|
@ -1802,6 +1802,18 @@ impl Type {
|
||||||
None
|
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>(
|
pub fn iterate_method_candidates<T>(
|
||||||
&self,
|
&self,
|
||||||
db: &dyn HirDatabase,
|
db: &dyn HirDatabase,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue