From 336e1cf889204977d5d3d6bc74db0b9910a9cd16 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 9 Jul 2024 13:27:42 -0500 Subject: [PATCH] fix: Ensure dep/feature activates the dependency on 2024 This doesn't revert the last commit of #14018 so that it works properly on Editions 2021 and 2024. Fixes #14016 --- src/cargo/util/toml/mod.rs | 27 +++++++++++++++++++++++++-- tests/testsuite/features.rs | 32 +++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index c70badc45c3c..5bd6f79fe48a 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -322,7 +322,7 @@ fn resolve_toml( }); resolved_toml.package = Some(resolved_package); - resolved_toml.features = resolve_features(original_toml.features.as_ref())?; + resolved_toml.features = resolve_features(original_toml.features.as_ref(), edition)?; resolved_toml.lib = targets::resolve_lib( original_toml.lib.as_ref(), @@ -691,11 +691,34 @@ fn default_readme_from_package_root(package_root: &Path) -> Option { #[tracing::instrument(skip_all)] fn resolve_features( original_features: Option<&BTreeMap>>, + edition: Edition, ) -> CargoResult>>> { - let Some(resolved_features) = original_features.cloned() else { + let Some(mut resolved_features) = original_features.cloned() else { return Ok(None); }; + if Edition::Edition2024 <= edition { + for activations in resolved_features.values_mut() { + let mut deps = Vec::new(); + for feature_value in activations.iter() { + let feature_value = FeatureValue::new(InternedString::new(feature_value)); + let FeatureValue::DepFeature { + dep_name, + dep_feature: _, + weak: false, + } = feature_value + else { + continue; + }; + let dep = FeatureValue::Dep { dep_name }.to_string(); + if !activations.contains(&dep) { + deps.push(dep); + } + } + activations.extend(deps); + } + } + Ok(Some(resolved_features)) } diff --git a/tests/testsuite/features.rs b/tests/testsuite/features.rs index 5e8a5550d864..1213c4a5574a 100644 --- a/tests/testsuite/features.rs +++ b/tests/testsuite/features.rs @@ -1890,17 +1890,27 @@ fn strong_dep_feature_edition2024() { p.cargo("metadata") .masquerade_as_nightly_cargo(&["edition2024"]) - .with_status(101) - .with_stderr_data(str![[r#" -[ERROR] feature `optional_dep` includes `optional_dep/foo`, but `optional_dep` is not a dependency - --> Cargo.toml:9:32 - | -9 | optional_dep = ["optional_dep/foo"] - | ^^^^^^^^^^^^^^^^^^^^ - | -[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml` - -"#]]) + .with_stdout_data( + str![[r#" +{ + "metadata": null, + "packages": [ + { + "features": { + "optional_dep": [ + "optional_dep/foo", + "dep:optional_dep" + ] + }, + "name": "foo", + "...": "{...}" + } + ], + "...": "{...}" +} +"#]] + .json(), + ) .run(); }