Skip to content

Commit 21a0652

Browse files
committed
Bail out on overly generic substitutions
1 parent e547f35 commit 21a0652

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

src/librustc_mir/interpret/cast.rs

+1-5
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

+31-11
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,21 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
214214
self.frame().mir
215215
}
216216

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

@@ -228,13 +238,9 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
228238
substs: &'tcx Substs<'tcx>
229239
) -> EvalResult<'tcx, ty::Instance<'tcx>> {
230240
trace!("resolve: {:?}, {:#?}", def_id, substs);
231-
trace!("substs: {:#?}", self.substs());
232241
trace!("param_env: {:#?}", self.param_env);
233-
let substs = self.tcx.subst_and_normalize_erasing_regions(
234-
self.substs(),
235-
self.param_env,
236-
&substs,
237-
);
242+
let substs = self.subst_and_normalize_erasing_regions(substs)?;
243+
trace!("substs: {:#?}", substs);
238244
ty::Instance::resolve(
239245
*self.tcx,
240246
self.param_env,
@@ -274,6 +280,20 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
274280
}
275281
}
276282

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

src/librustc_mir/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
514514

515515
Constant(ref constant) => {
516516
let layout = from_known_layout(layout, || {
517-
let ty = self.monomorphize(mir_op.ty(self.mir(), *self.tcx), self.substs());
517+
let ty = self.monomorphize_in_frame(mir_op.ty(self.mir(), *self.tcx))?;
518518
self.layout_of(ty)
519519
})?;
520520
let op = self.const_value_to_op(*constant.literal)?;

src/librustc_mir/interpret/place.rs

+3-2
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

+2-2
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)