Skip to content

Commit

Permalink
Rollup merge of #112069 - clubby789:offset-of-sized-fields, r=WaffleL…
Browse files Browse the repository at this point in the history
…apkin

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~~
  • Loading branch information
matthiaskrgr authored May 31, 2023
2 parents 7ca941f + 6c18d1e commit f589451
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

mir::Rvalue::NullaryOp(ref null_op, ty) => {
let ty = self.monomorphize(ty);
assert!(bx.cx().type_is_sized(ty));
let layout = bx.cx().layout_of(ty);
let val = match null_op {
mir::NullOp::SizeOf => layout.size.bytes(),
mir::NullOp::AlignOf => layout.align.abi.bytes(),
mir::NullOp::SizeOf => {
assert!(bx.cx().type_is_sized(ty));
layout.size.bytes()
}
mir::NullOp::AlignOf => {
assert!(bx.cx().type_is_sized(ty));
layout.align.abi.bytes()
}
mir::NullOp::OffsetOf(fields) => {
layout.offset_of_subfield(bx.cx(), fields.iter().map(|f| f.index())).bytes()
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/offset-of/offset-of-unsized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// build-pass
// regression test for #112051

#![feature(offset_of)]

struct S<T: ?Sized> {
a: u64,
b: T,
}
trait Tr {}

fn main() {
let _a = core::mem::offset_of!(S<dyn Tr>, a);
let _b = core::mem::offset_of!((u64, dyn Tr), 0);
}

0 comments on commit f589451

Please sign in to comment.