offset_of: guard against invalid use (with unsized fields)

This commit is contained in:
Ralf Jung 2023-08-08 10:31:42 +02:00
parent 6742e2b185
commit 9215346d35

View File

@ -39,7 +39,7 @@ fn deref(&self) -> &&'a LayoutS {
/// Trait that needs to be implemented by the higher-level type representation
/// (e.g. `rustc_middle::ty::Ty`), to provide `rustc_target::abi` functionality.
pub trait TyAbiInterface<'a, C>: Sized {
pub trait TyAbiInterface<'a, C>: Sized + std::fmt::Debug {
fn ty_and_layout_for_variant(
this: TyAndLayout<'a, Self>,
cx: &C,
@ -135,6 +135,11 @@ pub fn offset_of_subfield<C>(self, cx: &C, indices: impl Iterator<Item = usize>)
for index in indices {
offset += layout.fields.offset(index);
layout = layout.field(cx, index);
assert!(
layout.is_sized(),
"offset of unsized field (type {:?}) cannot be computed statically",
layout.ty
);
}
offset