Skip to content

Commit dd2bb01

Browse files
committed
Use more accurate span for addr_of! suggestion
1 parent 42cf687 commit dd2bb01

File tree

5 files changed

+43
-64
lines changed

5 files changed

+43
-64
lines changed

Diff for: 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,

Diff for: 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> {

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ LL | let ref _a = X;
2020
= 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
2121
help: use `addr_of!` instead to create a raw pointer
2222
|
23-
LL - let ref _a = X;
24-
LL + let ref _a = addr_of!(X);
25-
|
23+
LL | let ref _a = addr_of!(X);
24+
| +++++++++ +
2625

2726
error[E0796]: creating a mutable reference to a mutable static
2827
--> $DIR/reference-to-mut-static-unsafe-fn.rs:16:26
@@ -33,9 +32,8 @@ LL | let ref mut _a = X;
3332
= note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
3433
help: use `addr_of_mut!` instead to create a raw pointer
3534
|
36-
LL - let ref mut _a = X;
37-
LL + let ref mut _a = addr_of_mut!(X);
38-
|
35+
LL | let ref mut _a = addr_of_mut!(X);
36+
| +++++++++++++ +
3937

4038
error[E0796]: creating a shared reference to a mutable static
4139
--> $DIR/reference-to-mut-static-unsafe-fn.rs:19:25

Diff for: tests/ui/static/reference-to-mut-static.e2021.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ LL | let ref _a = X;
4444
= 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
4545
help: use `addr_of!` instead to create a raw pointer
4646
|
47-
LL - let ref _a = X;
48-
LL + let ref _a = addr_of!(X);
49-
|
47+
LL | let ref _a = addr_of!(X);
48+
| +++++++++ +
5049

5150
error: creating a shared reference to mutable static is discouraged
5251
--> $DIR/reference-to-mut-static.rs:32:25

Diff for: tests/ui/static/reference-to-mut-static.e2024.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ LL | let ref _a = X;
3333
= 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
3434
help: use `addr_of!` instead to create a raw pointer
3535
|
36-
LL - let ref _a = X;
37-
LL + let ref _a = addr_of!(X);
38-
|
36+
LL | let ref _a = addr_of!(X);
37+
| +++++++++ +
3938

4039
error[E0796]: creating a shared reference to a mutable static
4140
--> $DIR/reference-to-mut-static.rs:32:25

0 commit comments

Comments
 (0)