Skip to content

Commit e46804b

Browse files
committed
Replace OffsetOf by an actual sum.
1 parent 27050c0 commit e46804b

File tree

70 files changed

+438
-666
lines changed

Some content is hidden

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

70 files changed

+438
-666
lines changed

compiler/rustc_borrowck/src/lib.rs

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

1566-
Rvalue::NullaryOp(_op, _ty) => {
1566+
Rvalue::NullaryOp(_op) => {
15671567
// nullary ops take no dynamic input; no borrowck effect.
15681568
}
15691569

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10461046
}
10471047
}
10481048

1049-
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
1050-
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
1049+
&Rvalue::NullaryOp(NullOp::ContractChecks | NullOp::UbChecks) => {}
10511050

10521051
Rvalue::ShallowInitBox(_operand, ty) => {
10531052
let trait_ref =
@@ -1634,8 +1633,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16341633
| Rvalue::BinaryOp(..)
16351634
| Rvalue::RawPtr(..)
16361635
| Rvalue::ThreadLocalRef(..)
1637-
| Rvalue::Discriminant(..)
1638-
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
1636+
| Rvalue::Discriminant(..) => {}
16391637
}
16401638
}
16411639

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -829,40 +829,15 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
829829
fx.bcx.ins().nop();
830830
}
831831
}
832-
Rvalue::NullaryOp(ref null_op, ty) => {
832+
Rvalue::NullaryOp(ref null_op) => {
833833
assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env()));
834-
let layout = fx.layout_of(fx.monomorphize(ty));
835834
let val = match null_op {
836-
NullOp::OffsetOf(fields) => fx
837-
.tcx
838-
.offset_of_subfield(
839-
ty::TypingEnv::fully_monomorphized(),
840-
layout,
841-
fields.iter(),
842-
)
843-
.bytes(),
844-
NullOp::UbChecks => {
845-
let val = fx.tcx.sess.ub_checks();
846-
let val = CValue::by_val(
847-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
848-
fx.layout_of(fx.tcx.types.bool),
849-
);
850-
lval.write_cvalue(fx, val);
851-
return;
852-
}
853-
NullOp::ContractChecks => {
854-
let val = fx.tcx.sess.contract_checks();
855-
let val = CValue::by_val(
856-
fx.bcx.ins().iconst(types::I8, i64::from(val)),
857-
fx.layout_of(fx.tcx.types.bool),
858-
);
859-
lval.write_cvalue(fx, val);
860-
return;
861-
}
835+
NullOp::UbChecks => fx.tcx.sess.ub_checks(),
836+
NullOp::ContractChecks => fx.tcx.sess.contract_checks(),
862837
};
863838
let val = CValue::by_val(
864-
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(val).unwrap()),
865-
fx.layout_of(fx.tcx.types.usize),
839+
fx.bcx.ins().iconst(types::I8, i64::from(val)),
840+
fx.layout_of(fx.tcx.types.bool),
866841
);
867842
lval.write_cvalue(fx, val);
868843
}

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
607607
}
608608
}
609609

610-
mir::Rvalue::NullaryOp(ref null_op, ty) => {
611-
let ty = self.monomorphize(ty);
612-
let layout = bx.cx().layout_of(ty);
610+
mir::Rvalue::NullaryOp(ref null_op) => {
613611
let val = match null_op {
614-
mir::NullOp::OffsetOf(fields) => {
615-
let val = bx
616-
.tcx()
617-
.offset_of_subfield(bx.typing_env(), layout, fields.iter())
618-
.bytes();
619-
bx.cx().const_usize(val)
620-
}
621612
mir::NullOp::UbChecks => {
622613
let val = bx.tcx().sess.ub_checks();
623614
bx.cx().const_bool(val)

compiler/rustc_const_eval/src/check_consts/check.rs

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

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

648-
Rvalue::NullaryOp(
649-
NullOp::OffsetOf(_) | NullOp::UbChecks | NullOp::ContractChecks,
650-
_,
651-
) => {}
648+
Rvalue::NullaryOp(NullOp::UbChecks | NullOp::ContractChecks) => {}
652649
Rvalue::ShallowInitBox(_, _) => {}
653650

654651
Rvalue::UnaryOp(op, operand) => {
@@ -856,7 +853,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
856853
}
857854

858855
// Intrinsics are language primitives, not regular calls, so treat them separately.
859-
if let Some(intrinsic) = tcx.intrinsic(callee) {
856+
if let Some(intrinsic) = tcx.intrinsic(callee)
857+
&& intrinsic.name != sym::offset_of
858+
{
860859
if !tcx.is_const_fn(callee) {
861860
// Non-const intrinsic.
862861
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};
@@ -174,6 +174,21 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
174174
let val = layout.align.bytes();
175175
self.write_scalar(Scalar::from_target_usize(val, self), dest)?;
176176
}
177+
sym::offset_of => {
178+
let tp_ty = instance.args.type_at(0);
179+
180+
let u32_layout = self.layout_of(self.tcx.types.u32)?;
181+
let variant = self.read_scalar(&args[0])?.to_bits(u32_layout.size)? as u32;
182+
let field = self.read_scalar(&args[1])?.to_bits(u32_layout.size)? as usize;
183+
184+
let layout = self.layout_of(tp_ty)?;
185+
let cx = ty::layout::LayoutCx::new(*self.tcx, self.typing_env);
186+
187+
let layout = layout.for_variant(&cx, VariantIdx::from_u32(variant));
188+
let offset = layout.fields.offset(field).bytes();
189+
190+
self.write_scalar(Scalar::from_target_usize(offset, self), dest)?;
191+
}
177192
sym::variant_count => {
178193
let tp_ty = instance.args.type_at(0);
179194
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
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),
526513
ContractChecks => ImmTy::from_bool(M::contract_checks(self)?, *self.tcx),
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)