1- use crate :: ty:: { self , Ty , TyCtxt , TyVid , IntVid , FloatVid , RegionVid } ;
1+ use crate :: ty:: { self , Ty , TyCtxt , TyVid , IntVid , FloatVid , RegionVid , ConstVid } ;
22use crate :: ty:: fold:: { TypeFoldable , TypeFolder } ;
33use crate :: mir:: interpret:: ConstValue ;
44
55use super :: InferCtxt ;
6- use super :: RegionVariableOrigin ;
6+ use super :: { RegionVariableOrigin , ConstVariableOrigin } ;
77use super :: type_variable:: TypeVariableOrigin ;
88
9+ use rustc_data_structures:: unify as ut;
10+ use ut:: UnifyKey ;
11+
12+ use std:: cell:: RefMut ;
913use std:: ops:: Range ;
1014
15+ fn const_vars_since_snapshot < ' tcx > (
16+ mut table : RefMut < ' _ , ut:: UnificationTable < ut:: InPlace < ConstVid < ' tcx > > > > ,
17+ snapshot : & ut:: Snapshot < ut:: InPlace < ConstVid < ' tcx > > > ,
18+ ) -> ( Range < ConstVid < ' tcx > > , Vec < ConstVariableOrigin > ) {
19+ let range = table. vars_since_snapshot ( snapshot) ;
20+ ( range. start ..range. end , ( range. start . index ..range. end . index ) . map ( |index| {
21+ table. probe_value ( ConstVid :: from_index ( index) ) . origin . clone ( )
22+ } ) . collect ( ) )
23+ }
24+
1125impl < ' a , ' gcx , ' tcx > InferCtxt < ' a , ' gcx , ' tcx > {
1226 /// This rather funky routine is used while processing expected
1327 /// types. What happens here is that we want to propagate a
@@ -80,13 +94,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
8094 let region_vars = self . borrow_region_constraints ( ) . vars_since_snapshot (
8195 & snapshot. region_constraints_snapshot ,
8296 ) ;
97+ let const_vars = const_vars_since_snapshot (
98+ self . const_unification_table . borrow_mut ( ) ,
99+ & snapshot. const_snapshot ,
100+ ) ;
83101
84102 let fudger = InferenceFudger {
85103 infcx : self ,
86104 type_vars,
87105 int_vars,
88106 float_vars,
89107 region_vars,
108+ const_vars,
90109 } ;
91110
92111 Ok ( ( fudger, value) )
@@ -105,7 +124,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
105124 if fudger. type_vars . 0 . is_empty ( ) &&
106125 fudger. int_vars . is_empty ( ) &&
107126 fudger. float_vars . is_empty ( ) &&
108- fudger. region_vars . 0 . is_empty ( ) {
127+ fudger. region_vars . 0 . is_empty ( ) &&
128+ fudger. const_vars . 0 . is_empty ( ) {
109129 Ok ( value)
110130 } else {
111131 Ok ( value. fold_with ( & mut fudger) )
@@ -119,6 +139,7 @@ pub struct InferenceFudger<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
119139 int_vars : Range < IntVid > ,
120140 float_vars : Range < FloatVid > ,
121141 region_vars : ( Range < RegionVid > , Vec < RegionVariableOrigin > ) ,
142+ const_vars : ( Range < ConstVid < ' tcx > > , Vec < ConstVariableOrigin > ) ,
122143}
123144
124145impl < ' a , ' gcx , ' tcx > TypeFolder < ' gcx , ' tcx > for InferenceFudger < ' a , ' gcx , ' tcx > {
@@ -166,9 +187,9 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for InferenceFudger<'a, 'gcx, 'tcx>
166187 }
167188
168189 fn fold_region ( & mut self , r : ty:: Region < ' tcx > ) -> ty:: Region < ' tcx > {
169- if let ty:: ReVar ( vid) = r {
190+ if let ty:: ReVar ( vid) = * r {
170191 if self . region_vars . 0 . contains ( & vid) {
171- let idx = ( vid. index ( ) - self . region_vars . 0 . start . index ( ) ) as usize ;
192+ let idx = vid. index ( ) - self . region_vars . 0 . start . index ( ) ;
172193 let origin = self . region_vars . 1 [ idx] ;
173194 return self . infcx . next_region_var ( origin) ;
174195 }
@@ -177,14 +198,12 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for InferenceFudger<'a, 'gcx, 'tcx>
177198 }
178199
179200 fn fold_const ( & mut self , ct : & ' tcx ty:: Const < ' tcx > ) -> & ' tcx ty:: Const < ' tcx > {
180- if let ty:: Const { val : ConstValue :: Infer ( ty:: InferConst :: Var ( vid) ) , ty } = * ct {
181- if self . const_variables . contains ( & vid) {
182- // This variable was created during the
183- // fudging. Recreate it with a fresh variable
184- // here.
185- let origin = self . infcx . const_unification_table . borrow_mut ( )
186- . probe_value ( vid)
187- . origin ;
201+ if let ty:: Const { val : ConstValue :: Infer ( ty:: InferConst :: Var ( vid) ) , ty } = ct {
202+ if self . const_vars . 0 . contains ( & vid) {
203+ // This variable was created during the fudging.
204+ // Recreate it with a fresh variable here.
205+ let idx = ( vid. index - self . const_vars . 0 . start . index ) as usize ;
206+ let origin = self . const_vars . 1 [ idx] ;
188207 self . infcx . next_const_var ( ty, origin)
189208 } else {
190209 ct
0 commit comments