Skip to content

Add support for sysroot --extern flag #96061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ where
location: ExternLocation::ExactPaths(locations),
is_private_dep: false,
add_prelude: true,
sysroot_dep: false,
}
}

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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<impl Iterator<Item = &CanonicalizedPath>> {
Expand Down Expand Up @@ -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(
Expand All @@ -2207,6 +2216,7 @@ pub fn parse_externs(
);
}
}
"sysroot" => sysroot_dep = true,
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
}
}
Expand All @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/extern-flag/no-sysroot.rs
Original file line number Diff line number Diff line change
@@ -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`
}
11 changes: 11 additions & 0 deletions src/test/ui/extern-flag/no-sysroot.stderr
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions src/test/ui/extern-flag/sysroot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass
// aux-crate:sysroot:somedep=somedep.rs
// compile-flags: -Zunstable-options -Dunused-crate-dependencies
// edition:2018

fn main() {
}