]> git.lizzy.rs Git - rust.git/commitdiff
Implement fold_const for BoundVarReplacer
authorvarkor <github@varkor.com>
Sat, 9 Mar 2019 16:54:50 +0000 (16:54 +0000)
committervarkor <github@varkor.com>
Wed, 1 May 2019 22:10:57 +0000 (23:10 +0100)
src/librustc/infer/canonical/substitute.rs
src/librustc/infer/higher_ranked/mod.rs
src/librustc/infer/mod.rs
src/librustc/ty/fold.rs

index 5af4e8366818bfee581c52356dba8c459462dd2e..6b716d6c3b8ea2831cb104277f59674f7117a5a6 100644 (file)
@@ -70,6 +70,13 @@ pub(super) fn substitute_value<'a, 'tcx, T>(
             }
         };
 
-        tcx.replace_escaping_bound_vars(value, fld_r, fld_t).0
+        let fld_c = |bound_ct: ty::BoundVar, _| {
+            match var_values.var_values[bound_ct].unpack() {
+                UnpackedKind::Const(ct) => ct,
+                c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
+            }
+        };
+
+        tcx.replace_escaping_bound_vars(value, fld_r, fld_t, fld_c).0
     }
 }
index 7c83fe7fd69467c6eb68e971cacc55356973aff5..ec77bb39e7d2afd6a22a75c5aaf173e156c3a955 100644 (file)
@@ -4,10 +4,12 @@
 use super::combine::CombineFields;
 use super::{HigherRankedType, InferCtxt, PlaceholderMap};
 
-use crate::infer::CombinedSnapshot;
+use crate::infer::{CombinedSnapshot, ConstVariableOrigin};
 use crate::ty::relate::{Relate, RelateResult, TypeRelation};
 use crate::ty::{self, Binder, TypeFoldable};
 
+use syntax_pos::DUMMY_SP;
+
 impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
     pub fn higher_ranked_sub<T>(
         &mut self,
@@ -99,7 +101,16 @@ pub fn replace_bound_vars_with_placeholders<T>(
             }))
         };
 
-        let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t);
+        let fld_c = |_: ty::BoundVar, ty| {
+            self.next_const_var_in_universe(
+                ty,
+                // FIXME(const_generics): do we want a placeholder const?
+                ConstVariableOrigin::MiscVariable(DUMMY_SP),
+                next_universe,
+            )
+        };
+
+        let (result, map) = self.tcx.replace_bound_vars(binder, fld_r, fld_t, fld_c);
 
         debug!(
             "replace_bound_vars_with_placeholders(\
index 12321f2e355c3b78c742ec4d29cc081d59e509e8..80816100faed7885d962672bbdb30293229d64c1 100644 (file)
@@ -1476,7 +1476,8 @@ pub fn replace_bound_vars_with_fresh_vars<T>(
     {
         let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
         let fld_t = |_| self.next_ty_var(TypeVariableOrigin::MiscVariable(span));
-        self.tcx.replace_bound_vars(value, fld_r, fld_t)
+        let fld_c = |_, ty| self.next_const_var(ty, ConstVariableOrigin::MiscVariable(span));
+        self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
     }
 
     /// See the [`region_constraints::verify_generic_bound`] method.
index 462bba94dd3f48be7be5221699ea57bbcce09324..13b5885f51d293db21aa150f845b1408807bf262 100644 (file)
@@ -431,22 +431,26 @@ struct BoundVarReplacer<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
 
     fld_r: &'a mut (dyn FnMut(ty::BoundRegion) -> ty::Region<'tcx> + 'a),
     fld_t: &'a mut (dyn FnMut(ty::BoundTy) -> Ty<'tcx> + 'a),
+    fld_c: &'a mut (dyn FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::LazyConst<'tcx> + 'a),
 }
 
 impl<'a, 'gcx, 'tcx> BoundVarReplacer<'a, 'gcx, 'tcx> {
-    fn new<F, G>(
+    fn new<F, G, H>(
         tcx: TyCtxt<'a, 'gcx, 'tcx>,
         fld_r: &'a mut F,
-        fld_t: &'a mut G
+        fld_t: &'a mut G,
+        fld_c: &'a mut H,
     ) -> Self
         where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
-              G: FnMut(ty::BoundTy) -> Ty<'tcx>
+              G: FnMut(ty::BoundTy) -> Ty<'tcx>,
+              H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::LazyConst<'tcx>,
     {
         BoundVarReplacer {
             tcx,
             current_index: ty::INNERMOST,
             fld_r,
             fld_t,
+            fld_c,
         }
     }
 }
@@ -508,7 +512,29 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
     }
 
     fn fold_const(&mut self, ct: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
-        ct // FIXME(const_generics)
+        if let ty::LazyConst::Evaluated(ty::Const {
+            val: ConstValue::Infer(ty::InferConst::Canonical(debruijn, bound_const)),
+            ty,
+        }) = *ct {
+            if debruijn == self.current_index {
+                let fld_c = &mut self.fld_c;
+                let ct = fld_c(bound_const, ty);
+                ty::fold::shift_vars(
+                    self.tcx,
+                    &ct,
+                    self.current_index.as_u32()
+                )
+            } else {
+                ct
+            }
+        } else {
+            if !ct.has_vars_bound_at_or_above(self.current_index) {
+                // Nothing more to substitute.
+                ct
+            } else {
+                ct.super_fold_with(self)
+            }
+        }
     }
 }
 
@@ -532,27 +558,34 @@ pub fn replace_late_bound_regions<T, F>(
         where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
               T: TypeFoldable<'tcx>
     {
-        // identity for bound types
+        // identity for bound types and consts
         let fld_t = |bound_ty| self.mk_ty(ty::Bound(ty::INNERMOST, bound_ty));
-        self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t)
+        let fld_c = |bound_ct, ty| {
+            self.mk_const_infer(ty::InferConst::Canonical(ty::INNERMOST, bound_ct), ty)
+        };
+        self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t, fld_c)
     }
 
     /// Replaces all escaping bound vars. The `fld_r` closure replaces escaping
-    /// bound regions while the `fld_t` closure replaces escaping bound types.
-    pub fn replace_escaping_bound_vars<T, F, G>(
+    /// bound regions; the `fld_t` closure replaces escaping bound types and the `fld_c`
+    /// closure replaces escaping bound consts.
+    pub fn replace_escaping_bound_vars<T, F, G, H>(
         self,
         value: &T,
         mut fld_r: F,
-        mut fld_t: G
+        mut fld_t: G,
+        mut fld_c: H,
     ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
         where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
               G: FnMut(ty::BoundTy) -> Ty<'tcx>,
-              T: TypeFoldable<'tcx>
+              H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::LazyConst<'tcx>,
+              T: TypeFoldable<'tcx>,
     {
         use rustc_data_structures::fx::FxHashMap;
 
         let mut region_map = BTreeMap::new();
         let mut type_map = FxHashMap::default();
+        let mut const_map = FxHashMap::default();
 
         if !value.has_escaping_bound_vars() {
             (value.clone(), region_map)
@@ -565,7 +598,16 @@ pub fn replace_escaping_bound_vars<T, F, G>(
                 *type_map.entry(bound_ty).or_insert_with(|| fld_t(bound_ty))
             };
 
-            let mut replacer = BoundVarReplacer::new(self, &mut real_fld_r, &mut real_fld_t);
+            let mut real_fld_c = |bound_ct, ty| {
+                *const_map.entry(bound_ct).or_insert_with(|| fld_c(bound_ct, ty))
+            };
+
+            let mut replacer = BoundVarReplacer::new(
+                self,
+                &mut real_fld_r,
+                &mut real_fld_t,
+                &mut real_fld_c,
+            );
             let result = value.fold_with(&mut replacer);
             (result, region_map)
         }
@@ -574,17 +616,19 @@ pub fn replace_escaping_bound_vars<T, F, G>(
     /// Replaces all types or regions bound by the given `Binder`. The `fld_r`
     /// closure replaces bound regions while the `fld_t` closure replaces bound
     /// types.
-    pub fn replace_bound_vars<T, F, G>(
+    pub fn replace_bound_vars<T, F, G, H>(
         self,
         value: &Binder<T>,
         fld_r: F,
-        fld_t: G
+        fld_t: G,
+        fld_c: H,
     ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
         where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
               G: FnMut(ty::BoundTy) -> Ty<'tcx>,
+              H: FnMut(ty::BoundVar, Ty<'tcx>) -> &'tcx ty::LazyConst<'tcx>,
               T: TypeFoldable<'tcx>
     {
-        self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t)
+        self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t, fld_c)
     }
 
     /// Replaces any late-bound regions bound in `value` with