]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_interface/src/queries.rs
Rollup merge of #107027 - GuillaumeGomez:rm-extra-removal, r=tmiasko
[rust.git] / compiler / rustc_interface / src / queries.rs
1 use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
2 use crate::interface::{Compiler, Result};
3 use crate::passes::{self, BoxedResolver, QueryContext};
4
5 use rustc_ast as ast;
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;
22 use std::any::Any;
23 use std::cell::{RefCell, RefMut};
24 use std::rc::Rc;
25 use std::sync::Arc;
26
27 /// Represent the result of a query.
28 ///
29 /// This result can be stolen once with the [`steal`] method and generated with the [`compute`] method.
30 ///
31 /// [`steal`]: Steal::steal
32 /// [`compute`]: Self::compute
33 pub struct Query<T> {
34     /// `None` means no value has been computed yet.
35     result: RefCell<Option<Result<Steal<T>>>>,
36 }
37
38 impl<T> Query<T> {
39     fn compute<F: FnOnce() -> Result<T>>(&self, f: F) -> Result<QueryResult<'_, T>> {
40         RefMut::filter_map(
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()
44             },
45         )
46         .map_err(|r| *r.as_ref().unwrap().as_ref().map(|_| ()).unwrap_err())
47         .map(QueryResult)
48     }
49 }
50
51 pub struct QueryResult<'a, T>(RefMut<'a, Steal<T>>);
52
53 impl<'a, T> std::ops::Deref for QueryResult<'a, T> {
54     type Target = RefMut<'a, Steal<T>>;
55
56     fn deref(&self) -> &Self::Target {
57         &self.0
58     }
59 }
60
61 impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
62     fn deref_mut(&mut self) -> &mut Self::Target {
63         &mut self.0
64     }
65 }
66
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)
70     }
71 }
72
73 impl<T> Default for Query<T> {
74     fn default() -> Self {
75         Query { result: RefCell::new(None) }
76     }
77 }
78
79 pub struct Queries<'tcx> {
80     compiler: &'tcx Compiler,
81     gcx: OnceCell<GlobalCtxt<'tcx>>,
82     queries: OnceCell<TcxQueries<'tcx>>,
83
84     arena: WorkerLocal<Arena<'tcx>>,
85     hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
86
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>>,
95 }
96
97 impl<'tcx> Queries<'tcx> {
98     pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
99         Queries {
100             compiler,
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(),
113         }
114     }
115
116     fn session(&self) -> &Lrc<Session> {
117         &self.compiler.sess
118     }
119     fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
120         self.compiler.codegen_backend()
121     }
122
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)))
127         })
128     }
129
130     pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
131         self.parse
132             .compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
133     }
134
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();
139
140             let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
141             let (krate, lint_store) = passes::register_plugins(
142                 self.session(),
143                 &*self.codegen_backend().metadata_loader(),
144                 self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
145                 krate,
146                 crate_name,
147             )?;
148
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();
155
156             Ok((krate, Lrc::new(lint_store)))
157         })
158     }
159
160     fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
161         self.crate_name.compute(|| {
162             Ok({
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)
167             })
168         })
169     }
170
171     pub fn expansion(
172         &self,
173     ) -> Result<QueryResult<'_, (Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>>
174     {
175         trace!("expansion");
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(
182                 sess.clone(),
183                 self.codegen_backend().metadata_loader(),
184                 &krate,
185                 crate_name,
186             );
187             let krate = resolver.access(|resolver| {
188                 passes::configure_and_expand(sess, &lint_store, krate, crate_name, resolver)
189             })?;
190             Ok((Lrc::new(krate), Rc::new(RefCell::new(resolver)), lint_store))
191         })
192     }
193
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
199                 .and_then(|future| {
200                     let (prev_graph, prev_work_products) =
201                         sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
202
203                     rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
204                 })
205                 .unwrap_or_else(DepGraph::new_disabled);
206             Ok(dep_graph)
207         })
208     }
209
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();
214
215             let outputs = passes::prepare_outputs(self.session(), &krate, &resolver, crate_name)?;
216
217             let ty::ResolverOutputs {
218                 untracked,
219                 global_ctxt: untracked_resolutions,
220                 ast_lowering: untracked_resolver_for_lowering,
221             } = BoxedResolver::to_resolver_outputs(resolver);
222
223             let mut qcx = passes::create_global_ctxt(
224                 self.compiler,
225                 lint_store,
226                 self.dep_graph()?.steal(),
227                 untracked,
228                 &self.queries,
229                 &self.gcx,
230                 &self.arena,
231                 &self.hir_arena,
232             );
233
234             qcx.enter(|tcx| {
235                 let feed = tcx.feed_unit_query();
236                 feed.resolver_for_lowering(
237                     tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
238                 );
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);
244             });
245             Ok(qcx)
246         })
247     }
248
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();
253
254                 // Don't do code generation if there were any errors
255                 self.session().compile_status()?;
256
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();
260
261                 // Hook for UI tests.
262                 Self::check_for_rustc_errors_attr(tcx);
263
264                 Ok(passes::start_codegen(&***self.codegen_backend(), tcx))
265             })
266         })
267     }
268
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
271     /// an error.
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)]`.
277                 Some(list)
278                     if list.iter().any(|list_item| {
279                         matches!(
280                             list_item.ident().map(|i| i.name),
281                             Some(sym::delay_span_bug_from_inside_query)
282                         )
283                     }) =>
284                 {
285                     tcx.ensure().trigger_delay_span_bug(def_id);
286                 }
287
288                 // Bare `#[rustc_error]`.
289                 None => {
290                     tcx.sess.emit_fatal(RustcErrorFatal { span: tcx.def_span(def_id) });
291                 }
292
293                 // Some other attribute.
294                 Some(_) => {
295                     tcx.sess.emit_warning(RustcErrorUnexpectedAnnotation {
296                         span: tcx.def_span(def_id),
297                     });
298                 }
299             }
300         }
301     }
302
303     pub fn linker(&'tcx self) -> Result<Linker> {
304         let sess = self.session().clone();
305         let codegen_backend = self.codegen_backend().clone();
306
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())
309         });
310         let ongoing_codegen = self.ongoing_codegen()?.steal();
311
312         Ok(Linker {
313             sess,
314             codegen_backend,
315
316             dep_graph,
317             prepare_outputs,
318             crate_hash,
319             ongoing_codegen,
320         })
321     }
322 }
323
324 pub struct Linker {
325     // compilation inputs
326     sess: Lrc<Session>,
327     codegen_backend: Lrc<Box<dyn CodegenBackend>>,
328
329     // compilation outputs
330     dep_graph: DepGraph,
331     prepare_outputs: Arc<OutputFilenames>,
332     crate_hash: Svh,
333     ongoing_codegen: Box<dyn Any>,
334 }
335
336 impl Linker {
337     pub fn link(self) -> Result<()> {
338         let (codegen_results, work_products) = self.codegen_backend.join_codegen(
339             self.ongoing_codegen,
340             &self.sess,
341             &self.prepare_outputs,
342         )?;
343
344         self.sess.compile_status()?;
345
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)
350         });
351
352         let prof = self.sess.prof.clone();
353         prof.generic_activity("drop_dep_graph").run(move || drop(dep_graph));
354
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);
358
359         if !self
360             .sess
361             .opts
362             .output_types
363             .keys()
364             .any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
365         {
366             return Ok(());
367         }
368
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 }))?;
374             return Ok(());
375         }
376
377         let _timer = sess.prof.verbose_generic_activity("link_crate");
378         self.codegen_backend.link(&self.sess, codegen_results, &self.prepare_outputs)
379     }
380 }
381
382 impl Compiler {
383     pub fn enter<F, T>(&self, f: F) -> T
384     where
385         F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
386     {
387         let mut _timer = None;
388         let queries = Queries::new(self);
389         let ret = f(&queries);
390
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.
397             {
398                 let _prof_timer =
399                     queries.session().prof.generic_activity("self_profile_alloc_query_strings");
400                 gcx.enter(rustc_query_impl::alloc_self_profile_query_strings);
401             }
402
403             self.session()
404                 .time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
405         }
406
407         _timer = Some(self.session().timer("free_global_ctxt"));
408
409         ret
410     }
411 }