Skip to content

Commit 5ca7d48

Browse files
committed
Replace NullOp::SizeOf and NullOp::AlignOf by lang items.
1 parent 3f2a592 commit 5ca7d48

File tree

73 files changed

+547
-673
lines changed

Some content is hidden

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

73 files changed

+547
-673
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

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

1049-
&Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, ty) => {
1050-
let trait_ref =
1051-
ty::TraitRef::new(tcx, tcx.require_lang_item(LangItem::Sized, span), [ty]);
1052-
1053-
self.prove_trait_ref(
1054-
trait_ref,
1055-
location.to_locations(),
1056-
ConstraintCategory::SizedBound,
1057-
);
1058-
}
10591049
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
10601050
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
10611051

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
840840
assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env()));
841841
let layout = fx.layout_of(fx.monomorphize(ty));
842842
let val = match null_op {
843-
NullOp::SizeOf => layout.size.bytes(),
844-
NullOp::AlignOf => layout.align.bytes(),
845843
NullOp::OffsetOf(fields) => fx
846844
.tcx
847845
.offset_of_subfield(

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
611611
let ty = self.monomorphize(ty);
612612
let layout = bx.cx().layout_of(ty);
613613
let val = match null_op {
614-
mir::NullOp::SizeOf => {
615-
assert!(bx.cx().type_is_sized(ty));
616-
let val = layout.size.bytes();
617-
bx.cx().const_usize(val)
618-
}
619-
mir::NullOp::AlignOf => {
620-
assert!(bx.cx().type_is_sized(ty));
621-
let val = layout.align.bytes();
622-
bx.cx().const_usize(val)
623-
}
624614
mir::NullOp::OffsetOf(fields) => {
625615
let val = bx
626616
.tcx()

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,11 +646,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
646646
Rvalue::Cast(_, _, _) => {}
647647

648648
Rvalue::NullaryOp(
649-
NullOp::SizeOf
650-
| NullOp::AlignOf
651-
| NullOp::OffsetOf(_)
652-
| NullOp::UbChecks
653-
| NullOp::ContractChecks,
649+
NullOp::OffsetOf(_) | NullOp::UbChecks | NullOp::ContractChecks,
654650
_,
655651
) => {}
656652
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
156156
let b_ty = self.read_type_id(&args[1])?;
157157
self.write_scalar(Scalar::from_bool(a_ty == b_ty), dest)?;
158158
}
159+
sym::size_of => {
160+
let tp_ty = instance.args.type_at(0);
161+
let layout = self.layout_of(tp_ty)?;
162+
if !layout.is_sized() {
163+
span_bug!(self.cur_span(), "unsized type for `NullaryOp::SizeOf`");
164+
}
165+
let val = layout.size.bytes();
166+
self.write_scalar(Scalar::from_target_usize(val, self), dest)?;
167+
}
168+
sym::align_of => {
169+
let tp_ty = instance.args.type_at(0);
170+
let layout = self.layout_of(tp_ty)?;
171+
if !layout.is_sized() {
172+
span_bug!(self.cur_span(), "unsized type for `NullaryOp::AlignOf`");
173+
}
174+
let val = layout.align.bytes();
175+
self.write_scalar(Scalar::from_target_usize(val, self), dest)?;
176+
}
159177
sym::variant_count => {
160178
let tp_ty = instance.args.type_at(0);
161179
let ty = match tp_ty.kind() {

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -517,20 +517,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
517517
let usize_layout = || self.layout_of(self.tcx.types.usize).unwrap();
518518

519519
interp_ok(match null_op {
520-
SizeOf => {
521-
if !layout.is_sized() {
522-
span_bug!(self.cur_span(), "unsized type for `NullaryOp::SizeOf`");
523-
}
524-
let val = layout.size.bytes();
525-
ImmTy::from_uint(val, usize_layout())
526-
}
527-
AlignOf => {
528-
if !layout.is_sized() {
529-
span_bug!(self.cur_span(), "unsized type for `NullaryOp::AlignOf`");
530-
}
531-
let val = layout.align.bytes();
532-
ImmTy::from_uint(val, usize_layout())
533-
}
534520
OffsetOf(fields) => {
535521
let val =
536522
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();

compiler/rustc_hir/src/lang_items.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ language_item_table! {
168168
MetaSized, sym::meta_sized, meta_sized_trait, Target::Trait, GenericRequirement::Exact(0);
169169
PointeeSized, sym::pointee_sized, pointee_sized_trait, Target::Trait, GenericRequirement::Exact(0);
170170
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
171+
AlignOf, sym::mem_align_const, align_const, Target::AssocConst, GenericRequirement::Exact(0);
172+
SizeOf, sym::mem_size_const, size_const, Target::AssocConst, GenericRequirement::Exact(0);
173+
IsZST, sym::mem_is_zst_const, is_zst_const, Target::AssocConst, GenericRequirement::Exact(0);
174+
IsNotZST, sym::mem_is_non_zst_const,is_non_zst_const, Target::AssocConst, GenericRequirement::Exact(0);
171175
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
172176
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
173177
Copy, sym::copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,6 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10921092
NullaryOp(ref op, ref t) => {
10931093
let t = with_no_trimmed_paths!(format!("{}", t));
10941094
match op {
1095-
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
1096-
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
10971095
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
10981096
NullOp::UbChecks => write!(fmt, "UbChecks()"),
10991097
NullOp::ContractChecks => write!(fmt, "ContractChecks()"),

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,7 @@ impl<'tcx> Rvalue<'tcx> {
782782
op.ty(tcx, arg_ty)
783783
}
784784
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
785-
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
786-
tcx.types.usize
787-
}
785+
Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => tcx.types.usize,
788786
Rvalue::NullaryOp(NullOp::ContractChecks, _)
789787
| Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
790788
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
@@ -853,7 +851,7 @@ impl BorrowKind {
853851
impl<'tcx> NullOp<'tcx> {
854852
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
855853
match self {
856-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
854+
NullOp::OffsetOf(_) => tcx.types.usize,
857855
NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
858856
}
859857
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,10 +1559,6 @@ pub enum AggregateKind<'tcx> {
15591559

15601560
#[derive(Copy, Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
15611561
pub enum NullOp<'tcx> {
1562-
/// Returns the size of a value of that type
1563-
SizeOf,
1564-
/// Returns the minimum alignment of a type
1565-
AlignOf,
15661562
/// Returns the offset of a field
15671563
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
15681564
/// Returns whether we should perform some UB-checking at runtime.

0 commit comments

Comments
 (0)