Skip to content

Commit

Permalink
Introduce CrateRejections struct
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Sep 2, 2021
1 parent b3f850a commit f59198a
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions compiler/rustc_metadata/src/locator.rs
Expand Up @@ -255,11 +255,7 @@ crate struct CrateLocator<'a> {
pub is_proc_macro: bool,

// Mutable in-progress state or output.
rejected_via_hash: Vec<CrateMismatch>,
rejected_via_triple: Vec<CrateMismatch>,
rejected_via_kind: Vec<CrateMismatch>,
rejected_via_version: Vec<CrateMismatch>,
rejected_via_filename: Vec<CrateMismatch>,
crate_rejections: CrateRejections,
}

#[derive(Clone)]
Expand Down Expand Up @@ -343,20 +339,16 @@ impl<'a> CrateLocator<'a> {
sess.target_filesearch(path_kind)
},
is_proc_macro: false,
rejected_via_hash: Vec::new(),
rejected_via_triple: Vec::new(),
rejected_via_kind: Vec::new(),
rejected_via_version: Vec::new(),
rejected_via_filename: Vec::new(),
crate_rejections: CrateRejections::default(),
}
}

crate fn reset(&mut self) {
self.rejected_via_hash.clear();
self.rejected_via_triple.clear();
self.rejected_via_kind.clear();
self.rejected_via_version.clear();
self.rejected_via_filename.clear();
self.crate_rejections.via_hash.clear();
self.crate_rejections.via_triple.clear();
self.crate_rejections.via_kind.clear();
self.crate_rejections.via_version.clear();
self.crate_rejections.via_filename.clear();
}

crate fn maybe_load_library_crate(&mut self) -> Result<Option<Library>, CrateError> {
Expand Down Expand Up @@ -439,7 +431,7 @@ impl<'a> CrateLocator<'a> {
};
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
// keyed of the filename hash listed. For each filename, we also have a
Expand Down Expand Up @@ -610,7 +602,8 @@ impl<'a> CrateLocator<'a> {
let found_version = metadata.get_rustc_version();
if found_version != rustc_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 });
return None;
}
Expand All @@ -632,7 +625,7 @@ impl<'a> CrateLocator<'a> {

if root.triple() != &self.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(),
got: root.triple().to_string(),
});
Expand All @@ -643,7 +636,8 @@ impl<'a> CrateLocator<'a> {
if let Some(expected_hash) = self.hash {
if hash != expected_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() });
return None;
}
Expand Down Expand Up @@ -697,7 +691,8 @@ impl<'a> CrateLocator<'a> {
dylibs.insert(loc_canon, PathKind::ExternFlag);
}
} else {
self.rejected_via_filename
self.crate_rejections
.via_filename
.push(CrateMismatch { path: loc.original().clone(), got: String::new() });
}
}
Expand All @@ -713,11 +708,7 @@ impl<'a> CrateLocator<'a> {
triple: self.triple,
dll_prefix: self.target.dll_prefix.clone(),
dll_suffix: self.target.dll_suffix.clone(),
rejected_via_hash: self.rejected_via_hash,
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,
crate_rejections: self.crate_rejections,
})
}
}
Expand Down Expand Up @@ -844,6 +835,15 @@ struct CrateMismatch {
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.
/// If no candidate is accepted, then these reasons are presented to the user,
/// otherwise they are ignored.
Expand All @@ -853,11 +853,7 @@ crate struct CombinedLocatorError {
triple: TargetTriple,
dll_prefix: String,
dll_suffix: String,
rejected_via_hash: Vec<CrateMismatch>,
rejected_via_triple: Vec<CrateMismatch>,
rejected_via_kind: Vec<CrateMismatch>,
rejected_via_version: Vec<CrateMismatch>,
rejected_via_filename: Vec<CrateMismatch>,
crate_rejections: CrateRejections,
}

crate enum CrateError {
Expand Down Expand Up @@ -966,7 +962,7 @@ impl CrateError {
Some(r) => format!(" which `{}` depends on", r.name),
};
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!(
sess,
span,
Expand All @@ -976,7 +972,7 @@ impl CrateError {
add,
);
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 {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
}
Expand All @@ -987,7 +983,7 @@ impl CrateError {
}
err.note(&msg);
err
} else if !locator.rejected_via_triple.is_empty() {
} else if !locator.crate_rejections.via_triple.is_empty() {
let mut err = struct_span_err!(
sess,
span,
Expand All @@ -997,7 +993,7 @@ impl CrateError {
locator.triple,
add,
);
let mismatches = locator.rejected_via_triple.iter();
let mismatches = locator.crate_rejections.via_triple.iter();
for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!(
"\ncrate `{}`, target triple {}: {}",
Expand All @@ -1008,7 +1004,7 @@ impl CrateError {
}
err.note(&msg);
err
} else if !locator.rejected_via_kind.is_empty() {
} else if !locator.crate_rejections.via_kind.is_empty() {
let mut err = struct_span_err!(
sess,
span,
Expand All @@ -1018,13 +1014,13 @@ impl CrateError {
add,
);
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 {
msg.push_str(&format!("\ncrate `{}`: {}", crate_name, path.display()));
}
err.note(&msg);
err
} else if !locator.rejected_via_version.is_empty() {
} else if !locator.crate_rejections.via_version.is_empty() {
let mut err = struct_span_err!(
sess,
span,
Expand All @@ -1037,7 +1033,7 @@ impl CrateError {
"please recompile that crate using this compiler ({})",
rustc_version(),
));
let mismatches = locator.rejected_via_version.iter();
let mismatches = locator.crate_rejections.via_version.iter();
for CrateMismatch { path, got } in mismatches {
msg.push_str(&format!(
"\ncrate `{}` compiled by {}: {}",
Expand Down Expand Up @@ -1104,8 +1100,8 @@ impl CrateError {
err
};

if !locator.rejected_via_filename.is_empty() {
let mismatches = locator.rejected_via_filename.iter();
if !locator.crate_rejections.via_filename.is_empty() {
let mismatches = locator.crate_rejections.via_filename.iter();
for CrateMismatch { path, .. } in mismatches {
err.note(&format!(
"extern location for {} is of an unknown type: {}",
Expand Down

0 comments on commit f59198a

Please sign in to comment.