Skip to content

Commit 5594d3f

Browse files
committed
Replace OffsetOf by an actual sum.
1 parent ab67c37 commit 5594d3f

File tree

71 files changed

+479
-651
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+479
-651
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
15591559
self.consume_operand(location, (operand2, span), state);
15601560
}
15611561

1562-
Rvalue::NullaryOp(_op, _ty) => {
1562+
Rvalue::NullaryOp(_op) => {
15631563
// nullary ops take no dynamic input; no borrowck effect.
15641564
}
15651565

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
314314
self.consume_operand(location, operand2);
315315
}
316316

317-
Rvalue::NullaryOp(_op, _ty) => {}
317+
Rvalue::NullaryOp(_op) => {}
318318

319319
Rvalue::Aggregate(_, operands) => {
320320
for operand in operands {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10461046
}
10471047
}
10481048

1049-
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_), _) => {}
1049+
&Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => {}
10501050

10511051
Rvalue::ShallowInitBox(_operand, ty) => {
10521052
let trait_ref =
@@ -1633,8 +1633,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16331633
| Rvalue::BinaryOp(..)
16341634
| Rvalue::RawPtr(..)
16351635
| Rvalue::ThreadLocalRef(..)
1636-
| Rvalue::Discriminant(..)
1637-
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
1636+
| Rvalue::Discriminant(..) => {}
16381637
}
16391638
}
16401639

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -853,31 +853,14 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
853853
fx.bcx.ins().nop();
854854
}
855855
}
856-
Rvalue::NullaryOp(ref null_op, ty) => {
856+
Rvalue::NullaryOp(ref null_op) => {
857857
assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env()));
858-
let layout = fx.layout_of(fx.monomorphize(ty));
859858
let val = match null_op {
860-
NullOp::OffsetOf(fields) => fx
861-
.tcx
862-
.offset_of_subfield(
863-
ty::TypingEnv::fully_monomorphized(),
864-
layout,
865-
fields.iter(),
866-
)
867-
.bytes(),
868-
NullOp::RuntimeChecks(kind) => {
869-
let val = kind.value(fx.tcx.sess);
870-
let val = CValue::by_val(
871-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
872-
fx.layout_of(fx.tcx.types.bool),
873-
);
874-
lval.write_cvalue(fx, val);
875-
return;
876-
}
859+
NullOp::RuntimeChecks(kind) => kind.value(fx.tcx.sess),
877860
};
878861
let val = CValue::by_val(
879-
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()),
880-
fx.layout_of(fx.tcx.types.usize),
862+
fx.bcx.ins().iconst(types::I8, i64::from(val)),
863+
fx.layout_of(fx.tcx.types.bool),
881864
);
882865
lval.write_cvalue(fx, val);
883866
}

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -613,17 +613,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
613613
}
614614
}
615615

616-
mir::Rvalue::NullaryOp(ref null_op, ty) => {
617-
let ty = self.monomorphize(ty);
618-
let layout = bx.cx().layout_of(ty);
616+
mir::Rvalue::NullaryOp(ref null_op) => {
619617
let val = match null_op {
620-
mir::NullOp::OffsetOf(fields) => {
621-
let val = bx
622-
.tcx()
623-
.offset_of_subfield(bx.typing_env(), layout, fields.iter())
624-
.bytes();
625-
bx.cx().const_usize(val)
626-
}
627618
mir::NullOp::RuntimeChecks(kind) => {
628619
let val = kind.value(bx.tcx().sess);
629620
bx.cx().const_bool(val)

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
645645

646646
Rvalue::Cast(_, _, _) => {}
647647

648-
Rvalue::NullaryOp(NullOp::OffsetOf(_) | NullOp::RuntimeChecks(_), _) => {}
648+
Rvalue::NullaryOp(NullOp::RuntimeChecks(_)) => {}
649649
Rvalue::ShallowInitBox(_, _) => {}
650650

651651
Rvalue::UnaryOp(op, operand) => {
@@ -853,7 +853,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
853853
}
854854

855855
// Intrinsics are language primitives, not regular calls, so treat them separately.
856-
if let Some(intrinsic) = tcx.intrinsic(callee) {
856+
if let Some(intrinsic) = tcx.intrinsic(callee)
857+
&& intrinsic.name != sym::offset_of
858+
{
857859
if !tcx.is_const_fn(callee) {
858860
// Non-const intrinsic.
859861
self.check_op(ops::IntrinsicNonConst { name: intrinsic.name });

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod simd;
66

77
use std::assert_matches::assert_matches;
88

9-
use rustc_abi::{FieldIdx, HasDataLayout, Size};
9+
use rustc_abi::{FieldIdx, HasDataLayout, Size, VariantIdx};
1010
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
1111
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, read_target_uint, write_target_uint};
1212
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
@@ -203,6 +203,21 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
203203
let val = layout.align.bytes();
204204
self.write_scalar(Scalar::from_target_usize(val, self), dest)?;
205205
}
206+
sym::offset_of => {
207+
let tp_ty = instance.args.type_at(0);
208+
209+
let u32_layout = self.layout_of(self.tcx.types.u32)?;
210+
let variant = self.read_scalar(&args[0])?.to_bits(u32_layout.size)? as u32;
211+
let field = self.read_scalar(&args[1])?.to_bits(u32_layout.size)? as usize;
212+
213+
let layout = self.layout_of(tp_ty)?;
214+
let cx = ty::layout::LayoutCx::new(*self.tcx, self.typing_env);
215+
216+
let layout = layout.for_variant(&cx, VariantIdx::from_u32(variant));
217+
let offset = layout.fields.offset(field).bytes();
218+
219+
self.write_scalar(Scalar::from_target_usize(offset, self), dest)?;
220+
}
206221
sym::variant_count => {
207222
let tp_ty = instance.args.type_at(0);
208223
let ty = match tp_ty.kind() {

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_apfloat::{Float, FloatConvert};
44
use rustc_middle::mir::NullOp;
55
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
66
use rustc_middle::ty::layout::TyAndLayout;
7-
use rustc_middle::ty::{self, FloatTy, ScalarInt, Ty};
7+
use rustc_middle::ty::{self, FloatTy, ScalarInt};
88
use rustc_middle::{bug, mir, span_bug};
99
use rustc_span::sym;
1010
use tracing::trace;
@@ -506,22 +506,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
506506
}
507507
}
508508

509-
pub fn nullary_op(
510-
&self,
511-
null_op: NullOp<'tcx>,
512-
arg_ty: Ty<'tcx>,
513-
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
509+
pub fn nullary_op(&self, null_op: NullOp) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
514510
use rustc_middle::mir::NullOp::*;
515-
516-
let layout = self.layout_of(arg_ty)?;
517-
let usize_layout = || self.layout_of(self.tcx.types.usize).unwrap();
518-
519511
interp_ok(match null_op {
520-
OffsetOf(fields) => {
521-
let val =
522-
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();
523-
ImmTy::from_uint(val, usize_layout())
524-
}
525512
RuntimeChecks(r) => ImmTy::from_bool(M::runtime_checks(self, r)?, *self.tcx),
526513
})
527514
}

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
203203
self.write_immediate(*result, &dest)?;
204204
}
205205

206-
NullaryOp(null_op, ty) => {
207-
let ty = self.instantiate_from_current_frame_and_normalize_erasing_regions(ty)?;
208-
let val = self.nullary_op(null_op, ty)?;
206+
NullaryOp(null_op) => {
207+
let val = self.nullary_op(null_op)?;
209208
self.write_immediate(*val, &dest)?;
210209
}
211210

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ language_item_table! {
170170
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
171171
AlignOf, sym::mem_align_const, align_const, Target::AssocConst, GenericRequirement::Exact(0);
172172
SizeOf, sym::mem_size_const, size_const, Target::AssocConst, GenericRequirement::Exact(0);
173+
OffsetOf, sym::offset_of, offset_of, Target::Fn, GenericRequirement::Exact(1);
173174
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
174175
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
175176
Copy, sym::copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);

0 commit comments

Comments
 (0)