Skip to content

Commit 8d32be2

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.
1 parent 77a4fb6 commit 8d32be2

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ declare_lint_pass! {
7474
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
7575
MISSING_ABI,
7676
MISSING_UNSAFE_ON_EXTERN,
77+
MUSL_MISSING_CRT_STATIC,
7778
MUST_NOT_SUSPEND,
7879
NAMED_ARGUMENTS_USED_POSITIONALLY,
7980
NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
@@ -5593,3 +5594,17 @@ declare_lint! {
55935594
report_in_deps: false,
55945595
};
55955596
}
5597+
5598+
declare_lint! {
5599+
/// The `musl_missing_crt_static` lint detects when code is compiled for a target using musl libc
5600+
/// without explicitly specifying `+crt-static` or `-crt-static`.
5601+
///
5602+
/// ### Explanation
5603+
///
5604+
/// The musl targets will change to be dynamically linked by default in the future, to avoid a
5605+
/// sudden change in behavior, the desired linkage should be specified in the interim period.
5606+
pub MUSL_MISSING_CRT_STATIC,
5607+
Warn,
5608+
"on musl targets, the default for `crt-static` will change; explicitly set `+crt-static` or `-crt-static`",
5609+
// TOOD: Does this need a FutureIncompatibleInfo entry?
5610+
}

compiler/rustc_session/src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,8 @@ pub(crate) struct ThinLtoNotSupportedByBackend;
692692
#[derive(Diagnostic)]
693693
#[diag("`-Zpacked-stack` is only supported on s390x")]
694694
pub(crate) struct UnsupportedPackedStack;
695+
696+
#[derive(Diagnostic)]
697+
#[diag("on musl targets, the implicit default for `crt-static` is going to change")]
698+
#[help("explicitly pass `-C target-feature=+crt-static` or `-C target-feature=-crt-static`")]
699+
pub struct MuslMissingCrtStatic;

compiler/rustc_session/src/session.rs

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

8+
use rustc_ast::ast;
89
use rustc_data_structures::flock;
910
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
1011
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
@@ -17,19 +18,20 @@ use rustc_errors::emitter::{DynEmitter, HumanReadableErrorType, OutputTheme, std
1718
use rustc_errors::json::JsonEmitter;
1819
use rustc_errors::timings::TimingSectionHandler;
1920
use rustc_errors::{
20-
Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort,
21-
TerminalUrl,
21+
DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed,
22+
FatalAbort, TerminalUrl,
2223
};
2324
use rustc_feature::UnstableFeatures;
2425
use rustc_hir::limit::Limit;
26+
use rustc_lint_defs::builtin::MUSL_MISSING_CRT_STATIC;
2527
use rustc_macros::StableHash;
2628
pub use rustc_span::def_id::StableCrateId;
2729
use rustc_span::edition::Edition;
2830
use rustc_span::source_map::{FilePathMapping, SourceMap};
2931
use rustc_span::{RealFileName, Span, Symbol};
3032
use rustc_target::asm::InlineAsmArch;
3133
use rustc_target::spec::{
32-
Arch, CodeModel, DebuginfoKind, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
34+
Arch, CodeModel, DebuginfoKind, Env, Os, PanicStrategy, RelocModel, RelroLevel, SanitizerSet,
3335
SmallDataThresholdSupport, SplitDebuginfo, StackProtector, SymbolVisibility, Target,
3436
TargetTuple, TlsModel, apple,
3537
};
@@ -41,6 +43,7 @@ use crate::config::{
4143
FunctionReturn, Input, InstrumentCoverage, OptLevel, OutFileName, OutputType,
4244
SwitchWithOptPath,
4345
};
46+
use crate::errors::MuslMissingCrtStatic;
4447
use crate::filesearch::FileSearch;
4548
use crate::lint::LintId;
4649
use crate::parse::ParseSess;
@@ -396,6 +399,15 @@ impl Session {
396399
// We can't check `#![crate_type = "proc-macro"]` here.
397400
false
398401
} else {
402+
if self.target.env == Env::Musl && self.target.crt_static_default {
403+
self.psess.opt_span_buffer_lint(
404+
MUSL_MISSING_CRT_STATIC,
405+
None,
406+
ast::CRATE_NODE_ID,
407+
DecorateDiagCompat(Box::new(|dcx, _, _| dcx.create_warn(MuslMissingCrtStatic))),
408+
);
409+
}
410+
399411
self.target.crt_static_default
400412
}
401413
}

0 commit comments

Comments
 (0)