Skip to content

Commit 6b80e9c

Browse files
committed
Auto merge of #106934 - DrMeepster:offset_of, r=WaffleLapkin
Add offset_of! macro (RFC 3308) Implements rust-lang/rfcs#3308 (tracking issue #106655) by adding the built in macro `core::mem::offset_of`. Two of the future possibilities are also implemented: * Nested field accesses (without array indexing) * DST support (for `Sized` fields) I wrote this a few months ago, before the RFC merged. Now that it's merged, I decided to rebase and finish it. cc `@thomcc` (RFC author)
2 parents 22e6885 + 9dd0653 commit 6b80e9c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,15 @@ fn codegen_stmt<'tcx>(
781781
let operand = operand.load_scalar(fx);
782782
lval.write_cvalue(fx, CValue::by_val(operand, box_layout));
783783
}
784-
Rvalue::NullaryOp(null_op, ty) => {
784+
Rvalue::NullaryOp(ref null_op, ty) => {
785785
assert!(lval.layout().ty.is_sized(fx.tcx, ParamEnv::reveal_all()));
786786
let layout = fx.layout_of(fx.monomorphize(ty));
787787
let val = match null_op {
788788
NullOp::SizeOf => layout.size.bytes(),
789789
NullOp::AlignOf => layout.align.abi.bytes(),
790+
NullOp::OffsetOf(fields) => {
791+
layout.offset_of_subfield(fx, fields.iter().map(|f| f.index())).bytes()
792+
}
790793
};
791794
let val = CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), val.into());
792795
lval.write_cvalue(fx, val);

0 commit comments

Comments
 (0)