From bc6efb36a18fca360a1180e0ef257d203acf6be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-=C3=89tienne=20Meunier?= Date: Thu, 14 Nov 2024 17:21:39 +0100 Subject: [PATCH] Use the replacement source instead of the original source when generating `-C metadata` and `-C extra-filename` This commit fixes an issue when replacing two different sources with the same local one (for example using `cargo vendor`), where `rustc` errs saying that two types "defined at the same place in the same file" are "different". --- src/cargo/core/compiler/build_runner/compilation_files.rs | 1 + src/cargo/core/compiler/standard_lib.rs | 1 + src/cargo/core/compiler/unit.rs | 5 ++++- src/cargo/core/compiler/unit_dependencies.rs | 4 ++++ src/cargo/ops/cargo_compile/mod.rs | 3 +++ src/cargo/ops/cargo_compile/unit_generator.rs | 2 ++ 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/build_runner/compilation_files.rs b/src/cargo/core/compiler/build_runner/compilation_files.rs index 667cf0379e07..e3d7bb9de134 100644 --- a/src/cargo/core/compiler/build_runner/compilation_files.rs +++ b/src/cargo/core/compiler/build_runner/compilation_files.rs @@ -585,6 +585,7 @@ fn compute_metadata( // to pull crates from anywhere without worrying about conflicts. unit.pkg .package_id() + .with_source_id(unit.replaced_source) .stable_hash(bcx.ws.root()) .hash(&mut hasher); diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index a3b2ff8acd19..a55ff38b15c6 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -162,6 +162,7 @@ pub fn generate_std_roots( ); list.push(interner.intern( pkg, + pkg.package_id().source_id(), lib, profile, *kind, diff --git a/src/cargo/core/compiler/unit.rs b/src/cargo/core/compiler/unit.rs index 628b0b784ff1..3a8eee86c02c 100644 --- a/src/cargo/core/compiler/unit.rs +++ b/src/cargo/core/compiler/unit.rs @@ -4,7 +4,7 @@ use crate::core::compiler::unit_dependencies::IsArtifact; use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, CrateType}; use crate::core::manifest::{Target, TargetKind}; use crate::core::profiles::Profile; -use crate::core::Package; +use crate::core::{Package, SourceId}; use crate::util::hex::short_hash; use crate::util::interning::InternedString; use crate::util::GlobalContext; @@ -42,6 +42,7 @@ pub struct UnitInner { /// Information about available targets, which files to include/exclude, etc. Basically stuff in /// `Cargo.toml`. pub pkg: Package, + pub replaced_source: SourceId, /// Information about the specific target to build, out of the possible targets in `pkg`. Not /// to be confused with *target-triple* (or *target architecture* ...), the target arch for a /// build. @@ -226,6 +227,7 @@ impl UnitInterner { pub fn intern( &self, pkg: &Package, + replaced_source: SourceId, target: &Target, profile: Profile, kind: CompileKind, @@ -262,6 +264,7 @@ impl UnitInterner { }; let inner = self.intern_inner(&UnitInner { pkg: pkg.clone(), + replaced_source, target, profile, kind, diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 59e356fc6828..25f3ee749ec8 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -848,8 +848,12 @@ fn new_unit_dep_with_profile( _ => None, }; let features = state.activated_features(pkg.package_id(), features_for); + + let replaced_source = state.package_set.sources().get(pkg.package_id().source_id()).unwrap().replaced_source_id(); + let unit = state.interner.intern( pkg, + replaced_source, target, profile, kind, diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 17aaa922b8a2..03d07358ca88 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -695,6 +695,7 @@ fn traverse_and_share( canonical_profile.debuginfo = canonical_debuginfo; let unit_probe = interner.intern( &unit.pkg, + unit.replaced_source, &unit.target, canonical_profile, to_host.unwrap(), @@ -723,6 +724,7 @@ fn traverse_and_share( let new_unit = interner.intern( &unit.pkg, + unit.replaced_source, &unit.target, profile, canonical_kind, @@ -887,6 +889,7 @@ fn override_rustc_crate_types( target.set_kind(f(crate_types)); interner.intern( &unit.pkg, + unit.replaced_source, &target, unit.profile.clone(), unit.kind, diff --git a/src/cargo/ops/cargo_compile/unit_generator.rs b/src/cargo/ops/cargo_compile/unit_generator.rs index ce10e173c6c8..76b474a25d45 100644 --- a/src/cargo/ops/cargo_compile/unit_generator.rs +++ b/src/cargo/ops/cargo_compile/unit_generator.rs @@ -166,6 +166,7 @@ impl<'a> UnitGenerator<'a, '_> { let kind = kind.for_target(target); self.interner.intern( pkg, + pkg.package_id().source_id(), target, profile, kind, @@ -671,6 +672,7 @@ Rustdoc did not scrape the following examples because they require dev-dependenc } None => Vec::new(), }; + if target.is_lib() || unavailable_features.is_empty() { units.extend(self.new_units(pkg, target, mode)); } else if requires_features {