Skip to content

Commit 43abf90

Browse files
committed
Auto merge of #12970 - Eh2406:MaybeSummary, r=epage
query{_vec} use IndexSummary This builds on the work from #12749 and the discussion of the overall approach is at https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/better.20error.20messages.20for.20filtered.20versions.2E ### What does this PR try to resolve? Changing the two traits `Registry` and `Source` to use the new `IndexSummary' involves a lot of changes all throughout the code base. This would be hard to review if it also included any logic changes. So this PR is just adding the type to the trait and immediately unwrapping every place it is used. The hope is that reviewing changes to logic/ergonomics will be easier to review once the mechanical changes have been merged. ### How should we test and review this PR? This is an internal re-factoring and all the tests still pass.
2 parents 946fbf9 + 6835fa3 commit 43abf90

File tree

16 files changed

+122
-59
lines changed

16 files changed

+122
-59
lines changed

crates/resolver-tests/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use cargo::core::Resolve;
1717
use cargo::core::{Dependency, PackageId, Registry, Summary};
1818
use cargo::core::{GitReference, SourceId};
1919
use cargo::sources::source::QueryKind;
20+
use cargo::sources::IndexSummary;
2021
use cargo::util::{CargoResult, Config, Graph, IntoUrl, RustVersion};
2122

2223
use proptest::collection::{btree_map, vec};
@@ -131,7 +132,7 @@ pub fn resolve_with_config_raw(
131132
&mut self,
132133
dep: &Dependency,
133134
kind: QueryKind,
134-
f: &mut dyn FnMut(Summary),
135+
f: &mut dyn FnMut(IndexSummary),
135136
) -> Poll<CargoResult<()>> {
136137
for summary in self.list.iter() {
137138
let matched = match kind {
@@ -140,7 +141,7 @@ pub fn resolve_with_config_raw(
140141
};
141142
if matched {
142143
self.used.insert(summary.package_id());
143-
f(summary.clone());
144+
f(IndexSummary::Candidate(summary.clone()));
144145
}
145146
}
146147
Poll::Ready(Ok(()))

crates/xtask-bump-check/src/xtask.rs

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ fn check_crates_io<'a>(
377377
"`{name}@{current}` needs a bump because its should have a version newer than crates.io: {:?}`",
378378
possibilities
379379
.iter()
380+
.map(|s| s.as_summary())
380381
.map(|s| format!("{}@{}", s.name(), s.version()))
381382
.collect::<Vec<_>>(),
382383
);

src/cargo/core/compiler/future_incompat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn get_updates(ws: &Workspace<'_>, package_ids: &BTreeSet<PackageId>) -> Option<
348348
for (pkg_id, summaries) in summaries {
349349
let mut updated_versions: Vec<_> = summaries
350350
.iter()
351-
.map(|summary| summary.version())
351+
.map(|summary| summary.as_summary().version())
352352
.filter(|version| *version > pkg_id.version())
353353
.collect();
354354
updated_versions.sort();

src/cargo/core/registry.rs

+33-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::sources::config::SourceConfigMap;
77
use crate::sources::source::QueryKind;
88
use crate::sources::source::Source;
99
use crate::sources::source::SourceMap;
10+
use crate::sources::IndexSummary;
1011
use crate::util::errors::CargoResult;
1112
use crate::util::interning::InternedString;
1213
use crate::util::{CanonicalUrl, Config};
@@ -23,10 +24,14 @@ pub trait Registry {
2324
&mut self,
2425
dep: &Dependency,
2526
kind: QueryKind,
26-
f: &mut dyn FnMut(Summary),
27+
f: &mut dyn FnMut(IndexSummary),
2728
) -> Poll<CargoResult<()>>;
2829

29-
fn query_vec(&mut self, dep: &Dependency, kind: QueryKind) -> Poll<CargoResult<Vec<Summary>>> {
30+
fn query_vec(
31+
&mut self,
32+
dep: &Dependency,
33+
kind: QueryKind,
34+
) -> Poll<CargoResult<Vec<IndexSummary>>> {
3035
let mut ret = Vec::new();
3136
self.query(dep, kind, &mut |s| ret.push(s)).map_ok(|()| ret)
3237
}
@@ -337,6 +342,8 @@ impl<'cfg> PackageRegistry<'cfg> {
337342
}
338343
};
339344

345+
let summaries = summaries.into_iter().map(|s| s.into_summary()).collect();
346+
340347
let (summary, should_unlock) =
341348
match summary_for_patch(orig_patch, &locked, summaries, source) {
342349
Poll::Ready(x) => x,
@@ -481,13 +488,15 @@ impl<'cfg> PackageRegistry<'cfg> {
481488
Ok(())
482489
}
483490

484-
fn query_overrides(&mut self, dep: &Dependency) -> Poll<CargoResult<Option<Summary>>> {
491+
fn query_overrides(&mut self, dep: &Dependency) -> Poll<CargoResult<Option<IndexSummary>>> {
485492
for &s in self.overrides.iter() {
486493
let src = self.sources.get_mut(s).unwrap();
487494
let dep = Dependency::new_override(dep.package_name(), s);
488-
let mut results = ready!(src.query_vec(&dep, QueryKind::Exact))?;
489-
if !results.is_empty() {
490-
return Poll::Ready(Ok(Some(results.remove(0))));
495+
496+
let mut results = None;
497+
ready!(src.query(&dep, QueryKind::Exact, &mut |s| results = Some(s)))?;
498+
if results.is_some() {
499+
return Poll::Ready(Ok(results));
491500
}
492501
}
493502
Poll::Ready(Ok(None))
@@ -575,7 +584,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
575584
&mut self,
576585
dep: &Dependency,
577586
kind: QueryKind,
578-
f: &mut dyn FnMut(Summary),
587+
f: &mut dyn FnMut(IndexSummary),
579588
) -> Poll<CargoResult<()>> {
580589
assert!(self.patches_locked);
581590
let (override_summary, n, to_warn) = {
@@ -607,9 +616,9 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
607616
if patches.len() == 1 && dep.is_locked() {
608617
let patch = patches.remove(0);
609618
match override_summary {
610-
Some(summary) => (summary, 1, Some(patch)),
619+
Some(summary) => (summary, 1, Some(IndexSummary::Candidate(patch))),
611620
None => {
612-
f(patch);
621+
f(IndexSummary::Candidate(patch));
613622
return Poll::Ready(Ok(()));
614623
}
615624
}
@@ -646,7 +655,7 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
646655
// everything upstairs after locking the summary
647656
(None, Some(source)) => {
648657
for patch in patches.iter() {
649-
f(patch.clone());
658+
f(IndexSummary::Candidate(patch.clone()));
650659
}
651660

652661
// Our sources shouldn't ever come back to us with two
@@ -658,14 +667,18 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
658667
// already selected, then we skip this `summary`.
659668
let locked = &self.locked;
660669
let all_patches = &self.patches_available;
661-
let callback = &mut |summary: Summary| {
670+
let callback = &mut |summary: IndexSummary| {
662671
for patch in patches.iter() {
663672
let patch = patch.package_id().version();
664673
if summary.package_id().version() == patch {
665674
return;
666675
}
667676
}
668-
f(lock(locked, all_patches, summary))
677+
f(IndexSummary::Candidate(lock(
678+
locked,
679+
all_patches,
680+
summary.into_summary(),
681+
)))
669682
};
670683
return source.query(dep, kind, callback);
671684
}
@@ -702,9 +715,12 @@ impl<'cfg> Registry for PackageRegistry<'cfg> {
702715
"found an override with a non-locked list"
703716
)));
704717
} else if let Some(summary) = to_warn {
705-
self.warn_bad_override(&override_summary, &summary)?;
718+
self.warn_bad_override(override_summary.as_summary(), summary.as_summary())?;
706719
}
707-
f(self.lock(override_summary));
720+
f(IndexSummary::Candidate(
721+
self.lock(override_summary.into_summary()),
722+
));
723+
708724
Poll::Ready(Ok(()))
709725
}
710726

@@ -887,6 +903,8 @@ fn summary_for_patch(
887903
Vec::new()
888904
});
889905

906+
let orig_matches = orig_matches.into_iter().map(|s| s.into_summary()).collect();
907+
890908
let summary = ready!(summary_for_patch(orig_patch, &None, orig_matches, source))?;
891909

892910
// The unlocked version found a match. This returns a value to
@@ -907,7 +925,7 @@ fn summary_for_patch(
907925
});
908926
let mut vers = name_summaries
909927
.iter()
910-
.map(|summary| summary.version())
928+
.map(|summary| summary.as_summary().version())
911929
.collect::<Vec<_>>();
912930
let found = match vers.len() {
913931
0 => format!(""),

src/cargo/core/resolver/dep_cache.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a> RegistryQueryer<'a> {
105105

106106
let mut ret = Vec::new();
107107
let ready = self.registry.query(dep, QueryKind::Exact, &mut |s| {
108-
ret.push(s);
108+
ret.push(s.into_summary());
109109
})?;
110110
if ready.is_pending() {
111111
self.registry_cache
@@ -135,16 +135,19 @@ impl<'a> RegistryQueryer<'a> {
135135
return Poll::Pending;
136136
}
137137
};
138-
let s = summaries.next().ok_or_else(|| {
139-
anyhow::format_err!(
140-
"no matching package for override `{}` found\n\
138+
let s = summaries
139+
.next()
140+
.ok_or_else(|| {
141+
anyhow::format_err!(
142+
"no matching package for override `{}` found\n\
141143
location searched: {}\n\
142144
version required: {}",
143-
spec,
144-
dep.source_id(),
145-
dep.version_req()
146-
)
147-
})?;
145+
spec,
146+
dep.source_id(),
147+
dep.version_req()
148+
)
149+
})?
150+
.into_summary();
148151
let summaries = summaries.collect::<Vec<_>>();
149152
if !summaries.is_empty() {
150153
let bullets = summaries

src/cargo/core/resolver/errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub(super) fn activation_error(
228228
let mut new_dep = dep.clone();
229229
new_dep.set_version_req(OptVersionReq::Any);
230230

231-
let mut candidates = loop {
231+
let candidates = loop {
232232
match registry.query_vec(&new_dep, QueryKind::Exact) {
233233
Poll::Ready(Ok(candidates)) => break candidates,
234234
Poll::Ready(Err(e)) => return to_resolve_err(e),
@@ -239,6 +239,8 @@ pub(super) fn activation_error(
239239
}
240240
};
241241

242+
let mut candidates: Vec<_> = candidates.into_iter().map(|s| s.into_summary()).collect();
243+
242244
candidates.sort_unstable_by(|a, b| b.version().cmp(a.version()));
243245

244246
let mut msg = if !candidates.is_empty() {
@@ -303,7 +305,7 @@ pub(super) fn activation_error(
303305
} else {
304306
// Maybe the user mistyped the name? Like `dep-thing` when `Dep_Thing`
305307
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
306-
let mut candidates = loop {
308+
let candidates = loop {
307309
match registry.query_vec(&new_dep, QueryKind::Fuzzy) {
308310
Poll::Ready(Ok(candidates)) => break candidates,
309311
Poll::Ready(Err(e)) => return to_resolve_err(e),
@@ -314,6 +316,8 @@ pub(super) fn activation_error(
314316
}
315317
};
316318

319+
let mut candidates: Vec<_> = candidates.into_iter().map(|s| s.into_summary()).collect();
320+
317321
candidates.sort_unstable_by_key(|a| a.name());
318322
candidates.dedup_by(|a, b| a.name() == b.name());
319323
let mut candidates: Vec<_> = candidates

src/cargo/ops/cargo_add/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn get_latest_dependency(
545545
unreachable!("registry dependencies required, found a workspace dependency");
546546
}
547547
MaybeWorkspace::Other(query) => {
548-
let mut possibilities = loop {
548+
let possibilities = loop {
549549
match registry.query_vec(&query, QueryKind::Fuzzy) {
550550
std::task::Poll::Ready(res) => {
551551
break res?;
@@ -554,6 +554,11 @@ fn get_latest_dependency(
554554
}
555555
};
556556

557+
let mut possibilities: Vec<_> = possibilities
558+
.into_iter()
559+
.map(|s| s.into_summary())
560+
.collect();
561+
557562
possibilities.sort_by_key(|s| {
558563
// Fallback to a pre-release if no official release is available by sorting them as
559564
// less.
@@ -671,6 +676,12 @@ fn select_package(
671676
std::task::Poll::Pending => registry.block_until_ready()?,
672677
}
673678
};
679+
680+
let possibilities: Vec<_> = possibilities
681+
.into_iter()
682+
.map(|s| s.into_summary())
683+
.collect();
684+
674685
match possibilities.len() {
675686
0 => {
676687
let source = dependency
@@ -889,6 +900,7 @@ fn populate_available_features(
889900
// in the lock file for a given version requirement.
890901
let lowest_common_denominator = possibilities
891902
.iter()
903+
.map(|s| s.as_summary())
892904
.min_by_key(|s| {
893905
// Fallback to a pre-release if no official release is available by sorting them as
894906
// more.

src/cargo/ops/common_for_install_and_uninstall.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,11 @@ where
552552
Poll::Pending => source.block_until_ready()?,
553553
}
554554
};
555-
match deps.iter().max_by_key(|p| p.package_id()) {
555+
match deps
556+
.iter()
557+
.map(|s| s.as_summary())
558+
.max_by_key(|p| p.package_id())
559+
{
556560
Some(summary) => {
557561
if let (Some(current), Some(msrv)) = (current_rust_version, summary.rust_version()) {
558562
let msrv_req = msrv.to_caret_req();
@@ -571,6 +575,7 @@ where
571575
};
572576
if let Some(alt) = msrv_deps
573577
.iter()
578+
.map(|s| s.as_summary())
574579
.filter(|summary| {
575580
summary
576581
.rust_version()

src/cargo/sources/directory.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use std::fmt::{self, Debug, Formatter};
33
use std::path::{Path, PathBuf};
44
use std::task::Poll;
55

6-
use crate::core::{Dependency, Package, PackageId, SourceId, Summary};
6+
use crate::core::{Dependency, Package, PackageId, SourceId};
77
use crate::sources::source::MaybePackage;
88
use crate::sources::source::QueryKind;
99
use crate::sources::source::Source;
10+
use crate::sources::IndexSummary;
1011
use crate::sources::PathSource;
1112
use crate::util::errors::CargoResult;
1213
use crate::util::Config;
@@ -99,7 +100,7 @@ impl<'cfg> Source for DirectorySource<'cfg> {
99100
&mut self,
100101
dep: &Dependency,
101102
kind: QueryKind,
102-
f: &mut dyn FnMut(Summary),
103+
f: &mut dyn FnMut(IndexSummary),
103104
) -> Poll<CargoResult<()>> {
104105
if !self.updated {
105106
return Poll::Pending;
@@ -110,7 +111,7 @@ impl<'cfg> Source for DirectorySource<'cfg> {
110111
QueryKind::Fuzzy => true,
111112
});
112113
for summary in matches.map(|pkg| pkg.summary().clone()) {
113-
f(summary);
114+
f(IndexSummary::Candidate(summary));
114115
}
115116
Poll::Ready(Ok(()))
116117
}

src/cargo/sources/git/source.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
use crate::core::global_cache_tracker;
44
use crate::core::GitReference;
55
use crate::core::SourceId;
6-
use crate::core::{Dependency, Package, PackageId, Summary};
6+
use crate::core::{Dependency, Package, PackageId};
77
use crate::sources::git::utils::GitRemote;
88
use crate::sources::source::MaybePackage;
99
use crate::sources::source::QueryKind;
1010
use crate::sources::source::Source;
11+
use crate::sources::IndexSummary;
1112
use crate::sources::PathSource;
1213
use crate::util::cache_lock::CacheLockMode;
1314
use crate::util::errors::CargoResult;
@@ -192,7 +193,7 @@ impl<'cfg> Source for GitSource<'cfg> {
192193
&mut self,
193194
dep: &Dependency,
194195
kind: QueryKind,
195-
f: &mut dyn FnMut(Summary),
196+
f: &mut dyn FnMut(IndexSummary),
196197
) -> Poll<CargoResult<()>> {
197198
if let Some(src) = self.path_source.as_mut() {
198199
src.query(dep, kind, f)

src/cargo/sources/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ pub use self::config::SourceConfigMap;
3030
pub use self::directory::DirectorySource;
3131
pub use self::git::GitSource;
3232
pub use self::path::PathSource;
33-
pub use self::registry::{RegistrySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY};
33+
pub use self::registry::{
34+
IndexSummary, RegistrySource, CRATES_IO_DOMAIN, CRATES_IO_INDEX, CRATES_IO_REGISTRY,
35+
};
3436
pub use self::replaced::ReplacedSource;
3537

3638
pub mod config;

0 commit comments

Comments
 (0)