Skip to content

Warn when relying on default musl target static linkage behaviour #144513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
5 changes: 5 additions & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Copy link
Member

@Noratrieb Noratrieb Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lint will be seen by a lot of people, many of which may not be familiar with the details of compiler flags. I think this lint documents here should spend more effort explaining exactly how to pass the flag (via RUSTFLAGS) and what exactly it means (what is libc, what are the differences between static and dynamic linkage, why is this change being made exactly etc) in an accessible way. I can write down such a text if you want to, or you can do it yourself.

The lint output should then contain a link to this documentation here in the rustc book.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I think there are a lot of projects that have just copied the --target from somewhere because they heard that this makes a binary that works everywhere, without being familiar with the details of Rust compilation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively all these details can be in the blog post and we link to the blog post.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really mind where the details are, I think both ways are fine. However, before I write such a blog post, I'd like to know for how long we want to keep the lint around before we change the behavior, since I'd have to mention that in the blog post and it needs to be decided on either way, so better start that process now than later

/// 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`",
}
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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
}
}
Expand Down
Loading