Skip to content

Commit 61b9f03

Browse files
committed
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 <adrian@travitia.xyz>
1 parent 8708f3c commit 61b9f03

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths =
557557
lint_missing_unsafe_on_extern = extern blocks should be unsafe
558558
.suggestion = needs `unsafe` before the extern keyword
559559
560+
lint_missing_crt_static =
561+
on musl targets, the implicit default for `crt-static` is going to change
562+
.suggestion = explicitly pass `-C target-feature=+crt-static` or `-C target-feature=-crt-static`
563+
560564
lint_mixed_script_confusables =
561565
the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables
562566
.includes_note = the usage includes {$includes}

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ pub fn decorate_builtin_lint(
467467
}
468468
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
469469
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
470+
},
471+
BuiltinLintDiag::MissingCrtStatic => {
472+
lints::MissingCrtStatic.decorate_lint(diag);
470473
}
471474
}
472475
}

compiler/rustc_lint/src/lints.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,10 @@ pub(crate) enum DeprecatedWhereClauseLocationSugg {
29772977
},
29782978
}
29792979

2980+
#[derive(LintDiagnostic)]
2981+
#[diag(lint_missing_crt_static)]
2982+
pub(crate) struct MissingCrtStatic;
2983+
29802984
#[derive(LintDiagnostic)]
29812985
#[diag(lint_missing_unsafe_on_extern)]
29822986
pub(crate) struct MissingUnsafeOnExtern {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ declare_lint_pass! {
6969
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
7070
MISSING_ABI,
7171
MISSING_UNSAFE_ON_EXTERN,
72+
MUSL_MISSING_CRT_STATIC,
7273
MUST_NOT_SUSPEND,
7374
NAMED_ARGUMENTS_USED_POSITIONALLY,
7475
NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
@@ -5099,3 +5100,16 @@ declare_lint! {
50995100
report_in_deps: true,
51005101
};
51015102
}
5103+
5104+
declare_lint! {
5105+
/// The `musl_missing_crt_static` lint detects when code is compiled for a target using musl libc
5106+
/// without explicitly specifying `+crt-static` or `-crt-static`.
5107+
///
5108+
/// ### Explanation
5109+
///
5110+
/// The musl targets will change to be dynamically linked by default in the future, to avoid a
5111+
/// sudden change in behavior, the desired linkage should be specified in the interim period.
5112+
pub MUSL_MISSING_CRT_STATIC,
5113+
Warn,
5114+
"on musl targets, the default for `crt-static` will change; explicitly set `+crt-static` or `-crt-static`",
5115+
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ pub enum BuiltinLintDiag {
807807
cfg_name: Symbol,
808808
controlled_by: &'static str,
809809
},
810+
MissingCrtStatic,
810811
}
811812

812813
/// Lints that are buffered up early on in the `Session` before the

compiler/rustc_session/src/session.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::atomic::AtomicBool;
77
use std::{env, fmt, io};
88

99
use rand::{RngCore, rng};
10+
use rustc_ast::ast;
1011
use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN};
1112
use rustc_data_structures::flock;
1213
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
@@ -24,6 +25,8 @@ use rustc_errors::{
2425
Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort,
2526
TerminalUrl, fallback_fluent_bundle,
2627
};
28+
use rustc_lint_defs::BuiltinLintDiag;
29+
use rustc_lint_defs::builtin::MUSL_MISSING_CRT_STATIC;
2730
use rustc_macros::HashStable_Generic;
2831
pub use rustc_span::def_id::StableCrateId;
2932
use rustc_span::edition::Edition;
@@ -423,6 +426,15 @@ impl Session {
423426
// We can't check `#![crate_type = "proc-macro"]` here.
424427
false
425428
} else {
429+
if self.target.env == "musl" && self.target.crt_static_default {
430+
self.psess.opt_span_buffer_lint(
431+
MUSL_MISSING_CRT_STATIC,
432+
None,
433+
ast::CRATE_NODE_ID,
434+
BuiltinLintDiag::MissingCrtStatic,
435+
);
436+
}
437+
426438
self.target.crt_static_default
427439
}
428440
}

0 commit comments

Comments
 (0)