]> git.lizzy.rs Git - rust.git/commitdiff
typeck/pat.rs: extract `new_ref_ty`.
authorMazdak Farrokhzad <twingoow@gmail.com>
Sat, 24 Aug 2019 13:51:48 +0000 (15:51 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Sat, 24 Aug 2019 17:57:05 +0000 (19:57 +0200)
src/librustc_typeck/check/pat.rs

index 1f6f7901c9e6e909b89d7f9fb075cff7fa8b7ea0..ac025754ffbae04830d27eb2db8ecc6f1ce6ea9d 100644 (file)
@@ -426,9 +426,7 @@ fn check_pat_ident(
                 // If the binding is like `ref x | ref const x | ref mut x`
                 // then `x` is assigned a value of type `&M T` where M is the
                 // mutability and T is the expected type.
-                let region_var = self.next_region_var(infer::PatternRegion(pat.span));
-                let mt = ty::TypeAndMut { ty: expected, mutbl };
-                let region_ty = self.tcx.mk_ref(region_var, mt);
+                let region_ty = self.new_ref_ty(pat.span, mutbl, expected);
 
                 // `x` is assigned a value of type `&M T`, hence `&M T <: typeof(x)`
                 // is required. However, we use equality, which is stronger.
@@ -971,9 +969,7 @@ fn check_pat_ref(
                             span: inner.span,
                         }
                     );
-                    let mt = ty::TypeAndMut { ty: inner_ty, mutbl };
-                    let region = self.next_region_var(infer::PatternRegion(pat.span));
-                    let rptr_ty = tcx.mk_ref(region, mt);
+                    let rptr_ty = self.new_ref_ty(pat.span, mutbl, inner_ty);
                     debug!("check_pat_ref: demanding {:?} = {:?}", expected, rptr_ty);
                     let err = self.demand_eqtype_diag(pat.span, expected, rptr_ty);
 
@@ -995,6 +991,13 @@ fn check_pat_ref(
         }
     }
 
+    /// Create a reference type with a fresh region variable.
+    fn new_ref_ty(&self, span: Span, mutbl: hir::Mutability, ty: Ty<'tcx>) -> Ty<'tcx> {
+        let region = self.next_region_var(infer::PatternRegion(span));
+        let mt = ty::TypeAndMut { ty, mutbl };
+        self.tcx.mk_ref(region, mt)
+    }
+
     fn check_pat_slice(
         &self,
         span: Span,