From fb6040096ca2c21c354a500ab8fd0038d84be193 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sat, 12 Jan 2019 21:49:18 +0100 Subject: [PATCH 1/3] Querify local proc_macro_decls_static --- src/librustc/session/mod.rs | 2 -- .../back/symbol_export.rs | 5 ++-- src/librustc_codegen_utils/symbol_names.rs | 2 +- src/librustc_driver/driver.rs | 7 +++-- src/librustc_driver/proc_macro_decls.rs | 26 +++++++++++++++---- src/librustc_metadata/encoder.rs | 4 +-- 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index ba09480f93f3d..da2524b8188c5 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -70,7 +70,6 @@ pub struct Session { /// For a library crate, this is always none pub entry_fn: Once>, pub plugin_registrar_fn: Once>, - pub proc_macro_decls_static: Once>, pub sysroot: PathBuf, /// The name of the root source file of the crate, in the local file system. /// `None` means that there is no source file. @@ -1175,7 +1174,6 @@ pub fn build_session_( // For a library crate, this is always none entry_fn: Once::new(), plugin_registrar_fn: Once::new(), - proc_macro_decls_static: Once::new(), sysroot, local_crate_source_file, working_dir, diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index 928e171fe0fdc..c7bf221b9391d 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -147,9 +147,8 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, }) .collect(); - if let Some(id) = *tcx.sess.proc_macro_decls_static.get() { - let def_id = tcx.hir().local_def_id(id); - reachable_non_generics.insert(def_id, SymbolExportLevel::C); + if let Some(id) = tcx.proc_macro_decls_static(LOCAL_CRATE) { + reachable_non_generics.insert(id, SymbolExportLevel::C); } if let Some(id) = *tcx.sess.plugin_registrar_fn.get() { diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index 5a5d1b20b2151..72f534e486bef 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -247,7 +247,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_plugin_registrar_symbol(disambiguator); } - if *tcx.sess.proc_macro_decls_static.get() == Some(id) { + if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) { let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_proc_macro_decls_symbol(disambiguator); } diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 380f9afd68de6..71bc9968b1c46 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -1158,6 +1158,7 @@ where } pub fn default_provide(providers: &mut ty::query::Providers) { + proc_macro_decls::provide(providers); hir::provide(providers); borrowck::provide(providers); mir::provide(providers); @@ -1216,8 +1217,6 @@ where .set(time(sess, "looking for plugin registrar", || { plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map) })); - sess.proc_macro_decls_static - .set(proc_macro_decls::find(&hir_map)); let mut local_providers = ty::query::Providers::default(); default_provide(&mut local_providers); @@ -1250,6 +1249,10 @@ where time(sess, "loop checking", || loops::check_crate(tcx)); + time(sess, "looking for derive registrar", || { + proc_macro_decls::find(tcx) + }); + time(sess, "attribute checking", || { hir::check_attr::check_crate(tcx) }); diff --git a/src/librustc_driver/proc_macro_decls.rs b/src/librustc_driver/proc_macro_decls.rs index bb2ea6c2a97ca..093d15b7e3c57 100644 --- a/src/librustc_driver/proc_macro_decls.rs +++ b/src/librustc_driver/proc_macro_decls.rs @@ -1,15 +1,25 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor; -use rustc::hir::map::Map; +use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc::hir; +use rustc::ty::TyCtxt; +use rustc::ty::query::Providers; use syntax::ast; use syntax::attr; -pub fn find(hir_map: &Map) -> Option { - let krate = hir_map.krate(); +pub fn find<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Option { + tcx.proc_macro_decls_static(LOCAL_CRATE) +} + +fn proc_macro_decls_static<'tcx>( + tcx: TyCtxt<'_, 'tcx, 'tcx>, + cnum: CrateNum, +) -> Option { + assert_eq!(cnum, LOCAL_CRATE); let mut finder = Finder { decls: None }; - krate.visit_all_item_likes(&mut finder); - finder.decls + tcx.hir().krate().visit_all_item_likes(&mut finder); + + finder.decls.map(|id| tcx.hir().local_def_id(id)) } struct Finder { @@ -30,3 +40,9 @@ impl<'v> ItemLikeVisitor<'v> for Finder { } } +pub(crate) fn provide(providers: &mut Providers<'_>) { + *providers = Providers { + proc_macro_decls_static, + ..*providers + }; +} diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 032a4656efcda..56558a1550ab1 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -487,8 +487,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .get() .map(|id| tcx.hir().local_def_id(id).index), proc_macro_decls_static: if is_proc_macro { - let id = tcx.sess.proc_macro_decls_static.get().unwrap(); - Some(tcx.hir().local_def_id(id).index) + let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap(); + Some(id.index) } else { None }, From 59d7d7d54b4b31d6a20143484536c4806fa8a74e Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sun, 13 Jan 2019 01:06:50 +0100 Subject: [PATCH 2/3] Querify local plugin_registrar_fn --- src/librustc/session/mod.rs | 2 -- .../back/symbol_export.rs | 5 ++- src/librustc_codegen_utils/symbol_names.rs | 4 +-- src/librustc_driver/driver.rs | 10 +++--- src/librustc_lint/builtin.rs | 4 +-- src/librustc_metadata/encoder.rs | 5 +-- src/librustc_plugin/build.rs | 31 ++++++++++++++----- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index da2524b8188c5..5f9f62f7b136b 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -69,7 +69,6 @@ pub struct Session { pub parse_sess: ParseSess, /// For a library crate, this is always none pub entry_fn: Once>, - pub plugin_registrar_fn: Once>, pub sysroot: PathBuf, /// The name of the root source file of the crate, in the local file system. /// `None` means that there is no source file. @@ -1173,7 +1172,6 @@ pub fn build_session_( parse_sess: p_s, // For a library crate, this is always none entry_fn: Once::new(), - plugin_registrar_fn: Once::new(), sysroot, local_crate_source_file, working_dir, diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index c7bf221b9391d..bf69089a254a4 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -151,9 +151,8 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, reachable_non_generics.insert(id, SymbolExportLevel::C); } - if let Some(id) = *tcx.sess.plugin_registrar_fn.get() { - let def_id = tcx.hir().local_def_id(id); - reachable_non_generics.insert(def_id, SymbolExportLevel::C); + if let Some(id) = tcx.plugin_registrar_fn(LOCAL_CRATE) { + reachable_non_generics.insert(id, SymbolExportLevel::C); } Lrc::new(reachable_non_generics) diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index 72f534e486bef..9267f14f24234 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -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); - if let Some(id) = node_id { - if *tcx.sess.plugin_registrar_fn.get() == Some(id) { + if def_id.is_local() { + if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) { let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_plugin_registrar_symbol(disambiguator); } diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 71bc9968b1c46..6667db35b70e6 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -1159,6 +1159,7 @@ where pub fn default_provide(providers: &mut ty::query::Providers) { proc_macro_decls::provide(providers); + plugin::build::provide(providers); hir::provide(providers); borrowck::provide(providers); mir::provide(providers); @@ -1213,11 +1214,6 @@ where 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(); default_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, "looking for plugin registrar", || { + plugin::build::find_plugin_registrar(tcx) + }); + time(sess, "looking for derive registrar", || { proc_macro_decls::find(tcx) }); diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index 5678f30dabccd..72bcf8edfdd21 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -19,7 +19,7 @@ //! a `pub fn new()`. 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 hir::Node; use util::nodemap::NodeSet; @@ -860,7 +860,7 @@ impl LintPass for PluginAsLibrary { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary { 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. return; } diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 56558a1550ab1..2de1637fb0d9d 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -482,10 +482,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { has_global_allocator: has_global_allocator, has_panic_handler: has_panic_handler, has_default_lib_allocator: has_default_lib_allocator, - plugin_registrar_fn: tcx.sess - .plugin_registrar_fn - .get() - .map(|id| tcx.hir().local_def_id(id).index), + plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index), proc_macro_decls_static: if is_proc_macro { let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap(); Some(id.index) diff --git a/src/librustc_plugin/build.rs b/src/librustc_plugin/build.rs index eca2736ee5292..46c452668c3c8 100644 --- a/src/librustc_plugin/build.rs +++ b/src/librustc_plugin/build.rs @@ -2,11 +2,12 @@ use syntax::ast; use syntax::attr; -use errors; use syntax_pos::Span; -use rustc::hir::map::Map; use rustc::hir::itemlikevisit::ItemLikeVisitor; use rustc::hir; +use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; +use rustc::ty::TyCtxt; +use rustc::ty::query::Providers; struct RegistrarFinder { registrars: Vec<(ast::NodeId, Span)> , @@ -30,21 +31,27 @@ impl<'v> ItemLikeVisitor<'v> for RegistrarFinder { } /// Find the function marked with `#[plugin_registrar]`, if any. -pub fn find_plugin_registrar(diagnostic: &errors::Handler, - hir_map: &Map) - -> Option { - let krate = hir_map.krate(); +pub fn find_plugin_registrar<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Option { + tcx.plugin_registrar_fn(LOCAL_CRATE) +} + +fn plugin_registrar_fn<'tcx>( + tcx: TyCtxt<'_, 'tcx, 'tcx>, + cnum: CrateNum, +) -> Option { + assert_eq!(cnum, LOCAL_CRATE); 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() { 0 => None, 1 => { 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"); for &(_, span) in &finder.registrars { 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 + }; +} From 707a9a08bf3f048ca3c3b0dd752c2b1022242333 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Sun, 13 Jan 2019 23:55:47 +0100 Subject: [PATCH 3/3] Retain original pass order It shouldn't matter, but hey - better safe than sorry! --- src/librustc_driver/driver.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 6667db35b70e6..3b7de37ae4b3f 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -1243,8 +1243,6 @@ where // tcx available. time(sess, "dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx)); - time(sess, "loop checking", || loops::check_crate(tcx)); - time(sess, "looking for plugin registrar", || { plugin::build::find_plugin_registrar(tcx) }); @@ -1253,6 +1251,8 @@ where proc_macro_decls::find(tcx) }); + time(sess, "loop checking", || loops::check_crate(tcx)); + time(sess, "attribute checking", || { hir::check_attr::check_crate(tcx) });