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 @@ impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
/// Trait that needs to be implemented by the higher-level type representation /// Trait that needs to be implemented by the higher-level type representation
/// (e.g. `rustc_middle::ty::Ty`), to provide `rustc_target::abi` functionality. /// (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( fn ty_and_layout_for_variant(
this: TyAndLayout<'a, Self>, this: TyAndLayout<'a, Self>,
cx: &C, cx: &C,
@ -135,6 +135,11 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
for index in indices { for index in indices {
offset += layout.fields.offset(index); offset += layout.fields.offset(index);
layout = layout.field(cx, index); layout = layout.field(cx, index);
assert!(
layout.is_sized(),
"offset of unsized field (type {:?}) cannot be computed statically",
layout.ty
);
} }
offset offset