From 9215346d35f4315206131b4ef53138a10429fbde Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 8 Aug 2023 10:31:42 +0200 Subject: [PATCH] offset_of: guard against invalid use (with unsized fields) --- compiler/rustc_target/src/abi/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index 084c917cc31..dd435dbb0a3 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -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(self, cx: &C, indices: impl Iterator) 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