E5002
E5002 — Unsafe memory operation outside unsafe block
What the compiler reports
E5002{{.*}}Dereferencing a raw pointer requires an unsafe block
The fragments the test suite holds the compiler to. An ellipsis marks text the test does not constrain; the emitted message also carries a source location.
A program that triggers it
//
// Rust 2024: the body of an `unsafe fn` is not itself an unsafe context. The declaration states
// a contract on the caller; an unsafe operation inside the body still marks itself.
unsafe fn reads(ptr : *mut i32) -> i32 {
let v = *ptr;
return v;
}
fn main() -> i32 {
return 0;
}
From tests/frontend/fail/unsafe_fn_body_is_not_an_unsafe_context.vx, which asserts this diagnostic on every commit.
Related
Unsafe and FFIThe chapter covering the rule this code enforces.
All diagnosticsEvery code the compiler can emit, grouped by stage.
E5001Unsafe function call outside unsafe block.
Tensor<..>::uninit() is one: its elements hold whatever the memory held, so the program says with unsafe that it writes each one before reading it.