Skip to content

refactor(toml): Expose surce/spans for VirtualManifests #13603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util-schemas"
version = "0.3.0"
version = "0.3.1"
rust-version = "1.76.0" # MSRV:1
edition.workspace = true
license.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub use rust_version::RustVersion;
pub use rust_version::RustVersionError;

/// This type is used to deserialize `Cargo.toml` files.
#[derive(Debug, Deserialize, Serialize)]
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct TomlManifest {
// when adding new fields, be sure to check whether `requires_package` should disallow them
Expand Down
6 changes: 5 additions & 1 deletion src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::util::errors::CargoResult;
use crate::GlobalContext;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::rc::Rc;

use super::BuildConfig;

Expand Down Expand Up @@ -103,10 +104,13 @@ pub fn resolve_std<'gctx>(
/*custom_metadata*/ &None,
));
let virtual_manifest = crate::core::VirtualManifest::new(
Rc::default(),
Rc::new(toml_edit::ImDocument::parse("".to_owned()).expect("empty is valid TOML")),
Rc::default(),
Rc::default(),
/*replace*/ Vec::new(),
patch,
ws_config,
/*profiles*/ None,
crate::core::Features::default(),
None,
);
Expand Down
19 changes: 7 additions & 12 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ impl Features {
warnings: &mut Vec<String>,
) -> CargoResult<()> {
let nightly_features_allowed = self.nightly_features_allowed;
let is_local = self.is_local;
let Some((slot, feature)) = self.status(feature_name) else {
bail!("unknown cargo feature `{}`", feature_name)
};
Expand All @@ -567,19 +566,15 @@ impl Features {

match feature.stability {
Status::Stable => {
// The user can't do anything about non-local packages.
// Warnings are usually suppressed, but just being cautious here.
if is_local {
let warning = format!(
"the cargo feature `{}` has been stabilized in the {} \
let warning = format!(
"the cargo feature `{}` has been stabilized in the {} \
release and is no longer necessary to be listed in the \
manifest\n {}",
feature_name,
feature.version,
see_docs()
);
warnings.push(warning);
}
feature_name,
feature.version,
see_docs()
);
warnings.push(warning);
}
Status::Unstable if !nightly_features_allowed => bail!(
"the cargo feature `{}` requires a nightly version of \
Expand Down
49 changes: 41 additions & 8 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct Manifest {
// alternate forms of manifests:
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
summary: Summary,

Expand All @@ -57,7 +58,6 @@ pub struct Manifest {
include: Vec<String>,
metadata: ManifestMetadata,
custom_metadata: Option<toml::Value>,
profiles: Option<TomlProfiles>,
publish: Option<Vec<String>>,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
Expand Down Expand Up @@ -87,10 +87,16 @@ pub struct Warnings(Vec<DelayedWarning>);

#[derive(Clone, Debug)]
pub struct VirtualManifest {
// alternate forms of manifests:
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,

// this form of manifest:
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
profiles: Option<TomlProfiles>,
warnings: Warnings,
features: Features,
resolve_behavior: Option<ResolveBehavior>,
Expand Down Expand Up @@ -396,6 +402,7 @@ impl Manifest {
pub fn new(
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
summary: Summary,

Expand All @@ -407,7 +414,6 @@ impl Manifest {
links: Option<String>,
metadata: ManifestMetadata,
custom_metadata: Option<toml::Value>,
profiles: Option<TomlProfiles>,
publish: Option<Vec<String>>,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
Expand All @@ -425,6 +431,7 @@ impl Manifest {
Manifest {
contents,
document,
original_toml,
resolved_toml,
summary,

Expand All @@ -437,7 +444,6 @@ impl Manifest {
links,
metadata,
custom_metadata,
profiles,
publish,
replace,
patch,
Expand All @@ -462,6 +468,10 @@ impl Manifest {
pub fn document(&self) -> &toml_edit::ImDocument<String> {
&self.document
}
/// The [`TomlManifest`] as parsed from [`Manifest::document`]
pub fn original_toml(&self) -> &TomlManifest {
&self.original_toml
}
/// The [`TomlManifest`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
Expand Down Expand Up @@ -514,7 +524,7 @@ impl Manifest {
&self.warnings
}
pub fn profiles(&self) -> Option<&TomlProfiles> {
self.profiles.as_ref()
self.resolved_toml.profile.as_ref()
}
pub fn publish(&self) -> &Option<Vec<String>> {
&self.publish
Expand Down Expand Up @@ -622,24 +632,47 @@ impl Manifest {

impl VirtualManifest {
pub fn new(
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
original_toml: Rc<TomlManifest>,
resolved_toml: Rc<TomlManifest>,
replace: Vec<(PackageIdSpec, Dependency)>,
patch: HashMap<Url, Vec<Dependency>>,
workspace: WorkspaceConfig,
profiles: Option<TomlProfiles>,
features: Features,
resolve_behavior: Option<ResolveBehavior>,
) -> VirtualManifest {
VirtualManifest {
contents,
document,
original_toml,
resolved_toml,
replace,
patch,
workspace,
profiles,
warnings: Warnings::new(),
features,
resolve_behavior,
}
}

/// The raw contents of the original TOML
pub fn contents(&self) -> &str {
self.contents.as_str()
}
/// Collection of spans for the original TOML
pub fn document(&self) -> &toml_edit::ImDocument<String> {
&self.document
}
/// The [`TomlManifest`] as parsed from [`VirtualManifest::document`]
pub fn original_toml(&self) -> &TomlManifest {
&self.original_toml
}
/// The [`TomlManifest`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
}

pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace
}
Expand All @@ -653,7 +686,7 @@ impl VirtualManifest {
}

pub fn profiles(&self) -> Option<&TomlProfiles> {
self.profiles.as_ref()
self.resolved_toml.profile.as_ref()
}

pub fn warnings_mut(&mut self) -> &mut Warnings {
Expand Down
Loading