Skip to content

Commit

Permalink
Disallow use of static mut inside unsafe fn
Browse files Browse the repository at this point in the history
```rust
unsafe fn foo() {
static mut X: i32 = 23;
let y = &X;
}
```

This is the idea for the 2024 edition.
  • Loading branch information
obeis committed Nov 10, 2023
1 parent d5e84b2 commit 30c0f1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub mod dropck;
mod entry;
pub mod intrinsic;
pub mod intrinsicck;
mod region;
pub mod region;
pub mod wfcheck;

pub use check::check_abi;
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc_span::symbol::{sym, Ident};
use rustc_span::{Span, DUMMY_SP};
use std::fmt;

use crate::check::region::static_mut_ref;
use crate::errors;

trait RegionExt {
Expand Down Expand Up @@ -843,6 +844,15 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
_: Span,
_: LocalDefId,
) {
if let hir::intravisit::FnKind::ItemFn(_, _, h) = fk
&& matches!(h.unsafety, hir::Unsafety::Unsafe)
&& let body = self.tcx.hir().body(body_id)
&& let hir::ExprKind::Block(block, _) = body.value.kind
&& block.span.edition().at_least_rust_2024()
{
static_mut_ref(self.tcx, block.stmts);
}

let output = match fd.output {
hir::FnRetTy::DefaultReturn(_) => None,
hir::FnRetTy::Return(ty) => Some(ty),
Expand Down

0 comments on commit 30c0f1f

Please sign in to comment.