Skip to content
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

fix(toml): Remove underscore field support in 2024 #13804

Merged
merged 6 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
83 changes: 83 additions & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,53 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
let mut fixes = 0;

let root = document.as_table_mut();

if let Some(workspace) = root
.get_mut("workspace")
.and_then(|t| t.as_table_like_mut())
{
// strictly speaking, the edition doesn't apply to this table but it should be safe
// enough
fixes += rename_dep_fields_2024(workspace, "dependencies");
}

fixes += add_feature_for_unused_deps(pkg, root);
if rename_table(root, "project", "package") {
fixes += 1;
}
if let Some(target) = root.get_mut("lib").and_then(|t| t.as_table_like_mut()) {
fixes += rename_target_fields_2024(target);
}
fixes += rename_array_of_target_fields_2024(root, "bin");
fixes += rename_array_of_target_fields_2024(root, "example");
fixes += rename_array_of_target_fields_2024(root, "test");
fixes += rename_array_of_target_fields_2024(root, "bench");
fixes += rename_dep_fields_2024(root, "dependencies");
if rename_table(root, "dev_dependencies", "dev-dependencies") {
fixes += 1;
}
fixes += rename_dep_fields_2024(root, "dev-dependencies");
if rename_table(root, "build_dependencies", "build-dependencies") {
fixes += 1;
}
fixes += rename_dep_fields_2024(root, "build-dependencies");
for target in root
.get_mut("target")
.and_then(|t| t.as_table_like_mut())
.iter_mut()
.flat_map(|t| t.iter_mut())
.filter_map(|(_k, t)| t.as_table_like_mut())
{
fixes += rename_dep_fields_2024(target, "dependencies");
if rename_table(target, "dev_dependencies", "dev-dependencies") {
fixes += 1;
}
fixes += rename_dep_fields_2024(target, "dev-dependencies");
if rename_table(target, "build_dependencies", "build-dependencies") {
fixes += 1;
}
fixes += rename_dep_fields_2024(target, "build-dependencies");
}

if 0 < fixes {
let verb = if fixes == 1 { "fix" } else { "fixes" };
Expand All @@ -274,6 +317,46 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
Ok(())
}

fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str) -> usize {
let mut fixes = 0;
for target in parent
.get_mut(dep_kind)
.and_then(|t| t.as_table_like_mut())
.iter_mut()
.flat_map(|t| t.iter_mut())
.filter_map(|(_k, t)| t.as_table_like_mut())
{
if rename_table(target, "default_features", "default-features") {
fixes += 1;
}
}
fixes
}

fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize {
let mut fixes = 0;
for target in root
.get_mut(kind)
.and_then(|t| t.as_array_of_tables_mut())
.iter_mut()
.flat_map(|t| t.iter_mut())
{
fixes += rename_target_fields_2024(target);
}
fixes
}

fn rename_target_fields_2024(target: &mut dyn toml_edit::TableLike) -> usize {
let mut fixes = 0;
if rename_table(target, "crate_type", "crate-type") {
fixes += 1;
}
if rename_table(target, "proc_macro", "proc-macro") {
fixes += 1;
}
fixes
}

fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> bool {
epage marked this conversation as resolved.
Show resolved Hide resolved
let Some(old_key) = parent.key(old).cloned() else {
return false;
Expand Down
45 changes: 13 additions & 32 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,26 +2129,7 @@ a = {path = "a", default_features = false}
.with_stderr(
"\
[MIGRATING] Cargo.toml from 2021 edition to 2024
[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition
(in the `foo` package)
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
(in the `a` dependency)
[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition
(in the `foo` package)
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
(in the `a` dependency)
[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition
(in the `cfg(any())` platform target)
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
(in the `a` dependency)
[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition
(in the `cfg(any())` platform target)
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
(in the `a` dependency)
[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition
(in the `foo` library target)
[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition
(in the `ex` example target)
[FIXED] Cargo.toml (11 fixes)
Locking 2 packages to latest compatible versions
Checking a v0.0.1 ([CWD]/a)
[CHECKING] foo v0.0.0 ([CWD])
Expand All @@ -2165,7 +2146,7 @@ cargo-features = ["edition2024"]

[workspace.dependencies]
# Before default_features
a = {path = "a", default_features = false} # After default_features value
a = {path = "a", default-features = false} # After default_features value
# After default_features line

[package]
Expand All @@ -2175,40 +2156,40 @@ edition = "2021"
[lib]
name = "foo"
# Before crate_type
crate_type = ["staticlib", "dylib"] # After crate_type value
crate-type = ["staticlib", "dylib"] # After crate_type value
# After crate_type line

[[example]]
name = "ex"
path = "examples/ex.rs"
# Before crate_type
crate_type = ["proc-macro"] # After crate_type value
crate-type = ["proc-macro"] # After crate_type value
# After crate_type line

# Before dev_dependencies
[ dev_dependencies ] # After dev_dependencies header
[ dev-dependencies ] # After dev_dependencies header
# After dev_dependencies line
a = {path = "a", default_features = false}
a = {path = "a", default-features = false}
# After dev_dependencies table

# Before build_dependencies
[ build_dependencies ] # After build_dependencies header
[ build-dependencies ] # After build_dependencies header
# After build_dependencies line
a = {path = "a", default_features = false}
a = {path = "a", default-features = false}
# After build_dependencies table

# Before dev_dependencies
[ target.'cfg(any())'.dev_dependencies ] # After dev_dependencies header
[ target.'cfg(any())'.dev-dependencies ] # After dev_dependencies header
# After dev_dependencies line
a = {path = "a", default_features = false}
a = {path = "a", default-features = false}
# After dev_dependencies table

# Before build_dependencies
[ target.'cfg(any())'.build_dependencies ] # After build_dependencies header
[ target.'cfg(any())'.build-dependencies ] # After build_dependencies header
# After build_dependencies line
a = {path = "a", default_features = false}
a = {path = "a", default-features = false}
# After build_dependencies table
"#
"#,
);
}

Expand Down