Skip to content

Commit db2978a

Browse files
committed
Bail out on overly generic substitutions
1 parent a59eabb commit db2978a

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

src/librustc_mir/interpret/cast.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
109109
// The src operand does not matter, just its type
110110
match src.layout.ty.sty {
111111
ty::Closure(def_id, substs) => {
112-
let substs = self.tcx.subst_and_normalize_erasing_regions(
113-
self.substs(),
114-
ty::ParamEnv::reveal_all(),
115-
&substs,
116-
);
112+
let substs = self.subst_and_normalize_erasing_regions(substs)?;
117113
let instance = ty::Instance::resolve_closure(
118114
*self.tcx,
119115
def_id,

src/librustc_mir/interpret/eval_context.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,21 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
216216
self.frame().mir
217217
}
218218

219-
pub fn substs(&self) -> &'tcx Substs<'tcx> {
220-
if let Some(frame) = self.stack.last() {
221-
frame.instance.substs
222-
} else {
223-
Substs::empty()
219+
pub(super) fn subst_and_normalize_erasing_regions<T: TypeFoldable<'tcx>>(
220+
&self,
221+
substs: T,
222+
) -> EvalResult<'tcx, T> {
223+
match self.stack.last() {
224+
Some(frame) => Ok(self.tcx.subst_and_normalize_erasing_regions(
225+
frame.instance.substs,
226+
self.param_env,
227+
&substs,
228+
)),
229+
None => if substs.needs_subst() {
230+
err!(TooGeneric).into()
231+
} else {
232+
Ok(substs)
233+
},
224234
}
225235
}
226236

@@ -230,13 +240,9 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
230240
substs: &'tcx Substs<'tcx>
231241
) -> EvalResult<'tcx, ty::Instance<'tcx>> {
232242
trace!("resolve: {:?}, {:#?}", def_id, substs);
233-
trace!("substs: {:#?}", self.substs());
234243
trace!("param_env: {:#?}", self.param_env);
235-
let substs = self.tcx.subst_and_normalize_erasing_regions(
236-
self.substs(),
237-
self.param_env,
238-
&substs,
239-
);
244+
let substs = self.subst_and_normalize_erasing_regions(substs)?;
245+
trace!("substs: {:#?}", substs);
240246
ty::Instance::resolve(
241247
*self.tcx,
242248
self.param_env,
@@ -276,6 +282,20 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
276282
}
277283
}
278284

285+
pub fn monomorphize_in_frame<T: TypeFoldable<'tcx> + Subst<'tcx>>(
286+
&self,
287+
t: T,
288+
) -> EvalResult<'tcx, T> {
289+
match self.stack.last() {
290+
Some(frame) => Ok(self.monomorphize(t, frame.instance.substs)),
291+
None => if t.needs_subst() {
292+
err!(TooGeneric).into()
293+
} else {
294+
Ok(t)
295+
},
296+
}
297+
}
298+
279299
pub fn monomorphize<T: TypeFoldable<'tcx> + Subst<'tcx>>(
280300
&self,
281301
t: T,

src/librustc_mir/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
508508

509509
Constant(ref constant) => {
510510
let layout = from_known_layout(layout, || {
511-
let ty = self.monomorphize(mir_op.ty(self.mir(), *self.tcx), self.substs());
511+
let ty = self.monomorphize_in_frame(mir_op.ty(self.mir(), *self.tcx))?;
512512
self.layout_of(ty)
513513
})?;
514514
let op = self.const_value_to_op(*constant.literal)?;

src/librustc_mir/interpret/place.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc::hir;
99
use rustc::mir;
1010
use rustc::ty::{self, Ty};
1111
use rustc::ty::layout::{self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx};
12+
use rustc::ty::TypeFoldable;
1213

1314
use super::{
1415
GlobalId, AllocId, Allocation, Scalar, EvalResult, Pointer, PointerArithmetic,
@@ -583,8 +584,8 @@ where
583584
}
584585

585586
Static(ref static_) => {
586-
let ty = self.monomorphize(static_.ty, self.substs());
587-
let layout = self.layout_of(ty)?;
587+
assert!(!static_.ty.needs_subst());
588+
let layout = self.layout_of(static_.ty)?;
588589
let instance = ty::Instance::mono(*self.tcx, static_.def_id);
589590
let cid = GlobalId {
590591
instance,

src/librustc_mir/interpret/step.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
248248
}
249249

250250
NullaryOp(mir::NullOp::SizeOf, ty) => {
251-
let ty = self.monomorphize(ty, self.substs());
251+
let ty = self.monomorphize_in_frame(ty)?;
252252
let layout = self.layout_of(ty)?;
253253
assert!(!layout.is_unsized(),
254254
"SizeOf nullary MIR operator called for unsized type");
@@ -260,7 +260,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
260260
}
261261

262262
Cast(kind, ref operand, cast_ty) => {
263-
debug_assert_eq!(self.monomorphize(cast_ty, self.substs()), dest.layout.ty);
263+
debug_assert_eq!(self.monomorphize_in_frame(cast_ty)?, dest.layout.ty);
264264
let src = self.eval_operand(operand, None)?;
265265
self.cast(src, kind, dest)?;
266266
}

0 commit comments

Comments
 (0)