Skip to content

Commit 90004f0

Browse files
committed
unsafeck: Don't treat AscribeUserType as use
Previously, if the MIR had an AscribeUserType statement that ascribed a type to the pointee of a raw pointer, it would be treated as a dereference of the raw pointer for purposes of unsafe-checking. For example, the following code would be treated as containing a raw-pointer dereference: fn foo(ptr: *const bool) { let _: bool = *ptr; } Producing this error: error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block --> issue-80059.rs:2:12 | 2 | let _: bool = *ptr; | ^^^^ dereference of raw pointer Note that the error points to the type ascription as having a dereference! That's because the generated AscribeUserType MIR statement is treated as containing a dereference of `_1`: AscribeUserType((*_1), +, UserTypeProjection { base: UserType(1), projs: [] }); Now the unsafe-checker ignores uses inside `AscribeUserType` statements, which means this code now compiles successfully. ----- Note that this code: fn foo(ptr: *const bool) { let _: bool = *ptr; } does *not* produce an error (it compiles fine) because of the magical behavior of the `_` (wildcard) pattern (see rust-lang#80059).
1 parent c8915ee commit 90004f0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

compiler/rustc_mir/src/transform/check_unsafety.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,18 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
113113
| StatementKind::StorageLive(..)
114114
| StatementKind::StorageDead(..)
115115
| StatementKind::Retag { .. }
116-
| StatementKind::AscribeUserType(..)
117116
| StatementKind::Coverage(..)
118117
| StatementKind::Nop => {
119118
// safe (at least as emitted during MIR construction)
120119
}
120+
StatementKind::AscribeUserType(..) => {
121+
// safe (at least as emitted during MIR construction)
122+
// This is handled separately because we don't want
123+
// super_statement to be called.
124+
// See this for more:
125+
// https://github.com/rust-lang/rust/issues/80059#issuecomment-756968485
126+
return;
127+
}
121128

122129
StatementKind::LlvmInlineAsm { .. } => self.require_unsafe(
123130
UnsafetyViolationKind::General,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
fn foo(ptr: *const bool) {
4+
let _: bool = *ptr;
5+
}
6+
7+
fn main() {}

0 commit comments

Comments
 (0)