Introduce CrateRejections struct

This commit is contained in:
bjorn3 2021-08-31 17:02:36 +02:00
parent b3f850a50c
commit f59198ab96

View file

@ -255,11 +255,7 @@ crate struct CrateLocator<'a> {
pub is_proc_macro: bool, pub is_proc_macro: bool,
// Mutable in-progress state or output. // Mutable in-progress state or output.
rejected_via_hash: Vec<CrateMismatch>, crate_rejections: CrateRejections,
rejected_via_triple: Vec<CrateMismatch>,
rejected_via_kind: Vec<CrateMismatch>,
rejected_via_version: Vec<CrateMismatch>,
rejected_via_filename: Vec<CrateMismatch>,
} }
#[derive(Clone)] #[derive(Clone)]
@ -343,20 +339,16 @@ impl<'a> CrateLocator<'a> {
sess.target_filesearch(path_kind) sess.target_filesearch(path_kind)
}, },
is_proc_macro: false, is_proc_macro: false,
rejected_via_hash: Vec::new(), crate_rejections: CrateRejections::default(),
rejected_via_triple: Vec::new(),
rejected_via_kind: Vec::new(),
rejected_via_version: Vec::new(),
rejected_via_filename: Vec::new(),
} }
} }
crate fn reset(&mut self) { crate fn reset(&mut self) {
self.rejected_via_hash.clear(); self.crate_rejections.via_hash.clear();
self.rejected_via_triple.clear(); self.crate_rejections.via_triple.clear();
self.rejected_via_kind.clear(); self.crate_rejections.via_kind.clear();
self.rejected_via_version.clear(); self.crate_rejections.via_version.clear();
self.rejected_via_filename.clear(); self.crate_rejections.via_filename.clear();
} }
crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> { crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
@ -439,7 +431,7 @@ impl<'a> CrateLocator<'a> {
}; };
FileMatches FileMatches
}); });
self.rejected_via_kind.extend(staticlibs); self.crate_rejections.via_kind.extend(staticlibs);
// We have now collected all known libraries into a set of candidates // We have now collected all known libraries into a set of candidates
// keyed of the filename hash listed. For each filename, we also have a // keyed of the filename hash listed. For each filename, we also have a
@ -610,7 +602,8 @@ impl<'a> CrateLocator<'a> {
let found_version = metadata.get_rustc_version(); let found_version = metadata.get_rustc_version();
if found_version != rustc_version { if found_version != rustc_version {
info!("Rejecting via version: expected {} got {}", rustc_version, found_version); info!("Rejecting via version: expected {} got {}", rustc_version, found_version);
self.rejected_via_version self.crate_rejections
.via_version
.push(CrateMismatch { path: libpath.to_path_buf(), got: found_version }); .push(CrateMismatch { path: libpath.to_path_buf(), got: found_version });
return None; return None;
} }
@ -632,7 +625,7 @@ impl<'a> CrateLocator<'a> {
if root.triple() != &self.triple { if root.triple() != &self.triple {
info!("Rejecting via crate triple: expected {} got {}", self.triple, root.triple()); info!("Rejecting via crate triple: expected {} got {}", self.triple, root.triple());
self.rejected_via_triple.push(CrateMismatch { self.crate_rejections.via_triple.push(CrateMismatch {
path: libpath.to_path_buf(), path: libpath.to_path_buf(),
got: root.triple().to_string(), got: root.triple().to_string(),
}); });
@ -643,7 +636,8 @@ impl<'a> CrateLocator<'a> {
if let Some(expected_hash) = self.hash { if let Some(expected_hash) = self.hash {
if hash != expected_hash { if hash != expected_hash {
info!("Rejecting via hash: expected {} got {}", expected_hash, hash); info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
self.rejected_via_hash self.crate_rejections
.via_hash
.push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() }); .push(CrateMismatch { path: libpath.to_path_buf(), got: hash.to_string() });
return None; return None;
} }
@ -697,7 +691,8 @@ impl<'a> CrateLocator<'a> {
dylibs.insert(loc_canon, PathKind::ExternFlag); dylibs.insert(loc_canon, PathKind::ExternFlag);
} }
} else { } else {
self.rejected_via_filename self.crate_rejections
.via_filename
.push(CrateMismatch { path: loc.original().clone(), got: String::new() }); .push(CrateMismatch { path: loc.original().clone(), got: String::new() });
} }
} }
@ -713,11 +708,7 @@ impl<'a> CrateLocator<'a> {
triple: self.triple, triple: self.triple,
dll_prefix: self.target.dll_prefix.clone(), dll_prefix: self.target.dll_prefix.clone(),
dll_suffix: self.target.dll_suffix.clone(), dll_suffix: self.target.dll_suffix.clone(),
rejected_via_hash: self.rejected_via_hash, crate_rejections: self.crate_rejections,
rejected_via_triple: self.rejected_via_triple,
rejected_via_kind: self.rejected_via_kind,
rejected_via_version: self.rejected_via_version,
rejected_via_filename: self.rejected_via_filename,
}) })
} }
} }
@ -844,6 +835,15 @@ struct CrateMismatch {
got: String, got: String,
} }
#[derive(Clone, Default)]
struct CrateRejections {
via_hash: Vec<CrateMismatch>,
via_triple: Vec<CrateMismatch>,
via_kind: Vec<CrateMismatch>,
via_version: Vec<CrateMismatch>,
via_filename: Vec<CrateMismatch>,
}
/// Candidate rejection reasons collected during crate search. /// Candidate rejection reasons collected during crate search.
/// If no candidate is accepted, then these reasons are presented to the user, /// If no candidate is accepted, then these reasons are presented to the user,
/// otherwise they are ignored. /// otherwise they are ignored.
@ -853,11 +853,7 @@ crate struct CombinedLocatorError {
triple: TargetTriple, triple: TargetTriple,
dll_prefix: String, dll_prefix: String,
dll_suffix: String, dll_suffix: String,
rejected_via_hash: Vec<CrateMismatch>, crate_rejections: CrateRejections,
rejected_via_triple: Vec<CrateMismatch>,
rejected_via_kind: Vec<CrateMismatch>,
rejected_via_version: Vec<CrateMismatch>,
rejected_via_filename: Vec<CrateMismatch>,
} }
crate enum CrateError { crate enum CrateError {
@ -966,7 +962,7 @@ impl CrateError {
Some(r) => format!(" which `{}` depends on", r.name), Some(r) => format!(" which `{}` depends on", r.name),
}; };
let mut msg = "the following crate versions were found:".to_string(); let mut msg = "the following crate versions were found:".to_string();
let mut err = if !locator.rejected_via_hash.is_empty() { let mut err = if !locator.crate_rejections.via_hash.is_empty() {
let mut err = struct_span_err!( let mut err = struct_span_err!(
sess, sess,
span, span,
@ -976,7 +972,7 @@ impl CrateError {
add, add,
); );
err.note("perhaps that crate needs to be recompiled?"); err.note("perhaps that crate needs to be recompiled?");
let mismatches = locator.rejected_via_hash.iter(); let mismatches = locator.crate_rejections.via_hash.iter();
for CrateMismatch { path, .. } in mismatches { for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display())); msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
} }
@ -987,7 +983,7 @@ impl CrateError {
} }
err.note(&msg); err.note(&msg);
err err
} else if !locator.rejected_via_triple.is_empty() { } else if !locator.crate_rejections.via_triple.is_empty() {
let mut err = struct_span_err!( let mut err = struct_span_err!(
sess, sess,
span, span,
@ -997,7 +993,7 @@ impl CrateError {
locator.triple, locator.triple,
add, add,
); );
let mismatches = locator.rejected_via_triple.iter(); let mismatches = locator.crate_rejections.via_triple.iter();
for CrateMismatch { path, got } in mismatches { for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!( msg.push_str(&format!(
"\ncrate `{}`, target triple {}: {}", "\ncrate `{}`, target triple {}: {}",
@ -1008,7 +1004,7 @@ impl CrateError {
} }
err.note(&msg); err.note(&msg);
err err
} else if !locator.rejected_via_kind.is_empty() { } else if !locator.crate_rejections.via_kind.is_empty() {
let mut err = struct_span_err!( let mut err = struct_span_err!(
sess, sess,
span, span,
@ -1018,13 +1014,13 @@ impl CrateError {
add, add,
); );
err.help("please recompile that crate using --crate-type lib"); err.help("please recompile that crate using --crate-type lib");
let mismatches = locator.rejected_via_kind.iter(); let mismatches = locator.crate_rejections.via_kind.iter();
for CrateMismatch { path, .. } in mismatches { for CrateMismatch { path, .. } in mismatches {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display())); msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
} }
err.note(&msg); err.note(&msg);
err err
} else if !locator.rejected_via_version.is_empty() { } else if !locator.crate_rejections.via_version.is_empty() {
let mut err = struct_span_err!( let mut err = struct_span_err!(
sess, sess,
span, span,
@ -1037,7 +1033,7 @@ impl CrateError {
"please recompile that crate using this compiler ({})", "please recompile that crate using this compiler ({})",
rustc_version(), rustc_version(),
)); ));
let mismatches = locator.rejected_via_version.iter(); let mismatches = locator.crate_rejections.via_version.iter();
for CrateMismatch { path, got } in mismatches { for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!( msg.push_str(&format!(
"\ncrate `{}` compiled by {}: {}", "\ncrate `{}` compiled by {}: {}",
@ -1104,8 +1100,8 @@ impl CrateError {
err err
}; };
if !locator.rejected_via_filename.is_empty() { if !locator.crate_rejections.via_filename.is_empty() {
let mismatches = locator.rejected_via_filename.iter(); let mismatches = locator.crate_rejections.via_filename.iter();
for CrateMismatch { path, .. } in mismatches { for CrateMismatch { path, .. } in mismatches {
err.note(&format!( err.note(&format!(
"extern location for {} is of an unknown type: {}", "extern location for {} is of an unknown type: {}",