Skip to content

Commit c1b27ee

Browse files
committed
Fix unsafetyck disabling for custom MIR
1 parent 21ee03e commit c1b27ee

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

compiler/rustc_middle/src/mir/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ impl<'tcx> Body<'tcx> {
533533
};
534534
injection_phase > self.phase
535535
}
536+
537+
#[inline]
538+
pub fn is_custom_mir(&self) -> bool {
539+
self.injection_phase.is_some()
540+
}
536541
}
537542

538543
#[derive(Copy, Clone, PartialEq, Eq, Debug, TyEncodable, TyDecodable, HashStable)]

compiler/rustc_mir_transform/src/check_unsafety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ fn unsafety_check_result<'tcx>(
500500
// `mir_built` force this.
501501
let body = &tcx.mir_built(def).borrow();
502502

503-
if body.should_skip() {
503+
if body.is_custom_mir() {
504504
return tcx.arena.alloc(UnsafetyCheckResult {
505505
violations: Vec::new(),
506506
used_unsafe_blocks: FxHashSet::default(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// MIR for `raw_pointer` after built
2+
3+
fn raw_pointer(_1: *const i32) -> *const i32 {
4+
let mut _0: *const i32; // return place in scope 0 at $DIR/references.rs:+0:38: +0:48
5+
6+
bb0: {
7+
_0 = &raw const (*_1); // scope 0 at $DIR/references.rs:+4:9: +4:27
8+
return; // scope 0 at $DIR/references.rs:+5:9: +5:17
9+
}
10+
}

src/test/mir-opt/building/custom/references.rs

+14
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,22 @@ pub fn immut_ref(x: &i32) -> &i32 {
3636
)
3737
}
3838

39+
// EMIT_MIR references.raw_pointer.built.after.mir
40+
#[custom_mir(dialect = "built")]
41+
pub fn raw_pointer(x: *const i32) -> *const i32 {
42+
// Regression test for a bug in which unsafetyck was not correctly turned off for
43+
// `dialect = "built"`
44+
mir!({
45+
RET = addr_of!(*x);
46+
Return()
47+
})
48+
}
49+
3950
fn main() {
4051
let mut x = 5;
4152
assert_eq!(*mut_ref(&mut x), 5);
4253
assert_eq!(*immut_ref(&x), 5);
54+
unsafe {
55+
assert_eq!(*raw_pointer(addr_of!(x)), 5);
56+
}
4357
}

0 commit comments

Comments
 (0)