Skip to content

Commit

Permalink
Merge branch 'rust-lang:master' into galib-xcompile-doctest-warning-i…
Browse files Browse the repository at this point in the history
…12118
  • Loading branch information
progressive-galib authored Dec 12, 2024
2 parents 4639cf5 + 351b421 commit d3e21b0
Show file tree
Hide file tree
Showing 22 changed files with 289 additions and 180 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ rand = "0.8.5"
regex = "1.10.5"
rusqlite = { version = "0.32.0", features = ["bundled"] }
rustc-hash = "2.0.0"
rustc-stable-hash = "0.1.1"
rustfix = { version = "0.9.0", path = "crates/rustfix" }
same-file = "1.0.6"
schemars = "0.8.21"
Expand Down Expand Up @@ -194,6 +195,7 @@ rand.workspace = true
regex.workspace = true
rusqlite.workspace = true
rustc-hash.workspace = true
rustc-stable-hash.workspace = true
rustfix.workspace = true
same-file.workspace = true
semver.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ pub fn resolve_with_global_context_raw(
for summary in self.list.iter() {
let matched = match kind {
QueryKind::Exact => dep.matches(summary),
QueryKind::Alternatives => true,
QueryKind::RejectedVersions => dep.matches(summary),
QueryKind::AlternativeNames => true,
QueryKind::Normalized => true,
};
if matched {
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,8 @@ fn compute_metadata(
}
}

let c_metadata = UnitHash(c_metadata_hasher.finish());
let c_extra_filename = UnitHash(c_extra_filename_hasher.finish());
let c_metadata = UnitHash(Hasher::finish(&c_metadata_hasher));
let c_extra_filename = UnitHash(Hasher::finish(&c_extra_filename_hasher));
let unit_id = c_extra_filename;

let c_extra_filename = use_extra_filename.then_some(c_extra_filename);
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/compile_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ impl CompileTarget {
self.name.hash(&mut hasher);
}
}
hasher.finish()
Hasher::finish(&hasher)
}
}
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ fn calculate_normal(
local: Mutex::new(local),
memoized_hash: Mutex::new(None),
metadata,
config: config.finish(),
config: Hasher::finish(&config),
compile_kind,
rustflags: extra_flags,
fs_status: FsStatus::Stale,
Expand Down
12 changes: 3 additions & 9 deletions src/cargo/core/compiler/unit_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::util::interning::InternedString;
use crate::util::CargoResult;
use crate::GlobalContext;
use std::collections::HashMap;
use std::io::Write;

/// The dependency graph of Units.
pub type UnitGraph = HashMap<Unit, Vec<UnitDep>>;
Expand Down Expand Up @@ -121,15 +120,10 @@ pub fn emit_serialized_unit_graph(
}
})
.collect();
let s = SerializedUnitGraph {

gctx.shell().print_json(&SerializedUnitGraph {
version: VERSION,
units: ser_units,
roots,
};

let stdout = std::io::stdout();
let mut lock = stdout.lock();
serde_json::to_writer(&mut lock, &s)?;
drop(writeln!(lock));
Ok(())
})
}
17 changes: 9 additions & 8 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,10 @@ impl<'gctx> Registry for PackageRegistry<'gctx> {
let patch = patches.remove(0);
match override_summary {
Some(override_summary) => {
let override_summary = override_summary.into_summary();
self.warn_bad_override(&override_summary, &patch)?;
f(IndexSummary::Candidate(self.lock(override_summary)));
self.warn_bad_override(override_summary.as_summary(), &patch)?;
let override_summary =
override_summary.map_summary(|summary| self.lock(summary));
f(override_summary);
}
None => f(IndexSummary::Candidate(patch)),
}
Expand Down Expand Up @@ -733,8 +734,8 @@ impl<'gctx> Registry for PackageRegistry<'gctx> {
return;
}
}
let summary = summary.into_summary();
f(IndexSummary::Candidate(lock(locked, all_patches, summary)))
let summary = summary.map_summary(|summary| lock(locked, all_patches, summary));
f(summary)
};
return source.query(dep, kind, callback);
}
Expand All @@ -760,11 +761,11 @@ impl<'gctx> Registry for PackageRegistry<'gctx> {
"found an override with a non-locked list"
)));
}
let override_summary = override_summary.into_summary();
if let Some(to_warn) = to_warn {
self.warn_bad_override(&override_summary, to_warn.as_summary())?;
self.warn_bad_override(override_summary.as_summary(), to_warn.as_summary())?;
}
f(IndexSummary::Candidate(self.lock(override_summary)));
let override_summary = override_summary.map_summary(|summary| self.lock(summary));
f(override_summary);
}
}

Expand Down
129 changes: 94 additions & 35 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt;
use std::fmt::Write as _;
use std::task::Poll;

use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::sources::source::QueryKind;
use crate::sources::IndexSummary;
use crate::util::edit_distance::edit_distance;
use crate::util::{GlobalContext, OptVersionReq, VersionExt};
use anyhow::Error;
Expand Down Expand Up @@ -220,11 +222,11 @@ pub(super) fn activation_error(
// Maybe the user mistyped the ver_req? Like `dep="2"` when `dep="0.2"`
// was meant. So we re-query the registry with `dep="*"` so we can
// list a few versions that were actually found.
let mut new_dep = dep.clone();
new_dep.set_version_req(OptVersionReq::Any);
let mut wild_dep = dep.clone();
wild_dep.set_version_req(OptVersionReq::Any);

let candidates = loop {
match registry.query_vec(&new_dep, QueryKind::Exact) {
match registry.query_vec(&wild_dep, QueryKind::Exact) {
Poll::Ready(Ok(candidates)) => break candidates,
Poll::Ready(Err(e)) => return to_resolve_err(e),
Poll::Pending => match registry.block_until_ready() {
Expand Down Expand Up @@ -301,10 +303,23 @@ pub(super) fn activation_error(

msg
} else {
// Maybe something is wrong with the available versions
let mut version_candidates = loop {
match registry.query_vec(&dep, QueryKind::RejectedVersions) {
Poll::Ready(Ok(candidates)) => break candidates,
Poll::Ready(Err(e)) => return to_resolve_err(e),
Poll::Pending => match registry.block_until_ready() {
Ok(()) => continue,
Err(e) => return to_resolve_err(e),
},
}
};
version_candidates.sort_unstable_by_key(|a| a.as_summary().version().clone());

// Maybe the user mistyped the name? Like `dep-thing` when `Dep_Thing`
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
let candidates = loop {
match registry.query_vec(&new_dep, QueryKind::Alternatives) {
let name_candidates = loop {
match registry.query_vec(&wild_dep, QueryKind::AlternativeNames) {
Poll::Ready(Ok(candidates)) => break candidates,
Poll::Ready(Err(e)) => return to_resolve_err(e),
Poll::Pending => match registry.block_until_ready() {
Expand All @@ -313,58 +328,102 @@ pub(super) fn activation_error(
},
}
};

let mut candidates: Vec<_> = candidates.into_iter().map(|s| s.into_summary()).collect();

candidates.sort_unstable_by_key(|a| a.name());
candidates.dedup_by(|a, b| a.name() == b.name());
let mut candidates: Vec<_> = candidates
let mut name_candidates: Vec<_> = name_candidates
.into_iter()
.map(|s| s.into_summary())
.collect();
name_candidates.sort_unstable_by_key(|a| a.name());
name_candidates.dedup_by(|a, b| a.name() == b.name());
let mut name_candidates: Vec<_> = name_candidates
.iter()
.filter_map(|n| Some((edit_distance(&*new_dep.package_name(), &*n.name(), 3)?, n)))
.filter_map(|n| Some((edit_distance(&*wild_dep.package_name(), &*n.name(), 3)?, n)))
.collect();
candidates.sort_by_key(|o| o.0);
let mut msg: String;
if candidates.is_empty() {
msg = format!("no matching package named `{}` found\n", dep.package_name());
} else {
msg = format!(
"no matching package found\nsearched package name: `{}`\n",
name_candidates.sort_by_key(|o| o.0);

let mut msg = String::new();
if !version_candidates.is_empty() {
let _ = writeln!(
&mut msg,
"no matching versions for `{}` found",
dep.package_name()
);
let mut names = candidates
for candidate in version_candidates {
match candidate {
IndexSummary::Candidate(summary) => {
// HACK: If this was a real candidate, we wouldn't hit this case.
// so it must be a patch which get normalized to being a candidate
let _ =
writeln!(&mut msg, " version {} is unavailable", summary.version());
}
IndexSummary::Yanked(summary) => {
let _ = writeln!(&mut msg, " version {} is yanked", summary.version());
}
IndexSummary::Offline(summary) => {
let _ = writeln!(&mut msg, " version {} is not cached", summary.version());
}
IndexSummary::Unsupported(summary, schema_version) => {
if let Some(rust_version) = summary.rust_version() {
// HACK: technically its unsupported and we shouldn't make assumptions
// about the entry but this is limited and for diagnostics purposes
let _ = writeln!(
&mut msg,
" version {} requires cargo {}",
summary.version(),
rust_version
);
} else {
let _ = writeln!(
&mut msg,
" version {} requires a Cargo version that supports index version {}",
summary.version(),
schema_version
);
}
}
}
}
} else if !name_candidates.is_empty() {
let _ = writeln!(&mut msg, "no matching package found",);
let _ = writeln!(&mut msg, "searched package name: `{}`", dep.package_name());
let mut names = name_candidates
.iter()
.take(3)
.map(|c| c.1.name().as_str())
.collect::<Vec<_>>();

if candidates.len() > 3 {
if name_candidates.len() > 3 {
names.push("...");
}
// Vertically align first suggestion with missing crate name
// so a typo jumps out at you.
msg.push_str("perhaps you meant: ");
msg.push_str(&names.iter().enumerate().fold(
String::default(),
|acc, (i, el)| match i {
let suggestions = names
.iter()
.enumerate()
.fold(String::default(), |acc, (i, el)| match i {
0 => acc + el,
i if names.len() - 1 == i && candidates.len() <= 3 => acc + " or " + el,
i if names.len() - 1 == i && name_candidates.len() <= 3 => acc + " or " + el,
_ => acc + ", " + el,
},
));
msg.push('\n');
});
let _ = writeln!(&mut msg, "perhaps you meant: {suggestions}");
} else {
let _ = writeln!(
&mut msg,
"no matching package named `{}` found",
dep.package_name()
);
}

let mut location_searched_msg = registry.describe_source(dep.source_id());
if location_searched_msg.is_empty() {
location_searched_msg = format!("{}", dep.source_id());
}

msg.push_str(&format!("location searched: {}\n", location_searched_msg));
msg.push_str("required by ");
msg.push_str(&describe_path_in_context(
resolver_ctx,
&parent.package_id(),
));
let _ = writeln!(&mut msg, "location searched: {}", location_searched_msg);
let _ = write!(
&mut msg,
"required by {}",
describe_path_in_context(resolver_ctx, &parent.package_id()),
);

msg
};
Expand Down
Loading

0 comments on commit d3e21b0

Please sign in to comment.