about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 15:51:48 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-08-24 19:57:05 +0200
commit2ab69aef03f9744381e4a0cedb397c34d191f4f7 (patch)
tree8b492811789fbe3bf2b4abbab749af98b0be848c
parent97986b57046a7af6d5ec1eeca7a19346131dec59 (diff)
downloadrust-2ab69aef03f9744381e4a0cedb397c34d191f4f7.tar.gz
rust-2ab69aef03f9744381e4a0cedb397c34d191f4f7.zip
typeck/pat.rs: extract `new_ref_ty`.
-rw-r--r--src/librustc_typeck/check/pat.rs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs
index 1f6f7901c9e..ac025754ffb 100644
--- a/src/librustc_typeck/check/pat.rs
+++ b/src/librustc_typeck/check/pat.rs
@@ -426,9 +426,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 // 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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         }
     }
 
+    /// 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,