Skip to content

Commit f589451

Browse files
authored
Rollup merge of #112069 - clubby789:offset-of-sized-fields, r=WaffleLapkin
offset_of: don't require type to be `Sized` Fixes #112051 ~~The RFC [explicitly forbids](https://rust-lang.github.io/rfcs/3308-offset_of.html#limitations) non-`Sized` types, but it looks like only the fields being recursed into were checked. The sized check also seemed to have been completely missing for tuples~~
2 parents 7ca941f + 6c18d1e commit f589451

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -668,11 +668,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
668668

669669
mir::Rvalue::NullaryOp(ref null_op, ty) => {
670670
let ty = self.monomorphize(ty);
671-
assert!(bx.cx().type_is_sized(ty));
672671
let layout = bx.cx().layout_of(ty);
673672
let val = match null_op {
674-
mir::NullOp::SizeOf => layout.size.bytes(),
675-
mir::NullOp::AlignOf => layout.align.abi.bytes(),
673+
mir::NullOp::SizeOf => {
674+
assert!(bx.cx().type_is_sized(ty));
675+
layout.size.bytes()
676+
}
677+
mir::NullOp::AlignOf => {
678+
assert!(bx.cx().type_is_sized(ty));
679+
layout.align.abi.bytes()
680+
}
676681
mir::NullOp::OffsetOf(fields) => {
677682
layout.offset_of_subfield(bx.cx(), fields.iter().map(|f| f.index())).bytes()
678683
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// build-pass
2+
// regression test for #112051
3+
4+
#![feature(offset_of)]
5+
6+
struct S<T: ?Sized> {
7+
a: u64,
8+
b: T,
9+
}
10+
trait Tr {}
11+
12+
fn main() {
13+
let _a = core::mem::offset_of!(S<dyn Tr>, a);
14+
let _b = core::mem::offset_of!((u64, dyn Tr), 0);
15+
}

0 commit comments

Comments
 (0)