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): Report _ fied variants (e.g. dev_dependencies) as deprecated #13783

Merged
merged 10 commits into from
Apr 22, 2024
96 changes: 67 additions & 29 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,14 @@ fn inner_dependency_inherit_with<'a>(
this could become a hard error in the future"
))
}
if dependency.default_features.is_some() && dependency.default_features2.is_some() {
warn_on_deprecated("default-features", name, "dependency", warnings);
}
deprecated_underscore(
&dependency.default_features2,
&dependency.default_features,
"default-features",
name,
"dependency",
warnings,
);
inherit()?.get_dependency(name, package_root).map(|d| {
match d {
manifest::TomlDependency::Simple(s) => {
Expand Down Expand Up @@ -1157,18 +1162,28 @@ fn to_real_manifest(
}

validate_dependencies(original_toml.dependencies.as_ref(), None, None, warnings)?;
if original_toml.dev_dependencies.is_some() && original_toml.dev_dependencies2.is_some() {
warn_on_deprecated("dev-dependencies", package_name, "package", warnings);
}
deprecated_underscore(
&original_toml.dev_dependencies2,
&original_toml.dev_dependencies,
"dev-dependencies",
package_name,
"package",
warnings,
);
validate_dependencies(
original_toml.dev_dependencies(),
None,
Some(DepKind::Development),
warnings,
)?;
if original_toml.build_dependencies.is_some() && original_toml.build_dependencies2.is_some() {
warn_on_deprecated("build-dependencies", package_name, "package", warnings);
}
deprecated_underscore(
&original_toml.build_dependencies2,
&original_toml.build_dependencies,
"build-dependencies",
package_name,
"package",
warnings,
);
validate_dependencies(
original_toml.build_dependencies(),
None,
Expand All @@ -1185,18 +1200,28 @@ fn to_real_manifest(
None,
warnings,
)?;
if platform.build_dependencies.is_some() && platform.build_dependencies2.is_some() {
warn_on_deprecated("build-dependencies", name, "platform target", warnings);
}
deprecated_underscore(
&platform.build_dependencies2,
&platform.build_dependencies,
"build-dependencies",
name,
"platform target",
warnings,
);
validate_dependencies(
platform.build_dependencies(),
platform_kind.as_ref(),
Some(DepKind::Build),
warnings,
)?;
if platform.dev_dependencies.is_some() && platform.dev_dependencies2.is_some() {
warn_on_deprecated("dev-dependencies", name, "platform target", warnings);
}
deprecated_underscore(
&platform.dev_dependencies2,
&platform.dev_dependencies,
"dev-dependencies",
name,
"platform target",
warnings,
);
validate_dependencies(
platform.dev_dependencies(),
platform_kind.as_ref(),
Expand Down Expand Up @@ -1885,14 +1910,14 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(

let version = orig.version.as_deref();
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
if orig.default_features.is_some() && orig.default_features2.is_some() {
warn_on_deprecated(
"default-features",
name_in_toml,
"dependency",
manifest_ctx.warnings,
);
}
deprecated_underscore(
&orig.default_features2,
&orig.default_features,
"default-features",
name_in_toml,
"dependency",
manifest_ctx.warnings,
);
dep.set_features(orig.features.iter().flatten())
.set_default_features(orig.default_features().unwrap_or(true))
.set_optional(orig.optional.unwrap_or(false))
Expand Down Expand Up @@ -2304,12 +2329,25 @@ fn emit_diagnostic(
}

/// Warn about paths that have been deprecated and may conflict.
fn warn_on_deprecated(new_path: &str, name: &str, kind: &str, warnings: &mut Vec<String>) {
let old_path = new_path.replace("-", "_");
warnings.push(format!(
"conflicting between `{new_path}` and `{old_path}` in the `{name}` {kind}.\n
`{old_path}` is ignored and not recommended for use in the future"
))
fn deprecated_underscore<T>(
old: &Option<T>,
new: &Option<T>,
new_path: &str,
name: &str,
kind: &str,
warnings: &mut Vec<String>,
) {
if old.is_some() && new.is_some() {
let old_path = new_path.replace("-", "_");
warnings.push(format!(
"unused manifest key `{old_path}` in the `{name}` {kind}"
))
} else if old.is_some() {
let old_path = new_path.replace("-", "_");
warnings.push(format!(
"`{old_path}` is deprecated in favor of `{new_path}` and will not work in the 2024 edition\n(in the `{name}` {kind})"
))
}
}

fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {
Expand Down
34 changes: 17 additions & 17 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::core::compiler::CrateType;
use crate::core::{Edition, Feature, Features, Target};
use crate::util::errors::CargoResult;
use crate::util::restricted_names;
use crate::util::toml::warn_on_deprecated;
use crate::util::toml::deprecated_underscore;

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

fn validate_proc_macro(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
if target.proc_macro.is_some() && target.proc_macro2.is_some() {
warn_on_deprecated(
"proc-macro",
name_or_panic(target),
format!("{kind} target").as_str(),
warnings,
);
}
deprecated_underscore(
&target.proc_macro2,
&target.proc_macro,
"proc-macro",
name_or_panic(target),
format!("{kind} target").as_str(),
warnings,
);
}

fn validate_crate_types(target: &TomlTarget, kind: &str, warnings: &mut Vec<String>) {
if target.crate_type.is_some() && target.crate_type2.is_some() {
warn_on_deprecated(
"crate-type",
name_or_panic(target),
format!("{kind} target").as_str(),
warnings,
);
}
deprecated_underscore(
&target.crate_type2,
&target.crate_type,
"crate-type",
name_or_panic(target),
format!("{kind} target").as_str(),
warnings,
);
}
Loading