about summary refs log tree commit diff
path: root/compiler/rustc_interface/src/queries.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-25 09:35:53 +0000
committerbors <bors@rust-lang.org>2024-06-25 09:35:53 +0000
commitc2d2bb38c9067d983d13505c47e761308b1694db (patch)
tree2fe2616249708698fe3a1a5a21576ebce6996b8e /compiler/rustc_interface/src/queries.rs
parentbda221a0eb5019a939e34e0f4583f1c4b6e5b862 (diff)
parent8d1f5b30efc22244f271b82adf4e7e670835432a (diff)
downloadrust-c2d2bb38c9067d983d13505c47e761308b1694db.tar.gz
rust-c2d2bb38c9067d983d13505c47e761308b1694db.zip
Auto merge of #126834 - bjorn3:interface_refactor, r=michaelwoerister
Various refactorings to rustc_interface

This should make it easier to move the driver interface away from queries in the future. Many custom drivers call queries like `queries.global_ctxt()` before they are supposed to be called, breaking some things like certain `--print` and `-Zunpretty` options, `-Zparse-only` and emitting the dep info at the wrong point in time. They are also not actually necessary at all. Passing around the query output manually would avoid recomputation too and would be just as easy. Removing driver queries would also reduce the amount of global mutable state of the compiler. I'm not removing driver queries in this PR to avoid breaking the aforementioned custom drivers.
Diffstat (limited to 'compiler/rustc_interface/src/queries.rs')
-rw-r--r--compiler/rustc_interface/src/queries.rs137
1 files changed, 12 insertions, 125 deletions
diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs
index 1b9165342d4..cfd4304e893 100644
--- a/compiler/rustc_interface/src/queries.rs
+++ b/compiler/rustc_interface/src/queries.rs
@@ -1,26 +1,20 @@
-use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
+use crate::errors::FailedWritingFile;
 use crate::interface::{Compiler, Result};
-use crate::{errors, passes, util};
+use crate::{errors, passes};
 
 use rustc_ast as ast;
 use rustc_codegen_ssa::traits::CodegenBackend;
 use rustc_codegen_ssa::CodegenResults;
 use rustc_data_structures::steal::Steal;
 use rustc_data_structures::svh::Svh;
-use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
-use rustc_hir::def_id::{StableCrateId, StableCrateIdMap, LOCAL_CRATE};
-use rustc_hir::definitions::Definitions;
-use rustc_incremental::setup_dep_graph;
-use rustc_metadata::creader::CStore;
+use rustc_data_structures::sync::{OnceLock, WorkerLocal};
+use rustc_hir::def_id::LOCAL_CRATE;
 use rustc_middle::arena::Arena;
 use rustc_middle::dep_graph::DepGraph;
 use rustc_middle::ty::{GlobalCtxt, TyCtxt};
 use rustc_serialize::opaque::FileEncodeResult;
-use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
-use rustc_session::cstore::Untracked;
-use rustc_session::output::{collect_crate_types, find_crate_name};
+use rustc_session::config::{self, OutputFilenames, OutputType};
 use rustc_session::Session;
-use rustc_span::symbol::sym;
 use std::any::Any;
 use std::cell::{RefCell, RefMut};
 use std::sync::Arc;
@@ -106,133 +100,26 @@ impl<'tcx> Queries<'tcx> {
     }
 
     pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
-        self.parse.compute(|| {
-            passes::parse(&self.compiler.sess).map_err(|parse_error| parse_error.emit())
-        })
+        self.parse.compute(|| passes::parse(&self.compiler.sess))
     }
 
     pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
         self.gcx.compute(|| {
-            let sess = &self.compiler.sess;
-
-            let mut krate = self.parse()?.steal();
-
-            rustc_builtin_macros::cmdline_attrs::inject(
-                &mut krate,
-                &sess.psess,
-                &sess.opts.unstable_opts.crate_attr,
-            );
-
-            let pre_configured_attrs =
-                rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
-
-            // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
-            let crate_name = find_crate_name(sess, &pre_configured_attrs);
-            let crate_types = collect_crate_types(sess, &pre_configured_attrs);
-            let stable_crate_id = StableCrateId::new(
-                crate_name,
-                crate_types.contains(&CrateType::Executable),
-                sess.opts.cg.metadata.clone(),
-                sess.cfg_version,
-            );
-            let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
-            let dep_graph = setup_dep_graph(sess)?;
-
-            let cstore = FreezeLock::new(Box::new(CStore::new(
-                self.compiler.codegen_backend.metadata_loader(),
-            )) as _);
-            let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
-
-            let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
-            let untracked = Untracked {
-                cstore,
-                source_span: AppendOnlyIndexVec::new(),
-                definitions,
-                stable_crate_ids,
-            };
-
-            let qcx = passes::create_global_ctxt(
+            let krate = self.parse()?.steal();
+
+            passes::create_global_ctxt(
                 self.compiler,
-                crate_types,
-                stable_crate_id,
-                dep_graph,
-                untracked,
+                krate,
                 &self.gcx_cell,
                 &self.arena,
                 &self.hir_arena,
-            );
-
-            qcx.enter(|tcx| {
-                let feed = tcx.create_crate_num(stable_crate_id).unwrap();
-                assert_eq!(feed.key(), LOCAL_CRATE);
-                feed.crate_name(crate_name);
-
-                let feed = tcx.feed_unit_query();
-                feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
-                    sess,
-                    &pre_configured_attrs,
-                    crate_name,
-                )));
-                feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
-                feed.output_filenames(Arc::new(outputs));
-            });
-            Ok(qcx)
+            )
         })
     }
 
-    pub fn write_dep_info(&'tcx self) -> Result<()> {
-        self.global_ctxt()?.enter(|tcx| {
-            passes::write_dep_info(tcx);
-        });
-        Ok(())
-    }
-
-    /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
-    /// to write UI tests that actually test that compilation succeeds without reporting
-    /// an error.
-    fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
-        let Some((def_id, _)) = tcx.entry_fn(()) else { return };
-        for attr in tcx.get_attrs(def_id, sym::rustc_error) {
-            match attr.meta_item_list() {
-                // Check if there is a `#[rustc_error(delayed_bug_from_inside_query)]`.
-                Some(list)
-                    if list.iter().any(|list_item| {
-                        matches!(
-                            list_item.ident().map(|i| i.name),
-                            Some(sym::delayed_bug_from_inside_query)
-                        )
-                    }) =>
-                {
-                    tcx.ensure().trigger_delayed_bug(def_id);
-                }
-
-                // Bare `#[rustc_error]`.
-                None => {
-                    tcx.dcx().emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
-                }
-
-                // Some other attribute.
-                Some(_) => {
-                    tcx.dcx()
-                        .emit_warn(RustcErrorUnexpectedAnnotation { span: tcx.def_span(def_id) });
-                }
-            }
-        }
-    }
-
     pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
         self.global_ctxt()?.enter(|tcx| {
-            // Don't do code generation if there were any errors. Likewise if
-            // there were any delayed bugs, because codegen will likely cause
-            // more ICEs, obscuring the original problem.
-            if let Some(guar) = self.compiler.sess.dcx().has_errors_or_delayed_bugs() {
-                return Err(guar);
-            }
-
-            // Hook for UI tests.
-            Self::check_for_rustc_errors_attr(tcx);
-
-            let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx);
+            let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx)?;
 
             Ok(Linker {
                 dep_graph: tcx.dep_graph.clone(),