Skip to content

Commit 54ee781

Browse files
committed
Support profile overrides for -Z build-std
Pass both the main package graph and the build-std package graph to the verifier so that e.g. `[profile.dev.package.core]` is not rejected and ignored if `core` is being built.
1 parent d507fbe commit 54ee781

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

src/cargo/core/profiles.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ use crate::core::Feature;
2525
use crate::core::compiler::{CompileKind, CompileTarget, Unit};
2626
use crate::core::dependency::Artifact;
2727
use crate::core::resolver::features::FeaturesFor;
28-
use crate::core::{
29-
PackageId, PackageIdSpec, PackageIdSpecQuery, Resolve, Shell, Target, Workspace,
30-
};
28+
use crate::core::{PackageId, PackageIdSpec, PackageIdSpecQuery, Shell, Target, Workspace};
3129
use crate::util::interning::InternedString;
3230
use crate::util::toml::validate_profile;
3331
use crate::util::{CargoResult, GlobalContext, closest_msg, context};
@@ -352,11 +350,11 @@ impl Profiles {
352350
}
353351

354352
/// Used to check for overrides for non-existing packages.
355-
pub fn validate_packages(
353+
pub fn validate_packages<I: Iterator<Item = PackageId>>(
356354
&self,
357355
profiles: Option<&TomlProfiles>,
358356
shell: &mut Shell,
359-
resolve: &Resolve,
357+
packages: impl Fn() -> I,
360358
) -> CargoResult<()> {
361359
for (name, profile) in &self.by_name {
362360
// If the user did not specify an override, skip this. This is here
@@ -372,13 +370,13 @@ impl Profiles {
372370
{
373371
continue;
374372
}
375-
let found = validate_packages_unique(resolve, name, &profile.toml)?;
373+
let found = validate_packages_unique(&packages, name, &profile.toml)?;
376374
// We intentionally do not validate unmatched packages for config
377375
// profiles, in case they are defined in a central location. This
378376
// iterates over the manifest profiles only.
379377
if let Some(profiles) = profiles {
380378
if let Some(toml_profile) = profiles.get(name) {
381-
validate_packages_unmatched(shell, resolve, name, toml_profile, &found)?;
379+
validate_packages_unmatched(shell, &packages, name, toml_profile, &found)?;
382380
}
383381
}
384382
}
@@ -1335,8 +1333,8 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
13351333
///
13361334
/// For example `[profile.dev.package.bar]` and `[profile.dev.package."bar:0.5.0"]`
13371335
/// would both match `bar:0.5.0` which would be ambiguous.
1338-
fn validate_packages_unique(
1339-
resolve: &Resolve,
1336+
fn validate_packages_unique<I: Iterator<Item = PackageId>>(
1337+
packages: impl Fn() -> I,
13401338
name: &str,
13411339
toml: &Option<TomlProfile>,
13421340
) -> CargoResult<HashSet<PackageIdSpec>> {
@@ -1348,7 +1346,7 @@ fn validate_packages_unique(
13481346
};
13491347
// Verify that a package doesn't match multiple spec overrides.
13501348
let mut found = HashSet::new();
1351-
for pkg_id in resolve.iter() {
1349+
for pkg_id in packages() {
13521350
let matches: Vec<&PackageIdSpec> = overrides
13531351
.keys()
13541352
.filter_map(|key| match *key {
@@ -1389,9 +1387,9 @@ fn validate_packages_unique(
13891387
/// Check for any profile override specs that do not match any known packages.
13901388
///
13911389
/// This helps check for typos and mistakes.
1392-
fn validate_packages_unmatched(
1390+
fn validate_packages_unmatched<I: Iterator<Item = PackageId>>(
13931391
shell: &mut Shell,
1394-
resolve: &Resolve,
1392+
packages: impl Fn() -> I,
13951393
name: &str,
13961394
toml: &TomlProfile,
13971395
found: &HashSet<PackageIdSpec>,
@@ -1411,8 +1409,7 @@ fn validate_packages_unmatched(
14111409
});
14121410
for spec in missing_specs {
14131411
// See if there is an exact name match.
1414-
let name_matches: Vec<String> = resolve
1415-
.iter()
1412+
let name_matches: Vec<String> = packages()
14161413
.filter_map(|pkg_id| {
14171414
if pkg_id.name() == spec.name() {
14181415
Some(pkg_id.to_string())
@@ -1422,12 +1419,8 @@ fn validate_packages_unmatched(
14221419
})
14231420
.collect();
14241421
if name_matches.is_empty() {
1425-
let suggestion = closest_msg(
1426-
&spec.name(),
1427-
resolve.iter(),
1428-
|p| p.name().as_str(),
1429-
"package",
1430-
);
1422+
let suggestion =
1423+
closest_msg(&spec.name(), packages(), |p| p.name().as_str(), "package");
14311424
shell.warn(format!(
14321425
"profile package spec `{}` in profile `{}` did not match any packages{}",
14331426
spec, name, suggestion

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,14 @@ pub fn create_bcx<'a, 'gctx>(
344344
}
345345

346346
let profiles = Profiles::new(ws, build_config.requested_profile)?;
347-
profiles.validate_packages(
348-
ws.profiles(),
349-
&mut gctx.shell(),
350-
workspace_resolve.as_ref().unwrap_or(&resolve),
351-
)?;
347+
let pkg_resolve = workspace_resolve.as_ref().unwrap_or(&resolve);
348+
if let Some((std_resolve, _)) = &std_resolve_features {
349+
profiles.validate_packages(ws.profiles(), &mut gctx.shell(), || {
350+
pkg_resolve.iter().chain(std_resolve.iter())
351+
})?
352+
} else {
353+
profiles.validate_packages(ws.profiles(), &mut gctx.shell(), || pkg_resolve.iter())?
354+
};
352355

353356
// If `--target` has not been specified, then the unit graph is built
354357
// assuming `--target $HOST` was specified. See

tests/testsuite/standard_lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,47 @@ fn fetch() {
866866
.with_stderr_does_not_contain("[DOWNLOADED] [..]")
867867
.run();
868868
}
869+
870+
#[cargo_test(build_std_mock)]
871+
fn core_profile() {
872+
let setup = setup();
873+
874+
let p = project()
875+
.file(
876+
"Cargo.toml",
877+
r#"
878+
[package]
879+
name = "profile-check"
880+
edition = "2024"
881+
882+
[profile.dev.package.core]
883+
debug-assertions = false
884+
"#,
885+
)
886+
.file("src/lib.rs", "extern crate core;")
887+
.build();
888+
p.cargo("build -v")
889+
.build_std_arg(&setup, "compiler_builtins,core")
890+
.with_stderr_data(
891+
str![[r#"
892+
[UPDATING] `dummy-registry` index
893+
[DOWNLOADING] crates ...
894+
[DOWNLOADED] registry-dep-using-std v1.0.0 (registry `dummy-registry`)
895+
[DOWNLOADED] registry-dep-using-core v1.0.0 (registry `dummy-registry`)
896+
[DOWNLOADED] registry-dep-using-alloc v1.0.0 (registry `dummy-registry`)
897+
[COMPILING] compiler_builtins v0.1.0 ([..]/library/compiler_builtins)
898+
[COMPILING] core v0.1.0 ([..]/library/core)
899+
[COMPILING] profile-check v0.0.0 ([ROOT]/foo)
900+
[RUNNING] `[..] rustc --crate-name compiler_builtins [..]`
901+
[RUNNING] `[..] rustc --crate-name core [..] -C debug-assertions=off [..]`
902+
[RUNNING] `[..] rustc --crate-name profile_check [..]`
903+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
904+
905+
"#]]
906+
.unordered(),
907+
)
908+
.with_stderr_does_not_contain(
909+
"[RUNNING] `[..] rustc --crate-name compiler_builtins [..] -C debug-assertions=off [..]`",
910+
)
911+
.run();
912+
}

0 commit comments

Comments
 (0)