Skip to content

Commit 62c260d

Browse files
authored
Rollup merge of rust-lang#97738 - Kixiron:zst-panic, r=eddyb
Fix ICEs from zsts within unsized types with non-zero offsets - Fixes rust-lang#97732 - Fixes ICEs while compiling `alloc` with `-Z randomize-layout` r? ``@eddyb``
2 parents 2035b50 + 10336cf commit 62c260d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ pub fn unsize_ptr<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
216216
let mut result = None;
217217
for i in 0..src_layout.fields.count() {
218218
let src_f = src_layout.field(bx.cx(), i);
219-
assert_eq!(src_layout.fields.offset(i).bytes(), 0);
220-
assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
221219
if src_f.is_zst() {
222220
continue;
223221
}
222+
223+
assert_eq!(src_layout.fields.offset(i).bytes(), 0);
224+
assert_eq!(dst_layout.fields.offset(i).bytes(), 0);
224225
assert_eq!(src_layout.size, src_f.size);
225226

226227
let dst_f = dst_layout.field(bx.cx(), i);

src/test/ui/unsized/issue-97732.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// check-pass
2+
3+
#![feature(coerce_unsized)]
4+
5+
// Ensure that unsizing structs that contain ZSTs at non-zero offsets don't ICE
6+
7+
use std::ops::CoerceUnsized;
8+
9+
#[repr(C)]
10+
pub struct BoxWithZstTail<T: ?Sized>(Box<T>, ());
11+
12+
impl<S: ?Sized, T: ?Sized> CoerceUnsized<BoxWithZstTail<T>> for BoxWithZstTail<S> where
13+
Box<S>: CoerceUnsized<Box<T>>
14+
{
15+
}
16+
17+
pub fn noop_dyn_upcast_with_zst_tail(
18+
b: BoxWithZstTail<dyn ToString + Send>,
19+
) -> BoxWithZstTail<dyn ToString> {
20+
b
21+
}
22+
23+
fn main() {
24+
let original = "foo";
25+
let boxed = BoxWithZstTail(Box::new(original) as Box<dyn ToString + Send>, ());
26+
let noop_upcasted = noop_dyn_upcast_with_zst_tail(boxed);
27+
assert_eq!(original, noop_upcasted.0.to_string());
28+
}

0 commit comments

Comments
 (0)