]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_ty/src/infer/pat.rs
Merge #9558
[rust.git] / crates / hir_ty / src / infer / pat.rs
index 60b94a64222d367c02cdd8e0a1eec015b361bd46..603237f944beec4e1f8df3e279895ee8441538a5 100644 (file)
 };
 use hir_expand::name::Name;
 
-use super::{BindingMode, Expectation, InferenceContext};
+use super::{BindingMode, Expectation, InferenceContext, TypeMismatch};
 use crate::{
-    lower::lower_to_chalk_mutability, static_lifetime, Interner, Substitution, Ty, TyBuilder,
-    TyExt, TyKind,
+    infer::{Adjust, Adjustment, AutoBorrow},
+    lower::lower_to_chalk_mutability,
+    static_lifetime, Interner, Substitution, Ty, TyBuilder, TyExt, TyKind,
 };
 
 impl<'a> InferenceContext<'a> {
@@ -26,7 +27,7 @@ fn infer_tuple_struct_pat(
         id: PatId,
         ellipsis: Option<usize>,
     ) -> Ty {
-        let (ty, def) = self.resolve_variant(path);
+        let (ty, def) = self.resolve_variant(path, true);
         let var_data = def.map(|it| it.variant_data(self.db.upcast()));
         if let Some(variant) = def {
             self.write_variant_resolution(id.into(), variant);
@@ -67,7 +68,7 @@ fn infer_record_pat(
         default_bm: BindingMode,
         id: PatId,
     ) -> Ty {
-        let (ty, def) = self.resolve_variant(path);
+        let (ty, def) = self.resolve_variant(path, false);
         let var_data = def.map(|it| it.variant_data(self.db.upcast()));
         if let Some(variant) = def {
             self.write_variant_resolution(id.into(), variant);
@@ -94,20 +95,31 @@ fn infer_record_pat(
     pub(super) fn infer_pat(
         &mut self,
         pat: PatId,
-        mut expected: &Ty,
+        expected: &Ty,
         mut default_bm: BindingMode,
     ) -> Ty {
         let body = Arc::clone(&self.body); // avoid borrow checker problem
+        let mut expected = self.resolve_ty_shallow(expected);
 
         if is_non_ref_pat(&body, pat) {
+            let mut pat_adjustments = Vec::new();
             while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
-                expected = inner;
+                pat_adjustments.push(Adjustment {
+                    target: expected.clone(),
+                    kind: Adjust::Borrow(AutoBorrow::Ref(mutability)),
+                });
+                expected = self.resolve_ty_shallow(inner);
                 default_bm = match default_bm {
                     BindingMode::Move => BindingMode::Ref(mutability),
                     BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not),
                     BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
                 }
             }
+
+            if !pat_adjustments.is_empty() {
+                pat_adjustments.shrink_to_fit();
+                self.result.pat_adjustments.insert(pat, pat_adjustments);
+            }
         } else if let Pat::Ref { .. } = &body[pat] {
             cov_mark::hit!(match_ergonomics_ref);
             // When you encounter a `&pat` pattern, reset to Move.
@@ -147,9 +159,9 @@ pub(super) fn infer_pat(
             }
             Pat::Or(ref pats) => {
                 if let Some((first_pat, rest)) = pats.split_first() {
-                    let ty = self.infer_pat(*first_pat, expected, default_bm);
+                    let ty = self.infer_pat(*first_pat, &expected, default_bm);
                     for pat in rest {
-                        self.infer_pat(*pat, expected, default_bm);
+                        self.infer_pat(*pat, &expected, default_bm);
                     }
                     ty
                 } else {
@@ -173,18 +185,18 @@ pub(super) fn infer_pat(
             Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
                 p.as_deref(),
                 subpats,
-                expected,
+                &expected,
                 default_bm,
                 pat,
                 *ellipsis,
             ),
             Pat::Record { path: p, args: fields, ellipsis: _ } => {
-                self.infer_record_pat(p.as_deref(), fields, expected, default_bm, pat)
+                self.infer_record_pat(p.as_deref(), fields, &expected, default_bm, pat)
             }
             Pat::Path(path) => {
                 // FIXME use correct resolver for the surrounding expression
                 let resolver = self.resolver.clone();
-                self.infer_path(&resolver, &path, pat.into()).unwrap_or(self.err_ty())
+                self.infer_path(&resolver, path, pat.into()).unwrap_or_else(|| self.err_ty())
             }
             Pat::Bind { mode, name: _, subpat } => {
                 let mode = if mode == &BindingAnnotation::Unannotated {
@@ -193,9 +205,9 @@ pub(super) fn infer_pat(
                     BindingMode::convert(*mode)
                 };
                 let inner_ty = if let Some(subpat) = subpat {
-                    self.infer_pat(*subpat, expected, default_bm)
+                    self.infer_pat(*subpat, &expected, default_bm)
                 } else {
-                    expected.clone()
+                    expected
                 };
                 let inner_ty = self.insert_type_vars_shallow(inner_ty);
 
@@ -206,7 +218,6 @@ pub(super) fn infer_pat(
                     }
                     BindingMode::Move => inner_ty.clone(),
                 };
-                let bound_ty = self.resolve_ty_as_possible(bound_ty);
                 self.write_pat_ty(pat, bound_ty);
                 return inner_ty;
             }
@@ -265,10 +276,11 @@ pub(super) fn infer_pat(
         };
         // use a new type variable if we got error type here
         let ty = self.insert_type_vars_shallow(ty);
-        if !self.unify(&ty, expected) {
-            // FIXME record mismatch, we need to change the type of self.type_mismatches for that
+        if !self.unify(&ty, &expected) {
+            self.result
+                .type_mismatches
+                .insert(pat.into(), TypeMismatch { expected, actual: ty.clone() });
         }
-        let ty = self.resolve_ty_as_possible(ty);
         self.write_pat_ty(pat, ty.clone());
         ty
     }
@@ -289,6 +301,11 @@ fn is_non_ref_pat(body: &hir_def::body::Body, pat: PatId) -> bool {
             Expr::Literal(Literal::String(..)) => false,
             _ => true,
         },
+        Pat::Bind {
+            mode: BindingAnnotation::Mutable | BindingAnnotation::Unannotated,
+            subpat: Some(subpat),
+            ..
+        } => is_non_ref_pat(body, *subpat),
         Pat::Wild | Pat::Bind { .. } | Pat::Ref { .. } | Pat::Box { .. } | Pat::Missing => false,
     }
 }