Skip to content

Commit

Permalink
Auto merge of #8194 - sunshowers:activate, r=ehuss
Browse files Browse the repository at this point in the history
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!)
  • Loading branch information
bors committed May 5, 2020
2 parents 91dc1e1 + e94face commit d7966eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
20 changes: 9 additions & 11 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,36 +208,34 @@ impl ResolvedFeatures {
pkg_id: PackageId,
features_for: FeaturesFor,
) -> Vec<InternedString> {
self.activated_features_int(pkg_id, features_for, true)
self.activated_features_int(pkg_id, features_for)
.expect("activated_features for invalid package")
}

/// Variant of `activated_features` that returns an empty Vec if this is
/// Variant of `activated_features` that returns `None` if this is
/// not a valid pkg_id/is_build combination. Used in places which do
/// not know which packages are activated (like `cargo clean`).
pub fn activated_features_unverified(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
) -> Vec<InternedString> {
self.activated_features_int(pkg_id, features_for, false)
) -> Option<Vec<InternedString>> {
self.activated_features_int(pkg_id, features_for).ok()
}

fn activated_features_int(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
verify: bool,
) -> Vec<InternedString> {
) -> CargoResult<Vec<InternedString>> {
if let Some(legacy) = &self.legacy {
legacy.get(&pkg_id).map_or_else(Vec::new, |v| v.clone())
Ok(legacy.get(&pkg_id).map_or_else(Vec::new, |v| v.clone()))
} else {
let is_build = self.opts.decouple_host_deps && features_for == FeaturesFor::HostDep;
if let Some(fs) = self.activated_features.get(&(pkg_id, is_build)) {
fs.iter().cloned().collect()
} else if verify {
panic!("features did not find {:?} {:?}", pkg_id, is_build)
Ok(fs.iter().cloned().collect())
} else {
Vec::new()
anyhow::bail!("features did not find {:?} {:?}", pkg_id, is_build)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/cargo_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
// Use unverified here since this is being more
// exhaustive than what is actually needed.
let features_for = unit_for.map_to_features_for();
let features =
features.activated_features_unverified(pkg.package_id(), features_for);
let features = features
.activated_features_unverified(pkg.package_id(), features_for)
.unwrap_or_default();
units.push(interner.intern(
pkg, target, profile, *kind, *mode, features, /*is_std*/ false,
));
Expand Down
5 changes: 4 additions & 1 deletion src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,10 @@ pub fn resolve_all_features(
.proc_macro();
for dep in deps {
let features_for = FeaturesFor::from_for_host(is_proc_macro || dep.is_build());
for feature in resolved_features.activated_features_unverified(dep_id, features_for) {
for feature in resolved_features
.activated_features_unverified(dep_id, features_for)
.unwrap_or_default()
{
features.insert(format!("{}/{}", dep.name_in_toml(), feature));
}
}
Expand Down

0 comments on commit d7966eb

Please sign in to comment.