Skip to content

Commit b60a155

Browse files
committed
Auto merge of #13804 - epage:underscore, r=weihanglo
fix(toml): Remove underscore field support in 2024 ### What does this PR try to resolve? This is part of the 2024 Edition and is part of rust-lang/rust#123754 and #13629 ### How should we test and review this PR? ### Additional information
2 parents 1dadee6 + 8ab7683 commit b60a155

File tree

5 files changed

+740
-63
lines changed

5 files changed

+740
-63
lines changed

src/cargo/ops/fix.rs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,42 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
254254
let mut fixes = 0;
255255

256256
let root = document.as_table_mut();
257+
258+
if let Some(workspace) = root
259+
.get_mut("workspace")
260+
.and_then(|t| t.as_table_like_mut())
261+
{
262+
// strictly speaking, the edition doesn't apply to this table but it should be safe
263+
// enough
264+
fixes += rename_dep_fields_2024(workspace, "dependencies");
265+
}
266+
257267
fixes += add_feature_for_unused_deps(pkg, root);
258-
if rename_table(root, "project", "package") {
259-
fixes += 1;
268+
fixes += rename_table(root, "project", "package");
269+
if let Some(target) = root.get_mut("lib").and_then(|t| t.as_table_like_mut()) {
270+
fixes += rename_target_fields_2024(target);
271+
}
272+
fixes += rename_array_of_target_fields_2024(root, "bin");
273+
fixes += rename_array_of_target_fields_2024(root, "example");
274+
fixes += rename_array_of_target_fields_2024(root, "test");
275+
fixes += rename_array_of_target_fields_2024(root, "bench");
276+
fixes += rename_dep_fields_2024(root, "dependencies");
277+
fixes += rename_table(root, "dev_dependencies", "dev-dependencies");
278+
fixes += rename_dep_fields_2024(root, "dev-dependencies");
279+
fixes += rename_table(root, "build_dependencies", "build-dependencies");
280+
fixes += rename_dep_fields_2024(root, "build-dependencies");
281+
for target in root
282+
.get_mut("target")
283+
.and_then(|t| t.as_table_like_mut())
284+
.iter_mut()
285+
.flat_map(|t| t.iter_mut())
286+
.filter_map(|(_k, t)| t.as_table_like_mut())
287+
{
288+
fixes += rename_dep_fields_2024(target, "dependencies");
289+
fixes += rename_table(target, "dev_dependencies", "dev-dependencies");
290+
fixes += rename_dep_fields_2024(target, "dev-dependencies");
291+
fixes += rename_table(target, "build_dependencies", "build-dependencies");
292+
fixes += rename_dep_fields_2024(target, "build-dependencies");
260293
}
261294

262295
if 0 < fixes {
@@ -274,9 +307,43 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
274307
Ok(())
275308
}
276309

277-
fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> bool {
310+
fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str) -> usize {
311+
let mut fixes = 0;
312+
for target in parent
313+
.get_mut(dep_kind)
314+
.and_then(|t| t.as_table_like_mut())
315+
.iter_mut()
316+
.flat_map(|t| t.iter_mut())
317+
.filter_map(|(_k, t)| t.as_table_like_mut())
318+
{
319+
fixes += rename_table(target, "default_features", "default-features");
320+
}
321+
fixes
322+
}
323+
324+
fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize {
325+
let mut fixes = 0;
326+
for target in root
327+
.get_mut(kind)
328+
.and_then(|t| t.as_array_of_tables_mut())
329+
.iter_mut()
330+
.flat_map(|t| t.iter_mut())
331+
{
332+
fixes += rename_target_fields_2024(target);
333+
}
334+
fixes
335+
}
336+
337+
fn rename_target_fields_2024(target: &mut dyn toml_edit::TableLike) -> usize {
338+
let mut fixes = 0;
339+
fixes += rename_table(target, "crate_type", "crate-type");
340+
fixes += rename_table(target, "proc_macro", "proc-macro");
341+
fixes
342+
}
343+
344+
fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> usize {
278345
let Some(old_key) = parent.key(old).cloned() else {
279-
return false;
346+
return 0;
280347
};
281348

282349
let project = parent.remove(old).expect("returned early");
@@ -286,7 +353,7 @@ fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) ->
286353
*new_key.dotted_decor_mut() = old_key.dotted_decor().clone();
287354
*new_key.leaf_decor_mut() = old_key.leaf_decor().clone();
288355
}
289-
true
356+
1
290357
}
291358

292359
fn add_feature_for_unused_deps(pkg: &Package, parent: &mut dyn toml_edit::TableLike) -> usize {

src/cargo/util/toml/mod.rs

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ fn resolve_toml(
306306
};
307307

308308
if let Some(original_package) = original_toml.package() {
309+
let package_name = &original_package.name;
310+
309311
let resolved_package =
310312
resolve_package_toml(original_package, features, package_root, &inherit)?;
311313
let edition = resolved_package
@@ -341,6 +343,15 @@ fn resolve_toml(
341343
package_root,
342344
warnings,
343345
)?;
346+
deprecated_underscore(
347+
&original_toml.dev_dependencies2,
348+
&original_toml.dev_dependencies,
349+
"dev-dependencies",
350+
package_name,
351+
"package",
352+
edition,
353+
warnings,
354+
)?;
344355
resolved_toml.dev_dependencies = resolve_dependencies(
345356
gctx,
346357
edition,
@@ -352,6 +363,15 @@ fn resolve_toml(
352363
package_root,
353364
warnings,
354365
)?;
366+
deprecated_underscore(
367+
&original_toml.build_dependencies2,
368+
&original_toml.build_dependencies,
369+
"build-dependencies",
370+
package_name,
371+
"package",
372+
edition,
373+
warnings,
374+
)?;
355375
resolved_toml.build_dependencies = resolve_dependencies(
356376
gctx,
357377
edition,
@@ -376,6 +396,15 @@ fn resolve_toml(
376396
package_root,
377397
warnings,
378398
)?;
399+
deprecated_underscore(
400+
&platform.dev_dependencies2,
401+
&platform.dev_dependencies,
402+
"dev-dependencies",
403+
name,
404+
"platform target",
405+
edition,
406+
warnings,
407+
)?;
379408
let resolved_dev_dependencies = resolve_dependencies(
380409
gctx,
381410
edition,
@@ -387,6 +416,15 @@ fn resolve_toml(
387416
package_root,
388417
warnings,
389418
)?;
419+
deprecated_underscore(
420+
&platform.build_dependencies2,
421+
&platform.build_dependencies,
422+
"build-dependencies",
423+
name,
424+
"platform target",
425+
edition,
426+
warnings,
427+
)?;
390428
let resolved_build_dependencies = resolve_dependencies(
391429
gctx,
392430
edition,
@@ -617,6 +655,15 @@ fn resolve_dependencies<'a>(
617655
let mut resolved =
618656
dependency_inherit_with(v.clone(), name_in_toml, inherit, package_root, warnings)?;
619657
if let manifest::TomlDependency::Detailed(ref mut d) = resolved {
658+
deprecated_underscore(
659+
&d.default_features2,
660+
&d.default_features,
661+
"default-features",
662+
name_in_toml,
663+
"dependency",
664+
edition,
665+
warnings,
666+
)?;
620667
if d.public.is_some() {
621668
let public_feature = features.require(Feature::public_dependency());
622669
let with_public_feature = public_feature.is_ok();
@@ -1146,28 +1193,12 @@ fn to_real_manifest(
11461193
}
11471194

11481195
validate_dependencies(original_toml.dependencies.as_ref(), None, None, warnings)?;
1149-
deprecated_underscore(
1150-
&original_toml.dev_dependencies2,
1151-
&original_toml.dev_dependencies,
1152-
"dev-dependencies",
1153-
package_name,
1154-
"package",
1155-
warnings,
1156-
);
11571196
validate_dependencies(
11581197
original_toml.dev_dependencies(),
11591198
None,
11601199
Some(DepKind::Development),
11611200
warnings,
11621201
)?;
1163-
deprecated_underscore(
1164-
&original_toml.build_dependencies2,
1165-
&original_toml.build_dependencies,
1166-
"build-dependencies",
1167-
package_name,
1168-
"package",
1169-
warnings,
1170-
);
11711202
validate_dependencies(
11721203
original_toml.build_dependencies(),
11731204
None,
@@ -1184,28 +1215,12 @@ fn to_real_manifest(
11841215
None,
11851216
warnings,
11861217
)?;
1187-
deprecated_underscore(
1188-
&platform.build_dependencies2,
1189-
&platform.build_dependencies,
1190-
"build-dependencies",
1191-
name,
1192-
"platform target",
1193-
warnings,
1194-
);
11951218
validate_dependencies(
11961219
platform.build_dependencies(),
11971220
platform_kind.as_ref(),
11981221
Some(DepKind::Build),
11991222
warnings,
12001223
)?;
1201-
deprecated_underscore(
1202-
&platform.dev_dependencies2,
1203-
&platform.dev_dependencies,
1204-
"dev-dependencies",
1205-
name,
1206-
"platform target",
1207-
warnings,
1208-
);
12091224
validate_dependencies(
12101225
platform.dev_dependencies(),
12111226
platform_kind.as_ref(),
@@ -1811,14 +1826,6 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
18111826

18121827
let version = orig.version.as_deref();
18131828
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
1814-
deprecated_underscore(
1815-
&orig.default_features2,
1816-
&orig.default_features,
1817-
"default-features",
1818-
name_in_toml,
1819-
"dependency",
1820-
manifest_ctx.warnings,
1821-
);
18221829
dep.set_features(orig.features.iter().flatten())
18231830
.set_default_features(orig.default_features().unwrap_or(true))
18241831
.set_optional(orig.optional.unwrap_or(false))
@@ -2321,19 +2328,22 @@ fn deprecated_underscore<T>(
23212328
new_path: &str,
23222329
name: &str,
23232330
kind: &str,
2331+
edition: Edition,
23242332
warnings: &mut Vec<String>,
2325-
) {
2326-
if old.is_some() && new.is_some() {
2327-
let old_path = new_path.replace("-", "_");
2333+
) -> CargoResult<()> {
2334+
let old_path = new_path.replace("-", "_");
2335+
if old.is_some() && Edition::Edition2024 <= edition {
2336+
anyhow::bail!("`{old_path}` is unsupported as of the 2024 edition; instead use `{new_path}`\n(in the `{name}` {kind})");
2337+
} else if old.is_some() && new.is_some() {
23282338
warnings.push(format!(
23292339
"`{old_path}` is redundant with `{new_path}`, preferring `{new_path}` in the `{name}` {kind}"
23302340
))
23312341
} else if old.is_some() {
2332-
let old_path = new_path.replace("-", "_");
23332342
warnings.push(format!(
23342343
"`{old_path}` is deprecated in favor of `{new_path}` and will not work in the 2024 edition\n(in the `{name}` {kind})"
23352344
))
23362345
}
2346+
Ok(())
23372347
}
23382348

23392349
fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {

src/cargo/util/toml/targets.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ pub(super) fn to_targets(
9797
warnings,
9898
errors,
9999
)?;
100-
targets.extend(to_example_targets(
101-
&toml_examples,
102-
package_root,
103-
edition,
104-
warnings,
105-
)?);
100+
targets.extend(to_example_targets(&toml_examples, package_root, edition)?);
106101

107102
let toml_tests = resolve_tests(
108103
resolved_toml.test.as_ref(),
@@ -183,6 +178,10 @@ fn resolve_lib(
183178
// Check early to improve error messages
184179
validate_lib_name(&lib, warnings)?;
185180

181+
// Checking the original lib
182+
validate_proc_macro(&lib, "library", edition, warnings)?;
183+
validate_crate_types(&lib, "library", edition, warnings)?;
184+
186185
if lib.path.is_none() {
187186
if let Some(inferred) = inferred {
188187
lib.path = Some(PathValue(inferred));
@@ -218,8 +217,6 @@ fn to_lib_target(
218217
let Some(lib) = resolved_lib else {
219218
return Ok(None);
220219
};
221-
validate_proc_macro(lib, "library", warnings);
222-
validate_crate_types(lib, "library", warnings);
223220

224221
let path = lib.path.as_ref().expect("previously resolved");
225222
let path = package_root.join(&path.0);
@@ -442,14 +439,12 @@ fn to_example_targets(
442439
targets: &[TomlExampleTarget],
443440
package_root: &Path,
444441
edition: Edition,
445-
warnings: &mut Vec<String>,
446442
) -> CargoResult<Vec<Target>> {
447443
validate_unique_names(&targets, "example")?;
448444

449445
let mut result = Vec::new();
450446
for toml in targets {
451447
let path = package_root.join(&toml.path.as_ref().expect("previously resolved").0);
452-
validate_crate_types(&toml, "example", warnings);
453448
let crate_types = match toml.crate_types() {
454449
Some(kinds) => kinds.iter().map(|s| s.into()).collect(),
455450
None => Vec::new(),
@@ -637,6 +632,8 @@ fn resolve_targets_with_legacy_path(
637632

638633
for target in &toml_targets {
639634
validate_target_name(target, target_kind_human, target_kind, warnings)?;
635+
validate_proc_macro(target, target_kind_human, edition, warnings)?;
636+
validate_crate_types(target, target_kind_human, edition, warnings)?;
640637
}
641638

642639
let mut result = Vec::new();
@@ -1101,24 +1098,36 @@ fn name_or_panic(target: &TomlTarget) -> &str {
11011098
.unwrap_or_else(|| panic!("target name is required"))
11021099
}
11031100

1104-
fn validate_proc_macro(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
1101+
fn validate_proc_macro(
1102+
target: &TomlTarget,
1103+
kind: &str,
1104+
edition: Edition,
1105+
warnings: &mut Vec<String>,
1106+
) -> CargoResult<()> {
11051107
deprecated_underscore(
11061108
&target.proc_macro2,
11071109
&target.proc_macro,
11081110
"proc-macro",
11091111
name_or_panic(target),
11101112
format!("{kind} target").as_str(),
1113+
edition,
11111114
warnings,
1112-
);
1115+
)
11131116
}
11141117

1115-
fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
1118+
fn validate_crate_types(
1119+
target: &TomlTarget,
1120+
kind: &str,
1121+
edition: Edition,
1122+
warnings: &mut Vec<String>,
1123+
) -> CargoResult<()> {
11161124
deprecated_underscore(
11171125
&target.crate_type2,
11181126
&target.crate_type,
11191127
"crate-type",
11201128
name_or_panic(target),
11211129
format!("{kind} target").as_str(),
1130+
edition,
11221131
warnings,
1123-
);
1132+
)
11241133
}

0 commit comments

Comments
 (0)