Skip to content

Commit 4bee06c

Browse files
committed
change NullOp::OffsetOf to NullOp::FieldOffset that expects a ty::Field argument ty
1 parent 7e552c6 commit 4bee06c

File tree

21 files changed

+120
-83
lines changed

21 files changed

+120
-83
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16241624
)
16251625
.unwrap();
16261626
}
1627+
Rvalue::NullaryOp(NullOp::FieldOffset, ty) => match ty.kind() {
1628+
ty::Field(..) => {}
1629+
_ => bug!(
1630+
"FIXME(field_projections): should we report an error here, or `span_mirbug!`?"
1631+
),
1632+
},
16271633

16281634
Rvalue::Use(_)
16291635
| Rvalue::UnaryOp(_, _)
@@ -1632,8 +1638,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
16321638
| Rvalue::RawPtr(..)
16331639
| Rvalue::ThreadLocalRef(..)
16341640
| Rvalue::Len(..)
1635-
| Rvalue::Discriminant(..)
1636-
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
1641+
| Rvalue::Discriminant(..) => {}
16371642
}
16381643
}
16391644

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::InlineAsmOptions;
88
use rustc_codegen_ssa::base::is_call_from_compiler_builtins_to_upstream_monomorphization;
99
use rustc_data_structures::profiling::SelfProfilerRef;
1010
use rustc_index::IndexVec;
11+
use rustc_middle::bug;
1112
use rustc_middle::ty::TypeVisitableExt;
1213
use rustc_middle::ty::adjustment::PointerCoercion;
1314
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
@@ -853,14 +854,19 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
853854
let val = match null_op {
854855
NullOp::SizeOf => layout.size.bytes(),
855856
NullOp::AlignOf => layout.align.abi.bytes(),
856-
NullOp::OffsetOf(fields) => fx
857-
.tcx
858-
.offset_of_subfield(
859-
ty::TypingEnv::fully_monomorphized(),
860-
layout,
861-
fields.iter(),
862-
)
863-
.bytes(),
857+
NullOp::FieldOffset => {
858+
let &ty::Field(container, field_path) = layout.ty.kind() else {
859+
bug!("FIXME(field_projections): better error")
860+
};
861+
let layout = fx.layout_of(container);
862+
fx.tcx
863+
.offset_of_subfield(
864+
ty::TypingEnv::fully_monomorphized(),
865+
layout,
866+
field_path.iter(),
867+
)
868+
.bytes()
869+
}
864870
NullOp::UbChecks => {
865871
let val = fx.tcx.sess.ub_checks();
866872
let val = CValue::by_val(

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
619619
let val = layout.align.abi.bytes();
620620
bx.cx().const_usize(val)
621621
}
622-
mir::NullOp::OffsetOf(fields) => {
622+
mir::NullOp::FieldOffset => {
623+
let &ty::Field(container, field_path) = ty.kind() else {
624+
bug!("expected `ty::Field` for `NullOp::FieldOffset`, but got {ty:?}")
625+
};
626+
let layout = bx.cx().layout_of(container);
623627
let val = bx
624628
.tcx()
625-
.offset_of_subfield(bx.typing_env(), layout, fields.iter())
629+
.offset_of_subfield(bx.typing_env(), layout, field_path.iter())
626630
.bytes();
627631
bx.cx().const_usize(val)
628632
}

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
649649
Rvalue::NullaryOp(
650650
NullOp::SizeOf
651651
| NullOp::AlignOf
652-
| NullOp::OffsetOf(_)
652+
| NullOp::FieldOffset
653653
| NullOp::UbChecks
654654
| NullOp::ContractChecks,
655655
_,

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use std::assert_matches::assert_matches;
77
use rustc_abi::{FieldIdx, HasDataLayout, Size};
88
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
99
use rustc_middle::mir::interpret::{CTFE_ALLOC_SALT, read_target_uint, write_target_uint};
10-
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic, NullOp};
10+
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
1111
use rustc_middle::ty::layout::TyAndLayout;
1212
use rustc_middle::ty::{Ty, TyCtxt};
13-
use rustc_middle::{bug, err_inval, span_bug, ty};
13+
use rustc_middle::{bug, ty};
1414
use rustc_span::{Symbol, sym};
1515
use tracing::trace;
1616

@@ -638,8 +638,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
638638
rustc_apfloat::Round::NearestTiesToEven,
639639
)?,
640640

641-
sym::unaligned_field_offset => self.unaligned_field_offset(instance, dest)?,
642-
643641
// Unsupported intrinsic: skip the return_to_block below.
644642
_ => return interp_ok(false),
645643
}
@@ -649,25 +647,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
649647
interp_ok(true)
650648
}
651649

652-
fn unaligned_field_offset(
653-
&mut self,
654-
instance: ty::Instance<'tcx>,
655-
dest: &PlaceTy<'tcx, M::Provenance>,
656-
) -> InterpResult<'tcx, ()> {
657-
assert_eq!(instance.args.len(), 1);
658-
let ty = instance.args.type_at(0);
659-
match ty.kind() {
660-
&ty::Field(container, field_path) => {
661-
let offset = self.nullary_op(NullOp::OffsetOf(field_path), container)?;
662-
self.write_immediate(*offset, dest)
663-
}
664-
ty::Alias(..) | ty::Param(..) | ty::Placeholder(..) | ty::Infer(..) => {
665-
Err(err_inval!(TooGeneric)).into()
666-
}
667-
_ => span_bug!(self.cur_span(), "expected field representing type, found {ty}"),
668-
}
669-
}
670-
671650
pub(super) fn eval_nondiverging_intrinsic(
672651
&mut self,
673652
intrinsic: &NonDivergingIntrinsic<'tcx>,

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
508508

509509
pub fn nullary_op(
510510
&self,
511-
null_op: NullOp<'tcx>,
511+
null_op: NullOp,
512512
arg_ty: Ty<'tcx>,
513513
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
514514
use rustc_middle::mir::NullOp::*;
@@ -531,9 +531,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
531531
let val = layout.align.abi.bytes();
532532
ImmTy::from_uint(val, usize_layout())
533533
}
534-
OffsetOf(fields) => {
534+
FieldOffset => {
535+
let &ty::Field(container, field_path) = arg_ty.kind() else {
536+
span_bug!(
537+
self.cur_span(),
538+
"expected `ty::Field` for `NullOp::FieldOffset`, but got {arg_ty:?}"
539+
)
540+
};
541+
let layout = self.layout_of(container)?;
535542
let val =
536-
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();
543+
self.tcx.offset_of_subfield(self.typing_env, layout, field_path.iter()).bytes();
537544
ImmTy::from_uint(val, usize_layout())
538545
}
539546
UbChecks => ImmTy::from_bool(M::ub_checks(self)?, *self.tcx),

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10721072
match op {
10731073
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
10741074
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
1075-
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
1075+
NullOp::FieldOffset => write!(fmt, "FieldOffset({t})"),
10761076
NullOp::UbChecks => write!(fmt, "UbChecks()"),
10771077
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),
10781078
}

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl<'tcx> Rvalue<'tcx> {
751751
op.ty(tcx, arg_ty)
752752
}
753753
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
754-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
754+
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::FieldOffset, _) => {
755755
tcx.types.usize
756756
}
757757
Rvalue::NullaryOp(NullOp::ContractChecks, _)
@@ -819,10 +819,10 @@ impl BorrowKind {
819819
}
820820
}
821821

822-
impl<'tcx> NullOp<'tcx> {
823-
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
822+
impl NullOp {
823+
pub fn ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
824824
match self {
825-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
825+
NullOp::SizeOf | NullOp::AlignOf | NullOp::FieldOffset => tcx.types.usize,
826826
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
827827
}
828828
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use smallvec::SmallVec;
1919
use super::{BasicBlock, Const, Local, UserTypeProjection};
2020
use crate::mir::coverage::CoverageKind;
2121
use crate::ty::adjustment::PointerCoercion;
22-
use crate::ty::{self, FieldPath, GenericArgsRef, List, Region, Ty, UserTypeAnnotationIndex};
22+
use crate::ty::{self, GenericArgsRef, List, Region, Ty, UserTypeAnnotationIndex};
2323

2424
/// Represents the "flavors" of MIR.
2525
///
@@ -1439,7 +1439,7 @@ pub enum Rvalue<'tcx> {
14391439
BinaryOp(BinOp, Box<(Operand<'tcx>, Operand<'tcx>)>),
14401440

14411441
/// Computes a value as described by the operation.
1442-
NullaryOp(NullOp<'tcx>, Ty<'tcx>),
1442+
NullaryOp(NullOp, Ty<'tcx>),
14431443

14441444
/// Exactly like `BinaryOp`, but less operands.
14451445
///
@@ -1568,13 +1568,13 @@ pub enum AggregateKind<'tcx> {
15681568
}
15691569

15701570
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1571-
pub enum NullOp<'tcx> {
1571+
pub enum NullOp {
15721572
/// Returns the size of a value of that type
15731573
SizeOf,
15741574
/// Returns the minimum alignment of a type
15751575
AlignOf,
1576-
/// Returns the offset of a field
1577-
OffsetOf(FieldPath<'tcx>),
1576+
/// Returns the offset of the field represented by the type
1577+
FieldOffset,
15781578
/// Returns whether we should perform some UB-checking at runtime.
15791579
/// See the `ub_checks` intrinsic docs for details.
15801580
UbChecks,

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ TrivialTypeTraversalImpls! {
253253
crate::mir::FakeReadCause,
254254
crate::mir::Local,
255255
crate::mir::MirPhase,
256-
crate::mir::NullOp<'tcx>,
256+
crate::mir::NullOp,
257257
crate::mir::Promoted,
258258
crate::mir::RawPtrKind,
259259
crate::mir::RetagKind,

0 commit comments

Comments
 (0)