Skip to content

Commit 5cd7b31

Browse files
committed
Auto merge of #149649 - wesleywiser:revert_147793, r=wesleywiser
[beta] Revert "Replace NullOp::SizeOf and NullOp::AlignOf by lang items." Totally clean revert, tests pass on my local machine. Fixes #149081 on 1.92 beta cc `@lcnr` `@BoxyUwU` `@jieyouxu` `@cjgillot`
2 parents ac0aff2 + 9deb2c4 commit 5cd7b31

File tree

89 files changed

+764
-755
lines changed

Some content is hidden

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

89 files changed

+764
-755
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,16 @@ 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+
}
10491059
&Rvalue::NullaryOp(NullOp::ContractChecks, _) => {}
10501060
&Rvalue::NullaryOp(NullOp::UbChecks, _) => {}
10511061

compiler/rustc_codegen_cranelift/example/example.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ pub fn debug_tuple() -> DebugTuple {
7272
DebugTuple(())
7373
}
7474

75+
pub fn size_of<T>() -> usize {
76+
intrinsics::size_of::<T>()
77+
}
78+
7579
pub fn use_size_of() -> usize {
7680
size_of::<u64>()
7781
}

compiler/rustc_codegen_cranelift/example/mini_core.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
extern_types,
77
decl_macro,
88
rustc_attrs,
9-
rustc_private,
109
transparent_unions,
1110
auto_traits,
1211
freeze_impls,
@@ -595,7 +594,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
595594
impl<T> Box<T> {
596595
pub fn new(val: T) -> Box<T> {
597596
unsafe {
598-
let size = size_of::<T>();
597+
let size = intrinsics::size_of::<T>();
599598
let ptr = libc::malloc(size);
600599
intrinsics::copy(&val as *const T as *const u8, ptr, size);
601600
Box(Unique { pointer: NonNull(ptr as *const T), _marker: PhantomData }, Global)
@@ -647,11 +646,11 @@ pub mod intrinsics {
647646
#[rustc_intrinsic]
648647
pub fn abort() -> !;
649648
#[rustc_intrinsic]
650-
pub const fn size_of<T>() -> usize;
649+
pub fn size_of<T>() -> usize;
651650
#[rustc_intrinsic]
652651
pub unsafe fn size_of_val<T: ?::Sized>(val: *const T) -> usize;
653652
#[rustc_intrinsic]
654-
pub const fn align_of<T>() -> usize;
653+
pub fn align_of<T>() -> usize;
655654
#[rustc_intrinsic]
656655
pub unsafe fn align_of_val<T: ?::Sized>(val: *const T) -> usize;
657656
#[rustc_intrinsic]
@@ -716,23 +715,6 @@ impl<T> Index<usize> for [T] {
716715
}
717716
}
718717

719-
pub const fn size_of<T>() -> usize {
720-
<T as SizedTypeProperties>::SIZE
721-
}
722-
723-
pub const fn align_of<T>() -> usize {
724-
<T as SizedTypeProperties>::ALIGN
725-
}
726-
727-
trait SizedTypeProperties: Sized {
728-
#[lang = "mem_size_const"]
729-
const SIZE: usize = intrinsics::size_of::<Self>();
730-
731-
#[lang = "mem_align_const"]
732-
const ALIGN: usize = intrinsics::align_of::<Self>();
733-
}
734-
impl<T> SizedTypeProperties for T {}
735-
736718
extern "C" {
737719
type VaListImpl;
738720
}

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ fn start<T: Termination + 'static>(
109109
puts(*argv as *const i8);
110110
}
111111
unsafe {
112-
puts(*((argv as usize + size_of::<*const u8>()) as *const *const i8));
112+
puts(*((argv as usize + intrinsics::size_of::<*const u8>()) as *const *const i8));
113113
}
114114
unsafe {
115-
puts(*((argv as usize + 2 * size_of::<*const u8>()) as *const *const i8));
115+
puts(*((argv as usize + 2 * intrinsics::size_of::<*const u8>()) as *const *const i8));
116116
}
117117
}
118118

@@ -213,8 +213,8 @@ fn main() {
213213
assert_eq!(intrinsics::size_of_val(a) as u8, 16);
214214
assert_eq!(intrinsics::size_of_val(&0u32) as u8, 4);
215215

216-
assert_eq!(align_of::<u16>() as u8, 2);
217-
assert_eq!(intrinsics::align_of_val(&a) as u8, align_of::<&str>() as u8);
216+
assert_eq!(intrinsics::align_of::<u16>() as u8, 2);
217+
assert_eq!(intrinsics::align_of_val(&a) as u8, intrinsics::align_of::<&str>() as u8);
218218

219219
let u8_needs_drop = const { intrinsics::needs_drop::<u8>() };
220220
assert!(!u8_needs_drop);

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
833833
assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env()));
834834
let layout = fx.layout_of(fx.monomorphize(ty));
835835
let val = match null_op {
836+
NullOp::SizeOf => layout.size.bytes(),
837+
NullOp::AlignOf => layout.align.bytes(),
836838
NullOp::OffsetOf(fields) => fx
837839
.tcx
838840
.offset_of_subfield(

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,16 @@ 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+
}
614624
mir::NullOp::OffsetOf(fields) => {
615625
let val = bx
616626
.tcx()

compiler/rustc_const_eval/src/check_consts/check.rs

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

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

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ fn report_eval_error<'tcx>(
414414
let (error, backtrace) = error.into_parts();
415415
backtrace.print_backtrace();
416416

417+
let instance = with_no_trimmed_paths!(cid.instance.to_string());
418+
417419
super::report(
418420
ecx,
419421
error,
@@ -428,7 +430,7 @@ fn report_eval_error<'tcx>(
428430
diag.subdiagnostic(frame);
429431
}
430432
// Add after the frame rendering above, as it adds its own `instance` args.
431-
diag.arg("instance", with_no_trimmed_paths!(cid.instance.to_string()));
433+
diag.arg("instance", instance);
432434
diag.arg("num_frames", num_frames);
433435
},
434436
)

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,6 @@ 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 `size_of`");
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 `align_of`");
173-
}
174-
let val = layout.align.bytes();
175-
self.write_scalar(Scalar::from_target_usize(val, self), dest)?;
176-
}
177159
sym::variant_count => {
178160
let tp_ty = instance.args.type_at(0);
179161
let ty = match tp_ty.kind() {

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,20 @@ 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+
}
520534
OffsetOf(fields) => {
521535
let val =
522536
self.tcx.offset_of_subfield(self.typing_env, layout, fields.iter()).bytes();

0 commit comments

Comments
 (0)