Skip to content

Commit 8f091e4

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 9f54abe commit 8f091e4

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,10 @@ lint_mismatched_lifetime_syntaxes_suggestion_mixed =
599599
lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths =
600600
use `'_` for type paths
601601
602+
lint_missing_crt_static =
603+
on musl targets, the implicit default for `crt-static` is going to change
604+
.help = explicitly pass `-C target-feature=+crt-static` or `-C target-feature=-crt-static`
605+
602606
lint_mixed_script_confusables =
603607
the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables
604608
.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
@@ -300,6 +300,9 @@ pub fn decorate_builtin_lint(
300300
lints::UnusedVisibility { span }.decorate_lint(diag)
301301
}
302302
BuiltinLintDiag::AttributeLint(kind) => decorate_attribute_lint(sess, tcx, &kind, diag),
303+
BuiltinLintDiag::MissingCrtStatic => {
304+
lints::MissingCrtStatic.decorate_lint(diag);
305+
}
303306
}
304307
}
305308

compiler/rustc_lint/src/lints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,18 @@ pub(crate) enum DeprecatedWhereClauseLocationSugg {
28002800
},
28012801
}
28022802

2803+
#[derive(LintDiagnostic)]
2804+
#[diag(lint_missing_crt_static)]
2805+
#[help]
2806+
pub(crate) struct MissingCrtStatic;
2807+
2808+
#[derive(LintDiagnostic)]
2809+
#[diag(lint_missing_unsafe_on_extern)]
2810+
pub(crate) struct MissingUnsafeOnExtern {
2811+
#[suggestion(code = "unsafe ", applicability = "machine-applicable")]
2812+
pub suggestion: Span,
2813+
}
2814+
28032815
#[derive(LintDiagnostic)]
28042816
#[diag(lint_single_use_lifetime)]
28052817
pub(crate) struct SingleUseLifetime {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ declare_lint_pass! {
6868
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
6969
MISSING_ABI,
7070
MISSING_UNSAFE_ON_EXTERN,
71+
MUSL_MISSING_CRT_STATIC,
7172
MUST_NOT_SUSPEND,
7273
NAMED_ARGUMENTS_USED_POSITIONALLY,
7374
NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
@@ -5315,3 +5316,16 @@ declare_lint! {
53155316
report_in_deps: false,
53165317
};
53175318
}
5319+
5320+
declare_lint! {
5321+
/// The `musl_missing_crt_static` lint detects when code is compiled for a target using musl libc
5322+
/// without explicitly specifying `+crt-static` or `-crt-static`.
5323+
///
5324+
/// ### Explanation
5325+
///
5326+
/// The musl targets will change to be dynamically linked by default in the future, to avoid a
5327+
/// sudden change in behavior, the desired linkage should be specified in the interim period.
5328+
pub MUSL_MISSING_CRT_STATIC,
5329+
Warn,
5330+
"on musl targets, the default for `crt-static` will change; explicitly set `+crt-static` or `-crt-static`",
5331+
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ pub enum BuiltinLintDiag {
748748
},
749749
UnusedVisibility(Span),
750750
AttributeLint(AttributeLintKind),
751+
MissingCrtStatic,
751752
}
752753

753754
#[derive(Debug, HashStable_Generic)]

compiler/rustc_session/src/session.rs

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

88
use rand::{RngCore, rng};
9+
use rustc_ast::ast;
910
use rustc_data_structures::base_n::{CASE_INSENSITIVE, ToBaseN};
1011
use rustc_data_structures::flock;
1112
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
@@ -18,18 +19,20 @@ use rustc_errors::json::JsonEmitter;
1819
use rustc_errors::timings::TimingSectionHandler;
1920
use rustc_errors::translation::Translator;
2021
use rustc_errors::{
21-
Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort,
22-
TerminalUrl, fallback_fluent_bundle,
22+
DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed,
23+
FatalAbort, TerminalUrl, fallback_fluent_bundle,
2324
};
2425
use rustc_hir::limit::Limit;
26+
use rustc_lint_defs::BuiltinLintDiag;
27+
use rustc_lint_defs::builtin::MUSL_MISSING_CRT_STATIC;
2528
use rustc_macros::HashStable_Generic;
2629
pub use rustc_span::def_id::StableCrateId;
2730
use rustc_span::edition::Edition;
2831
use rustc_span::source_map::{FilePathMapping, SourceMap};
2932
use rustc_span::{RealFileName, Span, Symbol};
3033
use rustc_target::asm::InlineAsmArch;
3134
use rustc_target::spec::{
32-
Arch, CodeModel, DebuginfoKind, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
35+
Arch, CodeModel, DebuginfoKind, Env, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
3336
SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target,
3437
TargetTuple, TlsModel, apple,
3538
};
@@ -363,6 +366,15 @@ impl Session {
363366
// We can't check `#![crate_type = "proc-macro"]` here.
364367
false
365368
} else {
369+
if self.target.env == Env::Musl && self.target.crt_static_default {
370+
self.psess.opt_span_buffer_lint(
371+
MUSL_MISSING_CRT_STATIC,
372+
None,
373+
ast::CRATE_NODE_ID,
374+
DecorateDiagCompat::Builtin(BuiltinLintDiag::MissingCrtStatic),
375+
);
376+
}
377+
366378
self.target.crt_static_default
367379
}
368380
}

0 commit comments

Comments
 (0)