Skip to content

refactor: Expose source/spans to Manifest for emitting lints #13593

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 3 commits into from
Mar 15, 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
34 changes: 25 additions & 9 deletions src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl EitherManifest {
#[derive(Clone, Debug)]
pub struct Manifest {
// alternate forms of manifests:
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
resolved_toml: Rc<TomlManifest>,
summary: Summary,

Expand Down Expand Up @@ -389,6 +391,8 @@ compact_debug! {

impl Manifest {
pub fn new(
contents: Rc<String>,
document: Rc<toml_edit::ImDocument<String>>,
resolved_toml: Rc<TomlManifest>,
summary: Summary,

Expand Down Expand Up @@ -416,6 +420,8 @@ impl Manifest {
embedded: bool,
) -> Manifest {
Manifest {
contents,
document,
resolved_toml,
summary,

Expand Down Expand Up @@ -445,6 +451,25 @@ impl Manifest {
}
}

/// 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`] with all fields expanded
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
}
pub fn summary(&self) -> &Summary {
&self.summary
}
pub fn summary_mut(&mut self) -> &mut Summary {
&mut self.summary
}

pub fn dependencies(&self) -> &[Dependency] {
self.summary.dependencies()
}
Expand All @@ -469,12 +494,6 @@ impl Manifest {
pub fn package_id(&self) -> PackageId {
self.summary.package_id()
}
pub fn summary(&self) -> &Summary {
&self.summary
}
pub fn summary_mut(&mut self) -> &mut Summary {
&mut self.summary
}
pub fn targets(&self) -> &[Target] {
&self.targets
}
Expand All @@ -500,9 +519,6 @@ impl Manifest {
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
&self.replace
}
pub fn resolved_toml(&self) -> &TomlManifest {
&self.resolved_toml
}
pub fn patch(&self) -> &HashMap<Url, Vec<Dependency>> {
&self.patch
}
Expand Down
11 changes: 10 additions & 1 deletion src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,19 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
let orig_resolve = ops::load_pkg_lockfile(ws)?;

// Convert Package -> TomlManifest -> Manifest -> Package
let contents = orig_pkg.manifest().contents();
let document = orig_pkg.manifest().document();
let toml_manifest =
prepare_for_publish(orig_pkg.manifest().resolved_toml(), ws, orig_pkg.root())?;
let source_id = orig_pkg.package_id().source_id();
let manifest = to_real_manifest(toml_manifest, source_id, orig_pkg.manifest_path(), gctx)?;
let manifest = to_real_manifest(
contents.to_owned(),
document.clone(),
toml_manifest,
source_id,
orig_pkg.manifest_path(),
gctx,
)?;
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());

// Regenerate Cargo.lock using the old one as a guide.
Expand Down
7 changes: 6 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub fn read_manifest(

(|| {
if toml.package().is_some() {
to_real_manifest(toml, source_id, path, gctx).map(EitherManifest::Real)
to_real_manifest(contents, document, toml, source_id, path, gctx)
.map(EitherManifest::Real)
} else {
to_virtual_manifest(toml, source_id, path, gctx).map(EitherManifest::Virtual)
}
Expand Down Expand Up @@ -443,6 +444,8 @@ pub fn prepare_for_publish(

#[tracing::instrument(skip_all)]
pub fn to_real_manifest(
contents: String,
document: toml_edit::ImDocument<String>,
me: manifest::TomlManifest,
source_id: SourceId,
manifest_file: &Path,
Expand Down Expand Up @@ -1208,6 +1211,8 @@ pub fn to_real_manifest(
_unused_keys: Default::default(),
};
let mut manifest = Manifest::new(
Rc::new(contents),
Rc::new(document),
Rc::new(resolved_toml),
summary,
default_kind,
Expand Down