Skip to content

Commit 30c0f1f

Browse files
committed
Disallow use of static mut inside unsafe fn
```rust unsafe fn foo() { static mut X: i32 = 23; let y = &X; } ``` This is the idea for the 2024 edition.
1 parent d5e84b2 commit 30c0f1f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub mod dropck;
6868
mod entry;
6969
pub mod intrinsic;
7070
pub mod intrinsicck;
71-
mod region;
71+
pub mod region;
7272
pub mod wfcheck;
7373

7474
pub use check::check_abi;

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc_span::symbol::{sym, Ident};
2525
use rustc_span::{Span, DUMMY_SP};
2626
use std::fmt;
2727

28+
use crate::check::region::static_mut_ref;
2829
use crate::errors;
2930

3031
trait RegionExt {
@@ -843,6 +844,15 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
843844
_: Span,
844845
_: LocalDefId,
845846
) {
847+
if let hir::intravisit::FnKind::ItemFn(_, _, h) = fk
848+
&& matches!(h.unsafety, hir::Unsafety::Unsafe)
849+
&& let body = self.tcx.hir().body(body_id)
850+
&& let hir::ExprKind::Block(block, _) = body.value.kind
851+
&& block.span.edition().at_least_rust_2024()
852+
{
853+
static_mut_ref(self.tcx, block.stmts);
854+
}
855+
846856
let output = match fd.output {
847857
hir::FnRetTy::DefaultReturn(_) => None,
848858
hir::FnRetTy::Return(ty) => Some(ty),

0 commit comments

Comments
 (0)