Skip to content

Commit 5965c15

Browse files
committed
fix(toml): Disallow ignored default-features when inheriting
1 parent 2e686d4 commit 5965c15

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,14 @@ fn resolve_dependencies<'a>(
692692

693693
let mut deps = BTreeMap::new();
694694
for (name_in_toml, v) in dependencies.iter() {
695-
let mut resolved =
696-
dependency_inherit_with(v.clone(), name_in_toml, inherit, package_root, warnings)?;
695+
let mut resolved = dependency_inherit_with(
696+
v.clone(),
697+
name_in_toml,
698+
inherit,
699+
package_root,
700+
edition,
701+
warnings,
702+
)?;
697703
if let manifest::TomlDependency::Detailed(ref mut d) = resolved {
698704
deprecated_underscore(
699705
&d.default_features2,
@@ -949,12 +955,13 @@ fn dependency_inherit_with<'a>(
949955
name: &str,
950956
inherit: &dyn Fn() -> CargoResult<&'a InheritableFields>,
951957
package_root: &Path,
958+
edition: Edition,
952959
warnings: &mut Vec<String>,
953960
) -> CargoResult<manifest::TomlDependency> {
954961
match dependency {
955962
manifest::InheritableDependency::Value(value) => Ok(value),
956963
manifest::InheritableDependency::Inherit(w) => {
957-
inner_dependency_inherit_with(w, name, inherit, package_root, warnings).with_context(|| {
964+
inner_dependency_inherit_with(w, name, inherit, package_root, edition, warnings).with_context(|| {
958965
format!(
959966
"error inheriting `{name}` from workspace root manifest's `workspace.dependencies.{name}`",
960967
)
@@ -968,6 +975,7 @@ fn inner_dependency_inherit_with<'a>(
968975
name: &str,
969976
inherit: &dyn Fn() -> CargoResult<&'a InheritableFields>,
970977
package_root: &Path,
978+
edition: Edition,
971979
warnings: &mut Vec<String>,
972980
) -> CargoResult<manifest::TomlDependency> {
973981
let ws_dep = inherit()?.get_dependency(name, package_root)?;
@@ -1002,12 +1010,12 @@ fn inner_dependency_inherit_with<'a>(
10021010
// workspace: default-features = true should ignore member
10031011
// default-features
10041012
(Some(false), Some(true)) => {
1005-
deprecated_ws_default_features(name, Some(true), warnings);
1013+
deprecated_ws_default_features(name, Some(true), edition, warnings)?;
10061014
}
10071015
// member: default-features = false and
10081016
// workspace: dep = "1.0" should ignore member default-features
10091017
(Some(false), None) => {
1010-
deprecated_ws_default_features(name, None, warnings);
1018+
deprecated_ws_default_features(name, None, edition, warnings)?;
10111019
}
10121020
_ => {}
10131021
}
@@ -1030,18 +1038,24 @@ fn inner_dependency_inherit_with<'a>(
10301038
fn deprecated_ws_default_features(
10311039
label: &str,
10321040
ws_def_feat: Option<bool>,
1041+
edition: Edition,
10331042
warnings: &mut Vec<String>,
1034-
) {
1043+
) -> CargoResult<()> {
10351044
let ws_def_feat = match ws_def_feat {
10361045
Some(true) => "true",
10371046
Some(false) => "false",
10381047
None => "not specified",
10391048
};
1040-
warnings.push(format!(
1041-
"`default-features` is ignored for {label}, since `default-features` was \
1049+
if Edition::Edition2024 <= edition {
1050+
anyhow::bail!("`default-features = false` cannot override workspace's `default-features`");
1051+
} else {
1052+
warnings.push(format!(
1053+
"`default-features` is ignored for {label}, since `default-features` was \
10421054
{ws_def_feat} for `workspace.dependencies.{label}`, \
10431055
this could become a hard error in the future"
1044-
))
1056+
));
1057+
}
1058+
Ok(())
10451059
}
10461060

10471061
#[tracing::instrument(skip_all)]

tests/testsuite/inheritable_workspace_fields.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,19 +1553,16 @@ fn warn_inherit_def_feat_true_member_def_feat_false_2024_edition() {
15531553

15541554
p.cargo("check")
15551555
.masquerade_as_nightly_cargo(&["edition2024"])
1556+
.with_status(101)
15561557
.with_stderr(
15571558
"\
1558-
[WARNING] [CWD]/Cargo.toml: `default-features` is ignored for dep, since `default-features` was \
1559-
true for `workspace.dependencies.dep`, this could become a hard error in the future
1560-
[UPDATING] `dummy-registry` index
1561-
[LOCKING] 3 packages to latest Rust [..] compatible versions
1562-
[DOWNLOADING] crates ...
1563-
[DOWNLOADED] fancy_dep v0.2.4 ([..])
1564-
[DOWNLOADED] dep v0.1.0 ([..])
1565-
[CHECKING] fancy_dep v0.2.4
1566-
[CHECKING] dep v0.1.0
1567-
[CHECKING] bar v0.2.0 ([CWD])
1568-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
1559+
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
1560+
1561+
Caused by:
1562+
error inheriting `dep` from workspace root manifest's `workspace.dependencies.dep`
1563+
1564+
Caused by:
1565+
`default-features = false` cannot override workspace's `default-features`
15691566
",
15701567
)
15711568
.run();
@@ -1656,19 +1653,16 @@ fn warn_inherit_simple_member_def_feat_false_2024_edition() {
16561653

16571654
p.cargo("check")
16581655
.masquerade_as_nightly_cargo(&["edition2024"])
1656+
.with_status(101)
16591657
.with_stderr(
16601658
"\
1661-
[WARNING] [CWD]/Cargo.toml: `default-features` is ignored for dep, since `default-features` was \
1662-
not specified for `workspace.dependencies.dep`, this could become a hard error in the future
1663-
[UPDATING] `dummy-registry` index
1664-
[LOCKING] 3 packages to latest compatible versions
1665-
[DOWNLOADING] crates ...
1666-
[DOWNLOADED] fancy_dep v0.2.4 ([..])
1667-
[DOWNLOADED] dep v0.1.0 ([..])
1668-
[CHECKING] fancy_dep v0.2.4
1669-
[CHECKING] dep v0.1.0
1670-
[CHECKING] bar v0.2.0 ([CWD])
1671-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
1659+
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
1660+
1661+
Caused by:
1662+
error inheriting `dep` from workspace root manifest's `workspace.dependencies.dep`
1663+
1664+
Caused by:
1665+
`default-features = false` cannot override workspace's `default-features`
16721666
",
16731667
)
16741668
.run();

0 commit comments

Comments
 (0)