Skip to content

Commit 13ac53e

Browse files
committed
Auto merge of #18003 - ChayimFriedman2:addr_of-static-mut, r=Veykril
Do not report missing unsafe on `addr_of[_mut]!(EXTERN_OR_MUT_STATIC)` The compiler no longer does as well; see rust-lang/rust#125834. Also require unsafe when accessing `extern` `static` (other than by `addr_of!()`). Fixes #17978.
2 parents aefe34d + 91f2016 commit 13ac53e

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

crates/hir-ty/src/diagnostics/unsafe_check.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use hir_def::{
55
body::Body,
66
hir::{Expr, ExprId, UnaryOp},
77
resolver::{resolver_for_expr, ResolveValueResult, Resolver, ValueNs},
8+
type_ref::Rawness,
89
DefWithBodyId,
910
};
1011

@@ -87,12 +88,20 @@ fn walk_unsafe(
8788
let g = resolver.update_to_inner_scope(db.upcast(), def, current);
8889
let value_or_partial = resolver.resolve_path_in_value_ns(db.upcast(), path);
8990
if let Some(ResolveValueResult::ValueNs(ValueNs::StaticId(id), _)) = value_or_partial {
90-
if db.static_data(id).mutable {
91+
let static_data = db.static_data(id);
92+
if static_data.mutable || static_data.is_extern {
9193
unsafe_expr_cb(UnsafeExpr { expr: current, inside_unsafe_block });
9294
}
9395
}
9496
resolver.reset_to_guard(g);
9597
}
98+
Expr::Ref { expr, rawness: Rawness::RawPtr, mutability: _ } => {
99+
if let Expr::Path(_) = body.exprs[*expr] {
100+
// Do not report unsafe for `addr_of[_mut]!(EXTERN_OR_MUT_STATIC)`,
101+
// see https://github.com/rust-lang/rust/pull/125834.
102+
return;
103+
}
104+
}
96105
Expr::MethodCall { .. } => {
97106
if infer
98107
.method_resolution(current)

crates/ide-diagnostics/src/handlers/missing_unsafe.rs

+50
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,56 @@ fn main() {
163163
);
164164
}
165165

166+
#[test]
167+
fn missing_unsafe_diagnostic_with_extern_static() {
168+
check_diagnostics(
169+
r#"
170+
//- minicore: copy
171+
172+
extern "C" {
173+
static EXTERN: i32;
174+
static mut EXTERN_MUT: i32;
175+
}
176+
177+
fn main() {
178+
let _x = EXTERN;
179+
//^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
180+
let _x = EXTERN_MUT;
181+
//^^^^^^^^^^💡 error: this operation is unsafe and requires an unsafe function or block
182+
unsafe {
183+
let _x = EXTERN;
184+
let _x = EXTERN_MUT;
185+
}
186+
}
187+
"#,
188+
);
189+
}
190+
191+
#[test]
192+
fn no_unsafe_diagnostic_with_addr_of_static() {
193+
check_diagnostics(
194+
r#"
195+
//- minicore: copy, addr_of
196+
197+
use core::ptr::{addr_of, addr_of_mut};
198+
199+
extern "C" {
200+
static EXTERN: i32;
201+
static mut EXTERN_MUT: i32;
202+
}
203+
static mut STATIC_MUT: i32 = 0;
204+
205+
fn main() {
206+
let _x = addr_of!(EXTERN);
207+
let _x = addr_of!(EXTERN_MUT);
208+
let _x = addr_of!(STATIC_MUT);
209+
let _x = addr_of_mut!(EXTERN_MUT);
210+
let _x = addr_of_mut!(STATIC_MUT);
211+
}
212+
"#,
213+
);
214+
}
215+
166216
#[test]
167217
fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
168218
check_diagnostics(

crates/ide/src/hover/tests.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ fn main() {
483483
file_id: FileId(
484484
1,
485485
),
486-
full_range: 632..867,
487-
focus_range: 693..699,
486+
full_range: 633..868,
487+
focus_range: 694..700,
488488
name: "FnOnce",
489489
kind: Trait,
490490
container_name: "function",
@@ -8479,8 +8479,8 @@ impl Iterator for S {
84798479
file_id: FileId(
84808480
1,
84818481
),
8482-
full_range: 7801..8043,
8483-
focus_range: 7866..7872,
8482+
full_range: 7802..8044,
8483+
focus_range: 7867..7873,
84848484
name: "Future",
84858485
kind: Trait,
84868486
container_name: "future",
@@ -8493,8 +8493,8 @@ impl Iterator for S {
84938493
file_id: FileId(
84948494
1,
84958495
),
8496-
full_range: 8673..9172,
8497-
focus_range: 8750..8758,
8496+
full_range: 8674..9173,
8497+
focus_range: 8751..8759,
84988498
name: "Iterator",
84998499
kind: Trait,
85008500
container_name: "iterator",

crates/test-utils/src/minicore.rs

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
//! todo: panic
6666
//! unimplemented: panic
6767
//! column:
68+
//! addr_of:
6869
6970
#![rustc_coherence_is_core]
7071

@@ -421,6 +422,17 @@ pub mod ptr {
421422
}
422423
// endregion:coerce_unsized
423424
// endregion:non_null
425+
426+
// region:addr_of
427+
#[rustc_macro_transparency = "semitransparent"]
428+
pub macro addr_of($place:expr) {
429+
&raw const $place
430+
}
431+
#[rustc_macro_transparency = "semitransparent"]
432+
pub macro addr_of_mut($place:expr) {
433+
&raw mut $place
434+
}
435+
// endregion:addr_of
424436
}
425437

426438
pub mod ops {

0 commit comments

Comments
 (0)