Auto merge of #13080 - epage:toml, r=weihanglo

refactor(toml): Decouple logic from schema

### What does this PR try to resolve?

This tries to decouple cargo logic from the schemas so we can split the schemas out into a separate crate for #12801

Profile layering was one of the few pieces of logic I kept in the schema, assuming its general enough / decoupled enough from cargo policies.

### How should we test and review this PR?

Each step is broken down into its own commit for easier analysis

### Additional information
This commit is contained in:
bors 2023-12-01 18:48:37 +00:00
commit b97cffe069
7 changed files with 1643 additions and 1681 deletions

View file

@ -168,7 +168,7 @@ pub const SEE_CHANNELS: &str =
/// - Update [`CLI_VALUES`] to include the new edition.
/// - Set [`LATEST_UNSTABLE`] to Some with the new edition.
/// - Add an unstable feature to the [`features!`] macro invocation below for the new edition.
/// - Gate on that new feature in [`TomlManifest::to_real_manifest`].
/// - Gate on that new feature in [`toml::to_real_manifest`].
/// - Update the shell completion files.
/// - Update any failing tests (hopefully there are very few).
/// - Update unstable.md to add a new section for this new edition (see [this example]).
@ -195,7 +195,7 @@ pub const SEE_CHANNELS: &str =
/// [`LATEST_STABLE`]: Edition::LATEST_STABLE
/// [this example]: https://github.com/rust-lang/cargo/blob/3ebb5f15a940810f250b68821149387af583a79e/src/doc/src/reference/unstable.md?plain=1#L1238-L1264
/// [`is_stable`]: Edition::is_stable
/// [`TomlManifest::to_real_manifest`]: crate::util::toml::schema::TomlManifest::to_real_manifest
/// [`toml::to_real_manifest`]: crate::util::toml::to_real_manifest
/// [`features!`]: macro.features.html
#[derive(Clone, Copy, Debug, Hash, PartialOrd, Ord, Eq, PartialEq, Serialize, Deserialize)]
pub enum Edition {

View file

@ -31,6 +31,7 @@ use crate::util::network::http::http_handle_and_timeout;
use crate::util::network::http::HttpTimeout;
use crate::util::network::retry::{Retry, RetryResult};
use crate::util::network::sleep::SleepTracker;
use crate::util::toml::prepare_for_publish;
use crate::util::RustVersion;
use crate::util::{self, internal, Config, Progress, ProgressStyle};
@ -197,10 +198,7 @@ impl Package {
}
pub fn to_registry_toml(&self, ws: &Workspace<'_>) -> CargoResult<String> {
let manifest = self
.manifest()
.original()
.prepare_for_publish(ws, self.root())?;
let manifest = prepare_for_publish(self.manifest().original(), ws, self.root())?;
let toml = toml::to_string_pretty(&manifest)?;
Ok(format!("{}\n{}", MANIFEST_PREAMBLE, toml))
}

View file

@ -32,6 +32,7 @@ use crate::util::toml::schema::TomlTrimPathsValue;
use crate::util::toml::schema::{
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
};
use crate::util::toml::validate_profile;
use crate::util::{closest_msg, config, CargoResult, Config};
use anyhow::{bail, Context as _};
use std::collections::{BTreeMap, HashMap, HashSet};
@ -1235,20 +1236,19 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
return Ok(None);
};
let mut warnings = Vec::new();
profile
.val
.validate(
name,
ws.config().cli_unstable(),
ws.unstable_features(),
&mut warnings,
validate_profile(
&profile.val,
name,
ws.config().cli_unstable(),
ws.unstable_features(),
&mut warnings,
)
.with_context(|| {
format!(
"config profile `{}` is not valid (defined in `{}`)",
name, profile.definition
)
.with_context(|| {
format!(
"config profile `{}` is not valid (defined in `{}`)",
name, profile.definition
)
})?;
})?;
for warning in warnings {
ws.config().shell().warn(warning)?;
}

View file

@ -437,7 +437,8 @@ impl<'cfg> Workspace<'cfg> {
url,
deps.iter()
.map(|(name, dep)| {
dep.to_dependency_split(
crate::util::toml::to_dependency(
dep,
name,
source,
&mut nested_paths,

View file

@ -16,7 +16,7 @@ use crate::sources::PathSource;
use crate::util::cache_lock::CacheLockMode;
use crate::util::config::JobsConfig;
use crate::util::errors::CargoResult;
use crate::util::toml::schema::TomlManifest;
use crate::util::toml::{prepare_for_publish, to_real_manifest};
use crate::util::{self, human_readable_bytes, restricted_names, Config, FileLock};
use crate::{drop_println, ops};
use anyhow::Context as _;
@ -454,14 +454,11 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
let orig_resolve = ops::load_pkg_lockfile(ws)?;
// Convert Package -> TomlManifest -> Manifest -> Package
let toml_manifest = orig_pkg
.manifest()
.original()
.prepare_for_publish(ws, orig_pkg.root())?;
let toml_manifest = prepare_for_publish(orig_pkg.manifest().original(), ws, orig_pkg.root())?;
let package_root = orig_pkg.root();
let source_id = orig_pkg.package_id().source_id();
let (manifest, _nested_paths) =
TomlManifest::to_real_manifest(toml_manifest, false, source_id, package_root, config)?;
to_real_manifest(toml_manifest, false, source_id, package_root, config)?;
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());
let max_rust_version = new_pkg.rust_version().cloned();

File diff suppressed because it is too large Load diff

View file

@ -670,6 +670,98 @@ pub struct TomlProfile {
pub trim_paths: Option<TomlTrimPaths>,
}
impl TomlProfile {
/// Overwrite self's values with the given profile.
pub fn merge(&mut self, profile: &Self) {
if let Some(v) = &profile.opt_level {
self.opt_level = Some(v.clone());
}
if let Some(v) = &profile.lto {
self.lto = Some(v.clone());
}
if let Some(v) = &profile.codegen_backend {
self.codegen_backend = Some(v.clone());
}
if let Some(v) = profile.codegen_units {
self.codegen_units = Some(v);
}
if let Some(v) = profile.debug {
self.debug = Some(v);
}
if let Some(v) = profile.debug_assertions {
self.debug_assertions = Some(v);
}
if let Some(v) = &profile.split_debuginfo {
self.split_debuginfo = Some(v.clone());
}
if let Some(v) = profile.rpath {
self.rpath = Some(v);
}
if let Some(v) = &profile.panic {
self.panic = Some(v.clone());
}
if let Some(v) = profile.overflow_checks {
self.overflow_checks = Some(v);
}
if let Some(v) = profile.incremental {
self.incremental = Some(v);
}
if let Some(v) = &profile.rustflags {
self.rustflags = Some(v.clone());
}
if let Some(other_package) = &profile.package {
match &mut self.package {
Some(self_package) => {
for (spec, other_pkg_profile) in other_package {
match self_package.get_mut(spec) {
Some(p) => p.merge(other_pkg_profile),
None => {
self_package.insert(spec.clone(), other_pkg_profile.clone());
}
}
}
}
None => self.package = Some(other_package.clone()),
}
}
if let Some(other_bo) = &profile.build_override {
match &mut self.build_override {
Some(self_bo) => self_bo.merge(other_bo),
None => self.build_override = Some(other_bo.clone()),
}
}
if let Some(v) = &profile.inherits {
self.inherits = Some(v.clone());
}
if let Some(v) = &profile.dir_name {
self.dir_name = Some(v.clone());
}
if let Some(v) = &profile.strip {
self.strip = Some(v.clone());
}
if let Some(v) = &profile.trim_paths {
self.trim_paths = Some(v.clone())
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub enum ProfilePackageSpec {
Spec(PackageIdSpec),