Skip to content

Commit a42d0d3

Browse files
committed
Default to --cap-lints=warn
1 parent 089b040 commit a42d0d3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,20 @@ impl SysrootBuilder {
143143
}
144144

145145
/// Appends the given flag.
146+
///
147+
/// If no `--cap-lints` argument is configured, we will add `--cap-lints=warn`.
148+
/// This emulates the usual behavior of Cargo: Lints are normally capped when building
149+
/// dependencies, except that they are not capped when building path dependencies, except that
150+
/// path dependencies are still capped if they are part of `-Zbuild-std`.
146151
pub fn rustflag(mut self, rustflag: impl Into<OsString>) -> Self {
147152
self.rustflags.push(rustflag.into());
148153
self
149154
}
150155

151156
/// Appends the given flags.
157+
///
158+
/// If no `--cap-lints` argument is configured, we will add `--cap-lints=warn`. See
159+
/// [`SysrootBuilder::rustflag`] for more explanation.
152160
pub fn rustflags(mut self, rustflags: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
153161
self.rustflags.extend(rustflags.into_iter().map(Into::into));
154162
self
@@ -326,6 +334,25 @@ path = "lib.rs"
326334
None => rustc_version::version_meta()?,
327335
};
328336

337+
// The whole point of this crate is to build the standard library in nonstandard
338+
// configurations, which may trip lints due to untested combinations of cfgs.
339+
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
340+
// of course are not using:
341+
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
342+
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
343+
// All the standard library crates are path dependencies, and they also sometimes pull in
344+
// separately-maintained crates like backtrace by treating their crate roots as module
345+
// roots. If we do not cap lints, we can get lint failures outside core or std.
346+
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
347+
// output of --print=file-names for crate-types that the target does not support.
348+
if !self.rustflags.iter().any(|flag| {
349+
// FIXME: OsStr::as_encoded_bytes is cleaner here
350+
flag.to_str()
351+
.map_or(false, |f| f.starts_with("--cap-lints"))
352+
}) {
353+
self.rustflags.push("--cap-lints=warn".into());
354+
}
355+
329356
// Check if we even need to do anything.
330357
let cur_hash = self.sysroot_compute_hash(src_dir, &rustc_version);
331358
if self.sysroot_read_hash() == Some(cur_hash) {

0 commit comments

Comments
 (0)