Skip to content

Commit b759b22

Browse files
committed
Auto merge of rust-lang#96067 - jsgf:extern-nounused, r=compiler-errors
Add support for `nounused` --extern flag This adds `nounused` to the set of extern flags: `--extern nounused:core=/path/to/core/libcore.rlib`. The effect of this flag is to suppress `unused-crate-dependencies` warnings relating to the crate.
2 parents b21759f + 9102edf commit b759b22

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-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+
nounused_dep: false,
6970
}
7071
}
7172

compiler/rustc_metadata/src/creader.rs

+4
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ impl<'a> CrateLoader<'a> {
907907
// Don't worry about pathless `--extern foo` sysroot references
908908
continue;
909909
}
910+
if entry.nounused_dep {
911+
// We're not worried about this one
912+
continue;
913+
}
910914
let name_interned = Symbol::intern(name);
911915
if self.used_extern_options.contains(&name_interned) {
912916
continue;

compiler/rustc_session/src/config.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ pub struct ExternEntry {
474474
/// This can be disabled with the `noprelude` option like
475475
/// `--extern noprelude:name`.
476476
pub add_prelude: bool,
477+
/// The extern entry shouldn't be considered for unused dependency warnings.
478+
///
479+
/// `--extern nounused:std=/path/to/lib/libstd.rlib`. This is used to
480+
/// suppress `unused-crate-dependencies` warnings.
481+
pub nounused_dep: bool,
477482
}
478483

479484
#[derive(Clone, Debug)]
@@ -512,7 +517,7 @@ impl Externs {
512517

513518
impl ExternEntry {
514519
fn new(location: ExternLocation) -> ExternEntry {
515-
ExternEntry { location, is_private_dep: false, add_prelude: false }
520+
ExternEntry { location, is_private_dep: false, add_prelude: false, nounused_dep: false }
516521
}
517522

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

21322137
let mut is_private_dep = false;
21332138
let mut add_prelude = true;
2139+
let mut nounused_dep = false;
21342140
if let Some(opts) = options {
21352141
if !is_unstable_enabled {
21362142
early_error(
@@ -2152,6 +2158,7 @@ pub fn parse_externs(
21522158
);
21532159
}
21542160
}
2161+
"nounused" => nounused_dep = true,
21552162
_ => early_error(error_format, &format!("unknown --extern option `{opt}`")),
21562163
}
21572164
}
@@ -2160,6 +2167,8 @@ pub fn parse_externs(
21602167
// Crates start out being not private, and go to being private `priv`
21612168
// is specified.
21622169
entry.is_private_dep |= is_private_dep;
2170+
// likewise `nounused`
2171+
entry.nounused_dep |= nounused_dep;
21632172
// If any flag is missing `noprelude`, then add to the prelude.
21642173
entry.add_prelude |= add_prelude;
21652174
}
+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_nounused`
6+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: external crate `somedep` unused in `no_nounused`: remove the dependency or add `use somedep as _;`
2+
--> $DIR/no-nounused.rs:5:1
3+
|
4+
LL | fn main() {
5+
| ^
6+
|
7+
= note: requested on the command line with `-D unused-crate-dependencies`
8+
9+
error: aborting due to previous error
10+

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

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

0 commit comments

Comments
 (0)