Work on type inference for ADT patterns

This commit is contained in:
Marcus Klaas de Vries 2019-01-16 16:34:33 +01:00 committed by Aleksey Kladov
parent 5648dcd36e
commit 3b0de53904
2 changed files with 32 additions and 3 deletions

View file

@ -876,6 +876,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
// a Ty itself
fn infer_pat(&mut self, pat: PatId, expected: &Expectation) -> Ty {
let body = Arc::clone(&self.body); // avoid borrow checker problem
match (&body[pat], &expected.ty) {
(Pat::Tuple(ref args), &Ty::Tuple(ref tuple_args))
if args.len() == tuple_args.len() =>
@ -890,6 +891,31 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
{
self.infer_pat(pat, &Expectation::has_type((&**sub_ty).clone()));
}
(pattern, &Ty::Adt { def_id, .. }) => {
let adt_def = def_id.resolve(self.db);
match (pattern, adt_def) {
(&Pat::Struct, Def::Struct(s)) => {}
(
&Pat::TupleStruct {
path: ref p,
args: ref sub_pats,
},
Def::Enum(ref e),
) => {
// TODO: resolve enum
}
(
&Pat::TupleStruct {
path: ref p,
args: ref sub_pats,
},
Def::EnumVariant(ref e),
) => {
let variant_data = self.db.enum_variant_data(e.def_id);
}
_ => {}
}
}
// TODO: implement more
(_, ref _expected_ty) => {}
};
@ -1197,7 +1223,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
decl_ty
};
self.infer_pat(*pat, &Expectation::has_type(ty))?;
self.infer_pat(*pat, &Expectation::has_type(ty));
}
Statement::Expr(expr) => {
self.infer_expr(*expr, &Expectation::none());
@ -1218,7 +1244,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let ty = self.make_ty(type_ref);
let ty = self.insert_type_vars(ty);
self.infer_pat(*pat, &Expectation::has_type(ty))?;
self.infer_pat(*pat, &Expectation::has_type(ty));
}
self.return_ty = {
let ty = self.make_ty(signature.ret_type());

View file

@ -362,7 +362,10 @@ fn test(x: &str, y: isize) {
fn infer_pattern() {
check_inference(
r#"
enum E { A { x: usize }, B }
enum E {
A { x: usize },
B
}
fn test(x: &i32) {
let y = x;