]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_interface/src/queries.rs
Rollup merge of #77739 - est31:remove_unused_code, r=petrochenkov,varkor
[rust.git] / compiler / rustc_interface / src / queries.rs
1 use crate::interface::{Compiler, Result};
2 use crate::passes::{self, BoxedResolver, QueryContext};
3
4 use rustc_ast as ast;
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_hir::Crate;
11 use rustc_incremental::DepGraphFuture;
12 use rustc_lint::LintStore;
13 use rustc_middle::arena::Arena;
14 use rustc_middle::dep_graph::DepGraph;
15 use rustc_middle::ty::steal::Steal;
16 use rustc_middle::ty::{GlobalCtxt, ResolverOutputs, TyCtxt};
17 use rustc_serialize::json;
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 std::any::Any;
22 use std::cell::{Ref, RefCell, RefMut};
23 use std::rc::Rc;
24
25 /// Represent the result of a query.
26 /// This result can be stolen with the `take` method and generated with the `compute` method.
27 pub struct Query<T> {
28     result: RefCell<Option<Result<T>>>,
29 }
30
31 impl<T> Query<T> {
32     fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<&Query<T>> {
33         let mut result = self.result.borrow_mut();
34         if result.is_none() {
35             *result = Some(f());
36         }
37         result.as_ref().unwrap().as_ref().map(|_| self).map_err(|err| *err)
38     }
39
40     /// Takes ownership of the query result. Further attempts to take or peek the query
41     /// result will panic unless it is generated by calling the `compute` method.
42     pub fn take(&self) -> T {
43         self.result.borrow_mut().take().expect("missing query result").unwrap()
44     }
45
46     /// Borrows the query result using the RefCell. Panics if the result is stolen.
47     pub fn peek(&self) -> Ref<'_, T> {
48         Ref::map(self.result.borrow(), |r| {
49             r.as_ref().unwrap().as_ref().expect("missing query result")
50         })
51     }
52
53     /// Mutably borrows the query result using the RefCell. Panics if the result is stolen.
54     pub fn peek_mut(&self) -> RefMut<'_, T> {
55         RefMut::map(self.result.borrow_mut(), |r| {
56             r.as_mut().unwrap().as_mut().expect("missing query result")
57         })
58     }
59 }
60
61 impl<T> Default for Query<T> {
62     fn default() -> Self {
63         Query { result: RefCell::new(None) }
64     }
65 }
66
67 pub struct Queries<'tcx> {
68     compiler: &'tcx Compiler,
69     gcx: OnceCell<GlobalCtxt<'tcx>>,
70
71     arena: WorkerLocal<Arena<'tcx>>,
72     hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
73
74     dep_graph_future: Query<Option<DepGraphFuture>>,
75     parse: Query<ast::Crate>,
76     crate_name: Query<String>,
77     register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
78     expansion: Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>,
79     dep_graph: Query<DepGraph>,
80     lower_to_hir: Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>,
81     prepare_outputs: Query<OutputFilenames>,
82     global_ctxt: Query<QueryContext<'tcx>>,
83     ongoing_codegen: Query<Box<dyn Any>>,
84 }
85
86 impl<'tcx> Queries<'tcx> {
87     pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
88         Queries {
89             compiler,
90             gcx: OnceCell::new(),
91             arena: WorkerLocal::new(|_| Arena::default()),
92             hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()),
93             dep_graph_future: Default::default(),
94             parse: Default::default(),
95             crate_name: Default::default(),
96             register_plugins: Default::default(),
97             expansion: Default::default(),
98             dep_graph: Default::default(),
99             lower_to_hir: Default::default(),
100             prepare_outputs: Default::default(),
101             global_ctxt: Default::default(),
102             ongoing_codegen: Default::default(),
103         }
104     }
105
106     fn session(&self) -> &Lrc<Session> {
107         &self.compiler.sess
108     }
109     fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
110         &self.compiler.codegen_backend()
111     }
112
113     pub fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
114         self.dep_graph_future.compute(|| {
115             Ok(self
116                 .session()
117                 .opts
118                 .build_dep_graph()
119                 .then(|| rustc_incremental::load_dep_graph(self.session())))
120         })
121     }
122
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| {
126                 parse_error.emit();
127                 ErrorReported
128             })
129         })
130     }
131
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();
136
137             let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
138             let result = passes::register_plugins(
139                 self.session(),
140                 &*self.codegen_backend().metadata_loader(),
141                 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
142                 krate,
143                 &crate_name,
144             );
145
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();
152
153             result
154         })
155     }
156
157     pub fn crate_name(&self) -> Result<&Query<String>> {
158         self.crate_name.compute(|| {
159             Ok(match self.compiler.crate_name {
160                 Some(ref crate_name) => crate_name.clone(),
161                 None => {
162                     let parse_result = self.parse()?;
163                     let krate = parse_result.peek();
164                     find_crate_name(self.session(), &krate.attrs, &self.compiler.input)
165                 }
166             })
167         })
168     }
169
170     pub fn expansion(
171         &self,
172     ) -> Result<&Query<(ast::Crate, Steal<Rc<RefCell<BoxedResolver>>>, Lrc<LintStore>)>> {
173         tracing::trace!("expansion");
174         self.expansion.compute(|| {
175             let crate_name = self.crate_name()?.peek().clone();
176             let (krate, lint_store) = self.register_plugins()?.take();
177             let _timer = self.session().timer("configure_and_expand");
178             passes::configure_and_expand(
179                 self.session().clone(),
180                 lint_store.clone(),
181                 self.codegen_backend().metadata_loader(),
182                 krate,
183                 &crate_name,
184             )
185             .map(|(krate, resolver)| {
186                 (krate, Steal::new(Rc::new(RefCell::new(resolver))), lint_store)
187             })
188         })
189     }
190
191     pub fn dep_graph(&self) -> Result<&Query<DepGraph>> {
192         self.dep_graph.compute(|| {
193             Ok(match self.dep_graph_future()?.take() {
194                 None => DepGraph::new_disabled(),
195                 Some(future) => {
196                     let (prev_graph, prev_work_products) =
197                         self.session().time("blocked_on_dep_graph_loading", || {
198                             future
199                                 .open()
200                                 .unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
201                                     message: format!("could not decode incremental cache: {:?}", e),
202                                 })
203                                 .open(self.session())
204                         });
205                     DepGraph::new(prev_graph, prev_work_products)
206                 }
207             })
208         })
209     }
210
211     pub fn lower_to_hir(&'tcx self) -> Result<&Query<(&'tcx Crate<'tcx>, Steal<ResolverOutputs>)>> {
212         self.lower_to_hir.compute(|| {
213             let expansion_result = self.expansion()?;
214             let peeked = expansion_result.peek();
215             let krate = &peeked.0;
216             let resolver = peeked.1.steal();
217             let lint_store = &peeked.2;
218             let hir = resolver.borrow_mut().access(|resolver| {
219                 Ok(passes::lower_to_hir(
220                     self.session(),
221                     lint_store,
222                     resolver,
223                     &*self.dep_graph()?.peek(),
224                     &krate,
225                     &self.hir_arena,
226                 ))
227             })?;
228             let hir = self.hir_arena.alloc(hir);
229             Ok((hir, Steal::new(BoxedResolver::to_resolver_outputs(resolver))))
230         })
231     }
232
233     pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
234         self.prepare_outputs.compute(|| {
235             let expansion_result = self.expansion()?;
236             let (krate, boxed_resolver, _) = &*expansion_result.peek();
237             let crate_name = self.crate_name()?;
238             let crate_name = crate_name.peek();
239             passes::prepare_outputs(
240                 self.session(),
241                 self.compiler,
242                 &krate,
243                 &boxed_resolver,
244                 &crate_name,
245             )
246         })
247     }
248
249     pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
250         self.global_ctxt.compute(|| {
251             let crate_name = self.crate_name()?.peek().clone();
252             let outputs = self.prepare_outputs()?.peek().clone();
253             let lint_store = self.expansion()?.peek().2.clone();
254             let hir = self.lower_to_hir()?.peek();
255             let dep_graph = self.dep_graph()?.peek().clone();
256             let (ref krate, ref resolver_outputs) = &*hir;
257             let _timer = self.session().timer("create_global_ctxt");
258             Ok(passes::create_global_ctxt(
259                 self.compiler,
260                 lint_store,
261                 krate,
262                 dep_graph,
263                 resolver_outputs.steal(),
264                 outputs,
265                 &crate_name,
266                 &self.gcx,
267                 &self.arena,
268             ))
269         })
270     }
271
272     pub fn ongoing_codegen(&'tcx self) -> Result<&Query<Box<dyn Any>>> {
273         self.ongoing_codegen.compute(|| {
274             let outputs = self.prepare_outputs()?;
275             self.global_ctxt()?.peek_mut().enter(|tcx| {
276                 tcx.analysis(LOCAL_CRATE).ok();
277
278                 // Don't do code generation if there were any errors
279                 self.session().compile_status()?;
280
281                 // Hook for compile-fail tests.
282                 Self::check_for_rustc_errors_attr(tcx);
283
284                 Ok(passes::start_codegen(&***self.codegen_backend(), tcx, &*outputs.peek()))
285             })
286         })
287     }
288
289     /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
290     /// to write compile-fail tests that actually test that compilation succeeds without reporting
291     /// an error.
292     fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
293         let def_id = match tcx.entry_fn(LOCAL_CRATE) {
294             Some((def_id, _)) => def_id,
295             _ => return,
296         };
297
298         let attrs = &*tcx.get_attrs(def_id.to_def_id());
299         let attrs = attrs.iter().filter(|attr| tcx.sess.check_name(attr, sym::rustc_error));
300         for attr in attrs {
301             match attr.meta_item_list() {
302                 // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
303                 Some(list)
304                     if list.iter().any(|list_item| {
305                         matches!(
306                             list_item.ident().map(|i| i.name),
307                             Some(sym::delay_span_bug_from_inside_query)
308                         )
309                     }) =>
310                 {
311                     tcx.ensure().trigger_delay_span_bug(def_id);
312                 }
313
314                 // Bare `#[rustc_error]`.
315                 None => {
316                     tcx.sess.span_fatal(
317                         tcx.def_span(def_id),
318                         "fatal error triggered by #[rustc_error]",
319                     );
320                 }
321
322                 // Some other attribute.
323                 Some(_) => {
324                     tcx.sess.span_warn(
325                         tcx.def_span(def_id),
326                         "unexpected annotation used with `#[rustc_error(...)]!",
327                     );
328                 }
329             }
330         }
331     }
332
333     pub fn linker(&'tcx self) -> Result<Linker> {
334         let dep_graph = self.dep_graph()?;
335         let prepare_outputs = self.prepare_outputs()?;
336         let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE));
337         let ongoing_codegen = self.ongoing_codegen()?;
338
339         let sess = self.session().clone();
340         let codegen_backend = self.codegen_backend().clone();
341
342         Ok(Linker {
343             sess,
344             dep_graph: dep_graph.peek().clone(),
345             prepare_outputs: prepare_outputs.take(),
346             crate_hash,
347             ongoing_codegen: ongoing_codegen.take(),
348             codegen_backend,
349         })
350     }
351 }
352
353 pub struct Linker {
354     sess: Lrc<Session>,
355     dep_graph: DepGraph,
356     prepare_outputs: OutputFilenames,
357     crate_hash: Svh,
358     ongoing_codegen: Box<dyn Any>,
359     codegen_backend: Lrc<Box<dyn CodegenBackend>>,
360 }
361
362 impl Linker {
363     pub fn link(self) -> Result<()> {
364         let (codegen_results, work_products) =
365             self.codegen_backend.join_codegen(self.ongoing_codegen, &self.sess)?;
366
367         self.sess.compile_status()?;
368
369         let sess = &self.sess;
370         let dep_graph = self.dep_graph;
371         sess.time("serialize_work_products", || {
372             rustc_incremental::save_work_product_index(&sess, &dep_graph, work_products)
373         });
374
375         let prof = self.sess.prof.clone();
376         prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
377
378         // Now that we won't touch anything in the incremental compilation directory
379         // any more, we can finalize it (which involves renaming it)
380         rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
381
382         if !self
383             .sess
384             .opts
385             .output_types
386             .keys()
387             .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
388         {
389             return Ok(());
390         }
391
392         if sess.opts.debugging_opts.no_link {
393             // FIXME: use a binary format to encode the `.rlink` file
394             let rlink_data = json::encode(&codegen_results).map_err(|err| {
395                 sess.fatal(&format!("failed to encode rlink: {}", err));
396             })?;
397             let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
398             std::fs::write(&rlink_file, rlink_data).map_err(|err| {
399                 sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
400             })?;
401             return Ok(());
402         }
403
404         self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
405     }
406 }
407
408 impl Compiler {
409     pub fn enter<F, T>(&self, f: F) -> T
410     where
411         F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
412     {
413         let mut _timer = None;
414         let queries = Queries::new(&self);
415         let ret = f(&queries);
416
417         if self.session().opts.debugging_opts.query_stats {
418             if let Ok(gcx) = queries.global_ctxt() {
419                 gcx.peek_mut().print_stats();
420             }
421         }
422
423         _timer = Some(self.session().timer("free_global_ctxt"));
424
425         ret
426     }
427 }