]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_interface/passes.rs
Have Queries own the GlobalCtxt.
[rust.git] / src / librustc_interface / passes.rs
index 36bd8345e4359a9666ff30d451d99a043e734a03..99945ebf8c4cfdfa7a838c31e8473aa09d98f122 100644 (file)
@@ -22,7 +22,7 @@
 use rustc_codegen_utils::codegen_backend::CodegenBackend;
 use rustc_codegen_utils::link::filename_for_metadata;
 use rustc_data_structures::{box_region_allow_access, declare_box_region_type, parallel};
-use rustc_data_structures::sync::{Lrc, ParallelIterator, par_iter};
+use rustc_data_structures::sync::{Lrc, Once, ParallelIterator, par_iter};
 use rustc_errors::PResult;
 use rustc_incremental;
 use rustc_metadata::cstore;
@@ -37,7 +37,7 @@
 use rustc_typeck as typeck;
 use syntax::{self, ast, visit};
 use syntax::early_buffered_lints::BufferedEarlyLint;
-use syntax_expand::base::{NamedSyntaxExtension, ExtCtxt};
+use syntax_expand::base::ExtCtxt;
 use syntax::mut_visit::MutVisitor;
 use syntax::util::node_count::NodeCounter;
 use syntax::symbol::Symbol;
@@ -119,7 +119,6 @@ pub fn configure_and_expand(
     metadata_loader: Box<MetadataLoaderDyn>,
     krate: ast::Crate,
     crate_name: &str,
-    plugin_info: PluginInfo,
 ) -> Result<(ast::Crate, BoxedResolver)> {
     // Currently, we ignore the name resolution data structures for the purposes of dependency
     // tracking. Instead we will run name resolution and include its output in the hash of each
@@ -137,7 +136,6 @@ pub fn configure_and_expand(
             &crate_name,
             &resolver_arenas,
             &*metadata_loader,
-            plugin_info,
         );
         let mut resolver = match res {
             Err(v) => {
@@ -164,17 +162,13 @@ pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ResolverOutp
     }
 }
 
-pub struct PluginInfo {
-    syntax_exts: Vec<NamedSyntaxExtension>,
-}
-
 pub fn register_plugins<'a>(
     sess: &'a Session,
     metadata_loader: &'a dyn MetadataLoader,
     register_lints: impl Fn(&Session, &mut lint::LintStore),
     mut krate: ast::Crate,
     crate_name: &str,
-) -> Result<(ast::Crate, PluginInfo, Lrc<lint::LintStore>)> {
+) -> Result<(ast::Crate, Lrc<lint::LintStore>)> {
     krate = time(sess, "attributes injection", || {
         syntax_ext::cmdline_attrs::inject(
             krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr
@@ -240,10 +234,9 @@ pub fn register_plugins<'a>(
         }
     });
 
-    let Registry { syntax_exts, llvm_passes, .. } = registry;
-    *sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
+    *sess.plugin_llvm_passes.borrow_mut() = registry.llvm_passes;
 
-    Ok((krate, PluginInfo { syntax_exts }, Lrc::new(lint_store)))
+    Ok((krate, Lrc::new(lint_store)))
 }
 
 fn configure_and_expand_inner<'a>(
@@ -253,7 +246,6 @@ fn configure_and_expand_inner<'a>(
     crate_name: &str,
     resolver_arenas: &'a ResolverArenas<'a>,
     metadata_loader: &'a MetadataLoaderDyn,
-    plugin_info: PluginInfo,
 ) -> Result<(ast::Crate, Resolver<'a>)> {
     time(sess, "pre-AST-expansion lint checks", || {
         lint::check_ast_crate(
@@ -290,10 +282,6 @@ fn configure_and_expand_inner<'a>(
 
     util::check_attr_crate_type(&krate.attrs, &mut resolver.lint_buffer());
 
-    syntax_ext::plugin_macro_defs::inject(
-        &mut krate, &mut resolver, plugin_info.syntax_exts, sess.edition()
-    );
-
     // Expand all macros
     krate = time(sess, "expansion", || {
         let _prof_timer = sess.prof.generic_activity("macro_expand_crate");
@@ -752,44 +740,41 @@ pub fn default_provide_extern(providers: &mut ty::query::Providers<'_>) {
     rustc_codegen_ssa::provide_extern(providers);
 }
 
-declare_box_region_type!(
-    pub BoxedGlobalCtxt,
-    for('tcx),
-    (&'tcx GlobalCtxt<'tcx>) -> ((), ())
-);
+pub struct BoxedGlobalCtxt<'tcx>(&'tcx GlobalCtxt<'tcx>);
 
-impl BoxedGlobalCtxt {
+impl<'gcx> BoxedGlobalCtxt<'gcx> {
     pub fn enter<F, R>(&mut self, f: F) -> R
     where
         F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R,
     {
-        self.access(|gcx| ty::tls::enter_global(gcx, |tcx| f(tcx)))
+        ty::tls::enter_global(self.0, |tcx| f(tcx))
+    }
+
+    pub fn print_stats(&self) {
+        self.0.queries.print_stats()
     }
 }
 
-pub fn create_global_ctxt(
-    compiler: &Compiler,
+pub fn create_global_ctxt<'gcx>(
+    compiler: &'gcx Compiler,
     lint_store: Lrc<lint::LintStore>,
-    mut hir_forest: hir::map::Forest,
+    hir_forest: &'gcx hir::map::Forest,
     mut resolver_outputs: ResolverOutputs,
     outputs: OutputFilenames,
     crate_name: &str,
-) -> BoxedGlobalCtxt {
-    let sess = compiler.session().clone();
+    global_ctxt: &'gcx Once<GlobalCtxt<'gcx>>,
+    arenas: &'gcx Once<AllArenas>,
+) -> BoxedGlobalCtxt<'gcx> {
+    let sess = &compiler.session();
     let codegen_backend = compiler.codegen_backend().clone();
-    let crate_name = crate_name.to_string();
     let defs = mem::take(&mut resolver_outputs.definitions);
     let override_queries = compiler.override_queries;
 
-    let ((), result) = BoxedGlobalCtxt::new(static move || {
-        let sess = &*sess;
-
-        let global_ctxt: Option<GlobalCtxt<'_>>;
-        let arenas = AllArenas::new();
+        let arenas = arenas.init_locking(|| AllArenas::new());
 
         // Construct the HIR map.
         let hir_map = time(sess, "indexing HIR", || {
-            hir::map::map_crate(sess, &*resolver_outputs.cstore, &mut hir_forest, &defs)
+            hir::map::map_crate(sess, &*resolver_outputs.cstore, &hir_forest, defs)
         });
 
         let query_result_on_disk_cache = time(sess, "load query result cache", || {
@@ -808,7 +793,7 @@ pub fn create_global_ctxt(
             callback(sess, &mut local_providers, &mut extern_providers);
         }
 
-        let gcx = TyCtxt::create_global_ctxt(
+        let gcx = global_ctxt.init_locking(move || TyCtxt::create_global_ctxt(
             sess,
             lint_store,
             local_providers,
@@ -819,26 +804,15 @@ pub fn create_global_ctxt(
             query_result_on_disk_cache,
             &crate_name,
             &outputs
-        );
-
-        global_ctxt = Some(gcx);
-        let gcx = global_ctxt.as_ref().unwrap();
+        ));
 
-        ty::tls::enter_global(gcx, |tcx| {
+        ty::tls::enter_global(&gcx, |tcx| {
             // Do some initialization of the DepGraph that can only be done with the
             // tcx available.
             time(tcx.sess, "dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
         });
 
-        yield BoxedGlobalCtxt::initial_yield(());
-        box_region_allow_access!(for('tcx), (&'tcx GlobalCtxt<'tcx>), (gcx));
-
-        if sess.opts.debugging_opts.query_stats {
-            gcx.queries.print_stats();
-        }
-    });
-
-    result
+    BoxedGlobalCtxt(gcx)
 }
 
 /// Runs the resolution, type-checking, region checking and other