Skip to content

Commit d29ad67

Browse files
committed
Auto merge of #13783 - epage:underscore, r=ehuss
fix(toml): Report `_` fied variants (e.g. `dev_dependencies`) as deprecated ### What does this PR try to resolve? This is prep for removing them in the 2024 Edition and is part of rust-lang/rust#123754 and #13629 This doesn't include 2024 Edition work because there is a risk of conflict with other work going on these areas. This changes us from - When using `-` and `_` variants: deprecated, will error some time - Otherwise, nothing To - When using `-` and `_` variants: unused field warning - When using only `_`: deprecation, will be removed in 2024 I decided to model this as an unused field warning because that is what this is and that is how any other use of `_` works. We might hard error during a transition period but I'd eventually want these to just make the code act like anything else in the end. ### How should we test and review this PR? ### Additional information
2 parents 14b46ec + d1f0247 commit d29ad67

File tree

11 files changed

+704
-364
lines changed

11 files changed

+704
-364
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,14 @@ fn inner_dependency_inherit_with<'a>(
904904
this could become a hard error in the future"
905905
))
906906
}
907-
if dependency.default_features.is_some() && dependency.default_features2.is_some() {
908-
warn_on_deprecated("default-features", name, "dependency", warnings);
909-
}
907+
deprecated_underscore(
908+
&dependency.default_features2,
909+
&dependency.default_features,
910+
"default-features",
911+
name,
912+
"dependency",
913+
warnings,
914+
);
910915
inherit()?.get_dependency(name, package_root).map(|d| {
911916
match d {
912917
manifest::TomlDependency::Simple(s) => {
@@ -1157,18 +1162,28 @@ fn to_real_manifest(
11571162
}
11581163

11591164
validate_dependencies(original_toml.dependencies.as_ref(), None, None, warnings)?;
1160-
if original_toml.dev_dependencies.is_some() && original_toml.dev_dependencies2.is_some() {
1161-
warn_on_deprecated("dev-dependencies", package_name, "package", warnings);
1162-
}
1165+
deprecated_underscore(
1166+
&original_toml.dev_dependencies2,
1167+
&original_toml.dev_dependencies,
1168+
"dev-dependencies",
1169+
package_name,
1170+
"package",
1171+
warnings,
1172+
);
11631173
validate_dependencies(
11641174
original_toml.dev_dependencies(),
11651175
None,
11661176
Some(DepKind::Development),
11671177
warnings,
11681178
)?;
1169-
if original_toml.build_dependencies.is_some() && original_toml.build_dependencies2.is_some() {
1170-
warn_on_deprecated("build-dependencies", package_name, "package", warnings);
1171-
}
1179+
deprecated_underscore(
1180+
&original_toml.build_dependencies2,
1181+
&original_toml.build_dependencies,
1182+
"build-dependencies",
1183+
package_name,
1184+
"package",
1185+
warnings,
1186+
);
11721187
validate_dependencies(
11731188
original_toml.build_dependencies(),
11741189
None,
@@ -1185,18 +1200,28 @@ fn to_real_manifest(
11851200
None,
11861201
warnings,
11871202
)?;
1188-
if platform.build_dependencies.is_some() && platform.build_dependencies2.is_some() {
1189-
warn_on_deprecated("build-dependencies", name, "platform target", warnings);
1190-
}
1203+
deprecated_underscore(
1204+
&platform.build_dependencies2,
1205+
&platform.build_dependencies,
1206+
"build-dependencies",
1207+
name,
1208+
"platform target",
1209+
warnings,
1210+
);
11911211
validate_dependencies(
11921212
platform.build_dependencies(),
11931213
platform_kind.as_ref(),
11941214
Some(DepKind::Build),
11951215
warnings,
11961216
)?;
1197-
if platform.dev_dependencies.is_some() && platform.dev_dependencies2.is_some() {
1198-
warn_on_deprecated("dev-dependencies", name, "platform target", warnings);
1199-
}
1217+
deprecated_underscore(
1218+
&platform.dev_dependencies2,
1219+
&platform.dev_dependencies,
1220+
"dev-dependencies",
1221+
name,
1222+
"platform target",
1223+
warnings,
1224+
);
12001225
validate_dependencies(
12011226
platform.dev_dependencies(),
12021227
platform_kind.as_ref(),
@@ -1885,14 +1910,14 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
18851910

18861911
let version = orig.version.as_deref();
18871912
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
1888-
if orig.default_features.is_some() && orig.default_features2.is_some() {
1889-
warn_on_deprecated(
1890-
"default-features",
1891-
name_in_toml,
1892-
"dependency",
1893-
manifest_ctx.warnings,
1894-
);
1895-
}
1913+
deprecated_underscore(
1914+
&orig.default_features2,
1915+
&orig.default_features,
1916+
"default-features",
1917+
name_in_toml,
1918+
"dependency",
1919+
manifest_ctx.warnings,
1920+
);
18961921
dep.set_features(orig.features.iter().flatten())
18971922
.set_default_features(orig.default_features().unwrap_or(true))
18981923
.set_optional(orig.optional.unwrap_or(false))
@@ -2304,12 +2329,25 @@ fn emit_diagnostic(
23042329
}
23052330

23062331
/// Warn about paths that have been deprecated and may conflict.
2307-
fn warn_on_deprecated(new_path: &str, name: &str, kind: &str, warnings: &mut Vec<String>) {
2308-
let old_path = new_path.replace("-", "_");
2309-
warnings.push(format!(
2310-
"conflicting between `{new_path}` and `{old_path}` in the `{name}` {kind}.\n
2311-
`{old_path}` is ignored and not recommended for use in the future"
2312-
))
2332+
fn deprecated_underscore<T>(
2333+
old: &Option<T>,
2334+
new: &Option<T>,
2335+
new_path: &str,
2336+
name: &str,
2337+
kind: &str,
2338+
warnings: &mut Vec<String>,
2339+
) {
2340+
if old.is_some() && new.is_some() {
2341+
let old_path = new_path.replace("-", "_");
2342+
warnings.push(format!(
2343+
"unused manifest key `{old_path}` in the `{name}` {kind}"
2344+
))
2345+
} else if old.is_some() {
2346+
let old_path = new_path.replace("-", "_");
2347+
warnings.push(format!(
2348+
"`{old_path}` is deprecated in favor of `{new_path}` and will not work in the 2024 edition\n(in the `{name}` {kind})"
2349+
))
2350+
}
23132351
}
23142352

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

src/cargo/util/toml/targets.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::core::compiler::CrateType;
2525
use crate::core::{Edition, Feature, Features, Target};
2626
use crate::util::errors::CargoResult;
2727
use crate::util::restricted_names;
28-
use crate::util::toml::warn_on_deprecated;
28+
use crate::util::toml::deprecated_underscore;
2929

3030
const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
3131
const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
@@ -1102,23 +1102,23 @@ fn name_or_panic(target: &TomlTarget) -> &str {
11021102
}
11031103

11041104
fn validate_proc_macro(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
1105-
if target.proc_macro.is_some() && target.proc_macro2.is_some() {
1106-
warn_on_deprecated(
1107-
"proc-macro",
1108-
name_or_panic(target),
1109-
format!("{kind} target").as_str(),
1110-
warnings,
1111-
);
1112-
}
1105+
deprecated_underscore(
1106+
&target.proc_macro2,
1107+
&target.proc_macro,
1108+
"proc-macro",
1109+
name_or_panic(target),
1110+
format!("{kind} target").as_str(),
1111+
warnings,
1112+
);
11131113
}
11141114

11151115
fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
1116-
if target.crate_type.is_some() && target.crate_type2.is_some() {
1117-
warn_on_deprecated(
1118-
"crate-type",
1119-
name_or_panic(target),
1120-
format!("{kind} target").as_str(),
1121-
warnings,
1122-
);
1123-
}
1116+
deprecated_underscore(
1117+
&target.crate_type2,
1118+
&target.crate_type,
1119+
"crate-type",
1120+
name_or_panic(target),
1121+
format!("{kind} target").as_str(),
1122+
warnings,
1123+
);
11241124
}

0 commit comments

Comments
 (0)