Skip to content

fix(toml): Report '<target>.edition' deprecation to users #15321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,28 @@ pub(super) fn to_targets(
normalized_toml.bin.as_deref().unwrap_or_default(),
package_root,
edition,
warnings,
)?);

targets.extend(to_example_targets(
normalized_toml.example.as_deref().unwrap_or_default(),
package_root,
edition,
warnings,
)?);

targets.extend(to_test_targets(
normalized_toml.test.as_deref().unwrap_or_default(),
package_root,
edition,
warnings,
)?);

targets.extend(to_bench_targets(
normalized_toml.bench.as_deref().unwrap_or_default(),
package_root,
edition,
warnings,
)?);

// processing the custom build script
Expand Down Expand Up @@ -259,7 +263,7 @@ fn to_lib_target(
};

let mut target = Target::lib_target(name_or_panic(lib), crate_types, path, edition);
configure(lib, &mut target)?;
configure(lib, &mut target, TARGET_KIND_HUMAN_LIB, warnings)?;
target.set_name_inferred(original_lib.map_or(true, |v| v.name.is_none()));
Ok(Some(target))
}
Expand Down Expand Up @@ -348,6 +352,7 @@ fn to_bin_targets(
bins: &[TomlBinTarget],
package_root: &Path,
edition: Edition,
warnings: &mut Vec<String>,
) -> CargoResult<Vec<Target>> {
// This loop performs basic checks on each of the TomlTarget in `bins`.
for bin in bins {
Expand All @@ -371,7 +376,7 @@ fn to_bin_targets(
edition,
);

configure(bin, &mut target)?;
configure(bin, &mut target, TARGET_KIND_HUMAN_BIN, warnings)?;
result.push(target);
}
Ok(result)
Expand Down Expand Up @@ -430,6 +435,7 @@ fn to_example_targets(
targets: &[TomlExampleTarget],
package_root: &Path,
edition: Edition,
warnings: &mut Vec<String>,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, TARGET_KIND_EXAMPLE)?;

Expand All @@ -448,7 +454,7 @@ fn to_example_targets(
toml.required_features.clone(),
edition,
);
configure(&toml, &mut target)?;
configure(&toml, &mut target, TARGET_KIND_HUMAN_EXAMPLE, warnings)?;
result.push(target);
}

Expand Down Expand Up @@ -487,6 +493,7 @@ fn to_test_targets(
targets: &[TomlTestTarget],
package_root: &Path,
edition: Edition,
warnings: &mut Vec<String>,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, TARGET_KIND_TEST)?;

Expand All @@ -499,7 +506,7 @@ fn to_test_targets(
toml.required_features.clone(),
edition,
);
configure(&toml, &mut target)?;
configure(&toml, &mut target, TARGET_KIND_HUMAN_TEST, warnings)?;
result.push(target);
}
Ok(result)
Expand Down Expand Up @@ -554,6 +561,7 @@ fn to_bench_targets(
targets: &[TomlBenchTarget],
package_root: &Path,
edition: Edition,
warnings: &mut Vec<String>,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, TARGET_KIND_BENCH)?;

Expand All @@ -566,7 +574,7 @@ fn to_bench_targets(
toml.required_features.clone(),
edition,
);
configure(&toml, &mut target)?;
configure(&toml, &mut target, TARGET_KIND_HUMAN_BENCH, warnings)?;
result.push(target);
}

Expand Down Expand Up @@ -892,7 +900,12 @@ fn validate_unique_names(targets: &[TomlTarget], target_kind: &str) -> CargoResu
Ok(())
}

fn configure(toml: &TomlTarget, target: &mut Target) -> CargoResult<()> {
fn configure(
toml: &TomlTarget,
target: &mut Target,
target_kind_human: &str,
warnings: &mut Vec<String>,
) -> CargoResult<()> {
let t2 = target.clone();
target
.set_tested(toml.test.unwrap_or_else(|| t2.tested()))
Expand All @@ -909,6 +922,10 @@ fn configure(toml: &TomlTarget, target: &mut Target) -> CargoResult<()> {
.set_for_host(toml.proc_macro().unwrap_or_else(|| t2.for_host()));

if let Some(edition) = toml.edition.clone() {
let name = target.name();
warnings.push(format!(
"`edition` is set on {target_kind_human} `{name}` which is deprecated"
));
target.set_edition(
edition
.parse()
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5404,6 +5404,7 @@ fn target_edition() {

p.cargo("build -v")
.with_stderr_data(str![[r#"
[WARNING] `edition` is set on library `foo` which is deprecated
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `rustc [..]--edition=2018 [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,13 +2255,15 @@ fn edition_change_invalidates() {
);
p.cargo("build")
.with_stderr_data(str![[r#"
[WARNING] `edition` is set on library `foo` which is deprecated
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
p.cargo("build -v")
.with_stderr_data(str![[r#"
[WARNING] `edition` is set on library `foo` which is deprecated
[FRESH] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

Expand Down
2 changes: 2 additions & 0 deletions tests/testsuite/freshness_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,7 @@ fn edition_change_invalidates() {
p.cargo("build -Zchecksum-freshness")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.with_stderr_data(str![[r#"
[WARNING] `edition` is set on library `foo` which is deprecated
[COMPILING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

Expand All @@ -2038,6 +2039,7 @@ fn edition_change_invalidates() {
p.cargo("build -Zchecksum-freshness -v")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.with_stderr_data(str![[r#"
[WARNING] `edition` is set on library `foo` which is deprecated
[FRESH] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

Expand Down