From 8f025c5c79f4f3ee0ae729f1f8e4c78bb0495a34 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 | 18 +++++++++++++++--- 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 1160bd9ffa749..0d2ab1e208382 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -599,6 +599,10 @@ lint_mismatched_lifetime_syntaxes_suggestion_mixed = lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths = use `'_` for type paths +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 f622de7f84d93..68984b5613164 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -300,6 +300,9 @@ pub fn decorate_builtin_lint( lints::UnusedVisibility { span }.decorate_lint(diag) } BuiltinLintDiag::AttributeLint(kind) => decorate_attribute_lint(sess, tcx, &kind, 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 fde9546bd1c75..b45a8f5abd356 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2800,6 +2800,11 @@ pub(crate) enum DeprecatedWhereClauseLocationSugg { }, } +#[derive(LintDiagnostic)] +#[diag(lint_missing_crt_static)] +#[help] +pub(crate) struct MissingCrtStatic; + #[derive(LintDiagnostic)] #[diag(lint_single_use_lifetime)] pub(crate) struct SingleUseLifetime { diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 8c69cc089cafb..8aa62d73f06f2 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -68,6 +68,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, @@ -5315,3 +5316,16 @@ declare_lint! { report_in_deps: false, }; } + +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 7b41cfbb43ef0..7e5af4e037c98 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -748,6 +748,7 @@ pub enum BuiltinLintDiag { }, UnusedVisibility(Span), AttributeLint(AttributeLintKind), + MissingCrtStatic, } #[derive(Debug, HashStable_Generic)] diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 1a0ec600af47d..9d730359ba54e 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -6,6 +6,7 @@ use std::sync::atomic::AtomicBool; use std::{env, 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}; @@ -18,10 +19,12 @@ use rustc_errors::json::JsonEmitter; use rustc_errors::timings::TimingSectionHandler; use rustc_errors::translation::Translator; use rustc_errors::{ - Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort, - TerminalUrl, fallback_fluent_bundle, + DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, + FatalAbort, TerminalUrl, fallback_fluent_bundle, }; use rustc_hir::limit::Limit; +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; @@ -29,7 +32,7 @@ use rustc_span::source_map::{FilePathMapping, SourceMap}; use rustc_span::{RealFileName, Span, Symbol}; use rustc_target::asm::InlineAsmArch; use rustc_target::spec::{ - Arch, CodeModel, DebuginfoKind, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet, + Arch, CodeModel, DebuginfoKind, Env, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet, SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target, TargetTuple, TlsModel, apple, }; @@ -363,6 +366,15 @@ impl Session { // We can't check `#![crate_type = "proc-macro"]` here. false } else { + if self.target.env == Env::Musl && self.target.crt_static_default { + self.psess.opt_span_buffer_lint( + MUSL_MISSING_CRT_STATIC, + None, + ast::CRATE_NODE_ID, + DecorateDiagCompat::Builtin(BuiltinLintDiag::MissingCrtStatic), + ); + } + self.target.crt_static_default } }