Skip to content

Commit 3ae047b

Browse files
authored
Rollup merge of #108790 - cjgillot:mono-cast, r=oli-obk
Do not ICE when interpreting a cast between non-monomorphic types Fixes #101596
2 parents 4bd6f7f + 858eab6 commit 3ae047b

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

compiler/rustc_const_eval/src/interpret/cast.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6767
}
6868

6969
Pointer(PointerCast::ReifyFnPointer) => {
70+
// All reifications must be monomorphic, bail out otherwise.
71+
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
72+
7073
// The src operand does not matter, just its type
7174
match *src.layout.ty.kind() {
7275
ty::FnDef(def_id, substs) => {
73-
// All reifications must be monomorphic, bail out otherwise.
74-
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
75-
7676
let instance = ty::Instance::resolve_for_fn_ptr(
7777
*self.tcx,
7878
self.param_env,
@@ -100,12 +100,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
100100
}
101101

102102
Pointer(PointerCast::ClosureFnPointer(_)) => {
103+
// All reifications must be monomorphic, bail out otherwise.
104+
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
105+
103106
// The src operand does not matter, just its type
104107
match *src.layout.ty.kind() {
105108
ty::Closure(def_id, substs) => {
106-
// All reifications must be monomorphic, bail out otherwise.
107-
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
108-
109109
let instance = ty::Instance::resolve_closure(
110110
*self.tcx,
111111
def_id,
@@ -359,8 +359,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
359359
let val = Immediate::new_dyn_trait(ptr, vtable, &*self.tcx);
360360
self.write_immediate(val, dest)
361361
}
362-
363362
_ => {
363+
// Do not ICE if we are not monomorphic enough.
364+
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
365+
ensure_monomorphic_enough(*self.tcx, cast_ty)?;
366+
364367
span_bug!(
365368
self.cur_span(),
366369
"invalid pointer unsizing {:?} -> {:?}",
@@ -404,12 +407,18 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
404407
}
405408
Ok(())
406409
}
407-
_ => span_bug!(
408-
self.cur_span(),
409-
"unsize_into: invalid conversion: {:?} -> {:?}",
410-
src.layout,
411-
dest.layout
412-
),
410+
_ => {
411+
// Do not ICE if we are not monomorphic enough.
412+
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
413+
ensure_monomorphic_enough(*self.tcx, cast_ty.ty)?;
414+
415+
span_bug!(
416+
self.cur_span(),
417+
"unsize_into: invalid conversion: {:?} -> {:?}",
418+
src.layout,
419+
dest.layout
420+
)
421+
}
413422
}
414423
}
415424
}

tests/ui/mir/unsize-trait.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Check that the interpreter does not ICE when trying to unsize `B` to `[u8]`.
2+
// This is a `build` test to ensure that const-prop-lint runs.
3+
// build-pass
4+
5+
#![feature(unsize)]
6+
7+
fn foo<B>(buffer: &mut [B; 2])
8+
where B: std::marker::Unsize<[u8]>,
9+
{
10+
let buffer: &[u8] = &buffer[0];
11+
}
12+
13+
fn main() {
14+
foo(&mut [[0], [5]]);
15+
}

0 commit comments

Comments
 (0)