]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_interface/src/queries.rs
Auto merge of #98463 - mystor:expand_expr_bool, r=eddyb
[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_codegen_ssa::CodegenResults;
7 use rustc_data_structures::svh::Svh;
8 use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
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_session::config::{self, OutputFilenames, OutputType};
17 use rustc_session::{output::find_crate_name, Session};
18 use rustc_span::symbol::sym;
19 use std::any::Any;
20 use std::cell::{Ref, RefCell, RefMut};
21 use std::rc::Rc;
22
23 /// Represent the result of a query.
24 ///
25 /// This result can be stolen with the [`take`] method and generated with the [`compute`] method.
26 ///
27 /// [`take`]: Self::take
28 /// [`compute`]: Self::compute
29 pub struct Query<T> {
30     result: RefCell<Option<Result<T>>>,
31 }
32
33 impl<T> Query<T> {
34     fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<&Query<T>> {
35         let mut result = self.result.borrow_mut();
36         if result.is_none() {
37             *result = Some(f());
38         }
39         result.as_ref().unwrap().as_ref().map(|_| self).map_err(|err| *err)
40     }
41
42     /// Takes ownership of the query result. Further attempts to take or peek the query
43     /// result will panic unless it is generated by calling the `compute` method.
44     pub fn take(&self) -> T {
45         self.result.borrow_mut().take().expect("missing query result").unwrap()
46     }
47
48     /// Borrows the query result using the RefCell. Panics if the result is stolen.
49     pub fn peek(&self) -> Ref<'_, T> {
50         Ref::map(self.result.borrow(), |r| {
51             r.as_ref().unwrap().as_ref().expect("missing query result")
52         })
53     }
54
55     /// Mutably borrows the query result using the RefCell. Panics if the result is stolen.
56     pub fn peek_mut(&self) -> RefMut<'_, T> {
57         RefMut::map(self.result.borrow_mut(), |r| {
58             r.as_mut().unwrap().as_mut().expect("missing query result")
59         })
60     }
61 }
62
63 impl<T> Default for Query<T> {
64     fn default() -> Self {
65         Query { result: RefCell::new(None) }
66     }
67 }
68
69 pub struct Queries<'tcx> {
70     compiler: &'tcx Compiler,
71     gcx: OnceCell<GlobalCtxt<'tcx>>,
72     queries: OnceCell<TcxQueries<'tcx>>,
73
74     arena: WorkerLocal<Arena<'tcx>>,
75     hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
76
77     dep_graph_future: Query<Option<DepGraphFuture>>,
78     parse: Query<ast::Crate>,
79     crate_name: Query<String>,
80     register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
81     expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
82     dep_graph: Query<DepGraph>,
83     prepare_outputs: Query<OutputFilenames>,
84     global_ctxt: Query<QueryContext<'tcx>>,
85     ongoing_codegen: Query<Box<dyn Any>>,
86 }
87
88 impl<'tcx> Queries<'tcx> {
89     pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
90         Queries {
91             compiler,
92             gcx: OnceCell::new(),
93             queries: OnceCell::new(),
94             arena: WorkerLocal::new(|_| Arena::default()),
95             hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
96             dep_graph_future: Default::default(),
97             parse: Default::default(),
98             crate_name: Default::default(),
99             register_plugins: Default::default(),
100             expansion: Default::default(),
101             dep_graph: Default::default(),
102             prepare_outputs: Default::default(),
103             global_ctxt: Default::default(),
104             ongoing_codegen: Default::default(),
105         }
106     }
107
108     fn session(&self) -> &Lrc<Session> {
109         &self.compiler.sess
110     }
111     fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
112         self.compiler.codegen_backend()
113     }
114
115     fn dep_graph_future(&self) -> Result<&Query<Option<DepGraphFuture>>> {
116         self.dep_graph_future.compute(|| {
117             let sess = self.session();
118             Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess)))
119         })
120     }
121
122     pub fn parse(&self) -> Result<&Query<ast::Crate>> {
123         self.parse.compute(|| {
124             passes::parse(self.session(), &self.compiler.input)
125                 .map_err(|mut parse_error| parse_error.emit())
126         })
127     }
128
129     pub fn register_plugins(&self) -> Result<&Query<(ast::Crate, Lrc<LintStore>)>> {
130         self.register_plugins.compute(|| {
131             let crate_name = self.crate_name()?.peek().clone();
132             let krate = self.parse()?.take();
133
134             let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
135             let (krate, lint_store) = passes::register_plugins(
136                 self.session(),
137                 &*self.codegen_backend().metadata_loader(),
138                 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
139                 krate,
140                 &crate_name,
141             )?;
142
143             // Compute the dependency graph (in the background). We want to do
144             // this as early as possible, to give the DepGraph maximum time to
145             // load before dep_graph() is called, but it also can't happen
146             // until after rustc_incremental::prepare_session_directory() is
147             // called, which happens within passes::register_plugins().
148             self.dep_graph_future().ok();
149
150             Ok((krate, Lrc::new(lint_store)))
151         })
152     }
153
154     pub fn crate_name(&self) -> Result<&Query<String>> {
155         self.crate_name.compute(|| {
156             Ok({
157                 let parse_result = self.parse()?;
158                 let krate = parse_result.peek();
159                 // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
160                 find_crate_name(self.session(), &krate.attrs, &self.compiler.input)
161             })
162         })
163     }
164
165     pub fn expansion(
166         &self,
167     ) -> Result<&Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>> {
168         tracing::trace!("expansion");
169         self.expansion.compute(|| {
170             let crate_name = self.crate_name()?.peek().clone();
171             let (krate, lint_store) = self.register_plugins()?.take();
172             let _timer = self.session().timer("configure_and_expand");
173             let sess = self.session();
174             let mut resolver = passes::create_resolver(
175                 sess.clone(),
176                 self.codegen_backend().metadata_loader(),
177                 &krate,
178                 &crate_name,
179             );
180             let krate = resolver.access(|resolver| {
181                 passes::configure_and_expand(sess, &lint_store, krate, &crate_name, resolver)
182             })?;
183             Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
184         })
185     }
186
187     fn dep_graph(&self) -> Result<&Query<DepGraph>> {
188         self.dep_graph.compute(|| {
189             let sess = self.session();
190             let future_opt = self.dep_graph_future()?.take();
191             let dep_graph = future_opt
192                 .and_then(|future| {
193                     let (prev_graph, prev_work_products) =
194                         sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
195
196                     rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
197                 })
198                 .unwrap_or_else(DepGraph::new_disabled);
199             Ok(dep_graph)
200         })
201     }
202
203     pub fn prepare_outputs(&self) -> Result<&Query<OutputFilenames>> {
204         self.prepare_outputs.compute(|| {
205             let (krate, boxed_resolver, _) = &*self.expansion()?.peek();
206             let crate_name = self.crate_name()?.peek();
207             passes::prepare_outputs(
208                 self.session(),
209                 self.compiler,
210                 krate,
211                 &*boxed_resolver,
212                 &crate_name,
213             )
214         })
215     }
216
217     pub fn global_ctxt(&'tcx self) -> Result<&Query<QueryContext<'tcx>>> {
218         self.global_ctxt.compute(|| {
219             let crate_name = self.crate_name()?.peek().clone();
220             let outputs = self.prepare_outputs()?.peek().clone();
221             let dep_graph = self.dep_graph()?.peek().clone();
222             let (krate, resolver, lint_store) = self.expansion()?.take();
223             Ok(passes::create_global_ctxt(
224                 self.compiler,
225                 lint_store,
226                 krate,
227                 dep_graph,
228                 resolver,
229                 outputs,
230                 &crate_name,
231                 &self.queries,
232                 &self.gcx,
233                 &self.arena,
234                 &self.hir_arena,
235             ))
236         })
237     }
238
239     pub fn ongoing_codegen(&'tcx self) -> Result<&Query<Box<dyn Any>>> {
240         self.ongoing_codegen.compute(|| {
241             let outputs = self.prepare_outputs()?;
242             self.global_ctxt()?.peek_mut().enter(|tcx| {
243                 tcx.analysis(()).ok();
244
245                 // Don't do code generation if there were any errors
246                 self.session().compile_status()?;
247
248                 // Hook for UI tests.
249                 Self::check_for_rustc_errors_attr(tcx);
250
251                 Ok(passes::start_codegen(&***self.codegen_backend(), tcx, &*outputs.peek()))
252             })
253         })
254     }
255
256     /// Check for the `#[rustc_error]` annotation, which forces an error in codegen. This is used
257     /// to write UI tests that actually test that compilation succeeds without reporting
258     /// an error.
259     fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
260         let Some((def_id, _)) = tcx.entry_fn(()) else { return };
261         for attr in tcx.get_attrs(def_id, sym::rustc_error) {
262             match attr.meta_item_list() {
263                 // Check if there is a `#[rustc_error(delay_span_bug_from_inside_query)]`.
264                 Some(list)
265                     if list.iter().any(|list_item| {
266                         matches!(
267                             list_item.ident().map(|i| i.name),
268                             Some(sym::delay_span_bug_from_inside_query)
269                         )
270                     }) =>
271                 {
272                     tcx.ensure().trigger_delay_span_bug(def_id);
273                 }
274
275                 // Bare `#[rustc_error]`.
276                 None => {
277                     tcx.sess.span_fatal(
278                         tcx.def_span(def_id),
279                         "fatal error triggered by #[rustc_error]",
280                     );
281                 }
282
283                 // Some other attribute.
284                 Some(_) => {
285                     tcx.sess.span_warn(
286                         tcx.def_span(def_id),
287                         "unexpected annotation used with `#[rustc_error(...)]!",
288                     );
289                 }
290             }
291         }
292     }
293
294     pub fn linker(&'tcx self) -> Result<Linker> {
295         let sess = self.session().clone();
296         let codegen_backend = self.codegen_backend().clone();
297
298         let dep_graph = self.dep_graph()?.peek().clone();
299         let prepare_outputs = self.prepare_outputs()?.take();
300         let crate_hash = self.global_ctxt()?.peek_mut().enter(|tcx| tcx.crate_hash(LOCAL_CRATE));
301         let ongoing_codegen = self.ongoing_codegen()?.take();
302
303         Ok(Linker {
304             sess,
305             codegen_backend,
306
307             dep_graph,
308             prepare_outputs,
309             crate_hash,
310             ongoing_codegen,
311         })
312     }
313 }
314
315 pub struct Linker {
316     // compilation inputs
317     sess: Lrc<Session>,
318     codegen_backend: Lrc<Box<dyn CodegenBackend>>,
319
320     // compilation outputs
321     dep_graph: DepGraph,
322     prepare_outputs: OutputFilenames,
323     crate_hash: Svh,
324     ongoing_codegen: Box<dyn Any>,
325 }
326
327 impl Linker {
328     pub fn link(self) -> Result<()> {
329         let (codegen_results, work_products) = self.codegen_backend.join_codegen(
330             self.ongoing_codegen,
331             &self.sess,
332             &self.prepare_outputs,
333         )?;
334
335         self.sess.compile_status()?;
336
337         let sess = &self.sess;
338         let dep_graph = self.dep_graph;
339         sess.time("serialize_work_products", || {
340             rustc_incremental::save_work_product_index(sess, &dep_graph, work_products)
341         });
342
343         let prof = self.sess.prof.clone();
344         prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
345
346         // Now that we won't touch anything in the incremental compilation directory
347         // any more, we can finalize it (which involves renaming it)
348         rustc_incremental::finalize_session_directory(&self.sess, self.crate_hash);
349
350         if !self
351             .sess
352             .opts
353             .output_types
354             .keys()
355             .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
356         {
357             return Ok(());
358         }
359
360         if sess.opts.debugging_opts.no_link {
361             let encoded = CodegenResults::serialize_rlink(&codegen_results);
362             let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
363             std::fs::write(&rlink_file, encoded).map_err(|err| {
364                 sess.fatal(&format!("failed to write file {}: {}", rlink_file.display(), err));
365             })?;
366             return Ok(());
367         }
368
369         let _timer = sess.prof.verbose_generic_activity("link_crate");
370         self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
371     }
372 }
373
374 impl Compiler {
375     pub fn enter<F, T>(&self, f: F) -> T
376     where
377         F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
378     {
379         let mut _timer = None;
380         let queries = Queries::new(self);
381         let ret = f(&queries);
382
383         // NOTE: intentionally does not compute the global context if it hasn't been built yet,
384         // since that likely means there was a parse error.
385         if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() {
386             // We assume that no queries are run past here. If there are new queries
387             // after this point, they'll show up as "<unknown>" in self-profiling data.
388             {
389                 let _prof_timer =
390                     queries.session().prof.generic_activity("self_profile_alloc_query_strings");
391                 gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
392             }
393
394             self.session()
395                 .time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
396         }
397
398         _timer = Some(self.session().timer("free_global_ctxt"));
399
400         ret
401     }
402 }