From 1c4b4ae6c23119d56662fe3343973437e4b2f1e8 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 looking for proc-macro for a dependency, the feature resolver v2 checks every target from a dep if `proc-macro = true`. However, it's not possible to depend on a non-lib target as a proc-macro. Therefore, it is sufficient to check if `proc-macro = true` for `[lib]` target for 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 8be2b48a0614..807bdc4533a8 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 878324290d9c..189514572873 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(); }