Skip to content

Commit 83b2c95

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 b803948 commit 83b2c95

File tree

2 files changed

+10
-1
lines changed

2 files changed

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

+9
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,14 @@ 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+
{
852+
static_mut_ref(self.tcx, block.stmts);
853+
}
854+
846855
let output = match fd.output {
847856
hir::FnRetTy::DefaultReturn(_) => None,
848857
hir::FnRetTy::Return(ty) => Some(ty),

0 commit comments

Comments
 (0)