Skip to content

Commit 36108cb

Browse files
committed
Auto merge of #13593 - epage:toml2, r=Muscraft
refactor: Expose source/spans to Manifest for emitting lints ### What does this PR try to resolve? This is a follow up to #13589. This does nothing on its own. This is meant to short-circuit some of my refactorings so Muscraft can start on their work on adding lints while I work to move out existing warnings into being able to be converted to lints. ### How should we test and review this PR? This includes documentation changes suggested in #13589 ### Additional information
2 parents c319962 + a1fc7fe commit 36108cb

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/cargo/core/manifest.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ impl EitherManifest {
4242
#[derive(Clone, Debug)]
4343
pub struct Manifest {
4444
// alternate forms of manifests:
45+
contents: Rc<String>,
46+
document: Rc<toml_edit::ImDocument<String>>,
4547
resolved_toml: Rc<TomlManifest>,
4648
summary: Summary,
4749

@@ -389,6 +391,8 @@ compact_debug! {
389391

390392
impl Manifest {
391393
pub fn new(
394+
contents: Rc<String>,
395+
document: Rc<toml_edit::ImDocument<String>>,
392396
resolved_toml: Rc<TomlManifest>,
393397
summary: Summary,
394398

@@ -416,6 +420,8 @@ impl Manifest {
416420
embedded: bool,
417421
) -> Manifest {
418422
Manifest {
423+
contents,
424+
document,
419425
resolved_toml,
420426
summary,
421427

@@ -445,6 +451,25 @@ impl Manifest {
445451
}
446452
}
447453

454+
/// The raw contents of the original TOML
455+
pub fn contents(&self) -> &str {
456+
self.contents.as_str()
457+
}
458+
/// Collection of spans for the original TOML
459+
pub fn document(&self) -> &toml_edit::ImDocument<String> {
460+
&self.document
461+
}
462+
/// The [`TomlManifest`] with all fields expanded
463+
pub fn resolved_toml(&self) -> &TomlManifest {
464+
&self.resolved_toml
465+
}
466+
pub fn summary(&self) -> &Summary {
467+
&self.summary
468+
}
469+
pub fn summary_mut(&mut self) -> &mut Summary {
470+
&mut self.summary
471+
}
472+
448473
pub fn dependencies(&self) -> &[Dependency] {
449474
self.summary.dependencies()
450475
}
@@ -469,12 +494,6 @@ impl Manifest {
469494
pub fn package_id(&self) -> PackageId {
470495
self.summary.package_id()
471496
}
472-
pub fn summary(&self) -> &Summary {
473-
&self.summary
474-
}
475-
pub fn summary_mut(&mut self) -> &mut Summary {
476-
&mut self.summary
477-
}
478497
pub fn targets(&self) -> &[Target] {
479498
&self.targets
480499
}
@@ -500,9 +519,6 @@ impl Manifest {
500519
pub fn replace(&self) -> &[(PackageIdSpec, Dependency)] {
501520
&self.replace
502521
}
503-
pub fn resolved_toml(&self) -> &TomlManifest {
504-
&self.resolved_toml
505-
}
506522
pub fn patch(&self) -> &HashMap<Url, Vec<Dependency>> {
507523
&self.patch
508524
}

src/cargo/ops/cargo_package.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,19 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
453453
let orig_resolve = ops::load_pkg_lockfile(ws)?;
454454

455455
// Convert Package -> TomlManifest -> Manifest -> Package
456+
let contents = orig_pkg.manifest().contents();
457+
let document = orig_pkg.manifest().document();
456458
let toml_manifest =
457459
prepare_for_publish(orig_pkg.manifest().resolved_toml(), ws, orig_pkg.root())?;
458460
let source_id = orig_pkg.package_id().source_id();
459-
let manifest = to_real_manifest(toml_manifest, source_id, orig_pkg.manifest_path(), gctx)?;
461+
let manifest = to_real_manifest(
462+
contents.to_owned(),
463+
document.clone(),
464+
toml_manifest,
465+
source_id,
466+
orig_pkg.manifest_path(),
467+
gctx,
468+
)?;
460469
let new_pkg = Package::new(manifest, orig_pkg.manifest_path());
461470

462471
// Regenerate Cargo.lock using the old one as a guide.

src/cargo/util/toml/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ pub fn read_manifest(
5757

5858
(|| {
5959
if toml.package().is_some() {
60-
to_real_manifest(toml, source_id, path, gctx).map(EitherManifest::Real)
60+
to_real_manifest(contents, document, toml, source_id, path, gctx)
61+
.map(EitherManifest::Real)
6162
} else {
6263
to_virtual_manifest(toml, source_id, path, gctx).map(EitherManifest::Virtual)
6364
}
@@ -443,6 +444,8 @@ pub fn prepare_for_publish(
443444

444445
#[tracing::instrument(skip_all)]
445446
pub fn to_real_manifest(
447+
contents: String,
448+
document: toml_edit::ImDocument<String>,
446449
me: manifest::TomlManifest,
447450
source_id: SourceId,
448451
manifest_file: &Path,
@@ -1208,6 +1211,8 @@ pub fn to_real_manifest(
12081211
_unused_keys: Default::default(),
12091212
};
12101213
let mut manifest = Manifest::new(
1214+
Rc::new(contents),
1215+
Rc::new(document),
12111216
Rc::new(resolved_toml),
12121217
summary,
12131218
default_kind,

0 commit comments

Comments
 (0)