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