Skip to content

Add support for nounused --extern flag #96067

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

Merged
merged 1 commit into from
Apr 24, 2022
Merged
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,
nounused_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 @@ -907,6 +907,10 @@ impl<'a> CrateLoader<'a> {
// Don't worry about pathless `--extern foo` sysroot references
continue;
}
if entry.nounused_dep {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... would something like ignore_unused be a better name for this? I know the convention in other compilers is X and noX for enabling or disabling X, but it's kinda hard to understand whether nounused is denying unused or allowing unused.

// We're not worried about this one
continue;
}
let name_interned = Symbol::intern(name);
if self.used_extern_options.contains(&name_interned) {
continue;
Expand Down
11 changes: 10 additions & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ pub struct ExternEntry {
/// This can be disabled with the `noprelude` option like
/// `--extern noprelude:name`.
pub add_prelude: bool,
/// The extern entry shouldn't be considered for unused dependency warnings.
///
/// `--extern nounused:std=/path/to/lib/libstd.rlib`. This is used to
/// suppress `unused-crate-dependencies` warnings.
pub nounused_dep: bool,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -512,7 +517,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, nounused_dep: false }
}

pub fn files(&self) -> Option<impl Iterator<Item = &CanonicalizedPath>> {
Expand Down Expand Up @@ -2131,6 +2136,7 @@ pub fn parse_externs(

let mut is_private_dep = false;
let mut add_prelude = true;
let mut nounused_dep = false;
if let Some(opts) = options {
if !is_unstable_enabled {
early_error(
Expand All @@ -2152,6 +2158,7 @@ pub fn parse_externs(
);
}
}
"nounused" => nounused_dep = true,
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
}
}
Expand All @@ -2160,6 +2167,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 `nounused`
entry.nounused_dep |= nounused_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-nounused.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_nounused`
}
10 changes: 10 additions & 0 deletions src/test/ui/extern-flag/no-nounused.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: external crate `somedep` unused in `no_nounused`: remove the dependency or add `use somedep as _;`
--> $DIR/no-nounused.rs:5:1
|
LL | fn main() {
| ^
|
= note: requested on the command line with `-D unused-crate-dependencies`

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/extern-flag/nounused.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may be able to use revisions to consolidate this into one test, but I guess I won't block this on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

// aux-crate:nounused:somedep=somedep.rs
// compile-flags: -Zunstable-options -Dunused-crate-dependencies
// edition:2018

fn main() {
}