Skip to content

Commit 2cda75c

Browse files
committed
rustc_metadata: Remove unnecessary use of crate store in plugin loader
1 parent 2c16f84 commit 2cda75c

File tree

4 files changed

+41
-83
lines changed

4 files changed

+41
-83
lines changed

src/librustc_interface/passes.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::hir::lowering::lower_crate;
99
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
1010
use rustc::lint;
1111
use rustc::middle::{self, reachable, resolve_lifetime, stability};
12-
use rustc::middle::cstore::CrateStore;
12+
use rustc::middle::cstore::{CrateStore, MetadataLoader};
1313
use rustc::ty::{self, AllArenas, Resolutions, TyCtxt, GlobalCtxt};
1414
use rustc::ty::steal::Steal;
1515
use rustc::traits;
@@ -226,7 +226,7 @@ pub struct PluginInfo {
226226

227227
pub fn register_plugins<'a>(
228228
sess: &'a Session,
229-
cstore: &'a CStore,
229+
metadata_loader: &'a dyn MetadataLoader,
230230
register_lints: impl Fn(&Session, &mut lint::LintStore),
231231
mut krate: ast::Crate,
232232
crate_name: &str,
@@ -274,9 +274,8 @@ pub fn register_plugins<'a>(
274274
let registrars = time(sess, "plugin loading", || {
275275
plugin::load::load_plugins(
276276
sess,
277-
&cstore,
277+
metadata_loader,
278278
&krate,
279-
crate_name,
280279
Some(sess.opts.debugging_opts.extra_plugins.clone()),
281280
)
282281
});

src/librustc_interface/queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Compiler {
118118
let empty: &(dyn Fn(&Session, &mut lint::LintStore) + Sync + Send) = &|_, _| {};
119119
let result = passes::register_plugins(
120120
self.session(),
121-
self.cstore(),
121+
&*self.codegen_backend().metadata_loader(),
122122
self.register_lints
123123
.as_ref()
124124
.map(|p| &**p)

src/librustc_metadata/creader.rs

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ use rustc::session::{Session, CrateDisambiguator};
1414
use rustc::session::config::{Sanitizer, self};
1515
use rustc_target::spec::{PanicStrategy, TargetTriple};
1616
use rustc::session::search_paths::PathKind;
17-
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource};
17+
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource, MetadataLoader};
1818
use rustc::util::common::record_time;
1919
use rustc::util::nodemap::FxHashSet;
2020
use rustc::hir::map::Definitions;
2121
use rustc::hir::def_id::LOCAL_CRATE;
2222

23-
use std::ops::Deref;
2423
use std::path::{Path, PathBuf};
2524
use std::{cmp, fs};
2625

@@ -58,29 +57,6 @@ fn dump_crates(cstore: &CStore) {
5857
});
5958
}
6059

61-
// Extra info about a crate loaded for plugins or exported macros.
62-
struct ExtensionCrate {
63-
metadata: PMDSource,
64-
dylib: Option<PathBuf>,
65-
target_only: bool,
66-
}
67-
68-
enum PMDSource {
69-
Registered(Lrc<cstore::CrateMetadata>),
70-
Owned(Library),
71-
}
72-
73-
impl Deref for PMDSource {
74-
type Target = MetadataBlob;
75-
76-
fn deref(&self) -> &MetadataBlob {
77-
match *self {
78-
PMDSource::Registered(ref cmd) => &cmd.blob,
79-
PMDSource::Owned(ref lib) => &lib.metadata
80-
}
81-
}
82-
}
83-
8460
enum LoadResult {
8561
Previous(CrateNum),
8662
Loaded(Library),
@@ -495,21 +471,27 @@ impl<'a> CrateLoader<'a> {
495471
self.resolve_crate(dep.name, span, dep_kind, Some((root, &dep))).0
496472
})).collect()
497473
}
474+
}
498475

499-
fn read_extension_crate(&self, name: Symbol, span: Span) -> ExtensionCrate {
476+
fn read_extension_crate(
477+
sess: &Session,
478+
metadata_loader: &dyn MetadataLoader,
479+
name: Symbol,
480+
span: Span,
481+
) -> (Library, bool) {
500482
info!("read extension crate `{}`", name);
501-
let target_triple = self.sess.opts.target_triple.clone();
483+
let target_triple = sess.opts.target_triple.clone();
502484
let host_triple = TargetTriple::from_triple(config::host_triple());
503485
let is_cross = target_triple != host_triple;
504486
let mut target_only = false;
505487
let mut locate_ctxt = locator::Context {
506-
sess: self.sess,
488+
sess,
507489
span,
508490
crate_name: name,
509491
hash: None,
510492
extra_filename: None,
511-
filesearch: self.sess.host_filesearch(PathKind::Crate),
512-
target: &self.sess.host,
493+
filesearch: sess.host_filesearch(PathKind::Crate),
494+
target: &sess.host,
513495
triple: host_triple,
514496
root: None,
515497
rejected_via_hash: vec![],
@@ -519,46 +501,32 @@ impl<'a> CrateLoader<'a> {
519501
rejected_via_filename: vec![],
520502
should_match_name: true,
521503
is_proc_macro: None,
522-
metadata_loader: &*self.cstore.metadata_loader,
504+
metadata_loader,
523505
};
524-
let library = self.load(&mut locate_ctxt).or_else(|| {
506+
507+
let library = locate_ctxt.maybe_load_library_crate().or_else(|| {
525508
if !is_cross {
526509
return None
527510
}
528511
// Try loading from target crates. This will abort later if we
529512
// try to load a plugin registrar function,
530513
target_only = true;
531514

532-
locate_ctxt.target = &self.sess.target.target;
515+
locate_ctxt.target = &sess.target.target;
533516
locate_ctxt.triple = target_triple;
534-
locate_ctxt.filesearch = self.sess.target_filesearch(PathKind::Crate);
517+
locate_ctxt.filesearch = sess.target_filesearch(PathKind::Crate);
535518

536-
self.load(&mut locate_ctxt)
519+
locate_ctxt.maybe_load_library_crate()
537520
});
538521
let library = match library {
539522
Some(l) => l,
540523
None => locate_ctxt.report_errs(),
541524
};
542525

543-
let (dylib, metadata) = match library {
544-
LoadResult::Previous(cnum) => {
545-
let data = self.cstore.get_crate_data(cnum);
546-
(data.source.dylib.clone(), PMDSource::Registered(data))
547-
}
548-
LoadResult::Loaded(library) => {
549-
let dylib = library.source.dylib.clone();
550-
let metadata = PMDSource::Owned(library);
551-
(dylib, metadata)
552-
}
553-
};
554-
555-
ExtensionCrate {
556-
metadata,
557-
dylib: dylib.map(|p| p.0),
558-
target_only,
559-
}
526+
(library, target_only)
560527
}
561528

529+
impl<'a> CrateLoader<'a> {
562530
fn dlsym_proc_macros(&self,
563531
path: &Path,
564532
disambiguator: CrateDisambiguator,
@@ -589,32 +557,33 @@ impl<'a> CrateLoader<'a> {
589557

590558
decls
591559
}
560+
}
592561

593562
/// Look for a plugin registrar. Returns library path, crate
594563
/// SVH and DefIndex of the registrar function.
595-
pub fn find_plugin_registrar(&self,
564+
pub fn find_plugin_registrar(sess: &Session,
565+
metadata_loader: &dyn MetadataLoader,
596566
span: Span,
597567
name: Symbol)
598568
-> Option<(PathBuf, CrateDisambiguator)> {
599-
let ekrate = self.read_extension_crate(name, span);
569+
let (library, target_only) = read_extension_crate(sess, metadata_loader, name, span);
600570

601-
if ekrate.target_only {
571+
if target_only {
602572
// Need to abort before syntax expansion.
603573
let message = format!("plugin `{}` is not available for triple `{}` \
604574
(only found {})",
605575
name,
606576
config::host_triple(),
607-
self.sess.opts.target_triple);
608-
span_fatal!(self.sess, span, E0456, "{}", &message);
577+
sess.opts.target_triple);
578+
span_fatal!(sess, span, E0456, "{}", &message);
609579
}
610580

611-
let root = ekrate.metadata.get_root();
612-
match ekrate.dylib.as_ref() {
581+
match library.source.dylib {
613582
Some(dylib) => {
614-
Some((dylib.to_path_buf(), root.disambiguator))
583+
Some((dylib.0, library.metadata.get_root().disambiguator))
615584
}
616585
None => {
617-
span_err!(self.sess, span, E0457,
586+
span_err!(sess, span, E0457,
618587
"plugin `{}` only found in rlib format, but must be available \
619588
in dylib format",
620589
name);
@@ -625,6 +594,7 @@ impl<'a> CrateLoader<'a> {
625594
}
626595
}
627596

597+
impl<'a> CrateLoader<'a> {
628598
fn inject_panic_runtime(&self, krate: &ast::Crate) {
629599
// If we're only compiling an rlib, then there's no need to select a
630600
// panic runtime, so we just skip this section entirely.
@@ -957,9 +927,7 @@ impl<'a> CrateLoader<'a> {
957927
data.dependencies.borrow_mut().push(krate);
958928
});
959929
}
960-
}
961930

962-
impl<'a> CrateLoader<'a> {
963931
pub fn postprocess(&self, krate: &ast::Crate) {
964932
self.inject_sanitizer_runtime();
965933
self.inject_profiler_runtime();

src/librustc_plugin/load.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Used by `rustc` when loading a plugin.
22
3+
use rustc::middle::cstore::MetadataLoader;
34
use rustc::session::Session;
4-
use rustc_metadata::creader::CrateLoader;
5-
use rustc_metadata::cstore::CStore;
5+
use rustc_metadata::creader;
66
use crate::registry::Registry;
77

88
use std::borrow::ToOwned;
@@ -25,7 +25,7 @@ pub struct PluginRegistrar {
2525

2626
struct PluginLoader<'a> {
2727
sess: &'a Session,
28-
reader: CrateLoader<'a>,
28+
metadata_loader: &'a dyn MetadataLoader,
2929
plugins: Vec<PluginRegistrar>,
3030
}
3131

@@ -37,11 +37,10 @@ fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
3737

3838
/// Read plugin metadata and dynamically load registrar functions.
3939
pub fn load_plugins(sess: &Session,
40-
cstore: &CStore,
40+
metadata_loader: &dyn MetadataLoader,
4141
krate: &ast::Crate,
42-
crate_name: &str,
4342
addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
44-
let mut loader = PluginLoader::new(sess, cstore, crate_name);
43+
let mut loader = PluginLoader { sess, metadata_loader, plugins: Vec::new() };
4544

4645
// do not report any error now. since crate attributes are
4746
// not touched by expansion, every use of plugin without
@@ -80,16 +79,8 @@ pub fn load_plugins(sess: &Session,
8079
}
8180

8281
impl<'a> PluginLoader<'a> {
83-
fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
84-
PluginLoader {
85-
sess,
86-
reader: CrateLoader::new(sess, cstore, crate_name),
87-
plugins: vec![],
88-
}
89-
}
90-
9182
fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
92-
let registrar = self.reader.find_plugin_registrar(span, name);
83+
let registrar = creader::find_plugin_registrar(self.sess, self.metadata_loader, span, name);
9384

9485
if let Some((lib, disambiguator)) = registrar {
9586
let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);

0 commit comments

Comments
 (0)