From 6470b66c542af335e02fcafa00d43c621bca1a60 Mon Sep 17 00:00:00 2001 From: Jens Reidel Date: Thu, 24 Jul 2025 16:32:26 +0200 Subject: [PATCH] Warn when relying on default musl target static linkage behaviour This introduces a new lint, "musl_missing_crt_static" that warns when compiling code for a musl target that defaults to static linkage without explicitly specifying -Ctarget-feature=+crt-static. The targets will be changed to link dynamically by default in the future, after which this lint can be removed again. Signed-off-by: Jens Reidel --- compiler/rustc_lint/messages.ftl | 4 ++++ compiler/rustc_lint/src/early/diagnostics.rs | 3 +++ compiler/rustc_lint/src/lints.rs | 5 +++++ compiler/rustc_lint_defs/src/builtin.rs | 14 ++++++++++++++ compiler/rustc_lint_defs/src/lib.rs | 1 + compiler/rustc_session/src/session.rs | 12 ++++++++++++ 6 files changed, 39 insertions(+) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 69fe7fe83ffda..4d2bb0a806e23 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -557,6 +557,10 @@ lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths = lint_missing_unsafe_on_extern = extern blocks should be unsafe .suggestion = needs `unsafe` before the extern keyword +lint_missing_crt_static = + on musl targets, the implicit default for `crt-static` is going to change + .help = explicitly pass `-C target-feature=+crt-static` or `-C target-feature=-crt-static` + lint_mixed_script_confusables = the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables .includes_note = the usage includes {$includes} diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index f0fbf5bc81e9b..9f5d374617772 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -468,5 +468,8 @@ pub fn decorate_builtin_lint( BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => { lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag) } + BuiltinLintDiag::MissingCrtStatic => { + lints::MissingCrtStatic.decorate_lint(diag); + } } } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index fd8d0f832aa47..9fdad01437b18 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2977,6 +2977,11 @@ pub(crate) enum DeprecatedWhereClauseLocationSugg { }, } +#[derive(LintDiagnostic)] +#[diag(lint_missing_crt_static)] +#[help] +pub(crate) struct MissingCrtStatic; + #[derive(LintDiagnostic)] #[diag(lint_missing_unsafe_on_extern)] pub(crate) struct MissingUnsafeOnExtern { diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index b1edb5c304425..f55f5e59d9389 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -69,6 +69,7 @@ declare_lint_pass! { MISPLACED_DIAGNOSTIC_ATTRIBUTES, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN, + MUSL_MISSING_CRT_STATIC, MUST_NOT_SUSPEND, NAMED_ARGUMENTS_USED_POSITIONALLY, NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE, @@ -5099,3 +5100,16 @@ declare_lint! { report_in_deps: true, }; } + +declare_lint! { + /// The `musl_missing_crt_static` lint detects when code is compiled for a target using musl libc + /// without explicitly specifying `+crt-static` or `-crt-static`. + /// + /// ### Explanation + /// + /// The musl targets will change to be dynamically linked by default in the future, to avoid a + /// sudden change in behavior, the desired linkage should be specified in the interim period. + pub MUSL_MISSING_CRT_STATIC, + Warn, + "on musl targets, the default for `crt-static` will change; explicitly set `+crt-static` or `-crt-static`", +} diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index fe068d96b7424..47d2782af0e73 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -807,6 +807,7 @@ pub enum BuiltinLintDiag { cfg_name: Symbol, controlled_by: &'static str, }, + MissingCrtStatic, } /// Lints that are buffered up early on in the `Session` before the diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 85bd8340c3cce..8aabbe2b53ed2 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -7,6 +7,7 @@ use std::sync::atomic::AtomicBool; use std::{env, fmt, io}; use rand::{RngCore, rng}; +use rustc_ast::ast; use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN}; use rustc_data_structures::flock; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; @@ -24,6 +25,8 @@ use rustc_errors::{ Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort, TerminalUrl, fallback_fluent_bundle, }; +use rustc_lint_defs::BuiltinLintDiag; +use rustc_lint_defs::builtin::MUSL_MISSING_CRT_STATIC; use rustc_macros::HashStable_Generic; pub use rustc_span::def_id::StableCrateId; use rustc_span::edition::Edition; @@ -423,6 +426,15 @@ impl Session { // We can't check `#![crate_type = "proc-macro"]` here. false } else { + if self.target.env == "musl" && self.target.crt_static_default { + self.psess.opt_span_buffer_lint( + MUSL_MISSING_CRT_STATIC, + None, + ast::CRATE_NODE_ID, + BuiltinLintDiag::MissingCrtStatic, + ); + } + self.target.crt_static_default } }