Skip to content

Commit cb45a36

Browse files
committed
Add support for sysroot --extern flag
This adds `sysroot` to the set of extern flags: `--extern sysroot:core=/path/to/core/libcore.rlib`. The primary effect of this flag is to suppress `unused-crate-dependencies` warnings relating to the crate. It's used for build environments where it's desirable for the build system to explicitly specify dependencies on sysroot crates rather than letting rustc find them implicitly. In such cases, these dependencies are likely to be added unconditionally, so there's no way that a user could act on an unused dependency warning.
1 parent e7575f9 commit cb45a36

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ where
6666
location: ExternLocation::ExactPaths(locations),
6767
is_private_dep: false,
6868
add_prelude: true,
69+
sysroot_dep: false,
6970
}
7071
}
7172

compiler/rustc_metadata/src/creader.rs

+4
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,10 @@ impl<'a> CrateLoader<'a> {
908908
// Don't worry about pathless `--extern foo` sysroot references
909909
continue;
910910
}
911+
if entry.sysroot_dep {
912+
// If it's an explicit sysroot dependency, let it slide
913+
continue;
914+
}
911915
let name_interned = Symbol::intern(name);
912916
if self.used_extern_options.contains(&name_interned) {
913917
continue;

compiler/rustc_session/src/config.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ pub struct ExternEntry {
477477
/// This can be disabled with the `noprelude` option like
478478
/// `--extern noprelude:name`.
479479
pub add_prelude: bool,
480+
/// The extern entry is an explicitly listed sysroot crate.
481+
///
482+
/// `--extern sysroot:std=/path/to/lib/libstd.rlib`
483+
/// This is useful for when the build system is using explicit sysroot
484+
/// dependencies rather than allowing rustc to find them implicitly. This is
485+
/// primarily used to suppress `unused-crate-dependencies` warnings, since
486+
/// these dependencies are added unconditionally.
487+
pub sysroot_dep: bool,
480488
}
481489

482490
#[derive(Clone, Debug)]
@@ -536,7 +544,7 @@ impl Externs {
536544

537545
impl ExternEntry {
538546
fn new(location: ExternLocation) -> ExternEntry {
539-
ExternEntry { location, is_private_dep: false, add_prelude: false }
547+
ExternEntry { location, is_private_dep: false, add_prelude: false, sysroot_dep: false }
540548
}
541549

542550
pub fn files(&self) -> Option<impl Iterator<Item = &CanonicalizedPath>> {
@@ -2186,6 +2194,7 @@ pub fn parse_externs(
21862194

21872195
let mut is_private_dep = false;
21882196
let mut add_prelude = true;
2197+
let mut sysroot_dep = false;
21892198
if let Some(opts) = options {
21902199
if !is_unstable_enabled {
21912200
early_error(
@@ -2207,6 +2216,7 @@ pub fn parse_externs(
22072216
);
22082217
}
22092218
}
2219+
"sysroot" => sysroot_dep = true,
22102220
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
22112221
}
22122222
}
@@ -2215,6 +2225,8 @@ pub fn parse_externs(
22152225
// Crates start out being not private, and go to being private `priv`
22162226
// is specified.
22172227
entry.is_private_dep |= is_private_dep;
2228+
// likewise `sysroot`
2229+
entry.sysroot_dep |= sysroot_dep;
22182230
// If any flag is missing `noprelude`, then add to the prelude.
22192231
entry.add_prelude |= add_prelude;
22202232
}

src/test/ui/extern-flag/no-sysroot.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// aux-crate:somedep=somedep.rs
2+
// compile-flags: -Zunstable-options -Dunused-crate-dependencies
3+
// edition:2018
4+
5+
fn main() { //~ ERROR external crate `somedep` unused in `no_sysroot`
6+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: external crate `somedep` unused in `no_sysroot`: remove the dependency or add `use somedep as _;`
2+
--> $DIR/no-sysroot.rs:5:1
3+
|
4+
LL | fn main() {
5+
| ^
6+
|
7+
= note: requested on the command line with `-D unused-crate-dependencies`
8+
= help: remove unnecessary dependency `somedep`
9+
10+
error: aborting due to previous error
11+

src/test/ui/extern-flag/sysroot.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
// aux-crate:sysroot:somedep=somedep.rs
3+
// compile-flags: -Zunstable-options -Dunused-crate-dependencies
4+
// edition:2018
5+
6+
fn main() {
7+
}

0 commit comments

Comments
 (0)