]> 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 8f5db1f407fef5c7337b1b59de655b8841379b1d..603237f944beec4e1f8df3e279895ee8441538a5 100644 (file)
@@ -12,8 +12,9 @@
 
 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);
@@ -103,7 +104,10 @@ pub(super) fn infer_pat(
         if is_non_ref_pat(&body, pat) {
             let mut pat_adjustments = Vec::new();
             while let Some((inner, _lifetime, mutability)) = expected.as_reference() {
-                pat_adjustments.push(expected.clone());
+                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),
@@ -192,7 +196,7 @@ pub(super) fn infer_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 {
@@ -275,7 +279,7 @@ pub(super) fn infer_pat(
         if !self.unify(&ty, &expected) {
             self.result
                 .type_mismatches
-                .insert(pat.into(), TypeMismatch { expected: expected, actual: ty.clone() });
+                .insert(pat.into(), TypeMismatch { expected, actual: ty.clone() });
         }
         self.write_pat_ty(pat, ty.clone());
         ty
@@ -297,10 +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, subpat: Some(subpat), .. }
-        | Pat::Bind { mode: BindingAnnotation::Unannotated, subpat: Some(subpat), .. } => {
-            is_non_ref_pat(body, *subpat)
-        }
+        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,
     }
 }