From c1b27eea45ef1fe330dd24dc3aa7028cb4d98e8c Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Sat, 3 Dec 2022 23:54:55 -0800 Subject: [PATCH] Fix unsafetyck disabling for custom MIR --- compiler/rustc_middle/src/mir/mod.rs | 5 +++++ compiler/rustc_mir_transform/src/check_unsafety.rs | 2 +- .../custom/references.raw_pointer.built.after.mir | 10 ++++++++++ src/test/mir-opt/building/custom/references.rs | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/test/mir-opt/building/custom/references.raw_pointer.built.after.mir diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index db4fe6f886b..77293ea6b22 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -533,6 +533,11 @@ impl<'tcx> Body<'tcx> { }; injection_phase > self.phase } + + #[inline] + pub fn is_custom_mir(&self) -> bool { + self.injection_phase.is_some() + } } #[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)] diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 782abd7804d..9c22b5df73c 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -500,7 +500,7 @@ fn unsafety_check_result<'tcx>( // `mir_built` force this. let body = &tcx.mir_built(def).borrow(); - if body.should_skip() { + if body.is_custom_mir() { return tcx.arena.alloc(UnsafetyCheckResult { violations: Vec::new(), used_unsafe_blocks: FxHashSet::default(), diff --git a/src/test/mir-opt/building/custom/references.raw_pointer.built.after.mir b/src/test/mir-opt/building/custom/references.raw_pointer.built.after.mir new file mode 100644 index 00000000000..775e5e3ad9b --- /dev/null +++ b/src/test/mir-opt/building/custom/references.raw_pointer.built.after.mir @@ -0,0 +1,10 @@ +// MIR for `raw_pointer` after built + +fn raw_pointer(_1: *const i32) -> *const i32 { + let mut _0: *const i32; // return place in scope 0 at $DIR/references.rs:+0:38: +0:48 + + bb0: { + _0 = &raw const (*_1); // scope 0 at $DIR/references.rs:+4:9: +4:27 + return; // scope 0 at $DIR/references.rs:+5:9: +5:17 + } +} diff --git a/src/test/mir-opt/building/custom/references.rs b/src/test/mir-opt/building/custom/references.rs index dee85722e86..c93f6ec624b 100644 --- a/src/test/mir-opt/building/custom/references.rs +++ b/src/test/mir-opt/building/custom/references.rs @@ -36,8 +36,22 @@ pub fn immut_ref(x: &i32) -> &i32 { ) } +// EMIT_MIR references.raw_pointer.built.after.mir +#[custom_mir(dialect = "built")] +pub fn raw_pointer(x: *const i32) -> *const i32 { + // Regression test for a bug in which unsafetyck was not correctly turned off for + // `dialect = "built"` + mir!({ + RET = addr_of!(*x); + Return() + }) +} + fn main() { let mut x = 5; assert_eq!(*mut_ref(&mut x), 5); assert_eq!(*immut_ref(&x), 5); + unsafe { + assert_eq!(*raw_pointer(addr_of!(x)), 5); + } }