Skip to content
/ rust Public
forked from rust-lang/rust

Commit 307c573

Browse files
committed
Auto merge of rust-lang#114614 - RalfJung:offset-of-sanity, r=cjgillot
offset_of: guard against invalid use (with unsized fields)
2 parents abc910b + 9215346 commit 307c573

File tree

1 file changed

+6
-1
lines changed
  • compiler/rustc_target/src/abi

1 file changed

+6
-1
lines changed

compiler/rustc_target/src/abi/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
3939

4040
/// Trait that needs to be implemented by the higher-level type representation
4141
/// (e.g. `rustc_middle::ty::Ty`), to provide `rustc_target::abi` functionality.
42-
pub trait TyAbiInterface<'a, C>: Sized {
42+
pub trait TyAbiInterface<'a, C>: Sized + std::fmt::Debug {
4343
fn ty_and_layout_for_variant(
4444
this: TyAndLayout<'a, Self>,
4545
cx: &C,
@@ -135,6 +135,11 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
135135
for index in indices {
136136
offset += layout.fields.offset(index);
137137
layout = layout.field(cx, index);
138+
assert!(
139+
layout.is_sized(),
140+
"offset of unsized field (type {:?}) cannot be computed statically",
141+
layout.ty
142+
);
138143
}
139144

140145
offset

0 commit comments

Comments
 (0)