From 60616087c0a704d2854ac8fd64991afe946d86c8 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Apr 2024 11:56:32 -0500 Subject: [PATCH] fix(toml): Remove underscore field support in 2024 This is part of the 2024 Edition and is part of rust-lang/rust#123754 and #13629 --- src/cargo/util/toml/mod.rs | 24 +- src/cargo/util/toml/targets.rs | 28 ++- tests/testsuite/bad_config.rs | 448 +++++++++++++++++++++++++++++++++ 3 files changed, 485 insertions(+), 15 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index ddddd8011fb0..66d6ecae1050 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -349,8 +349,9 @@ fn resolve_toml( "dev-dependencies", package_name, "package", + edition, warnings, - ); + )?; resolved_toml.dev_dependencies = resolve_dependencies( gctx, edition, @@ -368,8 +369,9 @@ fn resolve_toml( "build-dependencies", package_name, "package", + edition, warnings, - ); + )?; resolved_toml.build_dependencies = resolve_dependencies( gctx, edition, @@ -400,8 +402,9 @@ fn resolve_toml( "dev-dependencies", name, "platform target", + edition, warnings, - ); + )?; let resolved_dev_dependencies = resolve_dependencies( gctx, edition, @@ -419,8 +422,9 @@ fn resolve_toml( "build-dependencies", name, "platform target", + edition, warnings, - ); + )?; let resolved_build_dependencies = resolve_dependencies( gctx, edition, @@ -665,8 +669,9 @@ fn resolve_dependencies<'a>( "default-features", name_in_toml, "dependency", + edition, warnings, - ); + )?; if d.public.is_some() { let public_feature = features.require(Feature::public_dependency()); let with_public_feature = public_feature.is_ok(); @@ -2329,9 +2334,13 @@ fn deprecated_underscore( new_path: &str, name: &str, kind: &str, + edition: Edition, warnings: &mut Vec, -) { - if old.is_some() && new.is_some() { +) -> CargoResult<()> { + if old.is_some() && Edition::Edition2024 <= edition { + let old_path = new_path.replace("-", "_"); + anyhow::bail!("`{old_path}` is unsupported as of the 2024 edition; instead use `{new_path}`\n(in the `{name}` {kind})"); + } else if old.is_some() && new.is_some() { let old_path = new_path.replace("-", "_"); warnings.push(format!( "`{old_path}` is redundant with `{new_path}`, preferring `{new_path}` in the `{name}` {kind}" @@ -2342,6 +2351,7 @@ fn deprecated_underscore( "`{old_path}` is deprecated in favor of `{new_path}` and will not work in the 2024 edition\n(in the `{name}` {kind})" )) } + Ok(()) } fn warn_on_unused(unused: &BTreeSet, warnings: &mut Vec) { diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index 415b8f43e61b..1795cf24752e 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -179,8 +179,8 @@ fn resolve_lib( validate_lib_name(&lib, warnings)?; // Checking the original lib - validate_proc_macro(&lib, "library", warnings); - validate_crate_types(&lib, "library", warnings); + validate_proc_macro(&lib, "library", edition, warnings)?; + validate_crate_types(&lib, "library", edition, warnings)?; if lib.path.is_none() { if let Some(inferred) = inferred { @@ -632,8 +632,8 @@ fn resolve_targets_with_legacy_path( for target in &toml_targets { validate_target_name(target, target_kind_human, target_kind, warnings)?; - validate_proc_macro(target, target_kind_human, warnings); - validate_crate_types(target, target_kind_human, warnings); + validate_proc_macro(target, target_kind_human, edition, warnings)?; + validate_crate_types(target, target_kind_human, edition, warnings)?; } let mut result = Vec::new(); @@ -1098,24 +1098,36 @@ fn name_or_panic(target: &TomlTarget) -> &str { .unwrap_or_else(|| panic!("target name is required")) } -fn validate_proc_macro(target: &TomlTarget, kind: &str, warnings: &mut Vec) { +fn validate_proc_macro( + target: &TomlTarget, + kind: &str, + edition: Edition, + warnings: &mut Vec, +) -> CargoResult<()> { deprecated_underscore( &target.proc_macro2, &target.proc_macro, "proc-macro", name_or_panic(target), format!("{kind} target").as_str(), + edition, warnings, - ); + ) } -fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec) { +fn validate_crate_types( + target: &TomlTarget, + kind: &str, + edition: Edition, + warnings: &mut Vec, +) -> CargoResult<()> { deprecated_underscore( &target.crate_type2, &target.crate_type, "crate-type", name_or_panic(target), format!("{kind} target").as_str(), + edition, warnings, - ); + ) } diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs index a45e654b5fad..07ae1d768976 100644 --- a/tests/testsuite/bad_config.rs +++ b/tests/testsuite/bad_config.rs @@ -841,6 +841,50 @@ fn dev_dependencies2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn dev_dependencies2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.1.0" + edition = "2024" + + [dev_dependencies] + a = {path = "a"} + "#, + ) + .file("src/lib.rs", "") + .file( + "a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + "#, + ) + .file("a/src/lib.rs", "") + .build(); + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `dev_dependencies` is unsupported as of the 2024 edition; instead use `dev-dependencies` + (in the `foo` package) +", + ) + .run(); +} + #[cargo_test] fn dev_dependencies2_conflict() { let p = project() @@ -916,6 +960,50 @@ fn build_dependencies2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn build_dependencies2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.1.0" + edition = "2024" + + [build_dependencies] + a = {path = "a"} + "#, + ) + .file("src/lib.rs", "") + .file( + "a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + edition = "2015" + "#, + ) + .file("a/src/lib.rs", "") + .build(); + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `build_dependencies` is unsupported as of the 2024 edition; instead use `build-dependencies` + (in the `foo` package) +", + ) + .run(); +} + #[cargo_test] fn build_dependencies2_conflict() { let p = project() @@ -983,6 +1071,42 @@ fn lib_crate_type2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn lib_crate_type2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.5.0" + edition = "2024" + authors = ["wycats@example.com"] + + [lib] + name = "foo" + crate_type = ["staticlib", "dylib"] + "#, + ) + .file("src/lib.rs", "pub fn foo() {}") + .build(); + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `crate_type` is unsupported as of the 2024 edition; instead use `crate-type` + (in the `foo` library target) +", + ) + .run(); +} + #[cargo_test] fn lib_crate_type2_conflict() { let p = project() @@ -1060,6 +1184,59 @@ fn examples_crate_type2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn examples_crate_type2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.5.0" + edition = "2024" + authors = ["wycats@example.com"] + + [[example]] + name = "ex" + path = "examples/ex.rs" + crate_type = ["proc_macro"] + [[example]] + name = "goodbye" + path = "examples/ex-goodbye.rs" + crate_type = ["rlib", "staticlib"] + "#, + ) + .file("src/lib.rs", "") + .file( + "examples/ex.rs", + r#" + fn main() { println!("ex"); } + "#, + ) + .file( + "examples/ex-goodbye.rs", + r#" + fn main() { println!("goodbye"); } + "#, + ) + .build(); + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `crate_type` is unsupported as of the 2024 edition; instead use `crate-type` + (in the `ex` example target) +", + ) + .run(); +} + #[cargo_test] fn examples_crate_type2_conflict() { let p = project() @@ -1148,6 +1325,53 @@ fn cargo_platform_build_dependencies2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn cargo_platform_build_dependencies2_2024() { + let host = rustc_host(); + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.5.0" + edition = "2024" + authors = ["wycats@example.com"] + build = "build.rs" + + [target.{host}.build_dependencies] + build = {{ path = "build" }} + "#, + host = host + ), + ) + .file("src/main.rs", "fn main() { }") + .file( + "build.rs", + "extern crate build; fn main() { build::build(); }", + ) + .file("build/Cargo.toml", &basic_manifest("build", "0.5.0")) + .file("build/src/lib.rs", "pub fn build() {}") + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr(format!( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `build_dependencies` is unsupported as of the 2024 edition; instead use `build-dependencies` + (in the `{host}` platform target) +" + )) + .run(); +} + #[cargo_test] fn cargo_platform_build_dependencies2_conflict() { let host = rustc_host(); @@ -1228,6 +1452,52 @@ fn cargo_platform_dev_dependencies2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn cargo_platform_dev_dependencies2_2024() { + let host = rustc_host(); + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.5.0" + edition = "2024" + authors = ["wycats@example.com"] + + [target.{host}.dev_dependencies] + dev = {{ path = "dev" }} + "#, + host = host + ), + ) + .file("src/main.rs", "fn main() { }") + .file( + "tests/foo.rs", + "extern crate dev; #[test] fn foo() { dev::dev() }", + ) + .file("dev/Cargo.toml", &basic_manifest("dev", "0.5.0")) + .file("dev/src/lib.rs", "pub fn dev() {}") + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr(format!( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `dev_dependencies` is unsupported as of the 2024 edition; instead use `dev-dependencies` + (in the `{host}` platform target) +" + )) + .run(); +} + #[cargo_test] fn cargo_platform_dev_dependencies2_conflict() { let host = rustc_host(); @@ -1312,6 +1582,57 @@ fn default_features2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn default_features2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.1.0" + edition = "2024" + authors = [] + + [dependencies] + a = { path = "a", features = ["f1"], default_features = false } + "#, + ) + .file("src/lib.rs", "") + .file( + "a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.1.0" + edition = "2015" + authors = [] + + [features] + default = ["f1"] + f1 = [] + "#, + ) + .file("a/src/lib.rs", "") + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `default_features` is unsupported as of the 2024 edition; instead use `default-features` + (in the `a` dependency) +", + ) + .run(); +} + #[cargo_test] fn default_features2_conflict() { let p = project() @@ -1437,6 +1758,99 @@ warning: [CWD]/workspace_only/Cargo.toml: `default_features` is deprecated in fa .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn workspace_default_features2_2024() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["workspace_only", "dep_workspace_only", "package_only", "dep_package_only"] + + [workspace.dependencies] + dep_workspace_only = { path = "dep_workspace_only", default_features = true } + dep_package_only = { path = "dep_package_only" } + "#, + ) + .file( + "workspace_only/Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "workspace_only" + version = "0.1.0" + edition = "2024" + authors = [] + + [dependencies] + dep_workspace_only.workspace = true + "#, + ) + .file("workspace_only/src/lib.rs", "") + .file( + "dep_workspace_only/Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "dep_workspace_only" + version = "0.1.0" + edition = "2024" + authors = [] + "#, + ) + .file("dep_workspace_only/src/lib.rs", "") + .file( + "package_only/Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "package_only" + version = "0.1.0" + edition = "2024" + authors = [] + + [dependencies] + dep_package_only = { workspace = true, default_features = true } + "#, + ) + .file("package_only/src/lib.rs", "") + .file( + "dep_package_only/Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "dep_package_only" + version = "0.1.0" + edition = "2024" + authors = [] + "#, + ) + .file("dep_package_only/src/lib.rs", "") + .build(); + + p.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to load manifest for workspace member `[CWD]/workspace_only` +referenced by workspace at `[CWD]/Cargo.toml` + +Caused by: + failed to parse manifest at `[CWD]/workspace_only/Cargo.toml` + +Caused by: + `default_features` is unsupported as of the 2024 edition; instead use `default-features` + (in the `dep_workspace_only` dependency) +", + ) + .run(); +} + #[cargo_test] fn proc_macro2() { let foo = project() @@ -1464,6 +1878,40 @@ fn proc_macro2() { .run(); } +#[cargo_test(nightly, reason = "edition2024 is not stable")] +fn proc_macro2_2024() { + let foo = project() + .file( + "Cargo.toml", + r#" + cargo-features = ["edition2024"] + + [package] + name = "foo" + version = "0.1.0" + edition = "2024" + [lib] + proc_macro = true + "#, + ) + .file("src/lib.rs", "") + .build(); + + foo.cargo("check") + .masquerade_as_nightly_cargo(&["edition2024"]) + .with_status(101) + .with_stderr( + "\ +[ERROR] failed to parse manifest at `[CWD]/Cargo.toml` + +Caused by: + `proc_macro` is unsupported as of the 2024 edition; instead use `proc-macro` + (in the `foo` library target) +", + ) + .run(); +} + #[cargo_test] fn proc_macro2_conflict() { let foo = project()