Skip to content

Commit bdce69d

Browse files
committed
rustc_metadata: Give a constructor to CrateLocator
1 parent c6bcf60 commit bdce69d

File tree

2 files changed

+81
-48
lines changed

2 files changed

+81
-48
lines changed

src/librustc_metadata/creader.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -350,26 +350,19 @@ impl<'a> CrateLoader<'a> {
350350
(LoadResult::Previous(cnum), None)
351351
} else {
352352
info!("falling back to a load");
353-
let mut locator = CrateLocator {
354-
sess: self.sess,
355-
span,
356-
crate_name: name,
353+
let mut locator = CrateLocator::new(
354+
self.sess,
355+
self.metadata_loader,
356+
name,
357357
hash,
358358
host_hash,
359359
extra_filename,
360-
filesearch: self.sess.target_filesearch(path_kind),
361-
target: &self.sess.target.target,
362-
triple: self.sess.opts.target_triple.clone(),
360+
false, // is_host
361+
path_kind,
362+
span,
363363
root,
364-
rejected_via_hash: vec![],
365-
rejected_via_triple: vec![],
366-
rejected_via_kind: vec![],
367-
rejected_via_version: vec![],
368-
rejected_via_filename: vec![],
369-
should_match_name: true,
370-
is_proc_macro: Some(false),
371-
metadata_loader: self.metadata_loader,
372-
};
364+
Some(false), // is_proc_macro
365+
);
373366

374367
self.load(&mut locator).map(|r| (r, None)).or_else(|| {
375368
dep_kind = DepKind::UnexportedMacrosOnly;

src/librustc_metadata/locator.rs

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -248,32 +248,36 @@ use log::{debug, info, warn};
248248
use rustc_error_codes::*;
249249

250250
#[derive(Clone)]
251-
crate struct CrateMismatch {
251+
struct CrateMismatch {
252252
path: PathBuf,
253253
got: String,
254254
}
255255

256256
#[derive(Clone)]
257257
crate struct CrateLocator<'a> {
258-
pub sess: &'a Session,
259-
pub span: Span,
260-
pub crate_name: Symbol,
258+
// Immutable per-session configuration.
259+
sess: &'a Session,
260+
metadata_loader: &'a dyn MetadataLoader,
261+
262+
// Immutable per-search configuration.
263+
crate_name: Symbol,
261264
pub hash: Option<&'a Svh>,
262265
pub host_hash: Option<&'a Svh>,
263-
pub extra_filename: Option<&'a str>,
264-
// points to either self.sess.target.target or self.sess.host, must match triple
266+
extra_filename: Option<&'a str>,
265267
pub target: &'a Target,
266268
pub triple: TargetTriple,
267269
pub filesearch: FileSearch<'a>,
268-
pub root: Option<&'a CratePaths>,
269-
pub rejected_via_hash: Vec<CrateMismatch>,
270-
pub rejected_via_triple: Vec<CrateMismatch>,
271-
pub rejected_via_kind: Vec<CrateMismatch>,
272-
pub rejected_via_version: Vec<CrateMismatch>,
273-
pub rejected_via_filename: Vec<CrateMismatch>,
274-
pub should_match_name: bool,
270+
span: Span,
271+
root: Option<&'a CratePaths>,
275272
pub is_proc_macro: Option<bool>,
276-
pub metadata_loader: &'a dyn MetadataLoader,
273+
274+
// Mutable in-progress state or output.
275+
rejected_via_hash: Vec<CrateMismatch>,
276+
rejected_via_triple: Vec<CrateMismatch>,
277+
rejected_via_kind: Vec<CrateMismatch>,
278+
rejected_via_version: Vec<CrateMismatch>,
279+
rejected_via_filename: Vec<CrateMismatch>,
280+
should_match_name: bool,
277281
}
278282

279283
crate struct CratePaths {
@@ -299,6 +303,49 @@ impl fmt::Display for CrateFlavor {
299303
}
300304

301305
impl<'a> CrateLocator<'a> {
306+
crate fn new(
307+
sess: &'a Session,
308+
metadata_loader: &'a dyn MetadataLoader,
309+
crate_name: Symbol,
310+
hash: Option<&'a Svh>,
311+
host_hash: Option<&'a Svh>,
312+
extra_filename: Option<&'a str>,
313+
is_host: bool,
314+
path_kind: PathKind,
315+
span: Span,
316+
root: Option<&'a CratePaths>,
317+
is_proc_macro: Option<bool>,
318+
) -> CrateLocator<'a> {
319+
CrateLocator {
320+
sess,
321+
metadata_loader,
322+
crate_name,
323+
hash,
324+
host_hash,
325+
extra_filename,
326+
target: if is_host { &sess.host } else { &sess.target.target },
327+
triple: if is_host {
328+
TargetTriple::from_triple(config::host_triple())
329+
} else {
330+
sess.opts.target_triple.clone()
331+
},
332+
filesearch: if is_host {
333+
sess.host_filesearch(path_kind)
334+
} else {
335+
sess.target_filesearch(path_kind)
336+
},
337+
span,
338+
root,
339+
is_proc_macro,
340+
rejected_via_hash: Vec::new(),
341+
rejected_via_triple: Vec::new(),
342+
rejected_via_kind: Vec::new(),
343+
rejected_via_version: Vec::new(),
344+
rejected_via_filename: Vec::new(),
345+
should_match_name: true,
346+
}
347+
}
348+
302349
crate fn reset(&mut self) {
303350
self.rejected_via_hash.clear();
304351
self.rejected_via_triple.clear();
@@ -926,26 +973,19 @@ pub fn find_plugin_registrar(
926973
let host_triple = TargetTriple::from_triple(config::host_triple());
927974
let is_cross = target_triple != host_triple;
928975
let mut target_only = false;
929-
let mut locator = CrateLocator {
976+
let mut locator = CrateLocator::new(
930977
sess,
931-
span,
932-
crate_name: name,
933-
hash: None,
934-
host_hash: None,
935-
extra_filename: None,
936-
filesearch: sess.host_filesearch(PathKind::Crate),
937-
target: &sess.host,
938-
triple: host_triple,
939-
root: None,
940-
rejected_via_hash: vec![],
941-
rejected_via_triple: vec![],
942-
rejected_via_kind: vec![],
943-
rejected_via_version: vec![],
944-
rejected_via_filename: vec![],
945-
should_match_name: true,
946-
is_proc_macro: None,
947978
metadata_loader,
948-
};
979+
name,
980+
None, // hash
981+
None, // host_hash
982+
None, // extra_filename
983+
true, // is_host
984+
PathKind::Crate,
985+
span,
986+
None, // root
987+
None, // is_proc_macro
988+
);
949989

950990
let library = locator.maybe_load_library_crate().or_else(|| {
951991
if !is_cross {

0 commit comments

Comments
 (0)