Skip to content

Commit

Permalink
fix: proc-macro example from dep no longer affects feature resolution
Browse files Browse the repository at this point in the history
Previously when checking if a dependency is a proc-macro,
the v2 feature resolve resolver v2 looks for `proc-macro = true`
for every target of the dependency.
However, it's impossible to depend on a non-lib target as a proc-macro.

This fix switches to only check if `proc-macro = true` for `[lib]`
target for a dependency.
  • Loading branch information
weihanglo committed May 9, 2024
1 parent c2e41e9 commit 97bb4a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
// for various targets which are either specified in the manifest
// or on the cargo command-line.
let lib_fk = if fk == FeaturesFor::default() {
(self.track_for_host && (dep.is_build() || self.has_any_proc_macro(dep_id)))
(self.track_for_host && (dep.is_build() || self.has_proc_macro_lib(dep_id)))
.then(|| FeaturesFor::HostDep)
.unwrap_or_default()
} else {
Expand Down Expand Up @@ -966,4 +966,17 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
.expect("packages downloaded")
.proc_macro()
}

/// Whether the given package is a proc macro lib target.
///
/// This is useful for checking if a dependency is a proc macro,
/// as it is not possible to depend on a non-lib target as a proc-macro.
fn has_proc_macro_lib(&self, package_id: PackageId) -> bool {
self.package_set
.get_one(package_id)
.expect("packages downloaded")
.library()
.map(|lib| lib.proc_macro())
.unwrap_or_default()
}
}
10 changes: 7 additions & 3 deletions tests/testsuite/features2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2686,9 +2686,13 @@ fn dont_unify_proc_macro_example_from_dependency() {
.build();

p.cargo("check")
.with_status(101)
.with_stderr_contains(
"[..]activated_features for invalid package: features did not find PackageId [..]pm_helper[..]NormalOrDev[..]"
.with_stderr(
"\
[LOCKING] 2 packages to latest compatible versions
[CHECKING] pm_helper v0.0.0 ([CWD]/pm_helper)
[CHECKING] foo v0.0.0 ([CWD])
[FINISHED] `dev` [..]
",
)
.run();
}

0 comments on commit 97bb4a0

Please sign in to comment.