forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#119767 - GuillaumeGomez:rollup-fbp26yb, r=Gui…
…llaumeGomez Rollup of 9 pull requests Successful merges: - rust-lang#117556 (Disallow reference to `static mut` and adding `static_mut_ref` lint) - rust-lang#118748 (std: getrandom simplification for freebsd.) - rust-lang#119282 (Rework and improve the unstable documentation of check-cfg) - rust-lang#119527 (don't reexport atomic::ordering via rustc_data_structures, use std import) - rust-lang#119668 (Simplify implementation of MIR promotion) - rust-lang#119699 (Merge dead bb pruning and unreachable bb deduplication.) - rust-lang#119723 (Remove `-Zdont-buffer-diagnostics`.) - rust-lang#119756 (rustdoc-search: reuse individual types in function signatures) - rust-lang#119758 (GNU/Hurd: unconditionally use inline stack probes) r? `@ghost` `@rustbot` modify labels: rollup
- Loading branch information
Showing
107 changed files
with
1,963 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
pub mod check_consts; | ||
pub mod promote_consts; | ||
pub mod validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Reference of mutable static. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,edition2024,E0796 | ||
static mut X: i32 = 23; | ||
static mut Y: i32 = 24; | ||
unsafe { | ||
let y = &X; | ||
let ref x = X; | ||
let (x, y) = (&X, &Y); | ||
foo(&X); | ||
} | ||
fn foo<'a>(_x: &'a i32) {} | ||
``` | ||
|
||
Mutable statics can be written to by multiple threads: aliasing violations or | ||
data races will cause undefined behavior. | ||
|
||
Reference of mutable static is a hard error from 2024 edition. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use rustc_hir as hir; | ||
use rustc_hir_pretty::qpath_to_string; | ||
use rustc_lint_defs::builtin::STATIC_MUT_REF; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_span::Span; | ||
use rustc_type_ir::Mutability; | ||
|
||
use crate::errors; | ||
|
||
/// Check for shared or mutable references of `static mut` inside expression | ||
pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) { | ||
let span = expr.span; | ||
let hir_id = expr.hir_id; | ||
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind | ||
&& matches!(borrow_kind, hir::BorrowKind::Ref) | ||
&& let Some(var) = is_path_static_mut(*expr) | ||
{ | ||
handle_static_mut_ref( | ||
tcx, | ||
span, | ||
var, | ||
span.edition().at_least_rust_2024(), | ||
matches!(m, Mutability::Mut), | ||
hir_id, | ||
); | ||
} | ||
} | ||
|
||
/// Check for shared or mutable references of `static mut` inside statement | ||
pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) { | ||
if let hir::StmtKind::Local(loc) = stmt.kind | ||
&& let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind | ||
&& matches!(ba.0, rustc_ast::ByRef::Yes) | ||
&& let Some(init) = loc.init | ||
&& let Some(var) = is_path_static_mut(*init) | ||
{ | ||
handle_static_mut_ref( | ||
tcx, | ||
init.span, | ||
var, | ||
loc.span.edition().at_least_rust_2024(), | ||
matches!(ba.1, Mutability::Mut), | ||
stmt.hir_id, | ||
); | ||
} | ||
} | ||
|
||
fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> { | ||
if let hir::ExprKind::Path(qpath) = expr.kind | ||
&& let hir::QPath::Resolved(_, path) = qpath | ||
&& let hir::def::Res::Def(def_kind, _) = path.res | ||
&& let hir::def::DefKind::Static(mt) = def_kind | ||
&& matches!(mt, Mutability::Mut) | ||
{ | ||
return Some(qpath_to_string(&qpath)); | ||
} | ||
None | ||
} | ||
|
||
fn handle_static_mut_ref( | ||
tcx: TyCtxt<'_>, | ||
span: Span, | ||
var: String, | ||
e2024: bool, | ||
mutable: bool, | ||
hir_id: hir::HirId, | ||
) { | ||
if e2024 { | ||
let sugg = if mutable { | ||
errors::StaticMutRefSugg::Mut { span, var } | ||
} else { | ||
errors::StaticMutRefSugg::Shared { span, var } | ||
}; | ||
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg }); | ||
return; | ||
} | ||
|
||
let (label, sugg, shared) = if mutable { | ||
( | ||
errors::RefOfMutStaticLabel::Mut { span }, | ||
errors::RefOfMutStaticSugg::Mut { span, var }, | ||
"mutable ", | ||
) | ||
} else { | ||
( | ||
errors::RefOfMutStaticLabel::Shared { span }, | ||
errors::RefOfMutStaticSugg::Shared { span, var }, | ||
"shared ", | ||
) | ||
}; | ||
tcx.emit_spanned_lint( | ||
STATIC_MUT_REF, | ||
hir_id, | ||
span, | ||
errors::RefOfMutStatic { shared, why_note: (), label, sugg }, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.