Skip to content

Commit 0b3dce4

Browse files
committed
suppress unused_crate_deps for implicit build-std deps
1 parent 2eec0a2 commit 0b3dce4

File tree

5 files changed

+81
-65
lines changed

5 files changed

+81
-65
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,68 +1872,75 @@ pub fn extern_args(
18721872
let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
18731873

18741874
// Closure to add one dependency to `result`.
1875-
let mut link_to =
1876-
|dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
1877-
let mut value = OsString::new();
1878-
let mut opts = Vec::new();
1879-
let is_public_dependency_enabled = unit
1880-
.pkg
1881-
.manifest()
1882-
.unstable_features()
1883-
.require(Feature::public_dependency())
1884-
.is_ok()
1885-
|| build_runner.bcx.gctx.cli_unstable().public_dependency;
1886-
if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
1887-
opts.push("priv");
1888-
*unstable_opts = true;
1889-
}
1890-
if noprelude {
1891-
opts.push("noprelude");
1892-
*unstable_opts = true;
1893-
}
1894-
if !opts.is_empty() {
1895-
value.push(opts.join(","));
1896-
value.push(":");
1897-
}
1898-
value.push(extern_crate_name.as_str());
1899-
value.push("=");
1900-
1901-
let mut pass = |file| {
1902-
let mut value = value.clone();
1903-
value.push(file);
1904-
result.push(OsString::from("--extern"));
1905-
result.push(value);
1906-
};
1875+
let mut link_to = |dep: &UnitDep,
1876+
extern_crate_name: InternedString,
1877+
noprelude: bool,
1878+
nounused: bool|
1879+
-> CargoResult<()> {
1880+
let mut value = OsString::new();
1881+
let mut opts = Vec::new();
1882+
let is_public_dependency_enabled = unit
1883+
.pkg
1884+
.manifest()
1885+
.unstable_features()
1886+
.require(Feature::public_dependency())
1887+
.is_ok()
1888+
|| build_runner.bcx.gctx.cli_unstable().public_dependency;
1889+
if !dep.public && unit.target.is_lib() && is_public_dependency_enabled {
1890+
opts.push("priv");
1891+
*unstable_opts = true;
1892+
}
1893+
if noprelude {
1894+
opts.push("noprelude");
1895+
*unstable_opts = true;
1896+
}
1897+
if nounused {
1898+
opts.push("nounused");
1899+
*unstable_opts = true;
1900+
}
1901+
if !opts.is_empty() {
1902+
value.push(opts.join(","));
1903+
value.push(":");
1904+
}
1905+
value.push(extern_crate_name.as_str());
1906+
value.push("=");
1907+
1908+
let mut pass = |file| {
1909+
let mut value = value.clone();
1910+
value.push(file);
1911+
result.push(OsString::from("--extern"));
1912+
result.push(value);
1913+
};
19071914

1908-
let outputs = build_runner.outputs(&dep.unit)?;
1915+
let outputs = build_runner.outputs(&dep.unit)?;
19091916

1910-
if build_runner.only_requires_rmeta(unit, &dep.unit) || dep.unit.mode.is_check() {
1911-
// Example: rlib dependency for an rlib, rmeta is all that is required.
1912-
let output = outputs
1913-
.iter()
1914-
.find(|output| output.flavor == FileFlavor::Rmeta)
1915-
.expect("failed to find rmeta dep for pipelined dep");
1916-
pass(&output.path);
1917-
} else {
1918-
// Example: a bin needs `rlib` for dependencies, it cannot use rmeta.
1919-
for output in outputs.iter() {
1920-
if output.flavor == FileFlavor::Linkable {
1921-
pass(&output.path);
1922-
}
1923-
// If we use -Zembed-metadata=no, we also need to pass the path to the
1924-
// corresponding .rmeta file to the linkable artifact, because the
1925-
// normal dependency (rlib) doesn't contain the full metadata.
1926-
else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
1927-
pass(&output.path);
1928-
}
1917+
if build_runner.only_requires_rmeta(unit, &dep.unit) || dep.unit.mode.is_check() {
1918+
// Example: rlib dependency for an rlib, rmeta is all that is required.
1919+
let output = outputs
1920+
.iter()
1921+
.find(|output| output.flavor == FileFlavor::Rmeta)
1922+
.expect("failed to find rmeta dep for pipelined dep");
1923+
pass(&output.path);
1924+
} else {
1925+
// Example: a bin needs `rlib` for dependencies, it cannot use rmeta.
1926+
for output in outputs.iter() {
1927+
if output.flavor == FileFlavor::Linkable {
1928+
pass(&output.path);
1929+
}
1930+
// If we use -Zembed-metadata=no, we also need to pass the path to the
1931+
// corresponding .rmeta file to the linkable artifact, because the
1932+
// normal dependency (rlib) doesn't contain the full metadata.
1933+
else if no_embed_metadata && output.flavor == FileFlavor::Rmeta {
1934+
pass(&output.path);
19291935
}
19301936
}
1931-
Ok(())
1932-
};
1937+
}
1938+
Ok(())
1939+
};
19331940

19341941
for dep in deps {
19351942
if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
1936-
link_to(dep, dep.extern_crate_name, dep.noprelude)?;
1943+
link_to(dep, dep.extern_crate_name, dep.noprelude, dep.nounused)?;
19371944
}
19381945
}
19391946
if unit.target.proc_macro() {

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ fn attach_std_deps(
206206
// TODO: Does this `public` make sense?
207207
public: true,
208208
noprelude: true,
209+
nounused: true,
209210
}));
210211
found = true;
211212
}
@@ -911,6 +912,7 @@ fn new_unit_dep_with_profile(
911912
dep_name,
912913
public,
913914
noprelude: false,
915+
nounused: false,
914916
})
915917
}
916918

src/cargo/core/compiler/unit_graph.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub struct UnitDep {
4040
pub public: bool,
4141
/// If `true`, the dependency should not be added to Rust's prelude.
4242
pub noprelude: bool,
43+
/// If `true`, the dependency will not trigger Rust lint.
44+
pub nounused: bool,
4345
}
4446

4547
const VERSION: u32 = 1;
@@ -74,6 +76,9 @@ struct SerializedUnitDep {
7476
// This is only set on nightly since it is unstable.
7577
#[serde(skip_serializing_if = "Option::is_none")]
7678
noprelude: Option<bool>,
79+
// This is only set on nightly since it is unstable.
80+
#[serde(skip_serializing_if = "Option::is_none")]
81+
nounused: Option<bool>,
7782
// Intentionally not including `unit_for` because it is a low-level
7883
// internal detail that is mostly used for building the graph.
7984
}
@@ -101,16 +106,21 @@ pub fn emit_serialized_unit_graph(
101106
.iter()
102107
.map(|unit_dep| {
103108
// https://github.com/rust-lang/rust/issues/64260 when stabilized.
104-
let (public, noprelude) = if gctx.nightly_features_allowed {
105-
(Some(unit_dep.public), Some(unit_dep.noprelude))
109+
let (public, noprelude, nounused) = if gctx.nightly_features_allowed {
110+
(
111+
Some(unit_dep.public),
112+
Some(unit_dep.noprelude),
113+
Some(unit_dep.nounused),
114+
)
106115
} else {
107-
(None, None)
116+
(None, None, None)
108117
};
109118
SerializedUnitDep {
110119
index: indices[&unit_dep.unit],
111120
extern_crate_name: unit_dep.extern_crate_name,
112121
public,
113122
noprelude,
123+
nounused,
114124
}
115125
})
116126
.collect();

tests/build-std/main.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -472,15 +472,9 @@ fn build_std_does_not_warn_about_implicit_std_deps() {
472472
.env("RUSTFLAGS", "-W unused-crate-dependencies")
473473
.with_stderr_data(
474474
str![[r#"
475-
[WARNING] extern crate `alloc` is unused in crate `bar`
476-
[WARNING] extern crate `alloc` is unused in crate `buildstd_test`
477475
[WARNING] extern crate `bar` is unused in crate `buildstd_test`
478-
[WARNING] extern crate `compiler_builtins` is unused in crate `buildstd_test`
479-
[WARNING] extern crate `core` is unused in crate `buildstd_test`
480-
[WARNING] extern crate `proc_macro` is unused in crate `buildstd_test`
481-
[WARNING] `buildstd_test` (bin "buildstd_test") generated 5 warnings
476+
[WARNING] `buildstd_test` (bin "buildstd_test") generated 1 warning
482477
...
483-
484478
"#]]
485479
.unordered(),
486480
)

tests/testsuite/unit_graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn simple() {
6161
"extern_crate_name": "b",
6262
"index": 1,
6363
"noprelude": false,
64+
"nounused": false,
6465
"public": false
6566
}
6667
],
@@ -106,6 +107,7 @@ fn simple() {
106107
"extern_crate_name": "c",
107108
"index": 2,
108109
"noprelude": false,
110+
"nounused": false,
109111
"public": false
110112
}
111113
],
@@ -189,6 +191,7 @@ fn simple() {
189191
"extern_crate_name": "a",
190192
"index": 0,
191193
"noprelude": false,
194+
"nounused": false,
192195
"public": false
193196
}
194197
],

0 commit comments

Comments
 (0)