1 use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
2 use crate::interface::{Compiler, Result};
3 use crate::passes::{self, BoxedResolver, QueryContext};
6 use rustc_codegen_ssa::traits::CodegenBackend;
7 use rustc_codegen_ssa::CodegenResults;
8 use rustc_data_structures::steal::Steal;
9 use rustc_data_structures::svh::Svh;
10 use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
11 use rustc_hir::def_id::LOCAL_CRATE;
12 use rustc_incremental::DepGraphFuture;
13 use rustc_lint::LintStore;
14 use rustc_middle::arena::Arena;
15 use rustc_middle::dep_graph::DepGraph;
16 use rustc_middle::ty::{self, GlobalCtxt, TyCtxt};
17 use rustc_query_impl::Queries as TcxQueries;
18 use rustc_session::config::{self, OutputFilenames, OutputType};
19 use rustc_session::{output::find_crate_name, Session};
20 use rustc_span::symbol::sym;
21 use rustc_span::Symbol;
23 use std::cell::{RefCell, RefMut};
27 /// Represent the result of a query.
29 /// This result can be stolen once with the [`steal`] method and generated with the [`compute`] method.
31 /// [`steal`]: Steal::steal
32 /// [`compute`]: Self::compute
34 /// `None` means no value has been computed yet.
35 result: RefCell<Option<Result<Steal<T>>>>,
39 fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<QueryResult<'_, T>> {
41 self.result.borrow_mut(),
42 |r: &mut Option<Result<Steal<T>>>| -> Option<&mut Steal<T>> {
43 r.get_or_insert_with(|| f().map(Steal::new)).as_mut().ok()
46 .map_err(|r| *r.as_ref().unwrap().as_ref().map(|_| ()).unwrap_err())
51 pub struct QueryResult<'a, T>(RefMut<'a, Steal<T>>);
53 impl<'a, T> std::ops::Deref for QueryResult<'a, T> {
54 type Target = RefMut<'a, Steal<T>>;
56 fn deref(&self) -> &Self::Target {
61 impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
62 fn deref_mut(&mut self) -> &mut Self::Target {
67 impl<'a, 'tcx> QueryResult<'a, QueryContext<'tcx>> {
68 pub fn enter<T>(mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
69 (*self.0).get_mut().enter(f)
73 impl<T> Default for Query<T> {
74 fn default() -> Self {
75 Query { result: RefCell::new(None) }
79 pub struct Queries<'tcx> {
80 compiler: &'tcx Compiler,
81 gcx: OnceCell<GlobalCtxt<'tcx>>,
82 queries: OnceCell<TcxQueries<'tcx>>,
84 arena: WorkerLocal<Arena<'tcx>>,
85 hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
87 dep_graph_future: Query<Option<DepGraphFuture>>,
88 parse: Query<ast::Crate>,
89 crate_name: Query<Symbol>,
90 register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
91 expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
92 dep_graph: Query<DepGraph>,
93 global_ctxt: Query<QueryContext<'tcx>>,
94 ongoing_codegen: Query<Box<dyn Any>>,
97 impl<'tcx> Queries<'tcx> {
98 pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
101 gcx: OnceCell::new(),
102 queries: OnceCell::new(),
103 arena: WorkerLocal::new(|_| Arena::default()),
104 hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
105 dep_graph_future: Default::default(),
106 parse: Default::default(),
107 crate_name: Default::default(),
108 register_plugins: Default::default(),
109 expansion: Default::default(),
110 dep_graph: Default::default(),
111 global_ctxt: Default::default(),
112 ongoing_codegen: Default::default(),
116 fn session(&self) -> &Lrc<Session> {
119 fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
120 self.compiler.codegen_backend()
123 fn dep_graph_future(&self) -> Result<QueryResult<'_, Option<DepGraphFuture>>> {
124 self.dep_graph_future.compute(|| {
125 let sess = self.session();
126 Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess)))
130 pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
132 .compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
135 pub fn register_plugins(&self) -> Result<QueryResult<'_, (ast::Crate, Lrc<LintStore>)>> {
136 self.register_plugins.compute(|| {
137 let crate_name = *self.crate_name()?.borrow();
138 let krate = self.parse()?.steal();
140 let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
141 let (krate, lint_store) = passes::register_plugins(
143 &*self.codegen_backend().metadata_loader(),
144 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
149 // Compute the dependency graph (in the background). We want to do
150 // this as early as possible, to give the DepGraph maximum time to
151 // load before dep_graph() is called, but it also can't happen
152 // until after rustc_incremental::prepare_session_directory() is
153 // called, which happens within passes::register_plugins().
154 self.dep_graph_future().ok();
156 Ok((krate, Lrc::new(lint_store)))
160 fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
161 self.crate_name.compute(|| {
163 let parse_result = self.parse()?;
164 let krate = parse_result.borrow();
165 // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
166 find_crate_name(self.session(), &krate.attrs)
173 ) -> Result<QueryResult<'_, (Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>>
176 self.expansion.compute(|| {
177 let crate_name = *self.crate_name()?.borrow();
178 let (krate, lint_store) = self.register_plugins()?.steal();
179 let _timer = self.session().timer("configure_and_expand");
180 let sess = self.session();
181 let mut resolver = passes::create_resolver(
183 self.codegen_backend().metadata_loader(),
187 let krate = resolver.access(|resolver| {
188 passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver)
190 Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
194 fn dep_graph(&self) -> Result<QueryResult<'_, DepGraph>> {
195 self.dep_graph.compute(|| {
196 let sess = self.session();
197 let future_opt = self.dep_graph_future()?.steal();
198 let dep_graph = future_opt
200 let (prev_graph, prev_work_products) =
201 sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
203 rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
205 .unwrap_or_else(DepGraph::new_disabled);
210 pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, QueryContext<'tcx>>> {
211 self.global_ctxt.compute(|| {
212 let crate_name = *self.crate_name()?.borrow();
213 let (krate, resolver, lint_store) = self.expansion()?.steal();
215 let outputs = passes::prepare_outputs(self.session(), &krate, &resolver, crate_name)?;
217 let ty::ResolverOutputs {
219 global_ctxt: untracked_resolutions,
220 ast_lowering: untracked_resolver_for_lowering,
221 } = BoxedResolver::to_resolver_outputs(resolver);
223 let mut qcx = passes::create_global_ctxt(
226 self.dep_graph()?.steal(),
235 let feed = tcx.feed_unit_query();
236 feed.resolver_for_lowering(
237 tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
239 feed.resolutions(tcx.arena.alloc(untracked_resolutions));
240 feed.output_filenames(tcx.arena.alloc(std::sync::Arc::new(outputs)));
241 feed.features_query(tcx.sess.features_untracked());
242 let feed = tcx.feed_local_crate();
243 feed.crate_name(crate_name);
249 pub fn ongoing_codegen(&'tcx self) -> Result<QueryResult<'_, Box<dyn Any>>> {
250 self.ongoing_codegen.compute(|| {
251 self.global_ctxt()?.enter(|tcx| {
252 tcx.analysis(()).ok();
254 // Don't do code generation if there were any errors
255 self.session().compile_status()?;
257 // If we have any delayed bugs, for example because we created TyKind::Error earlier,
258 // it's likely that codegen will only cause more ICEs, obscuring the original problem
259 self.session().diagnostic().flush_delayed();
261 // Hook for UI tests.
262 Self::check_for_rustc_errors_attr(tcx);
264 Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
269 /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
270 /// to write UI tests that actually test that compilation succeeds without reporting
272 fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
273 let Some((def_id, _)) = tcx.entry_fn(()) else { return };
274 for attr in tcx.get_attrs(def_id, sym::rustc_error) {
275 match attr.meta_item_list() {
276 // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
278 if list.iter().any(|list_item| {
280 list_item.ident().map(|i| i.name),
281 Some(sym::delay_span_bug_from_inside_query)
285 tcx.ensure().trigger_delay_span_bug(def_id);
288 // Bare `#[rustc_error]`.
290 tcx.sess.emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
293 // Some other attribute.
295 tcx.sess.emit_warning(RustcErrorUnexpectedAnnotation {
296 span: tcx.def_span(def_id),
303 pub fn linker(&'tcx self) -> Result<Linker> {
304 let sess = self.session().clone();
305 let codegen_backend = self.codegen_backend().clone();
307 let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
308 (tcx.crate_hash(LOCAL_CRATE), tcx.output_filenames(()).clone(), tcx.dep_graph.clone())
310 let ongoing_codegen = self.ongoing_codegen()?.steal();
325 // compilation inputs
327 codegen_backend: Lrc<Box<dyn CodegenBackend>>,
329 // compilation outputs
331 prepare_outputs: Arc<OutputFilenames>,
333 ongoing_codegen: Box<dyn Any>,
337 pub fn link(self) -> Result<()> {
338 let (codegen_results, work_products) = self.codegen_backend.join_codegen(
339 self.ongoing_codegen,
341 &self.prepare_outputs,
344 self.sess.compile_status()?;
346 let sess = &self.sess;
347 let dep_graph = self.dep_graph;
348 sess.time("serialize_work_products", || {
349 rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
352 let prof = self.sess.prof.clone();
353 prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
355 // Now that we won't touch anything in the incremental compilation directory
356 // any more, we can finalize it (which involves renaming it)
357 rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
364 .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
369 if sess.opts.unstable_opts.no_link {
370 let encoded = CodegenResults::serialize_rlink(&codegen_results);
371 let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
372 std::fs::write(&rlink_file, encoded)
373 .map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
377 let _timer = sess.prof.verbose_generic_activity("link_crate");
378 self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
383 pub fn enter<F, T>(&self, f: F) -> T
385 F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
387 let mut _timer = None;
388 let queries = Queries::new(self);
389 let ret = f(&queries);
391 // NOTE: intentionally does not compute the global context if it hasn't been built yet,
392 // since that likely means there was a parse error.
393 if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() {
394 let gcx = gcx.get_mut();
395 // We assume that no queries are run past here. If there are new queries
396 // after this point, they'll show up as "<unknown>" in self-profiling data.
399 queries.session().prof.generic_activity("self_profile_alloc_query_strings");
400 gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
404 .time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
407 _timer = Some(self.session().timer("free_global_ctxt"));