Skip to content

Commit d1250bc

Browse files
authored
Rollup merge of #127929 - estebank:addr_of, r=compiler-errors
Use more accurate span for `addr_of!` suggestion Use a multipart suggestion instead of a single whole-span replacement: ``` error[E0796]: creating a shared reference to a mutable static --> $DIR/reference-to-mut-static-unsafe-fn.rs:10:18 | LL | let _y = &X; | ^^ shared reference to mutable static | = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior help: use `addr_of!` instead to create a raw pointer | LL | let _y = addr_of!(X); | ~~~~~~~~~ + ```
2 parents 1168837 + abf92c0 commit d1250bc

21 files changed

+74
-91
lines changed

compiler/rustc_hir_analysis/src/check/errs.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_hir as hir;
2-
use rustc_hir_pretty::qpath_to_string;
32
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
43
use rustc_middle::ty::{Mutability, TyCtxt};
54
use rustc_span::Span;
@@ -12,9 +11,17 @@ pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
1211
let hir_id = expr.hir_id;
1312
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind
1413
&& matches!(borrow_kind, hir::BorrowKind::Ref)
15-
&& let Some(var) = path_if_static_mut(tcx, expr)
14+
&& path_if_static_mut(expr)
1615
{
17-
handle_static_mut_ref(tcx, span, var, span.edition().at_least_rust_2024(), m, hir_id);
16+
handle_static_mut_ref(
17+
tcx,
18+
span,
19+
span.with_hi(expr.span.lo()),
20+
span.shrink_to_hi(),
21+
span.edition().at_least_rust_2024(),
22+
m,
23+
hir_id,
24+
);
1825
}
1926
}
2027

@@ -24,51 +31,53 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
2431
&& let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind
2532
&& let hir::ByRef::Yes(rmutbl) = ba.0
2633
&& let Some(init) = loc.init
27-
&& let Some(var) = path_if_static_mut(tcx, init)
34+
&& path_if_static_mut(init)
2835
{
2936
handle_static_mut_ref(
3037
tcx,
3138
init.span,
32-
var,
39+
init.span.shrink_to_lo(),
40+
init.span.shrink_to_hi(),
3341
loc.span.edition().at_least_rust_2024(),
3442
rmutbl,
3543
stmt.hir_id,
3644
);
3745
}
3846
}
3947

40-
fn path_if_static_mut(tcx: TyCtxt<'_>, expr: &hir::Expr<'_>) -> Option<String> {
48+
fn path_if_static_mut(expr: &hir::Expr<'_>) -> bool {
4149
if let hir::ExprKind::Path(qpath) = expr.kind
4250
&& let hir::QPath::Resolved(_, path) = qpath
4351
&& let hir::def::Res::Def(def_kind, _) = path.res
4452
&& let hir::def::DefKind::Static { safety: _, mutability: Mutability::Mut, nested: false } =
4553
def_kind
4654
{
47-
return Some(qpath_to_string(&tcx, &qpath));
55+
return true;
4856
}
49-
None
57+
false
5058
}
5159

5260
fn handle_static_mut_ref(
5361
tcx: TyCtxt<'_>,
5462
span: Span,
55-
var: String,
63+
lo: Span,
64+
hi: Span,
5665
e2024: bool,
5766
mutable: Mutability,
5867
hir_id: hir::HirId,
5968
) {
6069
if e2024 {
6170
let (sugg, shared) = if mutable == Mutability::Mut {
62-
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
71+
(errors::MutRefSugg::Mut { lo, hi }, "mutable")
6372
} else {
64-
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
73+
(errors::MutRefSugg::Shared { lo, hi }, "shared")
6574
};
6675
tcx.dcx().emit_err(errors::StaticMutRef { span, sugg, shared });
6776
} else {
6877
let (sugg, shared) = if mutable == Mutability::Mut {
69-
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
78+
(errors::MutRefSugg::Mut { lo, hi }, "mutable")
7079
} else {
71-
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
80+
(errors::MutRefSugg::Shared { lo, hi }, "shared")
7281
};
7382
tcx.emit_node_span_lint(
7483
STATIC_MUT_REFS,

compiler/rustc_hir_analysis/src/errors.rs

+13-39
Original file line numberDiff line numberDiff line change
@@ -1500,33 +1500,33 @@ pub struct StaticMutRef<'a> {
15001500
#[label]
15011501
pub span: Span,
15021502
#[subdiagnostic]
1503-
pub sugg: StaticMutRefSugg,
1503+
pub sugg: MutRefSugg,
15041504
pub shared: &'a str,
15051505
}
15061506

15071507
#[derive(Subdiagnostic)]
1508-
pub enum StaticMutRefSugg {
1509-
#[suggestion(
1508+
pub enum MutRefSugg {
1509+
#[multipart_suggestion(
15101510
hir_analysis_suggestion,
15111511
style = "verbose",
1512-
code = "addr_of!({var})",
15131512
applicability = "maybe-incorrect"
15141513
)]
15151514
Shared {
1516-
#[primary_span]
1517-
span: Span,
1518-
var: String,
1515+
#[suggestion_part(code = "addr_of!(")]
1516+
lo: Span,
1517+
#[suggestion_part(code = ")")]
1518+
hi: Span,
15191519
},
1520-
#[suggestion(
1520+
#[multipart_suggestion(
15211521
hir_analysis_suggestion_mut,
15221522
style = "verbose",
1523-
code = "addr_of_mut!({var})",
15241523
applicability = "maybe-incorrect"
15251524
)]
15261525
Mut {
1527-
#[primary_span]
1528-
span: Span,
1529-
var: String,
1526+
#[suggestion_part(code = "addr_of_mut!(")]
1527+
lo: Span,
1528+
#[suggestion_part(code = ")")]
1529+
hi: Span,
15301530
},
15311531
}
15321532

@@ -1539,36 +1539,10 @@ pub struct RefOfMutStatic<'a> {
15391539
#[label]
15401540
pub span: Span,
15411541
#[subdiagnostic]
1542-
pub sugg: RefOfMutStaticSugg,
1542+
pub sugg: MutRefSugg,
15431543
pub shared: &'a str,
15441544
}
15451545

1546-
#[derive(Subdiagnostic)]
1547-
pub enum RefOfMutStaticSugg {
1548-
#[suggestion(
1549-
hir_analysis_suggestion,
1550-
style = "verbose",
1551-
code = "addr_of!({var})",
1552-
applicability = "maybe-incorrect"
1553-
)]
1554-
Shared {
1555-
#[primary_span]
1556-
span: Span,
1557-
var: String,
1558-
},
1559-
#[suggestion(
1560-
hir_analysis_suggestion_mut,
1561-
style = "verbose",
1562-
code = "addr_of_mut!({var})",
1563-
applicability = "maybe-incorrect"
1564-
)]
1565-
Mut {
1566-
#[primary_span]
1567-
span: Span,
1568-
var: String,
1569-
},
1570-
}
1571-
15721546
#[derive(Diagnostic)]
15731547
#[diag(hir_analysis_not_supported_delegation)]
15741548
pub struct NotSupportedDelegation<'a> {

tests/ui/abi/statics/static-mut-foreign.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | static_bound(&rust_dbg_static_mut);
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | static_bound(addr_of!(rust_dbg_static_mut));
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: creating a mutable reference to mutable static is discouraged
1717
--> $DIR/static-mut-foreign.rs:33:22
@@ -25,7 +25,7 @@ LL | static_bound_set(&mut rust_dbg_static_mut);
2525
help: use `addr_of_mut!` instead to create a raw pointer
2626
|
2727
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));
28-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28+
| ~~~~~~~~~~~~~ +
2929

3030
warning: 2 warnings emitted
3131

tests/ui/borrowck/borrowck-access-permissions.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let _y2 = &mut static_x_mut;
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let _y2 = addr_of_mut!(static_x_mut);
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
1717
--> $DIR/borrowck-access-permissions.rs:10:19

tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let sfoo: *mut Foo = &mut SFOO;
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let sfoo: *mut Foo = addr_of_mut!(SFOO);
14-
| ~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/borrowck/issue-20801.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | unsafe { &mut GLOBAL_MUT_T }
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | unsafe { addr_of_mut!(GLOBAL_MUT_T) }
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
error[E0507]: cannot move out of a mutable reference
1717
--> $DIR/issue-20801.rs:27:22

tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | c1(&mut Y);
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | c1(addr_of_mut!(Y));
14-
| ~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: creating a mutable reference to mutable static is discouraged
1717
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:27:16
@@ -25,7 +25,7 @@ LL | c1(&mut Z);
2525
help: use `addr_of_mut!` instead to create a raw pointer
2626
|
2727
LL | c1(addr_of_mut!(Z));
28-
| ~~~~~~~~~~~~~~~
28+
| ~~~~~~~~~~~~~ +
2929

3030
warning: creating a mutable reference to mutable static is discouraged
3131
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:64:37
@@ -39,7 +39,7 @@ LL | borrowck_closures_unique::e(&mut X);
3939
help: use `addr_of_mut!` instead to create a raw pointer
4040
|
4141
LL | borrowck_closures_unique::e(addr_of_mut!(X));
42-
| ~~~~~~~~~~~~~~~
42+
| ~~~~~~~~~~~~~ +
4343

4444
error[E0594]: cannot assign to `x`, as it is not declared as mutable
4545
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:9:46

tests/ui/consts/const_let_assign2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let ptr = unsafe { &mut BB };
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | let ptr = unsafe { addr_of_mut!(BB) };
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/drop/issue-23338-ensure-param-drop-order.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | (mem::size_of_val(&trails) * 8) as u32
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/drop/issue-23611-enum-swap-in-drop.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | (mem::size_of_val(&trails) * 8) as u32
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/issues/issue-54410.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | println!("{:p}", unsafe { &symbol });
1919
help: use `addr_of!` instead to create a raw pointer
2020
|
2121
LL | println!("{:p}", unsafe { addr_of!(symbol) });
22-
| ~~~~~~~~~~~~~~~~
22+
| ~~~~~~~~~ +
2323

2424
error: aborting due to 1 previous error; 1 warning emitted
2525

tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | S1 { a: unsafe { &mut X1 } }
1111
help: use `addr_of_mut!` instead to create a raw pointer
1212
|
1313
LL | S1 { a: unsafe { addr_of_mut!(X1) } }
14-
| ~~~~~~~~~~~~~~~~
14+
| ~~~~~~~~~~~~~ +
1515

1616
warning: 1 warning emitted
1717

tests/ui/static/reference-to-mut-static-safe.e2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | let _x = &X;
1111
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | let _x = addr_of!(X);
14-
| ~~~~~~~~~~~
14+
| ~~~~~~~~~ +
1515

1616
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
1717
--> $DIR/reference-to-mut-static-safe.rs:9:15

tests/ui/static/reference-to-mut-static-safe.e2024.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | let _x = &X;
88
help: use `addr_of!` instead to create a raw pointer
99
|
1010
LL | let _x = addr_of!(X);
11-
| ~~~~~~~~~~~
11+
| ~~~~~~~~~ +
1212

1313
error[E0133]: use of mutable static is unsafe and requires unsafe block
1414
--> $DIR/reference-to-mut-static-safe.rs:9:15

tests/ui/static/reference-to-mut-static-unsafe-fn.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | let _y = &X;
88
help: use `addr_of!` instead to create a raw pointer
99
|
1010
LL | let _y = addr_of!(X);
11-
| ~~~~~~~~~~~
11+
| ~~~~~~~~~ +
1212

1313
error[E0796]: creating a shared reference to a mutable static
1414
--> $DIR/reference-to-mut-static-unsafe-fn.rs:13:22
@@ -20,7 +20,7 @@ LL | let ref _a = X;
2020
help: use `addr_of!` instead to create a raw pointer
2121
|
2222
LL | let ref _a = addr_of!(X);
23-
| ~~~~~~~~~~~
23+
| +++++++++ +
2424

2525
error[E0796]: creating a mutable reference to a mutable static
2626
--> $DIR/reference-to-mut-static-unsafe-fn.rs:16:26
@@ -32,7 +32,7 @@ LL | let ref mut _a = X;
3232
help: use `addr_of_mut!` instead to create a raw pointer
3333
|
3434
LL | let ref mut _a = addr_of_mut!(X);
35-
| ~~~~~~~~~~~~~~~
35+
| +++++++++++++ +
3636

3737
error[E0796]: creating a shared reference to a mutable static
3838
--> $DIR/reference-to-mut-static-unsafe-fn.rs:19:25
@@ -44,7 +44,7 @@ LL | let (_b, _c) = (&X, &mut Y);
4444
help: use `addr_of!` instead to create a raw pointer
4545
|
4646
LL | let (_b, _c) = (addr_of!(X), &mut Y);
47-
| ~~~~~~~~~~~
47+
| ~~~~~~~~~ +
4848

4949
error[E0796]: creating a mutable reference to a mutable static
5050
--> $DIR/reference-to-mut-static-unsafe-fn.rs:19:29
@@ -56,7 +56,7 @@ LL | let (_b, _c) = (&X, &mut Y);
5656
help: use `addr_of_mut!` instead to create a raw pointer
5757
|
5858
LL | let (_b, _c) = (&X, addr_of_mut!(Y));
59-
| ~~~~~~~~~~~~~~~
59+
| ~~~~~~~~~~~~~ +
6060

6161
error[E0796]: creating a shared reference to a mutable static
6262
--> $DIR/reference-to-mut-static-unsafe-fn.rs:23:13
@@ -68,7 +68,7 @@ LL | foo(&X);
6868
help: use `addr_of!` instead to create a raw pointer
6969
|
7070
LL | foo(addr_of!(X));
71-
| ~~~~~~~~~~~
71+
| ~~~~~~~~~ +
7272

7373
error: aborting due to 6 previous errors
7474

0 commit comments

Comments
 (0)