1 use crate::interface::{Compiler, Result};
2 use crate::passes::{self, BoxedResolver, QueryContext};
5 use rustc_codegen_ssa::traits::CodegenBackend;
6 use rustc_data_structures::svh::Svh;
7 use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
8 use rustc_errors::ErrorReported;
9 use rustc_hir::def_id::LOCAL_CRATE;
10 use rustc_incremental::DepGraphFuture;
11 use rustc_lint::LintStore;
12 use rustc_middle::arena::Arena;
13 use rustc_middle::dep_graph::DepGraph;
14 use rustc_middle::ty::{GlobalCtxt, TyCtxt};
15 use rustc_query_impl::Queries as TcxQueries;
16 use rustc_serialize::json;
17 use rustc_session::config::{self, OutputFilenames, OutputType};
18 use rustc_session::{output::find_crate_name, Session};
19 use rustc_span::symbol::sym;
21 use std::cell::{Ref, RefCell, RefMut};
24 /// Represent the result of a query.
26 /// This result can be stolen with the [`take`] method and generated with the [`compute`] method.
28 /// [`take`]: Self::take
29 /// [`compute`]: Self::compute
31 result: RefCell<Option<Result<T>>>,
35 fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<&Query<T>> {
36 let mut result = self.result.borrow_mut();
40 result.as_ref().unwrap().as_ref().map(|_| self).map_err(|err| *err)
43 /// Takes ownership of the query result. Further attempts to take or peek the query
44 /// result will panic unless it is generated by calling the `compute` method.
45 pub fn take(&self) -> T {
46 self.result.borrow_mut().take().expect("missing query result").unwrap()
49 /// Borrows the query result using the RefCell. Panics if the result is stolen.
50 pub fn peek(&self) -> Ref<'_, T> {
51 Ref::map(self.result.borrow(), |r| {
52 r.as_ref().unwrap().as_ref().expect("missing query result")
56 /// Mutably borrows the query result using the RefCell. Panics if the result is stolen.
57 pub fn peek_mut(&self) -> RefMut<'_, T> {
58 RefMut::map(self.result.borrow_mut(), |r| {
59 r.as_mut().unwrap().as_mut().expect("missing query result")
64 impl<T> Default for Query<T> {
65 fn default() -> Self {
66 Query { result: RefCell::new(None) }
70 pub struct Queries<'tcx> {
71 compiler: &'tcx Compiler,
72 gcx: OnceCell<GlobalCtxt<'tcx>>,
73 queries: OnceCell<TcxQueries<'tcx>>,
75 arena: WorkerLocal<Arena<'tcx>>,
76 hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
78 dep_graph_future: Query<Option<DepGraphFuture>>,
79 parse: Query<ast::Crate>,
80 crate_name: Query<String>,
81 register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
82 expansion: Query<(Rc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
83 dep_graph: Query<DepGraph>,
84 prepare_outputs: Query<OutputFilenames>,
85 global_ctxt: Query<QueryContext<'tcx>>,
86 ongoing_codegen: Query<Box<dyn Any>>,
89 impl<'tcx> Queries<'tcx> {
90 pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
94 queries: OnceCell::new(),
95 arena: WorkerLocal::new(|_| Arena::default()),
96 hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()),
97 dep_graph_future: Default::default(),
98 parse: Default::default(),
99 crate_name: Default::default(),
100 register_plugins: Default::default(),
101 expansion: Default::default(),
102 dep_graph: Default::default(),
103 prepare_outputs: Default::default(),
104 global_ctxt: Default::default(),
105 ongoing_codegen: Default::default(),
109 fn session(&self) -> &Lrc<Session> {
112 fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
113 self.compiler.codegen_backend()
116 fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
117 self.dep_graph_future.compute(|| {
118 let sess = self.session();
119 Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess)))
123 pub fn parse(&self) -> Result<&Query<ast::Crate>> {
124 self.parse.compute(|| {
125 passes::parse(self.session(), &self.compiler.input).map_err(|mut parse_error| {
132 pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> {
133 self.register_plugins.compute(|| {
134 let crate_name = self.crate_name()?.peek().clone();
135 let krate = self.parse()?.take();
137 let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
138 let (krate, lint_store) = passes::register_plugins(
140 &*self.codegen_backend().metadata_loader(),
141 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
146 // Compute the dependency graph (in the background). We want to do
147 // this as early as possible, to give the DepGraph maximum time to
148 // load before dep_graph() is called, but it also can't happen
149 // until after rustc_incremental::prepare_session_directory() is
150 // called, which happens within passes::register_plugins().
151 self.dep_graph_future().ok();
153 Ok((krate, Lrc::new(lint_store)))
157 pub fn crate_name(&self) -> Result<&Query<String>> {
158 self.crate_name.compute(|| {
160 let parse_result = self.parse()?;
161 let krate = parse_result.peek();
162 // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
163 find_crate_name(self.session(), &krate.attrs, &self.compiler.input)
170 ) -> Result<&Query<(Rc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>> {
171 tracing::trace!("expansion");
172 self.expansion.compute(|| {
173 let crate_name = self.crate_name()?.peek().clone();
174 let (krate, lint_store) = self.register_plugins()?.take();
175 let _timer = self.session().timer("configure_and_expand");
176 let sess = self.session();
177 let mut resolver = passes::create_resolver(
179 self.codegen_backend().metadata_loader(),
183 let krate = resolver.access(|resolver| {
184 passes::configure_and_expand(sess, &lint_store, krate, &crate_name, resolver)
186 Ok((Rc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
190 fn dep_graph(&self) -> Result<&Query<DepGraph>> {
191 self.dep_graph.compute(|| {
192 let sess = self.session();
193 let future_opt = self.dep_graph_future()?.take();
194 let dep_graph = future_opt
196 let (prev_graph, prev_work_products) =
197 sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
199 rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
201 .unwrap_or_else(DepGraph::new_disabled);
206 pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
207 self.prepare_outputs.compute(|| {
208 let (krate, boxed_resolver, _) = &*self.expansion()?.peek();
209 let crate_name = self.crate_name()?.peek();
210 passes::prepare_outputs(
220 pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
221 self.global_ctxt.compute(|| {
222 let crate_name = self.crate_name()?.peek().clone();
223 let outputs = self.prepare_outputs()?.peek().clone();
224 let dep_graph = self.dep_graph()?.peek().clone();
225 let (krate, resolver, lint_store) = self.expansion()?.take();
226 Ok(passes::create_global_ctxt(
242 pub fn ongoing_codegen(&'tcx self) -> Result<&Query<Box<dyn Any>>> {
243 self.ongoing_codegen.compute(|| {
244 let outputs = self.prepare_outputs()?;
245 self.global_ctxt()?.peek_mut().enter(|tcx| {
246 tcx.analysis(()).ok();
248 // Don't do code generation if there were any errors
249 self.session().compile_status()?;
251 // Hook for UI tests.
252 Self::check_for_rustc_errors_attr(tcx);
254 Ok(passes::start_codegen(&***self.codegen_backend(), tcx, &*outputs.peek()))
259 /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
260 /// to write UI tests that actually test that compilation succeeds without reporting
262 fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
263 let def_id = match tcx.entry_fn(()) {
264 Some((def_id, _)) => def_id,
268 let attrs = &*tcx.get_attrs(def_id);
269 let attrs = attrs.iter().filter(|attr| attr.has_name(sym::rustc_error));
271 match attr.meta_item_list() {
272 // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
274 if list.iter().any(|list_item| {
276 list_item.ident().map(|i| i.name),
277 Some(sym::delay_span_bug_from_inside_query)
281 tcx.ensure().trigger_delay_span_bug(def_id);
284 // Bare `#[rustc_error]`.
287 tcx.def_span(def_id),
288 "fatal error triggered by #[rustc_error]",
292 // Some other attribute.
295 tcx.def_span(def_id),
296 "unexpected annotation used with `#[rustc_error(...)]!",
303 pub fn linker(&'tcx self) -> Result<Linker> {
304 let sess = self.session().clone();
305 let codegen_backend = self.codegen_backend().clone();
307 let dep_graph = self.dep_graph()?.peek().clone();
308 let prepare_outputs = self.prepare_outputs()?.take();
309 let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE));
310 let ongoing_codegen = self.ongoing_codegen()?.take();
325 // compilation inputs
327 codegen_backend: Lrc<Box<dyn CodegenBackend>>,
329 // compilation outputs
331 prepare_outputs: OutputFilenames,
333 ongoing_codegen: Box<dyn Any>,
337 pub fn link(self) -> Result<()> {
338 let (codegen_results, work_products) =
339 self.codegen_backend.join_codegen(self.ongoing_codegen, &self.sess)?;
341 self.sess.compile_status()?;
343 let sess = &self.sess;
344 let dep_graph = self.dep_graph;
345 sess.time("serialize_work_products", || {
346 rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
349 let prof = self.sess.prof.clone();
350 prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
352 // Now that we won't touch anything in the incremental compilation directory
353 // any more, we can finalize it (which involves renaming it)
354 rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
361 .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
366 if sess.opts.debugging_opts.no_link {
367 // FIXME: use a binary format to encode the `.rlink` file
368 let rlink_data = json::encode(&codegen_results).map_err(|err| {
369 sess.fatal(&format!("failed to encode rlink: {}", err));
371 let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
372 std::fs::write(&rlink_file, rlink_data).map_err(|err| {
373 sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
378 let _timer = sess.prof.verbose_generic_activity("link_crate");
379 self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
384 pub fn enter<F, T>(&self, f: F) -> T
386 F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
388 let mut _timer = None;
389 let queries = Queries::new(self);
390 let ret = f(&queries);
392 // NOTE: intentionally does not compute the global context if it hasn't been built yet,
393 // since that likely means there was a parse error.
394 if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_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);
403 if self.session().opts.debugging_opts.query_stats {
404 gcx.enter(rustc_query_impl::print_stats);
408 .time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
411 _timer = Some(self.session().timer("free_global_ctxt"));