Skip to content

Commit 1627c60

Browse files
committed
Auto merge of #119558 - matthiaskrgr:rollup-ghm80lp, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #117449 (Avoid silencing relevant follow-up errors) - #117556 (Disallow reference to `static mut` and adding `static_mut_ref` lint) - #118521 (Enable address sanitizer for MSVC targets using INFERASANLIBS linker flag) - #118704 (Promote `riscv32{im|imafc}` targets to tier 2) - #119026 (std::net::bind using -1 for openbsd which in turn sets it to somaxconn.) - #119151 (Hide foreign `#[doc(hidden)]` paths in import suggestions) - #119195 (Make named_asm_labels lint not trigger on unicode and trigger on format args) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 139fb22 + 4c9143a commit 1627c60

File tree

280 files changed

+4500
-789
lines changed

Some content is hidden

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

280 files changed

+4500
-789
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3877,6 +3877,7 @@ dependencies = [
38773877
"rustc_feature",
38783878
"rustc_fluent_macro",
38793879
"rustc_hir",
3880+
"rustc_hir_pretty",
38803881
"rustc_index",
38813882
"rustc_infer",
38823883
"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 {

compiler/rustc_codegen_ssa/src/back/link.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -1186,15 +1186,22 @@ mod win {
11861186
}
11871187
}
11881188

1189-
fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) {
1190-
// On macOS the runtimes are distributed as dylibs which should be linked to
1191-
// both executables and dynamic shared objects. Everywhere else the runtimes
1192-
// are currently distributed as static libraries which should be linked to
1193-
// executables only.
1189+
fn add_sanitizer_libraries(
1190+
sess: &Session,
1191+
flavor: LinkerFlavor,
1192+
crate_type: CrateType,
1193+
linker: &mut dyn Linker,
1194+
) {
1195+
// On macOS and Windows using MSVC the runtimes are distributed as dylibs
1196+
// which should be linked to both executables and dynamic libraries.
1197+
// Everywhere else the runtimes are currently distributed as static
1198+
// libraries which should be linked to executables only.
11941199
let needs_runtime = !sess.target.is_like_android
11951200
&& match crate_type {
11961201
CrateType::Executable => true,
1197-
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => sess.target.is_like_osx,
1202+
CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => {
1203+
sess.target.is_like_osx || sess.target.is_like_msvc
1204+
}
11981205
CrateType::Rlib | CrateType::Staticlib => false,
11991206
};
12001207

@@ -1204,26 +1211,31 @@ fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut d
12041211

12051212
let sanitizer = sess.opts.unstable_opts.sanitizer;
12061213
if sanitizer.contains(SanitizerSet::ADDRESS) {
1207-
link_sanitizer_runtime(sess, linker, "asan");
1214+
link_sanitizer_runtime(sess, flavor, linker, "asan");
12081215
}
12091216
if sanitizer.contains(SanitizerSet::LEAK) {
1210-
link_sanitizer_runtime(sess, linker, "lsan");
1217+
link_sanitizer_runtime(sess, flavor, linker, "lsan");
12111218
}
12121219
if sanitizer.contains(SanitizerSet::MEMORY) {
1213-
link_sanitizer_runtime(sess, linker, "msan");
1220+
link_sanitizer_runtime(sess, flavor, linker, "msan");
12141221
}
12151222
if sanitizer.contains(SanitizerSet::THREAD) {
1216-
link_sanitizer_runtime(sess, linker, "tsan");
1223+
link_sanitizer_runtime(sess, flavor, linker, "tsan");
12171224
}
12181225
if sanitizer.contains(SanitizerSet::HWADDRESS) {
1219-
link_sanitizer_runtime(sess, linker, "hwasan");
1226+
link_sanitizer_runtime(sess, flavor, linker, "hwasan");
12201227
}
12211228
if sanitizer.contains(SanitizerSet::SAFESTACK) {
1222-
link_sanitizer_runtime(sess, linker, "safestack");
1229+
link_sanitizer_runtime(sess, flavor, linker, "safestack");
12231230
}
12241231
}
12251232

1226-
fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
1233+
fn link_sanitizer_runtime(
1234+
sess: &Session,
1235+
flavor: LinkerFlavor,
1236+
linker: &mut dyn Linker,
1237+
name: &str,
1238+
) {
12271239
fn find_sanitizer_runtime(sess: &Session, filename: &str) -> PathBuf {
12281240
let session_tlib =
12291241
filesearch::make_target_lib_path(&sess.sysroot, sess.opts.target_triple.triple());
@@ -1254,6 +1266,10 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
12541266
let rpath = path.to_str().expect("non-utf8 component in path");
12551267
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
12561268
linker.link_dylib(&filename, false, true);
1269+
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
1270+
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
1271+
// compatible ASAN library.
1272+
linker.arg("/INFERASANLIBS");
12571273
} else {
12581274
let filename = format!("librustc{channel}_rt.{name}.a");
12591275
let path = find_sanitizer_runtime(sess, &filename).join(&filename);
@@ -2076,7 +2092,7 @@ fn linker_with_args<'a>(
20762092
);
20772093

20782094
// Sanitizer libraries.
2079-
add_sanitizer_libraries(sess, crate_type, cmd);
2095+
add_sanitizer_libraries(sess, flavor, crate_type, cmd);
20802096

20812097
// Object code from the current crate.
20822098
// Take careful note of the ordering of the arguments we pass to the linker

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_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)]
@@ -214,6 +216,8 @@ fn resolve_stmt<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, stmt: &'tcx h
214216
let stmt_id = stmt.hir_id.local_id;
215217
debug!("resolve_stmt(stmt.id={:?})", stmt_id);
216218

219+
maybe_stmt_static_mut(visitor.tcx, *stmt);
220+
217221
// Every statement will clean up the temporaries created during
218222
// execution of that statement. Therefore each statement has an
219223
// associated destruction scope that represents the scope of the
@@ -232,6 +236,8 @@ fn resolve_stmt<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, stmt: &'tcx h
232236
fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
233237
debug!("resolve_expr - pre-increment {} expr = {:?}", visitor.expr_and_pat_count, expr);
234238

239+
maybe_expr_static_mut(visitor.tcx, *expr);
240+
235241
let prev_cx = visitor.cx;
236242
visitor.enter_node_scope_with_dtor(expr.hir_id.local_id);
237243

0 commit comments

Comments
 (0)