1
Fork 0

Querify local plugin_registrar_fn

This commit is contained in:
Igor Matuszewski 2019-01-13 01:06:50 +01:00
parent fb6040096c
commit 59d7d7d54b
7 changed files with 35 additions and 26 deletions

View file

@ -69,7 +69,6 @@ pub struct Session {
pub parse_sess: ParseSess, pub parse_sess: ParseSess,
/// For a library crate, this is always none /// For a library crate, this is always none
pub entry_fn: Once<Option<(NodeId, Span, config::EntryFnType)>>, pub entry_fn: Once<Option<(NodeId, Span, config::EntryFnType)>>,
pub plugin_registrar_fn: Once<Option<ast::NodeId>>,
pub sysroot: PathBuf, pub sysroot: PathBuf,
/// The name of the root source file of the crate, in the local file system. /// The name of the root source file of the crate, in the local file system.
/// `None` means that there is no source file. /// `None` means that there is no source file.
@ -1173,7 +1172,6 @@ pub fn build_session_(
parse_sess: p_s, parse_sess: p_s,
// For a library crate, this is always none // For a library crate, this is always none
entry_fn: Once::new(), entry_fn: Once::new(),
plugin_registrar_fn: Once::new(),
sysroot, sysroot,
local_crate_source_file, local_crate_source_file,
working_dir, working_dir,

View file

@ -151,9 +151,8 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
reachable_non_generics.insert(id, SymbolExportLevel::C); reachable_non_generics.insert(id, SymbolExportLevel::C);
} }
if let Some(id) = *tcx.sess.plugin_registrar_fn.get() { if let Some(id) = tcx.plugin_registrar_fn(LOCAL_CRATE) {
let def_id = tcx.hir().local_def_id(id); reachable_non_generics.insert(id, SymbolExportLevel::C);
reachable_non_generics.insert(def_id, SymbolExportLevel::C);
} }
Lrc::new(reachable_non_generics) Lrc::new(reachable_non_generics)

View file

@ -242,8 +242,8 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
let node_id = tcx.hir().as_local_node_id(def_id); let node_id = tcx.hir().as_local_node_id(def_id);
if let Some(id) = node_id { if def_id.is_local() {
if *tcx.sess.plugin_registrar_fn.get() == Some(id) { if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
let disambiguator = tcx.sess.local_crate_disambiguator(); let disambiguator = tcx.sess.local_crate_disambiguator();
return tcx.sess.generate_plugin_registrar_symbol(disambiguator); return tcx.sess.generate_plugin_registrar_symbol(disambiguator);
} }

View file

@ -1159,6 +1159,7 @@ where
pub fn default_provide(providers: &mut ty::query::Providers) { pub fn default_provide(providers: &mut ty::query::Providers) {
proc_macro_decls::provide(providers); proc_macro_decls::provide(providers);
plugin::build::provide(providers);
hir::provide(providers); hir::provide(providers);
borrowck::provide(providers); borrowck::provide(providers);
mir::provide(providers); mir::provide(providers);
@ -1213,11 +1214,6 @@ where
middle::entry::find_entry_point(sess, &hir_map, name) middle::entry::find_entry_point(sess, &hir_map, name)
}); });
sess.plugin_registrar_fn
.set(time(sess, "looking for plugin registrar", || {
plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map)
}));
let mut local_providers = ty::query::Providers::default(); let mut local_providers = ty::query::Providers::default();
default_provide(&mut local_providers); default_provide(&mut local_providers);
codegen_backend.provide(&mut local_providers); codegen_backend.provide(&mut local_providers);
@ -1249,6 +1245,10 @@ where
time(sess, "loop checking", || loops::check_crate(tcx)); time(sess, "loop checking", || loops::check_crate(tcx));
time(sess, "looking for plugin registrar", || {
plugin::build::find_plugin_registrar(tcx)
});
time(sess, "looking for derive registrar", || { time(sess, "looking for derive registrar", || {
proc_macro_decls::find(tcx) proc_macro_decls::find(tcx)
}); });

View file

@ -19,7 +19,7 @@
//! a `pub fn new()`. //! a `pub fn new()`.
use rustc::hir::def::Def; use rustc::hir::def::Def;
use rustc::hir::def_id::DefId; use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::ty::{self, Ty}; use rustc::ty::{self, Ty};
use hir::Node; use hir::Node;
use util::nodemap::NodeSet; use util::nodemap::NodeSet;
@ -860,7 +860,7 @@ impl LintPass for PluginAsLibrary {
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary {
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) { fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
if cx.sess().plugin_registrar_fn.get().is_some() { if cx.tcx.plugin_registrar_fn(LOCAL_CRATE).is_some() {
// We're compiling a plugin; it's fine to link other plugins. // We're compiling a plugin; it's fine to link other plugins.
return; return;
} }

View file

@ -482,10 +482,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
has_global_allocator: has_global_allocator, has_global_allocator: has_global_allocator,
has_panic_handler: has_panic_handler, has_panic_handler: has_panic_handler,
has_default_lib_allocator: has_default_lib_allocator, has_default_lib_allocator: has_default_lib_allocator,
plugin_registrar_fn: tcx.sess plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index),
.plugin_registrar_fn
.get()
.map(|id| tcx.hir().local_def_id(id).index),
proc_macro_decls_static: if is_proc_macro { proc_macro_decls_static: if is_proc_macro {
let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap(); let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap();
Some(id.index) Some(id.index)

View file

@ -2,11 +2,12 @@
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use errors;
use syntax_pos::Span; use syntax_pos::Span;
use rustc::hir::map::Map;
use rustc::hir::itemlikevisit::ItemLikeVisitor; use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir; use rustc::hir;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use rustc::ty::TyCtxt;
use rustc::ty::query::Providers;
struct RegistrarFinder { struct RegistrarFinder {
registrars: Vec<(ast::NodeId, Span)> , registrars: Vec<(ast::NodeId, Span)> ,
@ -30,21 +31,27 @@ impl<'v> ItemLikeVisitor<'v> for RegistrarFinder {
} }
/// Find the function marked with `#[plugin_registrar]`, if any. /// Find the function marked with `#[plugin_registrar]`, if any.
pub fn find_plugin_registrar(diagnostic: &errors::Handler, pub fn find_plugin_registrar<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Option<DefId> {
hir_map: &Map) tcx.plugin_registrar_fn(LOCAL_CRATE)
-> Option<ast::NodeId> { }
let krate = hir_map.krate();
fn plugin_registrar_fn<'tcx>(
tcx: TyCtxt<'_, 'tcx, 'tcx>,
cnum: CrateNum,
) -> Option<DefId> {
assert_eq!(cnum, LOCAL_CRATE);
let mut finder = RegistrarFinder { registrars: Vec::new() }; let mut finder = RegistrarFinder { registrars: Vec::new() };
krate.visit_all_item_likes(&mut finder); tcx.hir().krate().visit_all_item_likes(&mut finder);
match finder.registrars.len() { match finder.registrars.len() {
0 => None, 0 => None,
1 => { 1 => {
let (node_id, _) = finder.registrars.pop().unwrap(); let (node_id, _) = finder.registrars.pop().unwrap();
Some(node_id) Some(tcx.hir().local_def_id(node_id))
}, },
_ => { _ => {
let diagnostic = tcx.sess.diagnostic();
let mut e = diagnostic.struct_err("multiple plugin registration functions found"); let mut e = diagnostic.struct_err("multiple plugin registration functions found");
for &(_, span) in &finder.registrars { for &(_, span) in &finder.registrars {
e.span_note(span, "one is here"); e.span_note(span, "one is here");
@ -55,3 +62,11 @@ pub fn find_plugin_registrar(diagnostic: &errors::Handler,
} }
} }
} }
pub fn provide(providers: &mut Providers<'_>) {
*providers = Providers {
plugin_registrar_fn,
..*providers
};
}