Skip to content

Commit 91f2016

Browse files
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.
1 parent 3d21d5e commit 91f2016

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

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

+8
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

@@ -94,6 +95,13 @@ fn walk_unsafe(
9495
}
9596
resolver.reset_to_guard(g);
9697
}
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+
}
97105
Expr::MethodCall { .. } => {
98106
if infer
99107
.method_resolution(current)

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

+25
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ fn main() {
188188
);
189189
}
190190

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+
191216
#[test]
192217
fn no_missing_unsafe_diagnostic_with_safe_intrinsic() {
193218
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)