@@ -143,12 +143,20 @@ impl SysrootBuilder {
143
143
}
144
144
145
145
/// 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`.
146
151
pub fn rustflag ( mut self , rustflag : impl Into < OsString > ) -> Self {
147
152
self . rustflags . push ( rustflag. into ( ) ) ;
148
153
self
149
154
}
150
155
151
156
/// 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.
152
160
pub fn rustflags ( mut self , rustflags : impl IntoIterator < Item = impl Into < OsString > > ) -> Self {
153
161
self . rustflags . extend ( rustflags. into_iter ( ) . map ( Into :: into) ) ;
154
162
self
@@ -326,6 +334,25 @@ path = "lib.rs"
326
334
None => rustc_version:: version_meta ( ) ?,
327
335
} ;
328
336
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
+
329
356
// Check if we even need to do anything.
330
357
let cur_hash = self . sysroot_compute_hash ( src_dir, & rustc_version) ;
331
358
if self . sysroot_read_hash ( ) == Some ( cur_hash) {
0 commit comments