rust/tests/ui/static
Matthias Krüger 1b4b0e9a4d
Rollup merge of #125834 - workingjubilee:weaken-thir-unsafeck-for-addr-of-static-mut, r=compiler-errors
treat `&raw (const|mut) UNSAFE_STATIC` implied deref as safe

Fixes rust-lang/rust#125833

As reported in that and related issues, `static mut STATIC_MUT: T` is very often used in embedded code, and is in many ways equivalent to `static STATIC_CELL: SyncUnsafeCell<T>`. The Rust expression of `&raw mut STATIC_MUT` and `SyncUnsafeCell::get(&STATIC_CELL)` are approximately equal, and both evaluate to `*mut T`. The library function is safe because it has *declared itself* to be safe. However, the raw ref operator is unsafe because all uses of `static mut` are considered unsafe, even though the static's value is not used by this expression (unlike, for example, `&STATIC_MUT`).

We can fix this unnatural difference by simply adding the proper exclusion for the safety check inside the THIR unsafeck, so that we do not declare it unsafe if it is not.

While the primary concern here is `static mut`, this change is made for all instances of an "unsafe static", which includes a static declared inside `extern "abi" {}`. Hypothetically, we could go as far as generalizing this to all instances of `&raw (const|mut) *ptr`, but today we do not, as we have not actually considered the range of possible expressions that use a similar encoding. We do not even extend this to thread-local equivalents, because they have less clear semantics.
2024-07-23 13:06:54 +02:00
..
auxiliary
bad-const-type.rs
bad-const-type.stderr
duplicated-fields-issue-124464.rs
duplicated-fields-issue-124464.stderr
duplicated-fields-issue-125842.rs
duplicated-fields-issue-125842.stderr
issue-1660.rs
issue-5216.rs
issue-5216.stderr
issue-18118-2.rs
issue-18118-2.stderr
issue-18118.rs
issue-18118.stderr
issue-24446.rs
issue-24446.stderr
issue-24843.rs
issue-34194.rs
nested_item_main.rs
raw-ref-deref-with-unsafe.rs compiler: treat &raw (const|mut) UNSAFE_STATIC implied deref as safe 2024-07-22 14:54:36 -07:00
raw-ref-deref-without-unsafe.rs compiler: treat &raw (const|mut) UNSAFE_STATIC implied deref as safe 2024-07-22 14:54:36 -07:00
raw-ref-deref-without-unsafe.stderr compiler: treat &raw (const|mut) UNSAFE_STATIC implied deref as safe 2024-07-22 14:54:36 -07:00
raw-ref-extern-static.rs compiler: treat &raw (const|mut) UNSAFE_STATIC implied deref as safe 2024-07-22 14:54:36 -07:00
raw-ref-static-mut.rs compiler: treat &raw (const|mut) UNSAFE_STATIC implied deref as safe 2024-07-22 14:54:36 -07:00
refer-to-other-statics-by-value.rs
reference-to-mut-static-safe.e2021.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
reference-to-mut-static-safe.e2024.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
reference-to-mut-static-safe.rs Automatically taint InferCtxt when errors are emitted 2024-06-26 16:01:45 +00:00
reference-to-mut-static-unsafe-fn.rs Automatically taint InferCtxt when errors are emitted 2024-06-26 16:01:45 +00:00
reference-to-mut-static-unsafe-fn.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
reference-to-mut-static.e2021.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
reference-to-mut-static.e2024.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
reference-to-mut-static.rs
safe-extern-statics-mut.rs
safe-extern-statics-mut.stderr Use more accurate span for addr_of! suggestion 2024-07-18 18:39:20 +00:00
safe-extern-statics.rs
safe-extern-statics.stderr
static_sized_requirement.rs
static-closures.rs
static-closures.stderr
static-drop-scope.rs
static-drop-scope.stderr
static-extern-type.rs
static-items-cant-move.rs
static-items-cant-move.stderr
static-lifetime-bound.rs
static-lifetime-bound.stderr
static-lifetime.rs
static-lifetime.stderr
static-method-privacy.rs
static-method-privacy.stderr
static-mut-bad-types.rs
static-mut-bad-types.stderr
static-mut-foreign-requires-unsafe.rs
static-mut-foreign-requires-unsafe.stderr
static-mut-not-constant.rs
static-mut-not-constant.stderr
static-mut-not-pat.rs
static-mut-not-pat.stderr
static-mut-requires-unsafe.rs
static-mut-requires-unsafe.stderr
static-priv-by-default2.rs
static-priv-by-default2.stderr
static-reference-to-fn-1.rs
static-reference-to-fn-1.stderr
static-reference-to-fn-2.rs
static-reference-to-fn-2.stderr
static-region-bound.rs
static-region-bound.stderr
static-vec-repeat-not-constant.rs
static-vec-repeat-not-constant.stderr
thread-local-in-ctfe.rs
thread-local-in-ctfe.stderr