Skip to content

Commit d97f0a1

Browse files
committed
Disallow hidden references to mutable static
1 parent 8cef37d commit d97f0a1

File tree

2 files changed

+61
-18
lines changed

2 files changed

+61
-18
lines changed

compiler/rustc_hir_analysis/src/check/errs.rs

+59-16
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,36 @@ use crate::errors;
1010
pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
1111
let span = expr.span;
1212
let hir_id = expr.hir_id;
13-
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind
14-
&& matches!(borrow_kind, hir::BorrowKind::Ref)
15-
&& let Some(var) = path_if_static_mut(tcx, expr)
16-
{
17-
handle_static_mut_ref(tcx, span, var, span.edition().at_least_rust_2024(), m, hir_id);
13+
match expr.kind {
14+
hir::ExprKind::AddrOf(borrow_kind, m, expr) => {
15+
if matches!(borrow_kind, hir::BorrowKind::Ref)
16+
&& let Some(var) = path_if_static_mut(tcx, expr)
17+
{
18+
handle_static_mut_ref(
19+
tcx,
20+
span,
21+
var,
22+
span.edition().at_least_rust_2024(),
23+
m,
24+
hir_id,
25+
true,
26+
);
27+
}
28+
}
29+
hir::ExprKind::MethodCall(_, expr, _, _) | hir::ExprKind::Index(expr, _, _) => {
30+
if let Some(var) = path_if_static_mut(tcx, expr) {
31+
handle_static_mut_ref(
32+
tcx,
33+
span,
34+
var,
35+
span.edition().at_least_rust_2024(),
36+
Mutability::Not,
37+
hir_id,
38+
false,
39+
);
40+
}
41+
}
42+
_ => {}
1843
}
1944
}
2045

@@ -33,6 +58,7 @@ pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
3358
loc.span.edition().at_least_rust_2024(),
3459
rmutbl,
3560
stmt.hir_id,
61+
true,
3662
);
3763
}
3864
}
@@ -55,25 +81,42 @@ fn handle_static_mut_ref(
5581
e2024: bool,
5682
mutable: Mutability,
5783
hir_id: hir::HirId,
84+
suggest: bool,
5885
) {
86+
let shared = if mutable == Mutability::Mut { "mutable" } else { "shared" };
5987
if e2024 {
60-
let (sugg, shared) = if mutable == Mutability::Mut {
61-
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
62-
} else {
63-
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
64-
};
65-
tcx.sess.psess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared });
66-
} else {
67-
let (sugg, shared) = if mutable == Mutability::Mut {
68-
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
88+
if suggest {
89+
let sugg = if mutable == Mutability::Mut {
90+
errors::StaticMutRefSugg::Mut { span, var }
91+
} else {
92+
errors::StaticMutRefSugg::Shared { span, var }
93+
};
94+
tcx.sess.psess.dcx.emit_err(errors::StaticMutRef { span, sugg: Some(sugg), shared });
95+
return;
96+
}
97+
tcx.sess.psess.dcx.emit_err(errors::StaticMutRef { span, sugg: None, shared });
98+
return;
99+
}
100+
101+
if suggest {
102+
let sugg = if mutable == Mutability::Mut {
103+
errors::RefOfMutStaticSugg::Mut { span, var }
69104
} else {
70-
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
105+
errors::RefOfMutStaticSugg::Shared { span, var }
71106
};
72107
tcx.emit_node_span_lint(
73108
STATIC_MUT_REFS,
74109
hir_id,
75110
span,
76-
errors::RefOfMutStatic { span, sugg, shared },
111+
errors::RefOfMutStatic { span, sugg: Some(sugg), shared },
77112
);
113+
return;
78114
}
115+
116+
tcx.emit_node_span_lint(
117+
STATIC_MUT_REFS,
118+
hir_id,
119+
span,
120+
errors::RefOfMutStatic { span, sugg: None, shared },
121+
);
79122
}

compiler/rustc_hir_analysis/src/errors.rs

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

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

0 commit comments

Comments
 (0)