diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 790beef9959..f2143a91638 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -188,472 +188,6 @@ fn warn_on_deprecated(new_path: &str, name: &str, kind: &str, warnings: &mut Vec )) } -type TomlLibTarget = TomlTarget; -type TomlBinTarget = TomlTarget; -type TomlExampleTarget = TomlTarget; -type TomlTestTarget = TomlTarget; -type TomlBenchTarget = TomlTarget; - -#[derive(Clone, Debug, Serialize)] -#[serde(untagged)] -pub enum TomlDependency { - /// In the simple format, only a version is specified, eg. - /// `package = ""` - Simple(String), - /// The simple format is equivalent to a detailed dependency - /// specifying only a version, eg. - /// `package = { version = "" }` - Detailed(DetailedTomlDependency

), -} - -impl TomlDependency { - fn unused_keys(&self) -> Vec { - match self { - TomlDependency::Simple(_) => vec![], - TomlDependency::Detailed(detailed) => detailed.other.keys().cloned().collect(), - } - } -} - -impl TomlDependency

{ - pub(crate) fn to_dependency_split( - &self, - name: &str, - source_id: SourceId, - nested_paths: &mut Vec, - config: &Config, - warnings: &mut Vec, - platform: Option, - root: &Path, - features: &Features, - kind: Option, - ) -> CargoResult { - self.to_dependency( - name, - &mut Context { - deps: &mut Vec::new(), - source_id, - nested_paths, - config, - warnings, - platform, - root, - features, - }, - kind, - ) - } - - fn to_dependency( - &self, - name: &str, - cx: &mut Context<'_, '_>, - kind: Option, - ) -> CargoResult { - match *self { - TomlDependency::Simple(ref version) => DetailedTomlDependency::

{ - version: Some(version.clone()), - ..Default::default() - } - .to_dependency(name, cx, kind), - TomlDependency::Detailed(ref details) => details.to_dependency(name, cx, kind), - } - } - - fn is_version_specified(&self) -> bool { - match self { - TomlDependency::Detailed(d) => d.version.is_some(), - TomlDependency::Simple(..) => true, - } - } - - fn is_optional(&self) -> bool { - match self { - TomlDependency::Detailed(d) => d.optional.unwrap_or(false), - TomlDependency::Simple(..) => false, - } - } -} - -impl<'de, P: Deserialize<'de> + Clone> de::Deserialize<'de> for TomlDependency

{ - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - UntaggedEnumVisitor::new() - .expecting( - "a version string like \"0.9.8\" or a \ - detailed dependency like { version = \"0.9.8\" }", - ) - .string(|value| Ok(TomlDependency::Simple(value.to_owned()))) - .map(|value| value.deserialize().map(TomlDependency::Detailed)) - .deserialize(deserializer) - } -} - -pub trait ResolveToPath { - fn resolve(&self, config: &Config) -> PathBuf; -} - -impl ResolveToPath for String { - fn resolve(&self, _: &Config) -> PathBuf { - self.into() - } -} - -impl ResolveToPath for ConfigRelativePath { - fn resolve(&self, c: &Config) -> PathBuf { - self.resolve_path(c) - } -} - -#[derive(Deserialize, Serialize, Clone, Debug)] -#[serde(rename_all = "kebab-case")] -pub struct DetailedTomlDependency { - version: Option, - registry: Option, - /// The URL of the `registry` field. - /// This is an internal implementation detail. When Cargo creates a - /// package, it replaces `registry` with `registry-index` so that the - /// manifest contains the correct URL. All users won't have the same - /// registry names configured, so Cargo can't rely on just the name for - /// crates published by other users. - registry_index: Option, - // `path` is relative to the file it appears in. If that's a `Cargo.toml`, it'll be relative to - // that TOML file, and if it's a `.cargo/config` file, it'll be relative to that file. - path: Option

, - git: Option, - branch: Option, - tag: Option, - rev: Option, - features: Option>, - optional: Option, - default_features: Option, - #[serde(rename = "default_features")] - default_features2: Option, - package: Option, - public: Option, - - /// One or more of `bin`, `cdylib`, `staticlib`, `bin:`. - artifact: Option, - /// If set, the artifact should also be a dependency - lib: Option, - /// A platform name, like `x86_64-apple-darwin` - target: Option, - /// This is here to provide a way to see the "unused manifest keys" when deserializing - #[serde(skip_serializing)] - #[serde(flatten)] - other: BTreeMap, -} - -impl DetailedTomlDependency { - fn add_features(&mut self, features: Option>) { - self.features = match (self.features.clone(), features.clone()) { - (Some(dep_feat), Some(inherit_feat)) => Some( - dep_feat - .into_iter() - .chain(inherit_feat) - .collect::>(), - ), - (Some(dep_fet), None) => Some(dep_fet), - (None, Some(inherit_feat)) => Some(inherit_feat), - (None, None) => None, - }; - } - - fn update_optional(&mut self, optional: Option) { - self.optional = optional; - } - - fn resolve_path( - &mut self, - name: &str, - root_path: &Path, - package_root: &Path, - ) -> CargoResult<()> { - if let Some(rel_path) = &self.path { - self.path = Some(resolve_relative_path( - name, - root_path, - package_root, - rel_path, - )?) - } - Ok(()) - } -} - -impl DetailedTomlDependency

{ - fn to_dependency( - &self, - name_in_toml: &str, - cx: &mut Context<'_, '_>, - kind: Option, - ) -> CargoResult { - if self.version.is_none() && self.path.is_none() && self.git.is_none() { - let msg = format!( - "dependency ({}) specified without \ - providing a local path, Git repository, version, or \ - workspace dependency to use. This will be considered an \ - error in future versions", - name_in_toml - ); - cx.warnings.push(msg); - } - - if let Some(version) = &self.version { - if version.contains('+') { - cx.warnings.push(format!( - "version requirement `{}` for dependency `{}` \ - includes semver metadata which will be ignored, removing the \ - metadata is recommended to avoid confusion", - version, name_in_toml - )); - } - } - - if self.git.is_none() { - let git_only_keys = [ - (&self.branch, "branch"), - (&self.tag, "tag"), - (&self.rev, "rev"), - ]; - - for &(key, key_name) in &git_only_keys { - if key.is_some() { - bail!( - "key `{}` is ignored for dependency ({}).", - key_name, - name_in_toml - ); - } - } - } - - // Early detection of potentially misused feature syntax - // instead of generating a "feature not found" error. - if let Some(features) = &self.features { - for feature in features { - if feature.contains('/') { - bail!( - "feature `{}` in dependency `{}` is not allowed to contain slashes\n\ - If you want to enable features of a transitive dependency, \ - the direct dependency needs to re-export those features from \ - the `[features]` table.", - feature, - name_in_toml - ); - } - if feature.starts_with("dep:") { - bail!( - "feature `{}` in dependency `{}` is not allowed to use explicit \ - `dep:` syntax\n\ - If you want to enable an optional dependency, specify the name \ - of the optional dependency without the `dep:` prefix, or specify \ - a feature from the dependency's `[features]` table that enables \ - the optional dependency.", - feature, - name_in_toml - ); - } - } - } - - let new_source_id = match ( - self.git.as_ref(), - self.path.as_ref(), - self.registry.as_ref(), - self.registry_index.as_ref(), - ) { - (Some(_), _, Some(_), _) | (Some(_), _, _, Some(_)) => bail!( - "dependency ({}) specification is ambiguous. \ - Only one of `git` or `registry` is allowed.", - name_in_toml - ), - (_, _, Some(_), Some(_)) => bail!( - "dependency ({}) specification is ambiguous. \ - Only one of `registry` or `registry-index` is allowed.", - name_in_toml - ), - (Some(git), maybe_path, _, _) => { - if maybe_path.is_some() { - bail!( - "dependency ({}) specification is ambiguous. \ - Only one of `git` or `path` is allowed.", - name_in_toml - ); - } - - let n_details = [&self.branch, &self.tag, &self.rev] - .iter() - .filter(|d| d.is_some()) - .count(); - - if n_details > 1 { - bail!( - "dependency ({}) specification is ambiguous. \ - Only one of `branch`, `tag` or `rev` is allowed.", - name_in_toml - ); - } - - let reference = self - .branch - .clone() - .map(GitReference::Branch) - .or_else(|| self.tag.clone().map(GitReference::Tag)) - .or_else(|| self.rev.clone().map(GitReference::Rev)) - .unwrap_or(GitReference::DefaultBranch); - let loc = git.into_url()?; - - if let Some(fragment) = loc.fragment() { - let msg = format!( - "URL fragment `#{}` in git URL is ignored for dependency ({}). \ - If you were trying to specify a specific git revision, \ - use `rev = \"{}\"` in the dependency declaration.", - fragment, name_in_toml, fragment - ); - cx.warnings.push(msg) - } - - SourceId::for_git(&loc, reference)? - } - (None, Some(path), _, _) => { - let path = path.resolve(cx.config); - cx.nested_paths.push(path.clone()); - // If the source ID for the package we're parsing is a path - // source, then we normalize the path here to get rid of - // components like `..`. - // - // The purpose of this is to get a canonical ID for the package - // that we're depending on to ensure that builds of this package - // always end up hashing to the same value no matter where it's - // built from. - if cx.source_id.is_path() { - let path = cx.root.join(path); - let path = paths::normalize_path(&path); - SourceId::for_path(&path)? - } else { - cx.source_id - } - } - (None, None, Some(registry), None) => SourceId::alt_registry(cx.config, registry)?, - (None, None, None, Some(registry_index)) => { - let url = registry_index.into_url()?; - SourceId::for_registry(&url)? - } - (None, None, None, None) => SourceId::crates_io(cx.config)?, - }; - - let (pkg_name, explicit_name_in_toml) = match self.package { - Some(ref s) => (&s[..], Some(name_in_toml)), - None => (name_in_toml, None), - }; - - let version = self.version.as_deref(); - let mut dep = Dependency::parse(pkg_name, version, new_source_id)?; - if self.default_features.is_some() && self.default_features2.is_some() { - warn_on_deprecated("default-features", name_in_toml, "dependency", cx.warnings); - } - dep.set_features(self.features.iter().flatten()) - .set_default_features( - self.default_features - .or(self.default_features2) - .unwrap_or(true), - ) - .set_optional(self.optional.unwrap_or(false)) - .set_platform(cx.platform.clone()); - if let Some(registry) = &self.registry { - let registry_id = SourceId::alt_registry(cx.config, registry)?; - dep.set_registry_id(registry_id); - } - if let Some(registry_index) = &self.registry_index { - let url = registry_index.into_url()?; - let registry_id = SourceId::for_registry(&url)?; - dep.set_registry_id(registry_id); - } - - if let Some(kind) = kind { - dep.set_kind(kind); - } - if let Some(name_in_toml) = explicit_name_in_toml { - dep.set_explicit_name_in_toml(name_in_toml); - } - - if let Some(p) = self.public { - cx.features.require(Feature::public_dependency())?; - - if dep.kind() != DepKind::Normal { - bail!("'public' specifier can only be used on regular dependencies, not {:?} dependencies", dep.kind()); - } - - dep.set_public(p); - } - - if let (Some(artifact), is_lib, target) = ( - self.artifact.as_ref(), - self.lib.unwrap_or(false), - self.target.as_deref(), - ) { - if cx.config.cli_unstable().bindeps { - let artifact = Artifact::parse(&artifact.0, is_lib, target)?; - if dep.kind() != DepKind::Build - && artifact.target() == Some(ArtifactTarget::BuildDependencyAssumeTarget) - { - bail!( - r#"`target = "target"` in normal- or dev-dependencies has no effect ({})"#, - name_in_toml - ); - } - dep.set_artifact(artifact) - } else { - bail!("`artifact = …` requires `-Z bindeps` ({})", name_in_toml); - } - } else if self.lib.is_some() || self.target.is_some() { - for (is_set, specifier) in [ - (self.lib.is_some(), "lib"), - (self.target.is_some(), "target"), - ] { - if !is_set { - continue; - } - bail!( - "'{}' specifier cannot be used without an 'artifact = …' value ({})", - specifier, - name_in_toml - ) - } - } - Ok(dep) - } -} - -// Explicit implementation so we avoid pulling in P: Default -impl Default for DetailedTomlDependency

{ - fn default() -> Self { - Self { - version: Default::default(), - registry: Default::default(), - registry_index: Default::default(), - path: Default::default(), - git: Default::default(), - branch: Default::default(), - tag: Default::default(), - rev: Default::default(), - features: Default::default(), - optional: Default::default(), - default_features: Default::default(), - default_features2: Default::default(), - package: Default::default(), - public: Default::default(), - artifact: Default::default(), - lib: Default::default(), - target: Default::default(), - other: Default::default(), - } - } -} - /// This type is used to deserialize `Cargo.toml` files. #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] @@ -1921,1308 +1455,1695 @@ fn inheritable_from_path( } } -/// Returns the name of the README file for a [`TomlPackage`]. -pub fn readme_for_package(package_root: &Path, readme: Option<&StringOrBool>) -> Option { - match &readme { - None => default_readme_from_package_root(package_root), - Some(value) => match value { - StringOrBool::Bool(false) => None, - StringOrBool::Bool(true) => Some("README.md".to_string()), - StringOrBool::String(v) => Some(v.clone()), - }, - } +/// Returns the name of the README file for a [`TomlPackage`]. +pub fn readme_for_package(package_root: &Path, readme: Option<&StringOrBool>) -> Option { + match &readme { + None => default_readme_from_package_root(package_root), + Some(value) => match value { + StringOrBool::Bool(false) => None, + StringOrBool::Bool(true) => Some("README.md".to_string()), + StringOrBool::String(v) => Some(v.clone()), + }, + } +} + +const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"]; + +/// Checks if a file with any of the default README file names exists in the package root. +/// If so, returns a `String` representing that name. +fn default_readme_from_package_root(package_root: &Path) -> Option { + for &readme_filename in DEFAULT_README_FILES.iter() { + if package_root.join(readme_filename).is_file() { + return Some(readme_filename.to_string()); + } + } + + None +} + +/// Checks a list of build targets, and ensures the target names are unique within a vector. +/// If not, the name of the offending build target is returned. +fn unique_build_targets( + targets: &[Target], + package_root: &Path, +) -> Result<(), HashMap>> { + let mut source_targets = HashMap::<_, Vec<_>>::new(); + for target in targets { + if let TargetSourcePath::Path(path) = target.src_path() { + let full = package_root.join(path); + source_targets.entry(full).or_default().push(target.clone()); + } + } + + let conflict_targets = source_targets + .into_iter() + .filter(|(_, targets)| targets.len() > 1) + .collect::>(); + + if !conflict_targets.is_empty() { + return Err(conflict_targets); + } + + Ok(()) +} + +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct TomlWorkspace { + members: Option>, + #[serde(rename = "default-members")] + default_members: Option>, + exclude: Option>, + resolver: Option, + + // Properties that can be inherited by members. + package: Option, + dependencies: Option>, + lints: Option, + + // Note that this field must come last due to the way toml serialization + // works which requires tables to be emitted after all values. + metadata: Option, +} + +/// A group of fields that are inheritable by members of the workspace +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct InheritableFields { + // We use skip here since it will never be present when deserializing + // and we don't want it present when serializing + #[serde(skip)] + dependencies: Option>, + #[serde(skip)] + lints: Option, + + version: Option, + authors: Option>, + description: Option, + homepage: Option, + documentation: Option, + readme: Option, + keywords: Option>, + categories: Option>, + license: Option, + #[serde(rename = "license-file")] + license_file: Option, + repository: Option, + publish: Option, + edition: Option, + badges: Option>>, + exclude: Option>, + include: Option>, + #[serde(rename = "rust-version")] + rust_version: Option, + // We use skip here since it will never be present when deserializing + // and we don't want it present when serializing + #[serde(skip)] + ws_root: PathBuf, +} + +/// Defines simple getter methods for inheritable fields. +macro_rules! inheritable_field_getter { + ( $(($key:literal, $field:ident -> $ret:ty),)* ) => ( + $( + #[doc = concat!("Gets the field `workspace.", $key, "`.")] + pub fn $field(&self) -> CargoResult<$ret> { + let Some(val) = &self.$field else { + bail!("`workspace.{}` was not defined", $key); + }; + Ok(val.clone()) + } + )* + ) } -const DEFAULT_README_FILES: [&str; 3] = ["README.md", "README.txt", "README"]; +impl InheritableFields { + inheritable_field_getter! { + // Please keep this list lexicographically ordered. + ("dependencies", dependencies -> BTreeMap), + ("lints", lints -> TomlLints), + ("package.authors", authors -> Vec), + ("package.badges", badges -> BTreeMap>), + ("package.categories", categories -> Vec), + ("package.description", description -> String), + ("package.documentation", documentation -> String), + ("package.edition", edition -> String), + ("package.exclude", exclude -> Vec), + ("package.homepage", homepage -> String), + ("package.include", include -> Vec), + ("package.keywords", keywords -> Vec), + ("package.license", license -> String), + ("package.publish", publish -> VecStringOrBool), + ("package.repository", repository -> String), + ("package.rust-version", rust_version -> RustVersion), + ("package.version", version -> semver::Version), + } -/// Checks if a file with any of the default README file names exists in the package root. -/// If so, returns a `String` representing that name. -fn default_readme_from_package_root(package_root: &Path) -> Option { - for &readme_filename in DEFAULT_README_FILES.iter() { - if package_root.join(readme_filename).is_file() { - return Some(readme_filename.to_string()); + /// Gets a workspace dependency with the `name`. + pub fn get_dependency(&self, name: &str, package_root: &Path) -> CargoResult { + let Some(deps) = &self.dependencies else { + bail!("`workspace.dependencies` was not defined"); + }; + let Some(dep) = deps.get(name) else { + bail!("`dependency.{name}` was not found in `workspace.dependencies`"); + }; + let mut dep = dep.clone(); + if let TomlDependency::Detailed(detailed) = &mut dep { + detailed.resolve_path(name, self.ws_root(), package_root)?; } + Ok(dep) } - None -} + /// Gets the field `workspace.package.license-file`. + pub fn license_file(&self, package_root: &Path) -> CargoResult { + let Some(license_file) = &self.license_file else { + bail!("`workspace.package.license-file` was not defined"); + }; + resolve_relative_path("license-file", &self.ws_root, package_root, license_file) + } -/// Checks a list of build targets, and ensures the target names are unique within a vector. -/// If not, the name of the offending build target is returned. -fn unique_build_targets( - targets: &[Target], - package_root: &Path, -) -> Result<(), HashMap>> { - let mut source_targets = HashMap::<_, Vec<_>>::new(); - for target in targets { - if let TargetSourcePath::Path(path) = target.src_path() { - let full = package_root.join(path); - source_targets.entry(full).or_default().push(target.clone()); - } + /// Gets the field `workspace.package.readme`. + pub fn readme(&self, package_root: &Path) -> CargoResult { + let Some(readme) = readme_for_package(self.ws_root.as_path(), self.readme.as_ref()) else { + bail!("`workspace.package.readme` was not defined"); + }; + resolve_relative_path("readme", &self.ws_root, package_root, &readme) + .map(StringOrBool::String) } - let conflict_targets = source_targets - .into_iter() - .filter(|(_, targets)| targets.len() > 1) - .collect::>(); + pub fn ws_root(&self) -> &PathBuf { + &self.ws_root + } - if !conflict_targets.is_empty() { - return Err(conflict_targets); + pub fn update_deps(&mut self, deps: Option>) { + self.dependencies = deps; } - Ok(()) + pub fn update_lints(&mut self, lints: Option) { + self.lints = lints; + } + + pub fn update_ws_path(&mut self, ws_root: PathBuf) { + self.ws_root = ws_root; + } } -#[derive(Deserialize, Serialize, Clone, Debug, Default)] -pub struct TomlProfiles(BTreeMap); +/// Represents the `package`/`project` sections of a `Cargo.toml`. +/// +/// Note that the order of the fields matters, since this is the order they +/// are serialized to a TOML file. For example, you cannot have values after +/// the field `metadata`, since it is a table and values cannot appear after +/// tables. +#[derive(Deserialize, Serialize, Clone, Debug)] +#[serde(rename_all = "kebab-case")] +pub struct TomlPackage { + edition: Option, + rust_version: Option, + name: InternedString, + #[serde(deserialize_with = "version_trim_whitespace")] + version: MaybeWorkspaceSemverVersion, + authors: Option, + build: Option, + metabuild: Option, + default_target: Option, + forced_target: Option, + links: Option, + exclude: Option, + include: Option, + publish: Option, + workspace: Option, + im_a_teapot: Option, + autobins: Option, + autoexamples: Option, + autotests: Option, + autobenches: Option, + default_run: Option, -impl TomlProfiles { - pub fn get_all(&self) -> &BTreeMap { - &self.0 - } + // Package metadata. + description: Option, + homepage: Option, + documentation: Option, + readme: Option, + keywords: Option, + categories: Option, + license: Option, + license_file: Option, + repository: Option, + resolver: Option, - pub fn get(&self, name: &str) -> Option<&TomlProfile> { - self.0.get(name) - } + // Provide a helpful error message for a common user error. + #[serde(rename = "cargo-features", skip_serializing)] + _invalid_cargo_features: Option, - /// Checks syntax validity and unstable feature gate for each profile. - /// - /// It's a bit unfortunate both `-Z` flags and `cargo-features` are required, - /// because profiles can now be set in either `Cargo.toml` or `config.toml`. - pub fn validate( + // Note that this field must come last due to the way toml serialization + // works which requires tables to be emitted after all values. + metadata: Option, +} + +impl TomlPackage { + pub fn to_package_id( &self, - cli_unstable: &CliUnstable, - features: &Features, - warnings: &mut Vec, - ) -> CargoResult<()> { - for (name, profile) in &self.0 { - profile.validate(name, cli_unstable, features, warnings)?; - } - Ok(()) + source_id: SourceId, + version: semver::Version, + ) -> CargoResult { + PackageId::new(self.name, version, source_id) } } -#[derive(Deserialize, Serialize, Clone, Debug, Default, Eq, PartialEq)] -#[serde(default, rename_all = "kebab-case")] -pub struct TomlProfile { - pub opt_level: Option, - pub lto: Option, - pub codegen_backend: Option, - pub codegen_units: Option, - pub debug: Option, - pub split_debuginfo: Option, - pub debug_assertions: Option, - pub rpath: Option, - pub panic: Option, - pub overflow_checks: Option, - pub incremental: Option, - pub dir_name: Option, - pub inherits: Option, - pub strip: Option, - // Note that `rustflags` is used for the cargo-feature `profile_rustflags` - pub rustflags: Option>, - // These two fields must be last because they are sub-tables, and TOML - // requires all non-tables to be listed first. - pub package: Option>, - pub build_override: Option>, +fn version_trim_whitespace<'de, D>(deserializer: D) -> Result +where + D: de::Deserializer<'de>, +{ + UntaggedEnumVisitor::new() + .expecting("SemVer version") + .string( + |value| match value.trim().parse().map_err(de::Error::custom) { + Ok(parsed) => Ok(MaybeWorkspace::Defined(parsed)), + Err(e) => Err(e), + }, + ) + .map(|value| value.deserialize().map(MaybeWorkspace::Workspace)) + .deserialize(deserializer) } -impl TomlProfile { - /// Checks stytax validity and unstable feature gate for a given profile. - pub fn validate( - &self, - name: &str, - cli_unstable: &CliUnstable, - features: &Features, - warnings: &mut Vec, - ) -> CargoResult<()> { - self.validate_profile(name, cli_unstable, features)?; - if let Some(ref profile) = self.build_override { - profile.validate_override("build-override")?; - profile.validate_profile(&format!("{name}.build-override"), cli_unstable, features)?; - } - if let Some(ref packages) = self.package { - for (override_name, profile) in packages { - profile.validate_override("package")?; - profile.validate_profile( - &format!("{name}.package.{override_name}"), - cli_unstable, - features, - )?; - } - } +/// This Trait exists to make [`MaybeWorkspace::Workspace`] generic. It makes deserialization of +/// [`MaybeWorkspace`] much easier, as well as making error messages for +/// [`MaybeWorkspace::resolve`] much nicer +/// +/// Implementors should have a field `workspace` with the type of `bool`. It is used to ensure +/// `workspace` is not `false` in a `Cargo.toml` +pub trait WorkspaceInherit { + /// This is the workspace table that is being inherited from. + /// For example `[workspace.dependencies]` would be the table "dependencies" + fn inherit_toml_table(&self) -> &str; - // Profile name validation - Self::validate_name(name)?; + /// This is used to output the value of the implementors `workspace` field + fn workspace(&self) -> bool; +} - if let Some(dir_name) = self.dir_name { - // This is disabled for now, as we would like to stabilize named - // profiles without this, and then decide in the future if it is - // needed. This helps simplify the UI a little. - bail!( - "dir-name=\"{}\" in profile `{}` is not currently allowed, \ - directory names are tied to the profile name for custom profiles", - dir_name, - name - ); - } +/// An enum that allows for inheriting keys from a workspace in a Cargo.toml. +#[derive(Serialize, Copy, Clone, Debug)] +#[serde(untagged)] +pub enum MaybeWorkspace { + /// The "defined" type, or the type that that is used when not inheriting from a workspace. + Defined(T), + /// The type when inheriting from a workspace. + Workspace(W), +} - // `inherits` validation - if matches!(self.inherits.map(|s| s.as_str()), Some("debug")) { - bail!( - "profile.{}.inherits=\"debug\" should be profile.{}.inherits=\"dev\"", - name, - name - ); +impl MaybeWorkspace { + fn resolve<'a>( + self, + label: &str, + get_ws_inheritable: impl FnOnce() -> CargoResult, + ) -> CargoResult { + match self { + MaybeWorkspace::Defined(value) => Ok(value), + MaybeWorkspace::Workspace(w) => get_ws_inheritable().with_context(|| { + format!( + "error inheriting `{label}` from workspace root manifest's `workspace.{}.{label}`", + w.inherit_toml_table(), + ) + }), } + } - match name { - "doc" => { - warnings.push("profile `doc` is deprecated and has no effect".to_string()); - } - "test" | "bench" => { - if self.panic.is_some() { - warnings.push(format!("`panic` setting is ignored for `{}` profile", name)) - } - } - _ => {} + fn resolve_with_self<'a>( + self, + label: &str, + get_ws_inheritable: impl FnOnce(&W) -> CargoResult, + ) -> CargoResult { + match self { + MaybeWorkspace::Defined(value) => Ok(value), + MaybeWorkspace::Workspace(w) => get_ws_inheritable(&w).with_context(|| { + format!( + "error inheriting `{label}` from workspace root manifest's `workspace.{}.{label}`", + w.inherit_toml_table(), + ) + }), } + } - if let Some(panic) = &self.panic { - if panic != "unwind" && panic != "abort" { - bail!( - "`panic` setting of `{}` is not a valid setting, \ - must be `unwind` or `abort`", - panic - ); - } + fn as_defined(&self) -> Option<&T> { + match self { + MaybeWorkspace::Workspace(_) => None, + MaybeWorkspace::Defined(defined) => Some(defined), } + } +} - if let Some(StringOrBool::String(arg)) = &self.lto { - if arg == "true" || arg == "false" { - bail!( - "`lto` setting of string `\"{arg}\"` for `{name}` profile is not \ - a valid setting, must be a boolean (`true`/`false`) or a string \ - (`\"thin\"`/`\"fat\"`/`\"off\"`) or omitted.", - ); - } - } +//. This already has a `Deserialize` impl from version_trim_whitespace +type MaybeWorkspaceSemverVersion = MaybeWorkspace; - Ok(()) - } +type MaybeWorkspaceString = MaybeWorkspace; +impl<'de> de::Deserialize<'de> for MaybeWorkspaceString { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + struct Visitor; - /// Validate dir-names and profile names according to RFC 2678. - pub fn validate_name(name: &str) -> CargoResult<()> { - if let Some(ch) = name - .chars() - .find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-') - { - bail!( - "invalid character `{}` in profile name `{}`\n\ - Allowed characters are letters, numbers, underscore, and hyphen.", - ch, - name - ); - } + impl<'de> de::Visitor<'de> for Visitor { + type Value = MaybeWorkspaceString; - const SEE_DOCS: &str = "See https://doc.rust-lang.org/cargo/reference/profiles.html \ - for more on configuring profiles."; + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + f.write_str("a string or workspace") + } - let lower_name = name.to_lowercase(); - if lower_name == "debug" { - bail!( - "profile name `{}` is reserved\n\ - To configure the default development profile, use the name `dev` \ - as in [profile.dev]\n\ - {}", - name, - SEE_DOCS - ); - } - if lower_name == "build-override" { - bail!( - "profile name `{}` is reserved\n\ - To configure build dependency settings, use [profile.dev.build-override] \ - and [profile.release.build-override]\n\ - {}", - name, - SEE_DOCS - ); - } + fn visit_string(self, value: String) -> Result + where + E: de::Error, + { + Ok(MaybeWorkspaceString::Defined(value)) + } - // These are some arbitrary reservations. We have no plans to use - // these, but it seems safer to reserve a few just in case we want to - // add more built-in profiles in the future. We can also uses special - // syntax like cargo:foo if needed. But it is unlikely these will ever - // be used. - if matches!( - lower_name.as_str(), - "build" - | "check" - | "clean" - | "config" - | "fetch" - | "fix" - | "install" - | "metadata" - | "package" - | "publish" - | "report" - | "root" - | "run" - | "rust" - | "rustc" - | "rustdoc" - | "target" - | "tmp" - | "uninstall" - ) || lower_name.starts_with("cargo") - { - bail!( - "profile name `{}` is reserved\n\ - Please choose a different name.\n\ - {}", - name, - SEE_DOCS - ); + fn visit_map(self, map: V) -> Result + where + V: de::MapAccess<'de>, + { + let mvd = de::value::MapAccessDeserializer::new(map); + TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + } } - Ok(()) + d.deserialize_any(Visitor) } +} - /// Validates a profile. - /// - /// This is a shallow check, which is reused for the profile itself and any overrides. - fn validate_profile( - &self, - name: &str, - cli_unstable: &CliUnstable, - features: &Features, - ) -> CargoResult<()> { - if let Some(codegen_backend) = &self.codegen_backend { - match ( - features.require(Feature::codegen_backend()), - cli_unstable.codegen_backend, - ) { - (Err(e), false) => return Err(e), - _ => {} +type MaybeWorkspaceRustVersion = MaybeWorkspace; +impl<'de> de::Deserialize<'de> for MaybeWorkspaceRustVersion { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + struct Visitor; + + impl<'de> de::Visitor<'de> for Visitor { + type Value = MaybeWorkspaceRustVersion; + + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + f.write_str("a semver or workspace") } - if codegen_backend.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') { - bail!( - "`profile.{}.codegen-backend` setting of `{}` is not a valid backend name.", - name, - codegen_backend, - ); + fn visit_string(self, value: String) -> Result + where + E: de::Error, + { + let value = value.parse::().map_err(|e| E::custom(e))?; + Ok(MaybeWorkspaceRustVersion::Defined(value)) } - } - if self.rustflags.is_some() { - match ( - features.require(Feature::profile_rustflags()), - cli_unstable.profile_rustflags, - ) { - (Err(e), false) => return Err(e), - _ => {} + + fn visit_map(self, map: V) -> Result + where + V: de::MapAccess<'de>, + { + let mvd = de::value::MapAccessDeserializer::new(map); + TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) } } - Ok(()) - } - /// Validation that is specific to an override. - fn validate_override(&self, which: &str) -> CargoResult<()> { - if self.package.is_some() { - bail!("package-specific profiles cannot be nested"); - } - if self.build_override.is_some() { - bail!("build-override profiles cannot be nested"); - } - if self.panic.is_some() { - bail!("`panic` may not be specified in a `{}` profile", which) - } - if self.lto.is_some() { - bail!("`lto` may not be specified in a `{}` profile", which) - } - if self.rpath.is_some() { - bail!("`rpath` may not be specified in a `{}` profile", which) - } - Ok(()) + d.deserialize_any(Visitor) } +} - /// Overwrite self's values with the given profile. - pub fn merge(&mut self, profile: &TomlProfile) { - if let Some(v) = &profile.opt_level { - self.opt_level = Some(v.clone()); - } +type MaybeWorkspaceVecString = MaybeWorkspace, TomlWorkspaceField>; +impl<'de> de::Deserialize<'de> for MaybeWorkspaceVecString { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + struct Visitor; - if let Some(v) = &profile.lto { - self.lto = Some(v.clone()); - } + impl<'de> de::Visitor<'de> for Visitor { + type Value = MaybeWorkspaceVecString; - if let Some(v) = profile.codegen_backend { - self.codegen_backend = Some(v); - } + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.write_str("a vector of strings or workspace") + } + fn visit_seq(self, v: A) -> Result + where + A: de::SeqAccess<'de>, + { + let seq = de::value::SeqAccessDeserializer::new(v); + Vec::deserialize(seq).map(MaybeWorkspace::Defined) + } - if let Some(v) = profile.codegen_units { - self.codegen_units = Some(v); + fn visit_map(self, map: V) -> Result + where + V: de::MapAccess<'de>, + { + let mvd = de::value::MapAccessDeserializer::new(map); + TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + } } - if let Some(v) = profile.debug { - self.debug = Some(v); - } + d.deserialize_any(Visitor) + } +} - if let Some(v) = profile.debug_assertions { - self.debug_assertions = Some(v); - } +type MaybeWorkspaceStringOrBool = MaybeWorkspace; +impl<'de> de::Deserialize<'de> for MaybeWorkspaceStringOrBool { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + struct Visitor; - if let Some(v) = &profile.split_debuginfo { - self.split_debuginfo = Some(v.clone()); - } + impl<'de> de::Visitor<'de> for Visitor { + type Value = MaybeWorkspaceStringOrBool; - if let Some(v) = profile.rpath { - self.rpath = Some(v); - } + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.write_str("a string, a bool, or workspace") + } - if let Some(v) = &profile.panic { - self.panic = Some(v.clone()); - } + fn visit_bool(self, v: bool) -> Result + where + E: de::Error, + { + let b = de::value::BoolDeserializer::new(v); + StringOrBool::deserialize(b).map(MaybeWorkspace::Defined) + } - if let Some(v) = profile.overflow_checks { - self.overflow_checks = Some(v); - } + fn visit_string(self, v: String) -> Result + where + E: de::Error, + { + let string = de::value::StringDeserializer::new(v); + StringOrBool::deserialize(string).map(MaybeWorkspace::Defined) + } - if let Some(v) = profile.incremental { - self.incremental = Some(v); + fn visit_map(self, map: V) -> Result + where + V: de::MapAccess<'de>, + { + let mvd = de::value::MapAccessDeserializer::new(map); + TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + } } - if let Some(v) = &profile.rustflags { - self.rustflags = Some(v.clone()); - } + d.deserialize_any(Visitor) + } +} - 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()), +type MaybeWorkspaceVecStringOrBool = MaybeWorkspace; +impl<'de> de::Deserialize<'de> for MaybeWorkspaceVecStringOrBool { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + struct Visitor; + + impl<'de> de::Visitor<'de> for Visitor { + type Value = MaybeWorkspaceVecStringOrBool; + + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.write_str("a boolean, a vector of strings, or workspace") } - } - 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()), + fn visit_bool(self, v: bool) -> Result + where + E: de::Error, + { + let b = de::value::BoolDeserializer::new(v); + VecStringOrBool::deserialize(b).map(MaybeWorkspace::Defined) } - } - if let Some(v) = &profile.inherits { - self.inherits = Some(*v); - } + fn visit_seq(self, v: A) -> Result + where + A: de::SeqAccess<'de>, + { + let seq = de::value::SeqAccessDeserializer::new(v); + VecStringOrBool::deserialize(seq).map(MaybeWorkspace::Defined) + } - if let Some(v) = &profile.dir_name { - self.dir_name = Some(*v); + fn visit_map(self, map: V) -> Result + where + V: de::MapAccess<'de>, + { + let mvd = de::value::MapAccessDeserializer::new(map); + TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + } } - if let Some(v) = &profile.strip { - self.strip = Some(v.clone()); + d.deserialize_any(Visitor) + } +} + +type MaybeWorkspaceBtreeMap = + MaybeWorkspace>, TomlWorkspaceField>; + +impl<'de> de::Deserialize<'de> for MaybeWorkspaceBtreeMap { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + let value = serde_value::Value::deserialize(deserializer)?; + + if let Ok(w) = TomlWorkspaceField::deserialize( + serde_value::ValueDeserializer::::new(value.clone()), + ) { + return if w.workspace() { + Ok(MaybeWorkspace::Workspace(w)) + } else { + Err(de::Error::custom("`workspace` cannot be false")) + }; } + BTreeMap::deserialize(serde_value::ValueDeserializer::::new(value)) + .map(MaybeWorkspace::Defined) } } -#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] -pub enum ProfilePackageSpec { - Spec(PackageIdSpec), - All, +#[derive(Deserialize, Serialize, Copy, Clone, Debug)] +pub struct TomlWorkspaceField { + #[serde(deserialize_with = "bool_no_false")] + workspace: bool, } -impl fmt::Display for ProfilePackageSpec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - ProfilePackageSpec::Spec(spec) => spec.fmt(f), - ProfilePackageSpec::All => f.write_str("*"), - } +impl WorkspaceInherit for TomlWorkspaceField { + fn inherit_toml_table(&self) -> &str { + "package" + } + + fn workspace(&self) -> bool { + self.workspace } } -impl ser::Serialize for ProfilePackageSpec { - fn serialize(&self, s: S) -> Result - where - S: ser::Serializer, - { - self.to_string().serialize(s) +fn bool_no_false<'de, D: de::Deserializer<'de>>(deserializer: D) -> Result { + let b: bool = Deserialize::deserialize(deserializer)?; + if b { + Ok(b) + } else { + Err(de::Error::custom("`workspace` cannot be false")) } } -impl<'de> de::Deserialize<'de> for ProfilePackageSpec { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - let string = String::deserialize(d)?; - if string == "*" { - Ok(ProfilePackageSpec::All) - } else { - PackageIdSpec::parse(&string) - .map_err(de::Error::custom) - .map(ProfilePackageSpec::Spec) +type MaybeWorkspaceDependency = MaybeWorkspace; + +impl MaybeWorkspaceDependency { + fn unused_keys(&self) -> Vec { + match self { + MaybeWorkspaceDependency::Defined(d) => d.unused_keys(), + MaybeWorkspaceDependency::Workspace(w) => w.other.keys().cloned().collect(), } } } -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct TomlOptLevel(pub String); - -impl ser::Serialize for TomlOptLevel { - fn serialize(&self, serializer: S) -> Result +impl<'de> de::Deserialize<'de> for MaybeWorkspaceDependency { + fn deserialize(deserializer: D) -> Result where - S: ser::Serializer, + D: de::Deserializer<'de>, { - match self.0.parse::() { - Ok(n) => n.serialize(serializer), - Err(_) => self.0.serialize(serializer), + let value = serde_value::Value::deserialize(deserializer)?; + + if let Ok(w) = TomlWorkspaceDependency::deserialize(serde_value::ValueDeserializer::< + D::Error, + >::new(value.clone())) + { + return if w.workspace() { + Ok(MaybeWorkspace::Workspace(w)) + } else { + Err(de::Error::custom("`workspace` cannot be false")) + }; } + TomlDependency::deserialize(serde_value::ValueDeserializer::::new(value)) + .map(MaybeWorkspace::Defined) } } -impl<'de> de::Deserialize<'de> for TomlOptLevel { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - use serde::de::Error as _; - UntaggedEnumVisitor::new() - .expecting("an optimization level") - .i64(|value| Ok(TomlOptLevel(value.to_string()))) - .string(|value| { - if value == "s" || value == "z" { - Ok(TomlOptLevel(value.to_string())) - } else { - Err(serde_untagged::de::Error::custom(format!( - "must be `0`, `1`, `2`, `3`, `s` or `z`, \ - but found the string: \"{}\"", - value - ))) +#[derive(Deserialize, Serialize, Clone, Debug)] +#[serde(rename_all = "kebab-case")] +pub struct TomlWorkspaceDependency { + workspace: bool, + features: Option>, + default_features: Option, + #[serde(rename = "default_features")] + default_features2: Option, + optional: Option, + /// This is here to provide a way to see the "unused manifest keys" when deserializing + #[serde(skip_serializing)] + #[serde(flatten)] + other: BTreeMap, +} + +impl TomlWorkspaceDependency { + fn resolve<'a>( + &self, + name: &str, + inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>, + cx: &mut Context<'_, '_>, + ) -> CargoResult { + fn default_features_msg(label: &str, ws_def_feat: Option, cx: &mut Context<'_, '_>) { + let ws_def_feat = match ws_def_feat { + Some(true) => "true", + Some(false) => "false", + None => "not specified", + }; + cx.warnings.push(format!( + "`default-features` is ignored for {label}, since `default-features` was \ + {ws_def_feat} for `workspace.dependencies.{label}`, \ + this could become a hard error in the future" + )) + } + if self.default_features.is_some() && self.default_features2.is_some() { + warn_on_deprecated("default-features", name, "dependency", cx.warnings); + } + inheritable()?.get_dependency(name, cx.root).map(|d| { + match d { + TomlDependency::Simple(s) => { + if let Some(false) = self.default_features.or(self.default_features2) { + default_features_msg(name, None, cx); + } + if self.optional.is_some() || self.features.is_some() { + TomlDependency::Detailed(DetailedTomlDependency { + version: Some(s), + optional: self.optional, + features: self.features.clone(), + ..Default::default() + }) + } else { + TomlDependency::Simple(s) + } } - }) - .deserialize(d) + TomlDependency::Detailed(d) => { + let mut d = d.clone(); + match ( + self.default_features.or(self.default_features2), + d.default_features.or(d.default_features2), + ) { + // member: default-features = true and + // workspace: default-features = false should turn on + // default-features + (Some(true), Some(false)) => { + d.default_features = Some(true); + } + // member: default-features = false and + // workspace: default-features = true should ignore member + // default-features + (Some(false), Some(true)) => { + default_features_msg(name, Some(true), cx); + } + // member: default-features = false and + // workspace: dep = "1.0" should ignore member default-features + (Some(false), None) => { + default_features_msg(name, None, cx); + } + _ => {} + } + d.add_features(self.features.clone()); + d.update_optional(self.optional); + TomlDependency::Detailed(d) + } + } + }) } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] -pub enum TomlDebugInfo { - None, - LineDirectivesOnly, - LineTablesOnly, - Limited, - Full, -} +impl WorkspaceInherit for TomlWorkspaceDependency { + fn inherit_toml_table(&self) -> &str { + "dependencies" + } -impl Display for TomlDebugInfo { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - TomlDebugInfo::None => f.write_char('0'), - TomlDebugInfo::Limited => f.write_char('1'), - TomlDebugInfo::Full => f.write_char('2'), - TomlDebugInfo::LineDirectivesOnly => f.write_str("line-directives-only"), - TomlDebugInfo::LineTablesOnly => f.write_str("line-tables-only"), - } + fn workspace(&self) -> bool { + self.workspace } } -impl ser::Serialize for TomlDebugInfo { - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { +#[derive(Clone, Debug, Serialize)] +#[serde(untagged)] +pub enum TomlDependency { + /// In the simple format, only a version is specified, eg. + /// `package = ""` + Simple(String), + /// The simple format is equivalent to a detailed dependency + /// specifying only a version, eg. + /// `package = { version = "" }` + Detailed(DetailedTomlDependency

), +} + +impl TomlDependency { + fn unused_keys(&self) -> Vec { match self { - Self::None => 0.serialize(serializer), - Self::LineDirectivesOnly => "line-directives-only".serialize(serializer), - Self::LineTablesOnly => "line-tables-only".serialize(serializer), - Self::Limited => 1.serialize(serializer), - Self::Full => 2.serialize(serializer), + TomlDependency::Simple(_) => vec![], + TomlDependency::Detailed(detailed) => detailed.other.keys().cloned().collect(), } } } -impl<'de> de::Deserialize<'de> for TomlDebugInfo { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - use serde::de::Error as _; - let expecting = "a boolean, 0, 1, 2, \"line-tables-only\", or \"line-directives-only\""; - UntaggedEnumVisitor::new() - .expecting(expecting) - .bool(|value| { - Ok(if value { - TomlDebugInfo::Full - } else { - TomlDebugInfo::None - }) - }) - .i64(|value| { - let debuginfo = match value { - 0 => TomlDebugInfo::None, - 1 => TomlDebugInfo::Limited, - 2 => TomlDebugInfo::Full, - _ => { - return Err(serde_untagged::de::Error::invalid_value( - Unexpected::Signed(value), - &expecting, - )) - } - }; - Ok(debuginfo) - }) - .string(|value| { - let debuginfo = match value { - "none" => TomlDebugInfo::None, - "limited" => TomlDebugInfo::Limited, - "full" => TomlDebugInfo::Full, - "line-directives-only" => TomlDebugInfo::LineDirectivesOnly, - "line-tables-only" => TomlDebugInfo::LineTablesOnly, - _ => { - return Err(serde_untagged::de::Error::invalid_value( - Unexpected::Str(value), - &expecting, - )) - } - }; - Ok(debuginfo) - }) - .deserialize(d) +impl TomlDependency

{ + pub(crate) fn to_dependency_split( + &self, + name: &str, + source_id: SourceId, + nested_paths: &mut Vec, + config: &Config, + warnings: &mut Vec, + platform: Option, + root: &Path, + features: &Features, + kind: Option, + ) -> CargoResult { + self.to_dependency( + name, + &mut Context { + deps: &mut Vec::new(), + source_id, + nested_paths, + config, + warnings, + platform, + root, + features, + }, + kind, + ) } -} - -/// A StringOrVec can be parsed from either a TOML string or array, -/// but is always stored as a vector. -#[derive(Clone, Debug, Serialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct StringOrVec(Vec); -impl StringOrVec { - pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, String> { - self.0.iter() + fn to_dependency( + &self, + name: &str, + cx: &mut Context<'_, '_>, + kind: Option, + ) -> CargoResult { + match *self { + TomlDependency::Simple(ref version) => DetailedTomlDependency::

{ + version: Some(version.clone()), + ..Default::default() + } + .to_dependency(name, cx, kind), + TomlDependency::Detailed(ref details) => details.to_dependency(name, cx, kind), + } } -} -impl<'de> de::Deserialize<'de> for StringOrVec { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - UntaggedEnumVisitor::new() - .expecting("string or list of strings") - .string(|value| Ok(StringOrVec(vec![value.to_owned()]))) - .seq(|value| value.deserialize().map(StringOrVec)) - .deserialize(deserializer) + fn is_version_specified(&self) -> bool { + match self { + TomlDependency::Detailed(d) => d.version.is_some(), + TomlDependency::Simple(..) => true, + } } -} -#[derive(Clone, Debug, Serialize, Eq, PartialEq)] -#[serde(untagged)] -pub enum StringOrBool { - String(String), - Bool(bool), + fn is_optional(&self) -> bool { + match self { + TomlDependency::Detailed(d) => d.optional.unwrap_or(false), + TomlDependency::Simple(..) => false, + } + } } -impl<'de> Deserialize<'de> for StringOrBool { +impl<'de, P: Deserialize<'de> + Clone> de::Deserialize<'de> for TomlDependency

{ fn deserialize(deserializer: D) -> Result where D: de::Deserializer<'de>, { UntaggedEnumVisitor::new() - .bool(|b| Ok(StringOrBool::Bool(b))) - .string(|s| Ok(StringOrBool::String(s.to_owned()))) + .expecting( + "a version string like \"0.9.8\" or a \ + detailed dependency like { version = \"0.9.8\" }", + ) + .string(|value| Ok(TomlDependency::Simple(value.to_owned()))) + .map(|value| value.deserialize().map(TomlDependency::Detailed)) .deserialize(deserializer) } } -#[derive(PartialEq, Clone, Debug, Serialize)] -#[serde(untagged)] -pub enum VecStringOrBool { - VecString(Vec), - Bool(bool), +#[derive(Deserialize, Serialize, Clone, Debug)] +#[serde(rename_all = "kebab-case")] +pub struct DetailedTomlDependency { + version: Option, + registry: Option, + /// The URL of the `registry` field. + /// This is an internal implementation detail. When Cargo creates a + /// package, it replaces `registry` with `registry-index` so that the + /// manifest contains the correct URL. All users won't have the same + /// registry names configured, so Cargo can't rely on just the name for + /// crates published by other users. + registry_index: Option, + // `path` is relative to the file it appears in. If that's a `Cargo.toml`, it'll be relative to + // that TOML file, and if it's a `.cargo/config` file, it'll be relative to that file. + path: Option

, + git: Option, + branch: Option, + tag: Option, + rev: Option, + features: Option>, + optional: Option, + default_features: Option, + #[serde(rename = "default_features")] + default_features2: Option, + package: Option, + public: Option, + + /// One or more of `bin`, `cdylib`, `staticlib`, `bin:`. + artifact: Option, + /// If set, the artifact should also be a dependency + lib: Option, + /// A platform name, like `x86_64-apple-darwin` + target: Option, + /// This is here to provide a way to see the "unused manifest keys" when deserializing + #[serde(skip_serializing)] + #[serde(flatten)] + other: BTreeMap, } -impl<'de> de::Deserialize<'de> for VecStringOrBool { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - UntaggedEnumVisitor::new() - .expecting("a boolean or vector of strings") - .bool(|value| Ok(VecStringOrBool::Bool(value))) - .seq(|value| value.deserialize().map(VecStringOrBool::VecString)) - .deserialize(deserializer) +impl DetailedTomlDependency { + fn add_features(&mut self, features: Option>) { + self.features = match (self.features.clone(), features.clone()) { + (Some(dep_feat), Some(inherit_feat)) => Some( + dep_feat + .into_iter() + .chain(inherit_feat) + .collect::>(), + ), + (Some(dep_fet), None) => Some(dep_fet), + (None, Some(inherit_feat)) => Some(inherit_feat), + (None, None) => None, + }; } -} -/// This Trait exists to make [`MaybeWorkspace::Workspace`] generic. It makes deserialization of -/// [`MaybeWorkspace`] much easier, as well as making error messages for -/// [`MaybeWorkspace::resolve`] much nicer -/// -/// Implementors should have a field `workspace` with the type of `bool`. It is used to ensure -/// `workspace` is not `false` in a `Cargo.toml` -pub trait WorkspaceInherit { - /// This is the workspace table that is being inherited from. - /// For example `[workspace.dependencies]` would be the table "dependencies" - fn inherit_toml_table(&self) -> &str; + fn update_optional(&mut self, optional: Option) { + self.optional = optional; + } - /// This is used to output the value of the implementors `workspace` field - fn workspace(&self) -> bool; + fn resolve_path( + &mut self, + name: &str, + root_path: &Path, + package_root: &Path, + ) -> CargoResult<()> { + if let Some(rel_path) = &self.path { + self.path = Some(resolve_relative_path( + name, + root_path, + package_root, + rel_path, + )?) + } + Ok(()) + } } -/// An enum that allows for inheriting keys from a workspace in a Cargo.toml. -#[derive(Serialize, Copy, Clone, Debug)] -#[serde(untagged)] -pub enum MaybeWorkspace { - /// The "defined" type, or the type that that is used when not inheriting from a workspace. - Defined(T), - /// The type when inheriting from a workspace. - Workspace(W), -} +impl DetailedTomlDependency

{ + fn to_dependency( + &self, + name_in_toml: &str, + cx: &mut Context<'_, '_>, + kind: Option, + ) -> CargoResult { + if self.version.is_none() && self.path.is_none() && self.git.is_none() { + let msg = format!( + "dependency ({}) specified without \ + providing a local path, Git repository, version, or \ + workspace dependency to use. This will be considered an \ + error in future versions", + name_in_toml + ); + cx.warnings.push(msg); + } -impl MaybeWorkspace { - fn resolve<'a>( - self, - label: &str, - get_ws_inheritable: impl FnOnce() -> CargoResult, - ) -> CargoResult { - match self { - MaybeWorkspace::Defined(value) => Ok(value), - MaybeWorkspace::Workspace(w) => get_ws_inheritable().with_context(|| { - format!( - "error inheriting `{label}` from workspace root manifest's `workspace.{}.{label}`", - w.inherit_toml_table(), - ) - }), + if let Some(version) = &self.version { + if version.contains('+') { + cx.warnings.push(format!( + "version requirement `{}` for dependency `{}` \ + includes semver metadata which will be ignored, removing the \ + metadata is recommended to avoid confusion", + version, name_in_toml + )); + } } - } - fn resolve_with_self<'a>( - self, - label: &str, - get_ws_inheritable: impl FnOnce(&W) -> CargoResult, - ) -> CargoResult { - match self { - MaybeWorkspace::Defined(value) => Ok(value), - MaybeWorkspace::Workspace(w) => get_ws_inheritable(&w).with_context(|| { - format!( - "error inheriting `{label}` from workspace root manifest's `workspace.{}.{label}`", - w.inherit_toml_table(), - ) - }), + if self.git.is_none() { + let git_only_keys = [ + (&self.branch, "branch"), + (&self.tag, "tag"), + (&self.rev, "rev"), + ]; + + for &(key, key_name) in &git_only_keys { + if key.is_some() { + bail!( + "key `{}` is ignored for dependency ({}).", + key_name, + name_in_toml + ); + } + } } - } - fn as_defined(&self) -> Option<&T> { - match self { - MaybeWorkspace::Workspace(_) => None, - MaybeWorkspace::Defined(defined) => Some(defined), + // Early detection of potentially misused feature syntax + // instead of generating a "feature not found" error. + if let Some(features) = &self.features { + for feature in features { + if feature.contains('/') { + bail!( + "feature `{}` in dependency `{}` is not allowed to contain slashes\n\ + If you want to enable features of a transitive dependency, \ + the direct dependency needs to re-export those features from \ + the `[features]` table.", + feature, + name_in_toml + ); + } + if feature.starts_with("dep:") { + bail!( + "feature `{}` in dependency `{}` is not allowed to use explicit \ + `dep:` syntax\n\ + If you want to enable an optional dependency, specify the name \ + of the optional dependency without the `dep:` prefix, or specify \ + a feature from the dependency's `[features]` table that enables \ + the optional dependency.", + feature, + name_in_toml + ); + } + } } - } -} - -type MaybeWorkspaceDependency = MaybeWorkspace; -impl MaybeWorkspaceDependency { - fn unused_keys(&self) -> Vec { - match self { - MaybeWorkspaceDependency::Defined(d) => d.unused_keys(), - MaybeWorkspaceDependency::Workspace(w) => w.other.keys().cloned().collect(), - } - } -} + let new_source_id = match ( + self.git.as_ref(), + self.path.as_ref(), + self.registry.as_ref(), + self.registry_index.as_ref(), + ) { + (Some(_), _, Some(_), _) | (Some(_), _, _, Some(_)) => bail!( + "dependency ({}) specification is ambiguous. \ + Only one of `git` or `registry` is allowed.", + name_in_toml + ), + (_, _, Some(_), Some(_)) => bail!( + "dependency ({}) specification is ambiguous. \ + Only one of `registry` or `registry-index` is allowed.", + name_in_toml + ), + (Some(git), maybe_path, _, _) => { + if maybe_path.is_some() { + bail!( + "dependency ({}) specification is ambiguous. \ + Only one of `git` or `path` is allowed.", + name_in_toml + ); + } -impl<'de> de::Deserialize<'de> for MaybeWorkspaceDependency { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - let value = serde_value::Value::deserialize(deserializer)?; + let n_details = [&self.branch, &self.tag, &self.rev] + .iter() + .filter(|d| d.is_some()) + .count(); - if let Ok(w) = TomlWorkspaceDependency::deserialize(serde_value::ValueDeserializer::< - D::Error, - >::new(value.clone())) - { - return if w.workspace() { - Ok(MaybeWorkspace::Workspace(w)) - } else { - Err(de::Error::custom("`workspace` cannot be false")) - }; - } - TomlDependency::deserialize(serde_value::ValueDeserializer::::new(value)) - .map(MaybeWorkspace::Defined) - } -} + if n_details > 1 { + bail!( + "dependency ({}) specification is ambiguous. \ + Only one of `branch`, `tag` or `rev` is allowed.", + name_in_toml + ); + } -#[derive(Deserialize, Serialize, Clone, Debug)] -#[serde(rename_all = "kebab-case")] -pub struct TomlWorkspaceDependency { - workspace: bool, - features: Option>, - default_features: Option, - #[serde(rename = "default_features")] - default_features2: Option, - optional: Option, - /// This is here to provide a way to see the "unused manifest keys" when deserializing - #[serde(skip_serializing)] - #[serde(flatten)] - other: BTreeMap, -} + let reference = self + .branch + .clone() + .map(GitReference::Branch) + .or_else(|| self.tag.clone().map(GitReference::Tag)) + .or_else(|| self.rev.clone().map(GitReference::Rev)) + .unwrap_or(GitReference::DefaultBranch); + let loc = git.into_url()?; -impl TomlWorkspaceDependency { - fn resolve<'a>( - &self, - name: &str, - inheritable: impl FnOnce() -> CargoResult<&'a InheritableFields>, - cx: &mut Context<'_, '_>, - ) -> CargoResult { - fn default_features_msg(label: &str, ws_def_feat: Option, cx: &mut Context<'_, '_>) { - let ws_def_feat = match ws_def_feat { - Some(true) => "true", - Some(false) => "false", - None => "not specified", - }; - cx.warnings.push(format!( - "`default-features` is ignored for {label}, since `default-features` was \ - {ws_def_feat} for `workspace.dependencies.{label}`, \ - this could become a hard error in the future" - )) - } - if self.default_features.is_some() && self.default_features2.is_some() { - warn_on_deprecated("default-features", name, "dependency", cx.warnings); - } - inheritable()?.get_dependency(name, cx.root).map(|d| { - match d { - TomlDependency::Simple(s) => { - if let Some(false) = self.default_features.or(self.default_features2) { - default_features_msg(name, None, cx); - } - if self.optional.is_some() || self.features.is_some() { - TomlDependency::Detailed(DetailedTomlDependency { - version: Some(s), - optional: self.optional, - features: self.features.clone(), - ..Default::default() - }) - } else { - TomlDependency::Simple(s) - } + if let Some(fragment) = loc.fragment() { + let msg = format!( + "URL fragment `#{}` in git URL is ignored for dependency ({}). \ + If you were trying to specify a specific git revision, \ + use `rev = \"{}\"` in the dependency declaration.", + fragment, name_in_toml, fragment + ); + cx.warnings.push(msg) } - TomlDependency::Detailed(d) => { - let mut d = d.clone(); - match ( - self.default_features.or(self.default_features2), - d.default_features.or(d.default_features2), - ) { - // member: default-features = true and - // workspace: default-features = false should turn on - // default-features - (Some(true), Some(false)) => { - d.default_features = Some(true); - } - // member: default-features = false and - // workspace: default-features = true should ignore member - // default-features - (Some(false), Some(true)) => { - default_features_msg(name, Some(true), cx); - } - // member: default-features = false and - // workspace: dep = "1.0" should ignore member default-features - (Some(false), None) => { - default_features_msg(name, None, cx); - } - _ => {} - } - d.add_features(self.features.clone()); - d.update_optional(self.optional); - TomlDependency::Detailed(d) + + SourceId::for_git(&loc, reference)? + } + (None, Some(path), _, _) => { + let path = path.resolve(cx.config); + cx.nested_paths.push(path.clone()); + // If the source ID for the package we're parsing is a path + // source, then we normalize the path here to get rid of + // components like `..`. + // + // The purpose of this is to get a canonical ID for the package + // that we're depending on to ensure that builds of this package + // always end up hashing to the same value no matter where it's + // built from. + if cx.source_id.is_path() { + let path = cx.root.join(path); + let path = paths::normalize_path(&path); + SourceId::for_path(&path)? + } else { + cx.source_id } } - }) - } -} - -impl WorkspaceInherit for TomlWorkspaceDependency { - fn inherit_toml_table(&self) -> &str { - "dependencies" - } + (None, None, Some(registry), None) => SourceId::alt_registry(cx.config, registry)?, + (None, None, None, Some(registry_index)) => { + let url = registry_index.into_url()?; + SourceId::for_registry(&url)? + } + (None, None, None, None) => SourceId::crates_io(cx.config)?, + }; - fn workspace(&self) -> bool { - self.workspace - } -} + let (pkg_name, explicit_name_in_toml) = match self.package { + Some(ref s) => (&s[..], Some(name_in_toml)), + None => (name_in_toml, None), + }; -//. This already has a `Deserialize` impl from version_trim_whitespace -type MaybeWorkspaceSemverVersion = MaybeWorkspace; + let version = self.version.as_deref(); + let mut dep = Dependency::parse(pkg_name, version, new_source_id)?; + if self.default_features.is_some() && self.default_features2.is_some() { + warn_on_deprecated("default-features", name_in_toml, "dependency", cx.warnings); + } + dep.set_features(self.features.iter().flatten()) + .set_default_features( + self.default_features + .or(self.default_features2) + .unwrap_or(true), + ) + .set_optional(self.optional.unwrap_or(false)) + .set_platform(cx.platform.clone()); + if let Some(registry) = &self.registry { + let registry_id = SourceId::alt_registry(cx.config, registry)?; + dep.set_registry_id(registry_id); + } + if let Some(registry_index) = &self.registry_index { + let url = registry_index.into_url()?; + let registry_id = SourceId::for_registry(&url)?; + dep.set_registry_id(registry_id); + } -type MaybeWorkspaceString = MaybeWorkspace; -impl<'de> de::Deserialize<'de> for MaybeWorkspaceString { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - struct Visitor; + if let Some(kind) = kind { + dep.set_kind(kind); + } + if let Some(name_in_toml) = explicit_name_in_toml { + dep.set_explicit_name_in_toml(name_in_toml); + } - impl<'de> de::Visitor<'de> for Visitor { - type Value = MaybeWorkspaceString; + if let Some(p) = self.public { + cx.features.require(Feature::public_dependency())?; - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - f.write_str("a string or workspace") + if dep.kind() != DepKind::Normal { + bail!("'public' specifier can only be used on regular dependencies, not {:?} dependencies", dep.kind()); } - fn visit_string(self, value: String) -> Result - where - E: de::Error, - { - Ok(MaybeWorkspaceString::Defined(value)) - } + dep.set_public(p); + } - fn visit_map(self, map: V) -> Result - where - V: de::MapAccess<'de>, - { - let mvd = de::value::MapAccessDeserializer::new(map); - TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + if let (Some(artifact), is_lib, target) = ( + self.artifact.as_ref(), + self.lib.unwrap_or(false), + self.target.as_deref(), + ) { + if cx.config.cli_unstable().bindeps { + let artifact = Artifact::parse(&artifact.0, is_lib, target)?; + if dep.kind() != DepKind::Build + && artifact.target() == Some(ArtifactTarget::BuildDependencyAssumeTarget) + { + bail!( + r#"`target = "target"` in normal- or dev-dependencies has no effect ({})"#, + name_in_toml + ); + } + dep.set_artifact(artifact) + } else { + bail!("`artifact = …` requires `-Z bindeps` ({})", name_in_toml); + } + } else if self.lib.is_some() || self.target.is_some() { + for (is_set, specifier) in [ + (self.lib.is_some(), "lib"), + (self.target.is_some(), "target"), + ] { + if !is_set { + continue; + } + bail!( + "'{}' specifier cannot be used without an 'artifact = …' value ({})", + specifier, + name_in_toml + ) } } - - d.deserialize_any(Visitor) + Ok(dep) } } -type MaybeWorkspaceRustVersion = MaybeWorkspace; -impl<'de> de::Deserialize<'de> for MaybeWorkspaceRustVersion { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - struct Visitor; +// Explicit implementation so we avoid pulling in P: Default +impl Default for DetailedTomlDependency

{ + fn default() -> Self { + Self { + version: Default::default(), + registry: Default::default(), + registry_index: Default::default(), + path: Default::default(), + git: Default::default(), + branch: Default::default(), + tag: Default::default(), + rev: Default::default(), + features: Default::default(), + optional: Default::default(), + default_features: Default::default(), + default_features2: Default::default(), + package: Default::default(), + public: Default::default(), + artifact: Default::default(), + lib: Default::default(), + target: Default::default(), + other: Default::default(), + } + } +} - impl<'de> de::Visitor<'de> for Visitor { - type Value = MaybeWorkspaceRustVersion; +#[derive(Deserialize, Serialize, Clone, Debug, Default)] +pub struct TomlProfiles(BTreeMap); - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - f.write_str("a semver or workspace") - } +impl TomlProfiles { + pub fn get_all(&self) -> &BTreeMap { + &self.0 + } - fn visit_string(self, value: String) -> Result - where - E: de::Error, - { - let value = value.parse::().map_err(|e| E::custom(e))?; - Ok(MaybeWorkspaceRustVersion::Defined(value)) - } + pub fn get(&self, name: &str) -> Option<&TomlProfile> { + self.0.get(name) + } - fn visit_map(self, map: V) -> Result - where - V: de::MapAccess<'de>, - { - let mvd = de::value::MapAccessDeserializer::new(map); - TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) - } + /// Checks syntax validity and unstable feature gate for each profile. + /// + /// It's a bit unfortunate both `-Z` flags and `cargo-features` are required, + /// because profiles can now be set in either `Cargo.toml` or `config.toml`. + pub fn validate( + &self, + cli_unstable: &CliUnstable, + features: &Features, + warnings: &mut Vec, + ) -> CargoResult<()> { + for (name, profile) in &self.0 { + profile.validate(name, cli_unstable, features, warnings)?; } - - d.deserialize_any(Visitor) + Ok(()) } } -type MaybeWorkspaceVecString = MaybeWorkspace, TomlWorkspaceField>; -impl<'de> de::Deserialize<'de> for MaybeWorkspaceVecString { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - struct Visitor; - - impl<'de> de::Visitor<'de> for Visitor { - type Value = MaybeWorkspaceVecString; - - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - f.write_str("a vector of strings or workspace") - } - fn visit_seq(self, v: A) -> Result - where - A: de::SeqAccess<'de>, - { - let seq = de::value::SeqAccessDeserializer::new(v); - Vec::deserialize(seq).map(MaybeWorkspace::Defined) - } +#[derive(Deserialize, Serialize, Clone, Debug, Default, Eq, PartialEq)] +#[serde(default, rename_all = "kebab-case")] +pub struct TomlProfile { + pub opt_level: Option, + pub lto: Option, + pub codegen_backend: Option, + pub codegen_units: Option, + pub debug: Option, + pub split_debuginfo: Option, + pub debug_assertions: Option, + pub rpath: Option, + pub panic: Option, + pub overflow_checks: Option, + pub incremental: Option, + pub dir_name: Option, + pub inherits: Option, + pub strip: Option, + // Note that `rustflags` is used for the cargo-feature `profile_rustflags` + pub rustflags: Option>, + // These two fields must be last because they are sub-tables, and TOML + // requires all non-tables to be listed first. + pub package: Option>, + pub build_override: Option>, +} - fn visit_map(self, map: V) -> Result - where - V: de::MapAccess<'de>, - { - let mvd = de::value::MapAccessDeserializer::new(map); - TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) +impl TomlProfile { + /// Checks stytax validity and unstable feature gate for a given profile. + pub fn validate( + &self, + name: &str, + cli_unstable: &CliUnstable, + features: &Features, + warnings: &mut Vec, + ) -> CargoResult<()> { + self.validate_profile(name, cli_unstable, features)?; + if let Some(ref profile) = self.build_override { + profile.validate_override("build-override")?; + profile.validate_profile(&format!("{name}.build-override"), cli_unstable, features)?; + } + if let Some(ref packages) = self.package { + for (override_name, profile) in packages { + profile.validate_override("package")?; + profile.validate_profile( + &format!("{name}.package.{override_name}"), + cli_unstable, + features, + )?; } } - d.deserialize_any(Visitor) - } -} + // Profile name validation + Self::validate_name(name)?; -type MaybeWorkspaceStringOrBool = MaybeWorkspace; -impl<'de> de::Deserialize<'de> for MaybeWorkspaceStringOrBool { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - struct Visitor; + if let Some(dir_name) = self.dir_name { + // This is disabled for now, as we would like to stabilize named + // profiles without this, and then decide in the future if it is + // needed. This helps simplify the UI a little. + bail!( + "dir-name=\"{}\" in profile `{}` is not currently allowed, \ + directory names are tied to the profile name for custom profiles", + dir_name, + name + ); + } - impl<'de> de::Visitor<'de> for Visitor { - type Value = MaybeWorkspaceStringOrBool; + // `inherits` validation + if matches!(self.inherits.map(|s| s.as_str()), Some("debug")) { + bail!( + "profile.{}.inherits=\"debug\" should be profile.{}.inherits=\"dev\"", + name, + name + ); + } - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - f.write_str("a string, a bool, or workspace") + match name { + "doc" => { + warnings.push("profile `doc` is deprecated and has no effect".to_string()); } - - fn visit_bool(self, v: bool) -> Result - where - E: de::Error, - { - let b = de::value::BoolDeserializer::new(v); - StringOrBool::deserialize(b).map(MaybeWorkspace::Defined) + "test" | "bench" => { + if self.panic.is_some() { + warnings.push(format!("`panic` setting is ignored for `{}` profile", name)) + } } + _ => {} + } - fn visit_string(self, v: String) -> Result - where - E: de::Error, - { - let string = de::value::StringDeserializer::new(v); - StringOrBool::deserialize(string).map(MaybeWorkspace::Defined) + if let Some(panic) = &self.panic { + if panic != "unwind" && panic != "abort" { + bail!( + "`panic` setting of `{}` is not a valid setting, \ + must be `unwind` or `abort`", + panic + ); } + } - fn visit_map(self, map: V) -> Result - where - V: de::MapAccess<'de>, - { - let mvd = de::value::MapAccessDeserializer::new(map); - TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + if let Some(StringOrBool::String(arg)) = &self.lto { + if arg == "true" || arg == "false" { + bail!( + "`lto` setting of string `\"{arg}\"` for `{name}` profile is not \ + a valid setting, must be a boolean (`true`/`false`) or a string \ + (`\"thin\"`/`\"fat\"`/`\"off\"`) or omitted.", + ); } } - d.deserialize_any(Visitor) + Ok(()) } -} -type MaybeWorkspaceVecStringOrBool = MaybeWorkspace; -impl<'de> de::Deserialize<'de> for MaybeWorkspaceVecStringOrBool { - fn deserialize(d: D) -> Result - where - D: de::Deserializer<'de>, - { - struct Visitor; + /// Validate dir-names and profile names according to RFC 2678. + pub fn validate_name(name: &str) -> CargoResult<()> { + if let Some(ch) = name + .chars() + .find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-') + { + bail!( + "invalid character `{}` in profile name `{}`\n\ + Allowed characters are letters, numbers, underscore, and hyphen.", + ch, + name + ); + } - impl<'de> de::Visitor<'de> for Visitor { - type Value = MaybeWorkspaceVecStringOrBool; + const SEE_DOCS: &str = "See https://doc.rust-lang.org/cargo/reference/profiles.html \ + for more on configuring profiles."; - fn expecting(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - f.write_str("a boolean, a vector of strings, or workspace") - } + let lower_name = name.to_lowercase(); + if lower_name == "debug" { + bail!( + "profile name `{}` is reserved\n\ + To configure the default development profile, use the name `dev` \ + as in [profile.dev]\n\ + {}", + name, + SEE_DOCS + ); + } + if lower_name == "build-override" { + bail!( + "profile name `{}` is reserved\n\ + To configure build dependency settings, use [profile.dev.build-override] \ + and [profile.release.build-override]\n\ + {}", + name, + SEE_DOCS + ); + } - fn visit_bool(self, v: bool) -> Result - where - E: de::Error, - { - let b = de::value::BoolDeserializer::new(v); - VecStringOrBool::deserialize(b).map(MaybeWorkspace::Defined) - } + // These are some arbitrary reservations. We have no plans to use + // these, but it seems safer to reserve a few just in case we want to + // add more built-in profiles in the future. We can also uses special + // syntax like cargo:foo if needed. But it is unlikely these will ever + // be used. + if matches!( + lower_name.as_str(), + "build" + | "check" + | "clean" + | "config" + | "fetch" + | "fix" + | "install" + | "metadata" + | "package" + | "publish" + | "report" + | "root" + | "run" + | "rust" + | "rustc" + | "rustdoc" + | "target" + | "tmp" + | "uninstall" + ) || lower_name.starts_with("cargo") + { + bail!( + "profile name `{}` is reserved\n\ + Please choose a different name.\n\ + {}", + name, + SEE_DOCS + ); + } - fn visit_seq(self, v: A) -> Result - where - A: de::SeqAccess<'de>, - { - let seq = de::value::SeqAccessDeserializer::new(v); - VecStringOrBool::deserialize(seq).map(MaybeWorkspace::Defined) + Ok(()) + } + + /// Validates a profile. + /// + /// This is a shallow check, which is reused for the profile itself and any overrides. + fn validate_profile( + &self, + name: &str, + cli_unstable: &CliUnstable, + features: &Features, + ) -> CargoResult<()> { + if let Some(codegen_backend) = &self.codegen_backend { + match ( + features.require(Feature::codegen_backend()), + cli_unstable.codegen_backend, + ) { + (Err(e), false) => return Err(e), + _ => {} } - fn visit_map(self, map: V) -> Result - where - V: de::MapAccess<'de>, - { - let mvd = de::value::MapAccessDeserializer::new(map); - TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace) + if codegen_backend.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') { + bail!( + "`profile.{}.codegen-backend` setting of `{}` is not a valid backend name.", + name, + codegen_backend, + ); + } + } + if self.rustflags.is_some() { + match ( + features.require(Feature::profile_rustflags()), + cli_unstable.profile_rustflags, + ) { + (Err(e), false) => return Err(e), + _ => {} } } + Ok(()) + } - d.deserialize_any(Visitor) + /// Validation that is specific to an override. + fn validate_override(&self, which: &str) -> CargoResult<()> { + if self.package.is_some() { + bail!("package-specific profiles cannot be nested"); + } + if self.build_override.is_some() { + bail!("build-override profiles cannot be nested"); + } + if self.panic.is_some() { + bail!("`panic` may not be specified in a `{}` profile", which) + } + if self.lto.is_some() { + bail!("`lto` may not be specified in a `{}` profile", which) + } + if self.rpath.is_some() { + bail!("`rpath` may not be specified in a `{}` profile", which) + } + Ok(()) } -} -type MaybeWorkspaceBtreeMap = - MaybeWorkspace>, TomlWorkspaceField>; + /// Overwrite self's values with the given profile. + pub fn merge(&mut self, profile: &TomlProfile) { + if let Some(v) = &profile.opt_level { + self.opt_level = Some(v.clone()); + } -impl<'de> de::Deserialize<'de> for MaybeWorkspaceBtreeMap { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - let value = serde_value::Value::deserialize(deserializer)?; + if let Some(v) = &profile.lto { + self.lto = Some(v.clone()); + } - if let Ok(w) = TomlWorkspaceField::deserialize( - serde_value::ValueDeserializer::::new(value.clone()), - ) { - return if w.workspace() { - Ok(MaybeWorkspace::Workspace(w)) - } else { - Err(de::Error::custom("`workspace` cannot be false")) - }; + if let Some(v) = profile.codegen_backend { + self.codegen_backend = Some(v); } - BTreeMap::deserialize(serde_value::ValueDeserializer::::new(value)) - .map(MaybeWorkspace::Defined) - } -} -#[derive(Deserialize, Serialize, Copy, Clone, Debug)] -pub struct TomlWorkspaceField { - #[serde(deserialize_with = "bool_no_false")] - workspace: bool, -} + if let Some(v) = profile.codegen_units { + self.codegen_units = Some(v); + } -impl WorkspaceInherit for TomlWorkspaceField { - fn inherit_toml_table(&self) -> &str { - "package" - } + if let Some(v) = profile.debug { + self.debug = Some(v); + } - fn workspace(&self) -> bool { - self.workspace - } -} + if let Some(v) = profile.debug_assertions { + self.debug_assertions = Some(v); + } -fn bool_no_false<'de, D: de::Deserializer<'de>>(deserializer: D) -> Result { - let b: bool = Deserialize::deserialize(deserializer)?; - if b { - Ok(b) - } else { - Err(de::Error::custom("`workspace` cannot be false")) - } -} + if let Some(v) = &profile.split_debuginfo { + self.split_debuginfo = Some(v.clone()); + } + + if let Some(v) = profile.rpath { + self.rpath = Some(v); + } -/// Represents the `package`/`project` sections of a `Cargo.toml`. -/// -/// Note that the order of the fields matters, since this is the order they -/// are serialized to a TOML file. For example, you cannot have values after -/// the field `metadata`, since it is a table and values cannot appear after -/// tables. -#[derive(Deserialize, Serialize, Clone, Debug)] -#[serde(rename_all = "kebab-case")] -pub struct TomlPackage { - edition: Option, - rust_version: Option, - name: InternedString, - #[serde(deserialize_with = "version_trim_whitespace")] - version: MaybeWorkspaceSemverVersion, - authors: Option, - build: Option, - metabuild: Option, - default_target: Option, - forced_target: Option, - links: Option, - exclude: Option, - include: Option, - publish: Option, - workspace: Option, - im_a_teapot: Option, - autobins: Option, - autoexamples: Option, - autotests: Option, - autobenches: Option, - default_run: Option, + if let Some(v) = &profile.panic { + self.panic = Some(v.clone()); + } - // Package metadata. - description: Option, - homepage: Option, - documentation: Option, - readme: Option, - keywords: Option, - categories: Option, - license: Option, - license_file: Option, - repository: Option, - resolver: Option, + if let Some(v) = profile.overflow_checks { + self.overflow_checks = Some(v); + } - // Provide a helpful error message for a common user error. - #[serde(rename = "cargo-features", skip_serializing)] - _invalid_cargo_features: Option, + if let Some(v) = profile.incremental { + self.incremental = Some(v); + } - // Note that this field must come last due to the way toml serialization - // works which requires tables to be emitted after all values. - metadata: Option, -} + if let Some(v) = &profile.rustflags { + self.rustflags = Some(v.clone()); + } -impl TomlPackage { - pub fn to_package_id( - &self, - source_id: SourceId, - version: semver::Version, - ) -> CargoResult { - PackageId::new(self.name, version, source_id) - } -} + 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()), + } + } -fn version_trim_whitespace<'de, D>(deserializer: D) -> Result -where - D: de::Deserializer<'de>, -{ - UntaggedEnumVisitor::new() - .expecting("SemVer version") - .string( - |value| match value.trim().parse().map_err(de::Error::custom) { - Ok(parsed) => Ok(MaybeWorkspace::Defined(parsed)), - Err(e) => Err(e), - }, - ) - .map(|value| value.deserialize().map(MaybeWorkspace::Workspace)) - .deserialize(deserializer) -} + 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()), + } + } -#[derive(Debug, Deserialize, Serialize, Clone)] -pub struct TomlWorkspace { - members: Option>, - #[serde(rename = "default-members")] - default_members: Option>, - exclude: Option>, - resolver: Option, + if let Some(v) = &profile.inherits { + self.inherits = Some(*v); + } - // Properties that can be inherited by members. - package: Option, - dependencies: Option>, - lints: Option, + if let Some(v) = &profile.dir_name { + self.dir_name = Some(*v); + } - // Note that this field must come last due to the way toml serialization - // works which requires tables to be emitted after all values. - metadata: Option, + if let Some(v) = &profile.strip { + self.strip = Some(v.clone()); + } + } } -/// A group of fields that are inheritable by members of the workspace -#[derive(Clone, Debug, Default, Deserialize, Serialize)] -pub struct InheritableFields { - // We use skip here since it will never be present when deserializing - // and we don't want it present when serializing - #[serde(skip)] - dependencies: Option>, - #[serde(skip)] - lints: Option, - - version: Option, - authors: Option>, - description: Option, - homepage: Option, - documentation: Option, - readme: Option, - keywords: Option>, - categories: Option>, - license: Option, - #[serde(rename = "license-file")] - license_file: Option, - repository: Option, - publish: Option, - edition: Option, - badges: Option>>, - exclude: Option>, - include: Option>, - #[serde(rename = "rust-version")] - rust_version: Option, - // We use skip here since it will never be present when deserializing - // and we don't want it present when serializing - #[serde(skip)] - ws_root: PathBuf, +#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] +pub enum ProfilePackageSpec { + Spec(PackageIdSpec), + All, } -/// Defines simple getter methods for inheritable fields. -macro_rules! inheritable_field_getter { - ( $(($key:literal, $field:ident -> $ret:ty),)* ) => ( - $( - #[doc = concat!("Gets the field `workspace.", $key, "`.")] - pub fn $field(&self) -> CargoResult<$ret> { - let Some(val) = &self.$field else { - bail!("`workspace.{}` was not defined", $key); - }; - Ok(val.clone()) - } - )* - ) +impl fmt::Display for ProfilePackageSpec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ProfilePackageSpec::Spec(spec) => spec.fmt(f), + ProfilePackageSpec::All => f.write_str("*"), + } + } } -impl InheritableFields { - inheritable_field_getter! { - // Please keep this list lexicographically ordered. - ("dependencies", dependencies -> BTreeMap), - ("lints", lints -> TomlLints), - ("package.authors", authors -> Vec), - ("package.badges", badges -> BTreeMap>), - ("package.categories", categories -> Vec), - ("package.description", description -> String), - ("package.documentation", documentation -> String), - ("package.edition", edition -> String), - ("package.exclude", exclude -> Vec), - ("package.homepage", homepage -> String), - ("package.include", include -> Vec), - ("package.keywords", keywords -> Vec), - ("package.license", license -> String), - ("package.publish", publish -> VecStringOrBool), - ("package.repository", repository -> String), - ("package.rust-version", rust_version -> RustVersion), - ("package.version", version -> semver::Version), +impl ser::Serialize for ProfilePackageSpec { + fn serialize(&self, s: S) -> Result + where + S: ser::Serializer, + { + self.to_string().serialize(s) } +} - /// Gets a workspace dependency with the `name`. - pub fn get_dependency(&self, name: &str, package_root: &Path) -> CargoResult { - let Some(deps) = &self.dependencies else { - bail!("`workspace.dependencies` was not defined"); - }; - let Some(dep) = deps.get(name) else { - bail!("`dependency.{name}` was not found in `workspace.dependencies`"); - }; - let mut dep = dep.clone(); - if let TomlDependency::Detailed(detailed) = &mut dep { - detailed.resolve_path(name, self.ws_root(), package_root)?; +impl<'de> de::Deserialize<'de> for ProfilePackageSpec { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + let string = String::deserialize(d)?; + if string == "*" { + Ok(ProfilePackageSpec::All) + } else { + PackageIdSpec::parse(&string) + .map_err(de::Error::custom) + .map(ProfilePackageSpec::Spec) } - Ok(dep) } +} - /// Gets the field `workspace.package.license-file`. - pub fn license_file(&self, package_root: &Path) -> CargoResult { - let Some(license_file) = &self.license_file else { - bail!("`workspace.package.license-file` was not defined"); - }; - resolve_relative_path("license-file", &self.ws_root, package_root, license_file) +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct TomlOptLevel(pub String); + +impl ser::Serialize for TomlOptLevel { + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + match self.0.parse::() { + Ok(n) => n.serialize(serializer), + Err(_) => self.0.serialize(serializer), + } } +} - /// Gets the field `workspace.package.readme`. - pub fn readme(&self, package_root: &Path) -> CargoResult { - let Some(readme) = readme_for_package(self.ws_root.as_path(), self.readme.as_ref()) else { - bail!("`workspace.package.readme` was not defined"); - }; - resolve_relative_path("readme", &self.ws_root, package_root, &readme) - .map(StringOrBool::String) +impl<'de> de::Deserialize<'de> for TomlOptLevel { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + use serde::de::Error as _; + UntaggedEnumVisitor::new() + .expecting("an optimization level") + .i64(|value| Ok(TomlOptLevel(value.to_string()))) + .string(|value| { + if value == "s" || value == "z" { + Ok(TomlOptLevel(value.to_string())) + } else { + Err(serde_untagged::de::Error::custom(format!( + "must be `0`, `1`, `2`, `3`, `s` or `z`, \ + but found the string: \"{}\"", + value + ))) + } + }) + .deserialize(d) } +} - pub fn ws_root(&self) -> &PathBuf { - &self.ws_root - } +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)] +pub enum TomlDebugInfo { + None, + LineDirectivesOnly, + LineTablesOnly, + Limited, + Full, +} - pub fn update_deps(&mut self, deps: Option>) { - self.dependencies = deps; +impl Display for TomlDebugInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + TomlDebugInfo::None => f.write_char('0'), + TomlDebugInfo::Limited => f.write_char('1'), + TomlDebugInfo::Full => f.write_char('2'), + TomlDebugInfo::LineDirectivesOnly => f.write_str("line-directives-only"), + TomlDebugInfo::LineTablesOnly => f.write_str("line-tables-only"), + } } +} - pub fn update_lints(&mut self, lints: Option) { - self.lints = lints; +impl ser::Serialize for TomlDebugInfo { + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + match self { + Self::None => 0.serialize(serializer), + Self::LineDirectivesOnly => "line-directives-only".serialize(serializer), + Self::LineTablesOnly => "line-tables-only".serialize(serializer), + Self::Limited => 1.serialize(serializer), + Self::Full => 2.serialize(serializer), + } } +} - pub fn update_ws_path(&mut self, ws_root: PathBuf) { - self.ws_root = ws_root; +impl<'de> de::Deserialize<'de> for TomlDebugInfo { + fn deserialize(d: D) -> Result + where + D: de::Deserializer<'de>, + { + use serde::de::Error as _; + let expecting = "a boolean, 0, 1, 2, \"line-tables-only\", or \"line-directives-only\""; + UntaggedEnumVisitor::new() + .expecting(expecting) + .bool(|value| { + Ok(if value { + TomlDebugInfo::Full + } else { + TomlDebugInfo::None + }) + }) + .i64(|value| { + let debuginfo = match value { + 0 => TomlDebugInfo::None, + 1 => TomlDebugInfo::Limited, + 2 => TomlDebugInfo::Full, + _ => { + return Err(serde_untagged::de::Error::invalid_value( + Unexpected::Signed(value), + &expecting, + )) + } + }; + Ok(debuginfo) + }) + .string(|value| { + let debuginfo = match value { + "none" => TomlDebugInfo::None, + "limited" => TomlDebugInfo::Limited, + "full" => TomlDebugInfo::Full, + "line-directives-only" => TomlDebugInfo::LineDirectivesOnly, + "line-tables-only" => TomlDebugInfo::LineTablesOnly, + _ => { + return Err(serde_untagged::de::Error::invalid_value( + Unexpected::Str(value), + &expecting, + )) + } + }; + Ok(debuginfo) + }) + .deserialize(d) } } +type TomlLibTarget = TomlTarget; +type TomlBinTarget = TomlTarget; +type TomlExampleTarget = TomlTarget; +type TomlTestTarget = TomlTarget; +type TomlBenchTarget = TomlTarget; + #[derive(Default, Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "kebab-case")] struct TomlTarget { @@ -3304,33 +3225,6 @@ impl TomlTarget { } } -#[derive(Clone)] -struct PathValue(PathBuf); - -impl fmt::Debug for PathValue { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) - } -} - -impl ser::Serialize for PathValue { - fn serialize(&self, serializer: S) -> Result - where - S: ser::Serializer, - { - self.0.serialize(serializer) - } -} - -impl<'de> de::Deserialize<'de> for PathValue { - fn deserialize(deserializer: D) -> Result - where - D: de::Deserializer<'de>, - { - Ok(PathValue(String::deserialize(deserializer)?.into())) - } -} - /// Corresponds to a `target` entry, but `TomlTarget` is already used. #[derive(Serialize, Deserialize, Debug, Clone)] struct TomlPlatform { @@ -3462,3 +3356,109 @@ impl<'de> de::Deserialize<'de> for InvalidCargoFeatures { )) } } + +pub trait ResolveToPath { + fn resolve(&self, config: &Config) -> PathBuf; +} + +impl ResolveToPath for String { + fn resolve(&self, _: &Config) -> PathBuf { + self.into() + } +} + +impl ResolveToPath for ConfigRelativePath { + fn resolve(&self, c: &Config) -> PathBuf { + self.resolve_path(c) + } +} + +/// A StringOrVec can be parsed from either a TOML string or array, +/// but is always stored as a vector. +#[derive(Clone, Debug, Serialize, Eq, PartialEq, PartialOrd, Ord)] +pub struct StringOrVec(Vec); + +impl StringOrVec { + pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, String> { + self.0.iter() + } +} + +impl<'de> de::Deserialize<'de> for StringOrVec { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + UntaggedEnumVisitor::new() + .expecting("string or list of strings") + .string(|value| Ok(StringOrVec(vec![value.to_owned()]))) + .seq(|value| value.deserialize().map(StringOrVec)) + .deserialize(deserializer) + } +} + +#[derive(Clone, Debug, Serialize, Eq, PartialEq)] +#[serde(untagged)] +pub enum StringOrBool { + String(String), + Bool(bool), +} + +impl<'de> Deserialize<'de> for StringOrBool { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + UntaggedEnumVisitor::new() + .bool(|b| Ok(StringOrBool::Bool(b))) + .string(|s| Ok(StringOrBool::String(s.to_owned()))) + .deserialize(deserializer) + } +} + +#[derive(PartialEq, Clone, Debug, Serialize)] +#[serde(untagged)] +pub enum VecStringOrBool { + VecString(Vec), + Bool(bool), +} + +impl<'de> de::Deserialize<'de> for VecStringOrBool { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + UntaggedEnumVisitor::new() + .expecting("a boolean or vector of strings") + .bool(|value| Ok(VecStringOrBool::Bool(value))) + .seq(|value| value.deserialize().map(VecStringOrBool::VecString)) + .deserialize(deserializer) + } +} + +#[derive(Clone)] +struct PathValue(PathBuf); + +impl fmt::Debug for PathValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl ser::Serialize for PathValue { + fn serialize(&self, serializer: S) -> Result + where + S: ser::Serializer, + { + self.0.serialize(serializer) + } +} + +impl<'de> de::Deserialize<'de> for PathValue { + fn deserialize(deserializer: D) -> Result + where + D: de::Deserializer<'de>, + { + Ok(PathValue(String::deserialize(deserializer)?.into())) + } +}