From cb45a366da664f69f7d5410674d9de4c088e1d8b Mon Sep 17 00:00:00 2001 From: Jeremy Fitzhardinge Date: Thu, 14 Apr 2022 15:09:00 -0700 Subject: [PATCH] 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. --- compiler/rustc_interface/src/tests.rs | 1 + compiler/rustc_metadata/src/creader.rs | 4 ++++ compiler/rustc_session/src/config.rs | 14 +++++++++++++- src/test/ui/extern-flag/no-sysroot.rs | 6 ++++++ src/test/ui/extern-flag/no-sysroot.stderr | 11 +++++++++++ src/test/ui/extern-flag/sysroot.rs | 7 +++++++ 6 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/extern-flag/no-sysroot.rs create mode 100644 src/test/ui/extern-flag/no-sysroot.stderr create mode 100644 src/test/ui/extern-flag/sysroot.rs diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index fe75ee8b37b8d..4c54d13dbce8c 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -66,6 +66,7 @@ where location: ExternLocation::ExactPaths(locations), is_private_dep: false, add_prelude: true, + sysroot_dep: false, } } diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index a9e3b55aeeedf..1e039b40b8762 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -908,6 +908,10 @@ impl<'a> CrateLoader<'a> { // Don't worry about pathless `--extern foo` sysroot references continue; } + if entry.sysroot_dep { + // If it's an explicit sysroot dependency, let it slide + continue; + } let name_interned = Symbol::intern(name); if self.used_extern_options.contains(&name_interned) { continue; diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 5a447aa623734..2b0a6bc10d791 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -477,6 +477,14 @@ pub struct ExternEntry { /// This can be disabled with the `noprelude` option like /// `--extern noprelude:name`. pub add_prelude: bool, + /// The extern entry is an explicitly listed sysroot crate. + /// + /// `--extern sysroot:std=/path/to/lib/libstd.rlib` + /// This is useful for when the build system is using explicit sysroot + /// dependencies rather than allowing rustc to find them implicitly. This is + /// primarily used to suppress `unused-crate-dependencies` warnings, since + /// these dependencies are added unconditionally. + pub sysroot_dep: bool, } #[derive(Clone, Debug)] @@ -536,7 +544,7 @@ impl Externs { impl ExternEntry { fn new(location: ExternLocation) -> ExternEntry { - ExternEntry { location, is_private_dep: false, add_prelude: false } + ExternEntry { location, is_private_dep: false, add_prelude: false, sysroot_dep: false } } pub fn files(&self) -> Option> { @@ -2186,6 +2194,7 @@ pub fn parse_externs( let mut is_private_dep = false; let mut add_prelude = true; + let mut sysroot_dep = false; if let Some(opts) = options { if !is_unstable_enabled { early_error( @@ -2207,6 +2216,7 @@ pub fn parse_externs( ); } } + "sysroot" => sysroot_dep = true, _ => early_error(error_format, &format!("unknown --extern option `{opt}`")), } } @@ -2215,6 +2225,8 @@ pub fn parse_externs( // Crates start out being not private, and go to being private `priv` // is specified. entry.is_private_dep |= is_private_dep; + // likewise `sysroot` + entry.sysroot_dep |= sysroot_dep; // If any flag is missing `noprelude`, then add to the prelude. entry.add_prelude |= add_prelude; } diff --git a/src/test/ui/extern-flag/no-sysroot.rs b/src/test/ui/extern-flag/no-sysroot.rs new file mode 100644 index 0000000000000..871599bdf4832 --- /dev/null +++ b/src/test/ui/extern-flag/no-sysroot.rs @@ -0,0 +1,6 @@ +// aux-crate:somedep=somedep.rs +// compile-flags: -Zunstable-options -Dunused-crate-dependencies +// edition:2018 + +fn main() { //~ ERROR external crate `somedep` unused in `no_sysroot` +} diff --git a/src/test/ui/extern-flag/no-sysroot.stderr b/src/test/ui/extern-flag/no-sysroot.stderr new file mode 100644 index 0000000000000..f0ac451a4dd2c --- /dev/null +++ b/src/test/ui/extern-flag/no-sysroot.stderr @@ -0,0 +1,11 @@ +error: external crate `somedep` unused in `no_sysroot`: remove the dependency or add `use somedep as _;` + --> $DIR/no-sysroot.rs:5:1 + | +LL | fn main() { + | ^ + | + = note: requested on the command line with `-D unused-crate-dependencies` + = help: remove unnecessary dependency `somedep` + +error: aborting due to previous error + diff --git a/src/test/ui/extern-flag/sysroot.rs b/src/test/ui/extern-flag/sysroot.rs new file mode 100644 index 0000000000000..a068700b483dc --- /dev/null +++ b/src/test/ui/extern-flag/sysroot.rs @@ -0,0 +1,7 @@ +// check-pass +// aux-crate:sysroot:somedep=somedep.rs +// compile-flags: -Zunstable-options -Dunused-crate-dependencies +// edition:2018 + +fn main() { +}