Skip to content

Commit 71f88a1

Browse files
committed
suppress unused_crate_deps for implicit build-std deps
1 parent e8db17f commit 71f88a1

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
@@ -1874,68 +1874,75 @@ pub fn extern_args(
18741874
let no_embed_metadata = build_runner.bcx.gctx.cli_unstable().no_embed_metadata;
18751875

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

1910-
let outputs = build_runner.outputs(&dep.unit)?;
1917+
let outputs = build_runner.outputs(&dep.unit)?;
19111918

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

19361943
for dep in deps {
19371944
if dep.unit.target.is_linkable() && !dep.unit.mode.is_doc() {
1938-
link_to(dep, dep.extern_crate_name, dep.noprelude)?;
1945+
link_to(dep, dep.extern_crate_name, dep.noprelude, dep.nounused)?;
19391946
}
19401947
}
19411948
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)