Skip to content

Commit 13edaa3

Browse files
committed
Auto merge of rust-lang#119767 - GuillaumeGomez:rollup-fbp26yb, r=GuillaumeGomez
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
2 parents be00c5a + f41d773 commit 13edaa3

File tree

107 files changed

+1963
-790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1963
-790
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3876,6 +3876,7 @@ dependencies = [
38763876
"rustc_feature",
38773877
"rustc_fluent_macro",
38783878
"rustc_hir",
3879+
"rustc_hir_pretty",
38793880
"rustc_index",
38803881
"rustc_infer",
38813882
"rustc_lint_defs",

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ fn start<T: Termination + 'static>(
111111
}
112112

113113
static mut NUM: u8 = 6 * 7;
114+
115+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
116+
#[allow(static_mut_ref)]
114117
static NUM_REF: &'static u8 = unsafe { &NUM };
115118

116119
unsafe fn zeroed<T>() -> T {

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ fn start<T: Termination + 'static>(
9898
}
9999

100100
static mut NUM: u8 = 6 * 7;
101+
102+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
103+
#[allow(static_mut_ref)]
101104
static NUM_REF: &'static u8 = unsafe { &NUM };
102105

103106
macro_rules! assert {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
pub mod check_consts;
2-
pub mod promote_consts;
32
pub mod validate;

compiler/rustc_data_structures/src/sync.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ mod parallel;
5656
pub use parallel::scope;
5757
pub use parallel::{join, par_for_each_in, par_map, parallel_guard, try_par_for_each_in};
5858

59-
pub use std::sync::atomic::Ordering;
60-
pub use std::sync::atomic::Ordering::SeqCst;
61-
6259
pub use vec::{AppendOnlyIndexVec, AppendOnlyVec};
6360

6461
mod vec;
@@ -67,8 +64,7 @@ mod freeze;
6764
pub use freeze::{FreezeLock, FreezeReadGuard, FreezeWriteGuard};
6865

6966
mod mode {
70-
use super::Ordering;
71-
use std::sync::atomic::AtomicU8;
67+
use std::sync::atomic::{AtomicU8, Ordering};
7268

7369
const UNINITIALIZED: u8 = 0;
7470
const DYN_NOT_THREAD_SAFE: u8 = 1;
@@ -113,6 +109,7 @@ cfg_match! {
113109
cfg(not(parallel_compiler)) => {
114110
use std::ops::Add;
115111
use std::cell::Cell;
112+
use std::sync::atomic::Ordering;
116113

117114
pub unsafe auto trait Send {}
118115
pub unsafe auto trait Sync {}

compiler/rustc_driver_impl/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults};
2525
use rustc_data_structures::profiling::{
2626
get_resident_set_size, print_time_passes_entry, TimePassesFormat,
2727
};
28-
use rustc_data_structures::sync::SeqCst;
2928
use rustc_errors::registry::{InvalidErrorCode, Registry};
3029
use rustc_errors::{markdown, ColorConfig};
3130
use rustc_errors::{DiagCtxt, ErrorGuaranteed, PResult};
@@ -476,7 +475,7 @@ fn run_compiler(
476475
eprintln!(
477476
"Fuel used by {}: {}",
478477
sess.opts.unstable_opts.print_fuel.as_ref().unwrap(),
479-
sess.print_fuel.load(SeqCst)
478+
sess.print_fuel.load(Ordering::SeqCst)
480479
);
481480
}
482481

compiler/rustc_error_codes/src/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ E0792: include_str!("./error_codes/E0792.md"),
515515
E0793: include_str!("./error_codes/E0793.md"),
516516
E0794: include_str!("./error_codes/E0794.md"),
517517
E0795: include_str!("./error_codes/E0795.md"),
518+
E0796: include_str!("./error_codes/E0796.md"),
518519
}
519520

520521
// Undocumented removed error codes. Note that many removed error codes are kept in the list above
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Reference of mutable static.
2+
3+
Erroneous code example:
4+
5+
```compile_fail,edition2024,E0796
6+
static mut X: i32 = 23;
7+
static mut Y: i32 = 24;
8+
9+
unsafe {
10+
let y = &X;
11+
let ref x = X;
12+
let (x, y) = (&X, &Y);
13+
foo(&X);
14+
}
15+
16+
fn foo<'a>(_x: &'a i32) {}
17+
```
18+
19+
Mutable statics can be written to by multiple threads: aliasing violations or
20+
data races will cause undefined behavior.
21+
22+
Reference of mutable static is a hard error from 2024 edition.

compiler/rustc_errors/src/diagnostic_builder.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
266266
/// Converts the builder to a `Diagnostic` for later emission,
267267
/// unless dcx has disabled such buffering.
268268
pub fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a DiagCtxt)> {
269-
let flags = self.dcx.inner.lock().flags;
270-
if flags.dont_buffer_diagnostics || flags.treat_err_as_bug.is_some() {
269+
if self.dcx.inner.lock().flags.treat_err_as_bug.is_some() {
271270
self.emit();
272271
return None;
273272
}

compiler/rustc_errors/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,6 @@ pub struct DiagCtxtFlags {
524524
/// If Some, the Nth error-level diagnostic is upgraded to bug-level.
525525
/// (rustc: see `-Z treat-err-as-bug`)
526526
pub treat_err_as_bug: Option<NonZeroUsize>,
527-
/// If true, immediately emit diagnostics that would otherwise be buffered.
528-
/// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`)
529-
pub dont_buffer_diagnostics: bool,
530527
/// Show macro backtraces.
531528
/// (rustc: see `-Z macro-backtrace`)
532529
pub macro_backtrace: bool,

compiler/rustc_hir_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_errors = { path = "../rustc_errors" }
1717
rustc_feature = { path = "../rustc_feature" }
1818
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1919
rustc_hir = { path = "../rustc_hir" }
20+
rustc_hir_pretty = { path = "../rustc_hir_pretty" }
2021
rustc_index = { path = "../rustc_index" }
2122
rustc_infer = { path = "../rustc_infer" }
2223
rustc_lint_defs = { path = "../rustc_lint_defs" }

compiler/rustc_hir_analysis/messages.ftl

+14
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,20 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
346346
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
347347
.label = `#[start]` function is not allowed to be `#[track_caller]`
348348
349+
hir_analysis_static_mut_ref = reference of mutable static is disallowed
350+
.label = reference of mutable static
351+
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
352+
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
353+
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
354+
355+
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
356+
.label = shared reference of mutable static
357+
.label_mut = mutable reference of mutable static
358+
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
359+
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
360+
.note = reference of mutable static is a hard error from 2024 edition
361+
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
362+
349363
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
350364
351365
hir_analysis_substs_on_overridden_impl = could not resolve substs on overridden impl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use rustc_hir as hir;
2+
use rustc_hir_pretty::qpath_to_string;
3+
use rustc_lint_defs::builtin::STATIC_MUT_REF;
4+
use rustc_middle::ty::TyCtxt;
5+
use rustc_span::Span;
6+
use rustc_type_ir::Mutability;
7+
8+
use crate::errors;
9+
10+
/// Check for shared or mutable references of `static mut` inside expression
11+
pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
12+
let span = expr.span;
13+
let hir_id = expr.hir_id;
14+
if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind
15+
&& matches!(borrow_kind, hir::BorrowKind::Ref)
16+
&& let Some(var) = is_path_static_mut(*expr)
17+
{
18+
handle_static_mut_ref(
19+
tcx,
20+
span,
21+
var,
22+
span.edition().at_least_rust_2024(),
23+
matches!(m, Mutability::Mut),
24+
hir_id,
25+
);
26+
}
27+
}
28+
29+
/// Check for shared or mutable references of `static mut` inside statement
30+
pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) {
31+
if let hir::StmtKind::Local(loc) = stmt.kind
32+
&& let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind
33+
&& matches!(ba.0, rustc_ast::ByRef::Yes)
34+
&& let Some(init) = loc.init
35+
&& let Some(var) = is_path_static_mut(*init)
36+
{
37+
handle_static_mut_ref(
38+
tcx,
39+
init.span,
40+
var,
41+
loc.span.edition().at_least_rust_2024(),
42+
matches!(ba.1, Mutability::Mut),
43+
stmt.hir_id,
44+
);
45+
}
46+
}
47+
48+
fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> {
49+
if let hir::ExprKind::Path(qpath) = expr.kind
50+
&& let hir::QPath::Resolved(_, path) = qpath
51+
&& let hir::def::Res::Def(def_kind, _) = path.res
52+
&& let hir::def::DefKind::Static(mt) = def_kind
53+
&& matches!(mt, Mutability::Mut)
54+
{
55+
return Some(qpath_to_string(&qpath));
56+
}
57+
None
58+
}
59+
60+
fn handle_static_mut_ref(
61+
tcx: TyCtxt<'_>,
62+
span: Span,
63+
var: String,
64+
e2024: bool,
65+
mutable: bool,
66+
hir_id: hir::HirId,
67+
) {
68+
if e2024 {
69+
let sugg = if mutable {
70+
errors::StaticMutRefSugg::Mut { span, var }
71+
} else {
72+
errors::StaticMutRefSugg::Shared { span, var }
73+
};
74+
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
75+
return;
76+
}
77+
78+
let (label, sugg, shared) = if mutable {
79+
(
80+
errors::RefOfMutStaticLabel::Mut { span },
81+
errors::RefOfMutStaticSugg::Mut { span, var },
82+
"mutable ",
83+
)
84+
} else {
85+
(
86+
errors::RefOfMutStaticLabel::Shared { span },
87+
errors::RefOfMutStaticSugg::Shared { span, var },
88+
"shared ",
89+
)
90+
};
91+
tcx.emit_spanned_lint(
92+
STATIC_MUT_REF,
93+
hir_id,
94+
span,
95+
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
96+
);
97+
}

compiler/rustc_hir_analysis/src/check/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ mod check;
6666
mod compare_impl_item;
6767
pub mod dropck;
6868
mod entry;
69+
mod errs;
6970
pub mod intrinsic;
7071
pub mod intrinsicck;
7172
mod region;

compiler/rustc_hir_analysis/src/check/region.rs

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use rustc_middle::ty::TyCtxt;
1818
use rustc_span::source_map;
1919
use rustc_span::Span;
2020

21+
use super::errs::{maybe_expr_static_mut, maybe_stmt_static_mut};
22+
2123
use std::mem;
2224

2325
#[derive(Debug, Copy, Clone)]
@@ -224,6 +226,8 @@ fn resolve_stmt<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, stmt: &'tcx h
224226
let stmt_id = stmt.hir_id.local_id;
225227
debug!("resolve_stmt(stmt.id={:?})", stmt_id);
226228

229+
maybe_stmt_static_mut(visitor.tcx, *stmt);
230+
227231
// Every statement will clean up the temporaries created during
228232
// execution of that statement. Therefore each statement has an
229233
// associated destruction scope that represents the scope of the
@@ -242,6 +246,8 @@ fn resolve_stmt<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, stmt: &'tcx h
242246
fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
243247
debug!("resolve_expr - pre-increment {} expr = {:?}", visitor.expr_and_pat_count, expr);
244248

249+
maybe_expr_static_mut(visitor.tcx, *expr);
250+
245251
let prev_cx = visitor.cx;
246252
visitor.enter_node_scope_with_dtor(expr.hir_id.local_id);
247253

compiler/rustc_hir_analysis/src/errors.rs

+91
Original file line numberDiff line numberDiff line change
@@ -1410,3 +1410,94 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
14101410
pub mut_key: &'a str,
14111411
pub ptr_ty: Ty<'a>,
14121412
}
1413+
1414+
#[derive(Diagnostic)]
1415+
#[diag(hir_analysis_static_mut_ref, code = "E0796")]
1416+
#[note]
1417+
pub struct StaticMutRef {
1418+
#[primary_span]
1419+
#[label]
1420+
pub span: Span,
1421+
#[subdiagnostic]
1422+
pub sugg: StaticMutRefSugg,
1423+
}
1424+
1425+
#[derive(Subdiagnostic)]
1426+
pub enum StaticMutRefSugg {
1427+
#[suggestion(
1428+
hir_analysis_suggestion,
1429+
style = "verbose",
1430+
code = "addr_of!({var})",
1431+
applicability = "maybe-incorrect"
1432+
)]
1433+
Shared {
1434+
#[primary_span]
1435+
span: Span,
1436+
var: String,
1437+
},
1438+
#[suggestion(
1439+
hir_analysis_suggestion_mut,
1440+
style = "verbose",
1441+
code = "addr_of_mut!({var})",
1442+
applicability = "maybe-incorrect"
1443+
)]
1444+
Mut {
1445+
#[primary_span]
1446+
span: Span,
1447+
var: String,
1448+
},
1449+
}
1450+
1451+
// STATIC_MUT_REF lint
1452+
#[derive(LintDiagnostic)]
1453+
#[diag(hir_analysis_static_mut_ref_lint)]
1454+
#[note]
1455+
pub struct RefOfMutStatic<'a> {
1456+
pub shared: &'a str,
1457+
#[note(hir_analysis_why_note)]
1458+
pub why_note: (),
1459+
#[subdiagnostic]
1460+
pub label: RefOfMutStaticLabel,
1461+
#[subdiagnostic]
1462+
pub sugg: RefOfMutStaticSugg,
1463+
}
1464+
1465+
#[derive(Subdiagnostic)]
1466+
pub enum RefOfMutStaticLabel {
1467+
#[label(hir_analysis_label)]
1468+
Shared {
1469+
#[primary_span]
1470+
span: Span,
1471+
},
1472+
#[label(hir_analysis_label_mut)]
1473+
Mut {
1474+
#[primary_span]
1475+
span: Span,
1476+
},
1477+
}
1478+
1479+
#[derive(Subdiagnostic)]
1480+
pub enum RefOfMutStaticSugg {
1481+
#[suggestion(
1482+
hir_analysis_suggestion,
1483+
style = "verbose",
1484+
code = "addr_of!({var})",
1485+
applicability = "maybe-incorrect"
1486+
)]
1487+
Shared {
1488+
#[primary_span]
1489+
span: Span,
1490+
var: String,
1491+
},
1492+
#[suggestion(
1493+
hir_analysis_suggestion_mut,
1494+
style = "verbose",
1495+
code = "addr_of_mut!({var})",
1496+
applicability = "maybe-incorrect"
1497+
)]
1498+
Mut {
1499+
#[primary_span]
1500+
span: Span,
1501+
var: String,
1502+
},
1503+
}

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ fn test_unstable_options_tracking_hash() {
659659
// tidy-alphabetical-start
660660
untracked!(assert_incr_state, Some(String::from("loaded")));
661661
untracked!(deduplicate_diagnostics, false);
662-
untracked!(dont_buffer_diagnostics, true);
663662
untracked!(dump_dep_graph, true);
664663
untracked!(dump_mir, Some(String::from("abc")));
665664
untracked!(dump_mir_dataflow, true);

0 commit comments

Comments
 (0)