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
This commit is contained in:
Ed Page 2024-04-25 11:56:32 -05:00
parent b81f94a8e9
commit aecb40baac
3 changed files with 485 additions and 15 deletions

View File

@ -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,
@ -657,8 +661,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();
@ -2323,9 +2328,13 @@ fn deprecated_underscore<T>(
new_path: &str,
name: &str,
kind: &str,
edition: Edition,
warnings: &mut Vec<String>,
) {
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}"
@ -2336,6 +2345,7 @@ fn deprecated_underscore<T>(
"`{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<String>, warnings: &mut Vec<String>) {

View File

@ -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<String>) {
fn validate_proc_macro(
target: &TomlTarget,
kind: &str,
edition: Edition,
warnings: &mut Vec<String>,
) -> 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<String>) {
fn validate_crate_types(
target: &TomlTarget,
kind: &str,
edition: Edition,
warnings: &mut Vec<String>,
) -> CargoResult<()> {
deprecated_underscore(
&target.crate_type2,
&target.crate_type,
"crate-type",
name_or_panic(target),
format!("{kind} target").as_str(),
edition,
warnings,
);
)
}

View File

@ -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()