Skip to content

Commit 7bf175f

Browse files
yodaldevoidvarkor
authored andcommitted
impl fold_const for RegionFudger
Signed-off-by: Gabriel Smith <ga29smith@gmail.com>
1 parent ef1b2ac commit 7bf175f

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/librustc/infer/const_variable.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ty::{self, InferConst};
55

66
use std::cmp;
77
use std::marker::PhantomData;
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_data_structures::snapshot_vec as sv;
910
use rustc_data_structures::unify as ut;
1011

@@ -23,6 +24,8 @@ pub enum ConstVariableOrigin {
2324
SubstitutionPlaceholder(Span),
2425
}
2526

27+
pub type ConstVariableMap<'tcx> = FxHashMap<ty::ConstVid<'tcx>, ConstVariableOrigin>;
28+
2629
struct ConstVariableData {
2730
origin: ConstVariableOrigin,
2831
}
@@ -184,6 +187,32 @@ impl<'tcx> ConstVariableTable<'tcx> {
184187
self.values.commit(snapshot);
185188
self.relations.commit(relation_snapshot);
186189
}
190+
191+
/// Returns a map `{V1 -> V2}`, where the keys `{V1}` are
192+
/// const-variables created during the snapshot, and the values
193+
/// `{V2}` are the root variables that they were unified with,
194+
/// along with their origin.
195+
pub fn consts_created_since_snapshot(
196+
&mut self,
197+
s: &Snapshot<'tcx>
198+
) -> ConstVariableMap<'tcx> {
199+
let actions_since_snapshot = self.values.actions_since_snapshot(&s.snapshot);
200+
201+
actions_since_snapshot
202+
.iter()
203+
.filter_map(|action| match action {
204+
&sv::UndoLog::NewElem(index) => Some(ty::ConstVid {
205+
index: index as u32,
206+
phantom: PhantomData,
207+
}),
208+
_ => None,
209+
})
210+
.map(|vid| {
211+
let origin = self.values.get(vid.index as usize).origin.clone();
212+
(vid, origin)
213+
})
214+
.collect()
215+
}
187216
}
188217

189218
impl<'tcx> ut::UnifyKey for ty::ConstVid<'tcx> {

src/librustc/infer/fudge.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::ty::{self, Ty, TyCtxt, TyVid, IntVid, FloatVid, RegionVid};
22
use crate::ty::fold::{TypeFoldable, TypeFolder};
3+
use crate::mir::interpret::ConstValue;
34

45
use super::InferCtxt;
56
use super::RegionVariableOrigin;
@@ -176,6 +177,33 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for InferenceFudger<'a, 'gcx, 'tcx>
176177
}
177178

178179
fn fold_const(&mut self, ct: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
179-
ct // FIXME(const_generics)
180+
if let ty::LazyConst::Evaluated(ty::Const {
181+
val: ConstValue::Infer(ty::InferConst::Var(vid)),
182+
ty,
183+
}) = *ct {
184+
match self.const_variables.get(&vid) {
185+
None => {
186+
// This variable was created before the
187+
// "fudging". Since we refresh all
188+
// variables to their binding anyhow, we know
189+
// that it is unbound, so we can just return
190+
// it.
191+
debug_assert!(
192+
self.infcx.const_unification_table.borrow_mut()
193+
.probe(vid)
194+
.is_unknown()
195+
);
196+
ct
197+
}
198+
Some(&origin) => {
199+
// This variable was created during the
200+
// fudging. Recreate it with a fresh variable
201+
// here.
202+
self.infcx.next_const_var(ty, origin)
203+
}
204+
}
205+
} else {
206+
ct.super_fold_with(self)
207+
}
180208
}
181209
}

0 commit comments

Comments
 (0)