diff --git a/compiler/rustc_codegen_cranelift/src/base.rs b/compiler/rustc_codegen_cranelift/src/base.rs index 7e99eb09b91ab..c97218797107b 100644 --- a/compiler/rustc_codegen_cranelift/src/base.rs +++ b/compiler/rustc_codegen_cranelift/src/base.rs @@ -829,13 +829,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt: fx.bcx.ins().nop(); } } - Rvalue::ShallowInitBox(ref operand, content_ty) => { - let content_ty = fx.monomorphize(content_ty); - let box_layout = fx.layout_of(Ty::new_box(fx.tcx, content_ty)); - let operand = codegen_operand(fx, operand); - let operand = operand.load_scalar(fx); - lval.write_cvalue(fx, CValue::by_val(operand, box_layout)); - } Rvalue::NullaryOp(ref null_op, ty) => { assert!(lval.layout().ty.is_sized(fx.tcx, fx.typing_env())); let layout = fx.layout_of(fx.monomorphize(ty)); @@ -924,6 +917,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt: lval.write_cvalue_transmute(fx, operand); } Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"), + Rvalue::ShallowInitBox(..) => bug!("`ShallowInitBox` in codegen"), } } StatementKind::StorageLive(_) diff --git a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs index 7a6cb2a51e317..0c626a9cd7817 100644 --- a/compiler/rustc_codegen_ssa/src/mir/rvalue.rs +++ b/compiler/rustc_codegen_ssa/src/mir/rvalue.rs @@ -724,15 +724,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } } } - mir::Rvalue::ShallowInitBox(ref operand, content_ty) => { - let operand = self.codegen_operand(bx, operand); - let val = operand.immediate(); - - let content_ty = self.monomorphize(content_ty); - let box_layout = bx.cx().layout_of(Ty::new_box(bx.tcx(), content_ty)); - - OperandRef { val: OperandValue::Immediate(val), layout: box_layout } - } mir::Rvalue::WrapUnsafeBinder(ref operand, binder_ty) => { let operand = self.codegen_operand(bx, operand); let binder_ty = self.monomorphize(binder_ty); @@ -740,6 +731,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { OperandRef { val: operand.val, layout } } mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"), + mir::Rvalue::ShallowInitBox(..) => bug!("`ShallowInitBox` in codegen"), } } diff --git a/compiler/rustc_index/src/lib.rs b/compiler/rustc_index/src/lib.rs index 9d647209c6f89..f61b5cf527982 100644 --- a/compiler/rustc_index/src/lib.rs +++ b/compiler/rustc_index/src/lib.rs @@ -50,3 +50,13 @@ macro_rules! static_assert_size { const _: (usize, usize) = ($size, ::std::mem::size_of::<$ty>()); }; } + +#[macro_export] +macro_rules! indexvec { + ($expr:expr; $n:expr) => { + IndexVec::from_raw(vec![$expr; $n]) + }; + ($($expr:expr),* $(,)?) => { + IndexVec::from_raw(vec![$($expr),*]) + }; +} diff --git a/compiler/rustc_mir_transform/src/coroutine.rs b/compiler/rustc_mir_transform/src/coroutine.rs index 26b6003e1095e..e5e9cd192afad 100644 --- a/compiler/rustc_mir_transform/src/coroutine.rs +++ b/compiler/rustc_mir_transform/src/coroutine.rs @@ -67,7 +67,7 @@ use rustc_hir as hir; use rustc_hir::lang_items::LangItem; use rustc_hir::{CoroutineDesugaring, CoroutineKind}; use rustc_index::bit_set::{BitMatrix, DenseBitSet, GrowableBitSet}; -use rustc_index::{Idx, IndexVec}; +use rustc_index::{Idx, IndexVec, indexvec}; use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::*; use rustc_middle::ty::util::Discr; @@ -289,7 +289,7 @@ impl<'tcx> TransformVisitor<'tcx> { let poll_def_id = self.tcx.require_lang_item(LangItem::Poll, source_info.span); let args = self.tcx.mk_args(&[self.old_ret_ty.into()]); let (variant_idx, operands) = if is_return { - (ZERO, IndexVec::from_raw(vec![val])) // Poll::Ready(val) + (ZERO, indexvec![val]) // Poll::Ready(val) } else { (ONE, IndexVec::new()) // Poll::Pending }; @@ -301,7 +301,7 @@ impl<'tcx> TransformVisitor<'tcx> { let (variant_idx, operands) = if is_return { (ZERO, IndexVec::new()) // None } else { - (ONE, IndexVec::from_raw(vec![val])) // Some(val) + (ONE, indexvec![val]) // Some(val) }; make_aggregate_adt(option_def_id, variant_idx, args, operands) } @@ -337,12 +337,7 @@ impl<'tcx> TransformVisitor<'tcx> { } else { ZERO // CoroutineState::Yielded(val) }; - make_aggregate_adt( - coroutine_state_def_id, - variant_idx, - args, - IndexVec::from_raw(vec![val]), - ) + make_aggregate_adt(coroutine_state_def_id, variant_idx, args, indexvec![val]) } }; @@ -1122,7 +1117,7 @@ fn return_poll_ready_assign<'tcx>(tcx: TyCtxt<'tcx>, source_info: SourceInfo) -> })); let ready_val = Rvalue::Aggregate( Box::new(AggregateKind::Adt(poll_def_id, VariantIdx::from_usize(0), args, None, None)), - IndexVec::from_raw(vec![val]), + indexvec![val], ); Statement::new(source_info, StatementKind::Assign(Box::new((Place::return_place(), ready_val)))) } diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index aae86a43127d9..fd5269d4ff8c8 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -480,6 +480,7 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> { Rvalue::Discriminant(place) => state.get_discr(place.as_ref(), &self.map), Rvalue::Use(operand) => return self.handle_operand(operand, state), Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"), + Rvalue::ShallowInitBox(..) => bug!("`ShallowInitBox` in runtime MIR"), Rvalue::Ref(..) | Rvalue::RawPtr(..) => { // We don't track such places. return ValueOrPlace::TOP; @@ -489,7 +490,6 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> { | Rvalue::Cast(..) | Rvalue::BinaryOp(..) | Rvalue::Aggregate(..) - | Rvalue::ShallowInitBox(..) | Rvalue::WrapUnsafeBinder(..) => { // No modification is possible through these r-values. return ValueOrPlace::TOP; diff --git a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs index 5c344a806880c..2a06cf4bd6019 100644 --- a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs +++ b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs @@ -2,12 +2,12 @@ //! //! Box is not actually a pointer so it is incorrect to dereference it directly. -use rustc_abi::FieldIdx; -use rustc_hir::def_id::DefId; +use rustc_abi::{FieldIdx, VariantIdx}; +use rustc_index::{IndexVec, indexvec}; use rustc_middle::mir::visit::MutVisitor; use rustc_middle::mir::*; use rustc_middle::span_bug; -use rustc_middle::ty::{Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use crate::patch::MirPatch; @@ -15,12 +15,12 @@ use crate::patch::MirPatch; fn build_ptr_tys<'tcx>( tcx: TyCtxt<'tcx>, pointee: Ty<'tcx>, - unique_did: DefId, - nonnull_did: DefId, + unique_def: ty::AdtDef<'tcx>, + nonnull_def: ty::AdtDef<'tcx>, ) -> (Ty<'tcx>, Ty<'tcx>, Ty<'tcx>) { let args = tcx.mk_args(&[pointee.into()]); - let unique_ty = tcx.type_of(unique_did).instantiate(tcx, args); - let nonnull_ty = tcx.type_of(nonnull_did).instantiate(tcx, args); + let unique_ty = Ty::new_adt(tcx, unique_def, args); + let nonnull_ty = Ty::new_adt(tcx, nonnull_def, args); let ptr_ty = Ty::new_imm_ptr(tcx, pointee); (unique_ty, nonnull_ty, ptr_ty) @@ -36,8 +36,8 @@ pub(super) fn build_projection<'tcx>( struct ElaborateBoxDerefVisitor<'a, 'tcx> { tcx: TyCtxt<'tcx>, - unique_did: DefId, - nonnull_did: DefId, + unique_def: ty::AdtDef<'tcx>, + nonnull_def: ty::AdtDef<'tcx>, local_decls: &'a mut LocalDecls<'tcx>, patch: MirPatch<'tcx>, } @@ -64,7 +64,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> { let source_info = self.local_decls[place.local].source_info; let (unique_ty, nonnull_ty, ptr_ty) = - build_ptr_tys(tcx, boxed_ty, self.unique_did, self.nonnull_did); + build_ptr_tys(tcx, boxed_ty, self.unique_def, self.nonnull_def); let ptr_local = self.patch.new_temp(ptr_ty, source_info.span); @@ -86,6 +86,68 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> { self.super_place(place, context, location); } + + fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) { + self.super_statement(stmt, location); + + let tcx = self.tcx; + let source_info = stmt.source_info; + + if let StatementKind::Assign(box (_, ref mut rvalue)) = stmt.kind + && let Rvalue::ShallowInitBox(ref mut mutptr_to_u8, pointee) = *rvalue + && let ty::Adt(box_adt, box_args) = Ty::new_box(tcx, pointee).kind() + { + let args = tcx.mk_args(&[pointee.into()]); + let (unique_ty, nonnull_ty, ptr_ty) = + build_ptr_tys(tcx, pointee, self.unique_def, self.nonnull_def); + let adt_kind = |def: ty::AdtDef<'tcx>, args| { + Box::new(AggregateKind::Adt(def.did(), VariantIdx::ZERO, args, None, None)) + }; + let zst = |ty| { + Operand::Constant(Box::new(ConstOperand { + span: source_info.span, + user_ty: None, + const_: Const::zero_sized(ty), + })) + }; + + let constptr = self.patch.new_temp(ptr_ty, source_info.span); + self.patch.add_assign( + location, + constptr.into(), + Rvalue::Cast(CastKind::Transmute, mutptr_to_u8.clone(), ptr_ty), + ); + + let nonnull = self.patch.new_temp(nonnull_ty, source_info.span); + self.patch.add_assign( + location, + nonnull.into(), + Rvalue::Aggregate( + adt_kind(self.nonnull_def, args), + indexvec![Operand::Move(constptr.into())], + ), + ); + + let unique = self.patch.new_temp(unique_ty, source_info.span); + let phantomdata_ty = + self.unique_def.non_enum_variant().fields[FieldIdx::ONE].ty(tcx, args); + self.patch.add_assign( + location, + unique.into(), + Rvalue::Aggregate( + adt_kind(self.unique_def, args), + indexvec![Operand::Move(nonnull.into()), zst(phantomdata_ty)], + ), + ); + + let global_alloc_ty = + box_adt.non_enum_variant().fields[FieldIdx::ONE].ty(tcx, box_args); + *rvalue = Rvalue::Aggregate( + adt_kind(*box_adt, box_args), + indexvec![Operand::Move(unique.into()), zst(global_alloc_ty)], + ); + } + } } pub(super) struct ElaborateBoxDerefs; @@ -97,18 +159,22 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs { let unique_did = tcx.adt_def(def_id).non_enum_variant().fields[FieldIdx::ZERO].did; - let Some(nonnull_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def() else { + let Some(unique_def) = tcx.type_of(unique_did).instantiate_identity().ty_adt_def() else { span_bug!(tcx.def_span(unique_did), "expected Box to contain Unique") }; - let nonnull_did = nonnull_def.non_enum_variant().fields[FieldIdx::ZERO].did; + let nonnull_did = unique_def.non_enum_variant().fields[FieldIdx::ZERO].did; + + let Some(nonnull_def) = tcx.type_of(nonnull_did).instantiate_identity().ty_adt_def() else { + span_bug!(tcx.def_span(nonnull_did), "expected Unique to contain Nonnull") + }; let patch = MirPatch::new(body); let local_decls = &mut body.local_decls; let mut visitor = - ElaborateBoxDerefVisitor { tcx, unique_did, nonnull_did, local_decls, patch }; + ElaborateBoxDerefVisitor { tcx, unique_def, nonnull_def, local_decls, patch }; for (block, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() { visitor.visit_basic_block_data(block, data); @@ -131,7 +197,7 @@ impl<'tcx> crate::MirPass<'tcx> for ElaborateBoxDerefs { new_projections.get_or_insert_with(|| base.projection.to_vec()); let (unique_ty, nonnull_ty, ptr_ty) = - build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did); + build_ptr_tys(tcx, boxed_ty, unique_def, nonnull_def); new_projections.extend_from_slice(&build_projection(unique_ty, nonnull_ty)); // While we can't project into `NonNull<_>` in a basic block diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index bdf53f45feacb..03073bb7cbdb0 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -1073,8 +1073,10 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> { } // Unsupported values. - Rvalue::ThreadLocalRef(..) | Rvalue::ShallowInitBox(..) => return None, - Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"), + Rvalue::ThreadLocalRef(..) => return None, + Rvalue::CopyForDeref(_) | Rvalue::ShallowInitBox(..) => { + bug!("forbidden in runtime MIR: {rvalue:?}") + } }; let ty = rvalue.ty(self.local_decls, self.tcx); Some(self.insert(ty, value)) diff --git a/compiler/rustc_mir_transform/src/validate.rs b/compiler/rustc_mir_transform/src/validate.rs index 2945298c3fdda..b5f055c0edcc5 100644 --- a/compiler/rustc_mir_transform/src/validate.rs +++ b/compiler/rustc_mir_transform/src/validate.rs @@ -670,7 +670,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { } } ProjectionElem::Deref - if self.body.phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) => + if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) => { let base_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty; @@ -970,7 +970,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { assert!(!adt_def.is_union()); let variant = &adt_def.variants()[idx]; if variant.fields.len() != fields.len() { - self.fail(location, "adt has the wrong number of initialized fields"); + self.fail(location, format!( + "adt {def_id:?} has the wrong number of initialized fields, expected {}, found {}", + fields.len(), + variant.fields.len(), + )); } for (src, dest) in std::iter::zip(fields, &variant.fields) { let dest_ty = self @@ -1175,6 +1179,9 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> { Rvalue::ShallowInitBox(operand, _) => { let a = operand.ty(&self.body.local_decls, self.tcx); check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..)); + if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) { + self.fail(location, format!("ShallowInitBox after ElaborateBoxDerefs")) + } } Rvalue::Cast(kind, operand, target_type) => { let op_ty = operand.ty(self.body, self.tcx); diff --git a/tests/mir-opt/box_expr.rs b/tests/mir-opt/box_expr.rs index 009a5ae54e089..6299c98718094 100644 --- a/tests/mir-opt/box_expr.rs +++ b/tests/mir-opt/box_expr.rs @@ -6,7 +6,10 @@ // EMIT_MIR box_expr.main.ElaborateDrops.diff fn main() { // CHECK-LABEL: fn main( - // CHECK: [[box:_.*]] = ShallowInitBox( + // CHECK: [[ptr:_.*]] = move {{_.*}} as *const S (Transmute); + // CHECK: [[nonnull:_.*]] = NonNull:: { pointer: move [[ptr]] }; + // CHECK: [[unique:_.*]] = Unique:: { pointer: move [[nonnull]], _marker: const PhantomData:: }; + // CHECK: [[box:_.*]] = Box::(move [[unique]], const std::alloc::Global); // CHECK: [[ptr:_.*]] = copy (([[box]].0: std::ptr::Unique).0: std::ptr::NonNull) as *const S (Transmute); // CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]]; // CHECK: [[ret]]: { diff --git a/tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff b/tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff index f43c0cca9ad28..16bae67cc9892 100644 --- a/tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff +++ b/tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff @@ -11,7 +11,10 @@ let mut _6: *mut u8; let mut _7: std::boxed::Box; let mut _8: *const i32; - let mut _9: *const i32; + let mut _9: std::ptr::NonNull; + let mut _10: std::ptr::Unique; + let mut _11: *const i32; + let mut _12: *const i32; scope 1 { debug x => _1; } @@ -31,13 +34,21 @@ bb1: { StorageLive(_7); - _7 = ShallowInitBox(move _6, i32); - _8 = copy ((_7.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); - (*_8) = const 42_i32; +- _8 = move _6 as *const i32 (Transmute); +- _9 = NonNull:: { pointer: move _8 }; +- _10 = Unique:: { pointer: move _9, _marker: const PhantomData:: }; ++ _8 = copy _6 as *const i32 (PtrToPtr); ++ _9 = NonNull:: { pointer: copy _8 }; ++ _10 = Unique:: { pointer: copy _9, _marker: const PhantomData:: }; + _7 = Box::(move _10, const std::alloc::Global); +- _11 = copy ((_7.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); +- (*_11) = const 42_i32; ++ _11 = copy _8; ++ (*_8) = const 42_i32; _3 = move _7; StorageDead(_7); - _9 = copy ((_3.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); - _2 = copy (*_9); + _12 = copy ((_3.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); + _2 = copy (*_12); - _1 = Add(move _2, const 0_i32); - StorageDead(_2); + _1 = copy _2; diff --git a/tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff b/tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff index 2c903b6d85349..3fbf7a57a76f8 100644 --- a/tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff +++ b/tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff @@ -11,7 +11,10 @@ let mut _6: *mut u8; let mut _7: std::boxed::Box; let mut _8: *const i32; - let mut _9: *const i32; + let mut _9: std::ptr::NonNull; + let mut _10: std::ptr::Unique; + let mut _11: *const i32; + let mut _12: *const i32; scope 1 { debug x => _1; } @@ -31,13 +34,21 @@ bb1: { StorageLive(_7); - _7 = ShallowInitBox(move _6, i32); - _8 = copy ((_7.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); - (*_8) = const 42_i32; +- _8 = move _6 as *const i32 (Transmute); +- _9 = NonNull:: { pointer: move _8 }; +- _10 = Unique:: { pointer: move _9, _marker: const PhantomData:: }; ++ _8 = copy _6 as *const i32 (PtrToPtr); ++ _9 = NonNull:: { pointer: copy _8 }; ++ _10 = Unique:: { pointer: copy _9, _marker: const PhantomData:: }; + _7 = Box::(move _10, const std::alloc::Global); +- _11 = copy ((_7.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); +- (*_11) = const 42_i32; ++ _11 = copy _8; ++ (*_8) = const 42_i32; _3 = move _7; StorageDead(_7); - _9 = copy ((_3.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); - _2 = copy (*_9); + _12 = copy ((_3.0: std::ptr::Unique).0: std::ptr::NonNull) as *const i32 (Transmute); + _2 = copy (*_12); - _1 = Add(move _2, const 0_i32); - StorageDead(_2); + _1 = copy _2; diff --git a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff index 5d7343a15bbea..f47e544385497 100644 --- a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff +++ b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.32bit.panic-abort.diff @@ -11,7 +11,7 @@ let mut _9: *const [()]; let mut _10: std::boxed::Box<()>; let mut _11: *const (); - let mut _25: usize; + let mut _27: usize; scope 1 { debug vp_ctx => _1; let _5: *const (); @@ -25,7 +25,7 @@ debug _x => _8; } scope 18 (inlined foo) { - let mut _26: *const [()]; + let mut _28: *const [()]; } } scope 16 (inlined slice_from_raw_parts::<()>) { @@ -39,18 +39,20 @@ let mut _13: usize; let mut _14: *mut u8; let mut _15: *const (); + let mut _16: std::ptr::NonNull<()>; + let mut _17: std::ptr::Unique<()>; scope 6 (inlined alloc::alloc::exchange_malloc) { - let _16: std::alloc::Layout; - let mut _17: std::result::Result, std::alloc::AllocError>; - let mut _18: isize; - let mut _20: !; + let _18: std::alloc::Layout; + let mut _19: std::result::Result, std::alloc::AllocError>; + let mut _20: isize; + let mut _22: !; scope 7 { - let _19: std::ptr::NonNull<[u8]>; + let _21: std::ptr::NonNull<[u8]>; scope 8 { scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) { scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) { scope 13 (inlined NonNull::<[u8]>::cast::) { - let mut _24: *mut [u8]; + let mut _26: *mut [u8]; scope 14 (inlined NonNull::<[u8]>::as_ptr) { } } @@ -63,9 +65,9 @@ } } scope 9 (inlined #[track_caller] Layout::from_size_align_unchecked) { - let mut _21: bool; - let _22: (); - let mut _23: std::ptr::Alignment; + let mut _23: bool; + let _24: (); + let mut _25: std::ptr::Alignment; } } } @@ -82,18 +84,20 @@ StorageLive(_13); StorageLive(_14); StorageLive(_15); + StorageLive(_16); + StorageLive(_17); - _12 = SizeOf(()); - _13 = AlignOf(()); + _12 = const 0_usize; + _13 = const 1_usize; - StorageLive(_16); StorageLive(_18); - StorageLive(_19); StorageLive(_20); - StorageLive(_22); StorageLive(_21); - _21 = UbChecks(); - switchInt(move _21) -> [0: bb6, otherwise: bb5]; + StorageLive(_22); + StorageLive(_24); + StorageLive(_23); + _23 = UbChecks(); + switchInt(move _23) -> [0: bb6, otherwise: bb5]; } bb1: { @@ -107,26 +111,33 @@ } bb3: { -- _20 = handle_alloc_error(move _16) -> unwind unreachable; -+ _20 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable; +- _22 = handle_alloc_error(move _18) -> unwind unreachable; ++ _22 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable; } bb4: { - _19 = copy ((_17 as Ok).0: std::ptr::NonNull<[u8]>); - StorageLive(_24); - _24 = copy _19 as *mut [u8] (Transmute); - _14 = copy _24 as *mut u8 (PtrToPtr); + _21 = copy ((_19 as Ok).0: std::ptr::NonNull<[u8]>); +- StorageLive(_26); ++ nop; + _26 = copy _21 as *mut [u8] (Transmute); + _14 = copy _26 as *mut u8 (PtrToPtr); +- StorageDead(_26); ++ nop; + StorageDead(_19); StorageDead(_24); - StorageDead(_17); StorageDead(_22); + StorageDead(_21); StorageDead(_20); - StorageDead(_19); StorageDead(_18); - StorageDead(_16); - _3 = ShallowInitBox(copy _14, ()); - _15 = copy ((_3.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute); +- _15 = copy _14 as *const () (PtrToPtr); ++ _15 = copy _26 as *const () (PtrToPtr); + _16 = NonNull::<()> { pointer: copy _15 }; + _17 = Unique::<()> { pointer: copy _16, _marker: const PhantomData::<()> }; + _3 = Box::<()>(move _17, const std::alloc::Global); - (*_15) = move _4; + (*_15) = const (); + StorageDead(_17); + StorageDead(_16); StorageDead(_15); StorageDead(_14); StorageDead(_13); @@ -146,21 +157,21 @@ + nop; StorageLive(_7); _7 = copy _5; - StorageLive(_25); - _25 = const 1_usize; -- _6 = *const [()] from (copy _7, copy _25); + StorageLive(_27); + _27 = const 1_usize; +- _6 = *const [()] from (copy _7, copy _27); + _6 = *const [()] from (copy _5, const 1_usize); - StorageDead(_25); + StorageDead(_27); StorageDead(_7); StorageLive(_8); StorageLive(_9); _9 = copy _6; - StorageLive(_26); -- _26 = copy _9; + StorageLive(_28); +- _28 = copy _9; - _8 = copy _9 as *mut () (PtrToPtr); -+ _26 = copy _6; ++ _28 = copy _6; + _8 = copy _5 as *mut () (PtrToPtr); - StorageDead(_26); + StorageDead(_28); StorageDead(_9); _0 = const (); StorageDead(_8); @@ -172,26 +183,26 @@ } bb5: { -- _22 = Layout::from_size_align_unchecked::precondition_check(copy _12, copy _13) -> [return: bb6, unwind unreachable]; -+ _22 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable]; +- _24 = Layout::from_size_align_unchecked::precondition_check(copy _12, copy _13) -> [return: bb6, unwind unreachable]; ++ _24 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable]; } bb6: { - StorageDead(_21); - StorageLive(_23); -- _23 = copy _13 as std::ptr::Alignment (Transmute); -- _16 = Layout { size: copy _12, align: move _23 }; -+ _23 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0); -+ _16 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}; StorageDead(_23); - StorageLive(_17); -- _17 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _16, const false) -> [return: bb7, unwind unreachable]; -+ _17 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable]; + StorageLive(_25); +- _25 = copy _13 as std::ptr::Alignment (Transmute); +- _18 = Layout { size: copy _12, align: move _25 }; ++ _25 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0); ++ _18 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}; + StorageDead(_25); + StorageLive(_19); +- _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _18, const false) -> [return: bb7, unwind unreachable]; ++ _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable]; } bb7: { - _18 = discriminant(_17); - switchInt(move _18) -> [0: bb4, 1: bb3, otherwise: bb2]; + _20 = discriminant(_19); + switchInt(move _20) -> [0: bb4, 1: bb3, otherwise: bb2]; } + } + diff --git a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff index ba5fe287b8e1c..fe88ff7a53e7d 100644 --- a/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff +++ b/tests/mir-opt/dont_reset_cast_kind_without_updating_operand.test.GVN.64bit.panic-abort.diff @@ -11,7 +11,7 @@ let mut _9: *const [()]; let mut _10: std::boxed::Box<()>; let mut _11: *const (); - let mut _25: usize; + let mut _27: usize; scope 1 { debug vp_ctx => _1; let _5: *const (); @@ -25,7 +25,7 @@ debug _x => _8; } scope 18 (inlined foo) { - let mut _26: *const [()]; + let mut _28: *const [()]; } } scope 16 (inlined slice_from_raw_parts::<()>) { @@ -39,18 +39,20 @@ let mut _13: usize; let mut _14: *mut u8; let mut _15: *const (); + let mut _16: std::ptr::NonNull<()>; + let mut _17: std::ptr::Unique<()>; scope 6 (inlined alloc::alloc::exchange_malloc) { - let _16: std::alloc::Layout; - let mut _17: std::result::Result, std::alloc::AllocError>; - let mut _18: isize; - let mut _20: !; + let _18: std::alloc::Layout; + let mut _19: std::result::Result, std::alloc::AllocError>; + let mut _20: isize; + let mut _22: !; scope 7 { - let _19: std::ptr::NonNull<[u8]>; + let _21: std::ptr::NonNull<[u8]>; scope 8 { scope 11 (inlined NonNull::<[u8]>::as_mut_ptr) { scope 12 (inlined NonNull::<[u8]>::as_non_null_ptr) { scope 13 (inlined NonNull::<[u8]>::cast::) { - let mut _24: *mut [u8]; + let mut _26: *mut [u8]; scope 14 (inlined NonNull::<[u8]>::as_ptr) { } } @@ -63,9 +65,9 @@ } } scope 9 (inlined #[track_caller] Layout::from_size_align_unchecked) { - let mut _21: bool; - let _22: (); - let mut _23: std::ptr::Alignment; + let mut _23: bool; + let _24: (); + let mut _25: std::ptr::Alignment; } } } @@ -82,18 +84,20 @@ StorageLive(_13); StorageLive(_14); StorageLive(_15); + StorageLive(_16); + StorageLive(_17); - _12 = SizeOf(()); - _13 = AlignOf(()); + _12 = const 0_usize; + _13 = const 1_usize; - StorageLive(_16); StorageLive(_18); - StorageLive(_19); StorageLive(_20); - StorageLive(_22); StorageLive(_21); - _21 = UbChecks(); - switchInt(move _21) -> [0: bb6, otherwise: bb5]; + StorageLive(_22); + StorageLive(_24); + StorageLive(_23); + _23 = UbChecks(); + switchInt(move _23) -> [0: bb6, otherwise: bb5]; } bb1: { @@ -107,26 +111,33 @@ } bb3: { -- _20 = handle_alloc_error(move _16) -> unwind unreachable; -+ _20 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable; +- _22 = handle_alloc_error(move _18) -> unwind unreachable; ++ _22 = handle_alloc_error(const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}) -> unwind unreachable; } bb4: { - _19 = copy ((_17 as Ok).0: std::ptr::NonNull<[u8]>); - StorageLive(_24); - _24 = copy _19 as *mut [u8] (Transmute); - _14 = copy _24 as *mut u8 (PtrToPtr); + _21 = copy ((_19 as Ok).0: std::ptr::NonNull<[u8]>); +- StorageLive(_26); ++ nop; + _26 = copy _21 as *mut [u8] (Transmute); + _14 = copy _26 as *mut u8 (PtrToPtr); +- StorageDead(_26); ++ nop; + StorageDead(_19); StorageDead(_24); - StorageDead(_17); StorageDead(_22); + StorageDead(_21); StorageDead(_20); - StorageDead(_19); StorageDead(_18); - StorageDead(_16); - _3 = ShallowInitBox(copy _14, ()); - _15 = copy ((_3.0: std::ptr::Unique<()>).0: std::ptr::NonNull<()>) as *const () (Transmute); +- _15 = copy _14 as *const () (PtrToPtr); ++ _15 = copy _26 as *const () (PtrToPtr); + _16 = NonNull::<()> { pointer: copy _15 }; + _17 = Unique::<()> { pointer: copy _16, _marker: const PhantomData::<()> }; + _3 = Box::<()>(move _17, const std::alloc::Global); - (*_15) = move _4; + (*_15) = const (); + StorageDead(_17); + StorageDead(_16); StorageDead(_15); StorageDead(_14); StorageDead(_13); @@ -146,21 +157,21 @@ + nop; StorageLive(_7); _7 = copy _5; - StorageLive(_25); - _25 = const 1_usize; -- _6 = *const [()] from (copy _7, copy _25); + StorageLive(_27); + _27 = const 1_usize; +- _6 = *const [()] from (copy _7, copy _27); + _6 = *const [()] from (copy _5, const 1_usize); - StorageDead(_25); + StorageDead(_27); StorageDead(_7); StorageLive(_8); StorageLive(_9); _9 = copy _6; - StorageLive(_26); -- _26 = copy _9; + StorageLive(_28); +- _28 = copy _9; - _8 = copy _9 as *mut () (PtrToPtr); -+ _26 = copy _6; ++ _28 = copy _6; + _8 = copy _5 as *mut () (PtrToPtr); - StorageDead(_26); + StorageDead(_28); StorageDead(_9); _0 = const (); StorageDead(_8); @@ -172,26 +183,26 @@ } bb5: { -- _22 = Layout::from_size_align_unchecked::precondition_check(copy _12, copy _13) -> [return: bb6, unwind unreachable]; -+ _22 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable]; +- _24 = Layout::from_size_align_unchecked::precondition_check(copy _12, copy _13) -> [return: bb6, unwind unreachable]; ++ _24 = Layout::from_size_align_unchecked::precondition_check(const 0_usize, const 1_usize) -> [return: bb6, unwind unreachable]; } bb6: { - StorageDead(_21); - StorageLive(_23); -- _23 = copy _13 as std::ptr::Alignment (Transmute); -- _16 = Layout { size: copy _12, align: move _23 }; -+ _23 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0); -+ _16 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}; StorageDead(_23); - StorageLive(_17); -- _17 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _16, const false) -> [return: bb7, unwind unreachable]; -+ _17 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable]; + StorageLive(_25); +- _25 = copy _13 as std::ptr::Alignment (Transmute); +- _18 = Layout { size: copy _12, align: move _25 }; ++ _25 = const std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0); ++ _18 = const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}; + StorageDead(_25); + StorageLive(_19); +- _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], copy _18, const false) -> [return: bb7, unwind unreachable]; ++ _19 = std::alloc::Global::alloc_impl(const alloc::alloc::exchange_malloc::promoted[0], const Layout {{ size: 0_usize, align: std::ptr::Alignment(std::ptr::alignment::AlignmentEnum::_Align1Shl0) }}, const false) -> [return: bb7, unwind unreachable]; } bb7: { - _18 = discriminant(_17); - switchInt(move _18) -> [0: bb4, 1: bb3, otherwise: bb2]; + _20 = discriminant(_19); + switchInt(move _20) -> [0: bb4, 1: bb3, otherwise: bb2]; } + } +