Skip to content

Commit d7966eb

Browse files
committed
Auto merge of #8194 - sunshowers:activate, r=ehuss
features: allow activated_features_unverified to communicate not-present Hi there! :) I'm currently writing [`cargo-guppy`](https://github.com/facebookincubator/cargo-guppy), a toolkit for analyzing Rust dependency graphs. As part of that I'm writing some tests to compare guppy to `cargo` (see facebookarchive/cargo-guppy#126), to ensure that it produces the same results for the subset of analyses `cargo` can do. While writing tests I found it useful to distinguish between packages that weren't present at all and packages that were activated but with no features. This commit adds that functionality to the `_unverified` method. (BTW, of possible interest to @ehuss, I've also found some interesting behavior differences between the v1 and v2 resolvers. Will file issues for them soon!)
2 parents 91dc1e1 + e94face commit d7966eb

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/cargo/core/resolver/features.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,36 +208,34 @@ impl ResolvedFeatures {
208208
pkg_id: PackageId,
209209
features_for: FeaturesFor,
210210
) -> Vec<InternedString> {
211-
self.activated_features_int(pkg_id, features_for, true)
211+
self.activated_features_int(pkg_id, features_for)
212+
.expect("activated_features for invalid package")
212213
}
213214

214-
/// Variant of `activated_features` that returns an empty Vec if this is
215+
/// Variant of `activated_features` that returns `None` if this is
215216
/// not a valid pkg_id/is_build combination. Used in places which do
216217
/// not know which packages are activated (like `cargo clean`).
217218
pub fn activated_features_unverified(
218219
&self,
219220
pkg_id: PackageId,
220221
features_for: FeaturesFor,
221-
) -> Vec<InternedString> {
222-
self.activated_features_int(pkg_id, features_for, false)
222+
) -> Option<Vec<InternedString>> {
223+
self.activated_features_int(pkg_id, features_for).ok()
223224
}
224225

225226
fn activated_features_int(
226227
&self,
227228
pkg_id: PackageId,
228229
features_for: FeaturesFor,
229-
verify: bool,
230-
) -> Vec<InternedString> {
230+
) -> CargoResult<Vec<InternedString>> {
231231
if let Some(legacy) = &self.legacy {
232-
legacy.get(&pkg_id).map_or_else(Vec::new, |v| v.clone())
232+
Ok(legacy.get(&pkg_id).map_or_else(Vec::new, |v| v.clone()))
233233
} else {
234234
let is_build = self.opts.decouple_host_deps && features_for == FeaturesFor::HostDep;
235235
if let Some(fs) = self.activated_features.get(&(pkg_id, is_build)) {
236-
fs.iter().cloned().collect()
237-
} else if verify {
238-
panic!("features did not find {:?} {:?}", pkg_id, is_build)
236+
Ok(fs.iter().cloned().collect())
239237
} else {
240-
Vec::new()
238+
anyhow::bail!("features did not find {:?} {:?}", pkg_id, is_build)
241239
}
242240
}
243241
}

src/cargo/ops/cargo_clean.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
129129
// Use unverified here since this is being more
130130
// exhaustive than what is actually needed.
131131
let features_for = unit_for.map_to_features_for();
132-
let features =
133-
features.activated_features_unverified(pkg.package_id(), features_for);
132+
let features = features
133+
.activated_features_unverified(pkg.package_id(), features_for)
134+
.unwrap_or_default();
134135
units.push(interner.intern(
135136
pkg, target, profile, *kind, *mode, features, /*is_std*/ false,
136137
));

src/cargo/ops/cargo_compile.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,10 @@ pub fn resolve_all_features(
978978
.proc_macro();
979979
for dep in deps {
980980
let features_for = FeaturesFor::from_for_host(is_proc_macro || dep.is_build());
981-
for feature in resolved_features.activated_features_unverified(dep_id, features_for) {
981+
for feature in resolved_features
982+
.activated_features_unverified(dep_id, features_for)
983+
.unwrap_or_default()
984+
{
982985
features.insert(format!("{}/{}", dep.name_in_toml(), feature));
983986
}
984987
}

0 commit comments

Comments
 (0)