Skip to content

Commit e5dc4ba

Browse files
committed
renumber types in ty::Const and relate them to mir::Constant
1 parent 7b637b7 commit e5dc4ba

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,17 @@ pub(in borrow_check) fn replace_regions_in_mir<'cx, 'gcx, 'tcx>(
4343
def_id: DefId,
4444
mir: &mut Mir<'tcx>,
4545
) -> UniversalRegions<'tcx> {
46+
debug!("replace_regions_in_mir(def_id={:?})", def_id);
47+
4648
// Compute named region information.
4749
let universal_regions = universal_regions::universal_regions(infcx, def_id);
4850

4951
// Replace all regions with fresh inference variables.
5052
renumber::renumber_mir(infcx, &universal_regions, mir);
5153

54+
let source = MirSource::item(def_id);
55+
mir_util::dump_mir(infcx.tcx, None, "renumber", &0, source, mir, |_, _| Ok(()));
56+
5257
universal_regions
5358
}
5459

src/librustc_mir/borrow_check/nll/renumber.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
138138
debug!("visit_region: region={:?}", region);
139139
}
140140

141+
fn visit_const(&mut self, constant: &mut &'tcx ty::Const<'tcx>, location: Location) {
142+
let ty_context = TyContext::Location(location);
143+
*constant = self.renumber_regions(ty_context, &*constant);
144+
}
145+
141146
fn visit_closure_substs(&mut self, substs: &mut ClosureSubsts<'tcx>, location: Location) {
142147
debug!(
143148
"visit_closure_substs(substs={:?}, location={:?})",

src/librustc_mir/borrow_check/nll/subtype_constraint_generation.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ impl<'cx, 'tcx> SubtypeConstraintGenerator<'cx, 'tcx> {
106106
if let ty::ReVar(vid) = r {
107107
*vid
108108
} else {
109-
self.universal_regions.indices[&r]
109+
*self.universal_regions
110+
.indices
111+
.get(&r)
112+
.unwrap_or_else(|| bug!("to_region_vid: bad region {:?}", r))
110113
}
111114
}
112115
}

src/librustc_mir/transform/type_check.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<'a, 'b, 'gcx, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'gcx, 'tcx> {
110110

111111
fn visit_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
112112
self.super_constant(constant, location);
113+
self.sanitize_constant(constant, location);
113114
self.sanitize_type(constant, constant.ty);
114115
}
115116

@@ -159,6 +160,52 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
159160
}
160161
}
161162

163+
/// Checks that the constant's `ty` field matches up with what
164+
/// would be expected from its literal.
165+
fn sanitize_constant(&mut self, constant: &Constant<'tcx>, location: Location) {
166+
debug!(
167+
"sanitize_constant(constant={:?}, location={:?})",
168+
constant,
169+
location
170+
);
171+
172+
let expected_ty = match constant.literal {
173+
Literal::Value { value } => value.ty,
174+
Literal::Promoted { .. } => {
175+
// FIXME -- promoted MIR return types reference
176+
// various "free regions" (e.g., scopes and things)
177+
// that they ought not to do. We have to figure out
178+
// how best to handle that -- probably we want treat
179+
// promoted MIR much like closures, renumbering all
180+
// their free regions and propagating constraints
181+
// upwards. We have the same acyclic guarantees, so
182+
// that should be possible. But for now, ignore them.
183+
//
184+
// let promoted_mir = &self.mir.promoted[index];
185+
// promoted_mir.return_ty()
186+
return;
187+
}
188+
};
189+
190+
debug!("sanitize_constant: expected_ty={:?}", expected_ty);
191+
192+
if let Err(terr) = self.cx
193+
.eq_types(expected_ty, constant.ty, location.at_self())
194+
{
195+
span_mirbug!(
196+
self,
197+
constant,
198+
"constant {:?} should have type {:?} but has {:?} ({:?})",
199+
constant,
200+
expected_ty,
201+
constant.ty,
202+
terr,
203+
);
204+
}
205+
}
206+
207+
/// Checks that the types internal to the `place` match up with
208+
/// what would be expected.
162209
fn sanitize_place(
163210
&mut self,
164211
place: &Place<'tcx>,

0 commit comments

Comments
 (0)