From 97bb4a0f2871c214fb4e1b66873afc1a3a3a9559 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 9 May 2024 01:16:36 -0400 Subject: [PATCH] fix: proc-macro example from dep no longer affects feature resolution 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. --- src/cargo/core/resolver/features.rs | 15 ++++++++++++++- tests/testsuite/features2.rs | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/resolver/features.rs b/src/cargo/core/resolver/features.rs index 8be2b48a061..807bdc4533a 100644 --- a/src/cargo/core/resolver/features.rs +++ b/src/cargo/core/resolver/features.rs @@ -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 { @@ -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() + } } diff --git a/tests/testsuite/features2.rs b/tests/testsuite/features2.rs index 878324290d9..18951457287 100644 --- a/tests/testsuite/features2.rs +++ b/tests/testsuite/features2.rs @@ -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(); }