]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/ty/query/plumbing.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / query / plumbing.rs
1 //! The implementation of the query system itself. This defines the macros that
2 //! generate the actual methods on tcx which find and execute the provider,
3 //! manage the caches, and so forth.
4
5 use crate::dep_graph::DepGraph;
6 use crate::ty::query::Query;
7 use crate::ty::tls::{self, ImplicitCtxt};
8 use crate::ty::{self, TyCtxt};
9 use rustc_query_system::query::QueryContext;
10 use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo};
11
12 use rustc_data_structures::fx::FxHashMap;
13 use rustc_data_structures::sync::Lock;
14 use rustc_data_structures::thin_vec::ThinVec;
15 use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level};
16 use rustc_span::def_id::DefId;
17 use rustc_span::Span;
18
19 impl QueryContext for TyCtxt<'tcx> {
20 type Query = Query<'tcx>;
21
22 fn incremental_verify_ich(&self) -> bool {
23 self.sess.opts.debugging_opts.incremental_verify_ich
24 }
25 fn verbose(&self) -> bool {
26 self.sess.verbose()
27 }
28
29 fn def_path_str(&self, def_id: DefId) -> String {
30 TyCtxt::def_path_str(*self, def_id)
31 }
32
33 fn dep_graph(&self) -> &DepGraph {
34 &self.dep_graph
35 }
36
37 fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
38 tls::with_related_context(*self, |icx| icx.query)
39 }
40
41 fn try_collect_active_jobs(
42 &self,
43 ) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self>>> {
44 self.queries.try_collect_active_jobs()
45 }
46
47 /// Executes a job by changing the `ImplicitCtxt` to point to the
48 /// new query job while it executes. It returns the diagnostics
49 /// captured during execution and the actual result.
50 #[inline(always)]
51 fn start_query<R>(
52 &self,
53 token: QueryJobId<Self::DepKind>,
54 diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
55 compute: impl FnOnce(Self) -> R,
56 ) -> R {
57 // The `TyCtxt` stored in TLS has the same global interner lifetime
58 // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
59 // when accessing the `ImplicitCtxt`.
60 tls::with_related_context(*self, move |current_icx| {
61 // Update the `ImplicitCtxt` to point to our new query job.
62 let new_icx = ImplicitCtxt {
63 tcx: *self,
64 query: Some(token),
65 diagnostics,
66 layout_depth: current_icx.layout_depth,
67 task_deps: current_icx.task_deps,
68 };
69
70 // Use the `ImplicitCtxt` while we execute the query.
71 tls::enter_context(&new_icx, |_| {
72 rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
73 })
74 })
75 }
76 }
77
78 impl<'tcx> TyCtxt<'tcx> {
79 #[inline(never)]
80 #[cold]
81 pub(super) fn report_cycle(
82 self,
83 CycleError { usage, cycle: stack }: CycleError<Query<'tcx>>,
84 ) -> DiagnosticBuilder<'tcx> {
85 assert!(!stack.is_empty());
86
87 let fix_span = |span: Span, query: &Query<'tcx>| {
88 self.sess.source_map().guess_head_span(query.default_span(self, span))
89 };
90
91 // Disable naming impls with types in this path, since that
92 // sometimes cycles itself, leading to extra cycle errors.
93 // (And cycle errors around impls tend to occur during the
94 // collect/coherence phases anyhow.)
95 ty::print::with_forced_impl_filename_line(|| {
96 let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
97 let mut err = struct_span_err!(
98 self.sess,
99 span,
100 E0391,
101 "cycle detected when {}",
102 stack[0].query.describe(self)
103 );
104
105 for i in 1..stack.len() {
106 let query = &stack[i].query;
107 let span = fix_span(stack[(i + 1) % stack.len()].span, query);
108 err.span_note(span, &format!("...which requires {}...", query.describe(self)));
109 }
110
111 err.note(&format!(
112 "...which again requires {}, completing the cycle",
113 stack[0].query.describe(self)
114 ));
115
116 if let Some((span, query)) = usage {
117 err.span_note(
118 fix_span(span, &query),
119 &format!("cycle used when {}", query.describe(self)),
120 );
121 }
122
123 err
124 })
125 }
126
127 pub fn try_print_query_stack(handler: &Handler) {
128 eprintln!("query stack during panic:");
129
130 // Be careful reyling on global state here: this code is called from
131 // a panic hook, which means that the global `Handler` may be in a weird
132 // state if it was responsible for triggering the panic.
133 ty::tls::with_context_opt(|icx| {
134 if let Some(icx) = icx {
135 let query_map = icx.tcx.queries.try_collect_active_jobs();
136
137 let mut current_query = icx.query;
138 let mut i = 0;
139
140 while let Some(query) = current_query {
141 let query_info =
142 if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) {
143 info
144 } else {
145 break;
146 };
147 let mut diag = Diagnostic::new(
148 Level::FailureNote,
149 &format!(
150 "#{} [{}] {}",
151 i,
152 query_info.info.query.name(),
153 query_info.info.query.describe(icx.tcx)
154 ),
155 );
156 diag.span =
157 icx.tcx.sess.source_map().guess_head_span(query_info.info.span).into();
158 handler.force_print_diagnostic(diag);
159
160 current_query = query_info.job.parent;
161 i += 1;
162 }
163 }
164 });
165
166 eprintln!("end of query stack");
167 }
168 }
169
170 macro_rules! handle_cycle_error {
171 ([][$tcx: expr, $error:expr]) => {{
172 $tcx.report_cycle($error).emit();
173 Value::from_cycle_error($tcx)
174 }};
175 ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
176 $tcx.report_cycle($error).emit();
177 $tcx.sess.abort_if_errors();
178 unreachable!()
179 }};
180 ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
181 $tcx.report_cycle($error).delay_as_bug();
182 Value::from_cycle_error($tcx)
183 }};
184 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
185 handle_cycle_error!([$($($modifiers)*)*][$($args)*])
186 };
187 }
188
189 macro_rules! is_anon {
190 ([]) => {{
191 false
192 }};
193 ([anon $($rest:tt)*]) => {{
194 true
195 }};
196 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
197 is_anon!([$($($modifiers)*)*])
198 };
199 }
200
201 macro_rules! is_eval_always {
202 ([]) => {{
203 false
204 }};
205 ([eval_always $($rest:tt)*]) => {{
206 true
207 }};
208 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
209 is_eval_always!([$($($modifiers)*)*])
210 };
211 }
212
213 macro_rules! query_storage {
214 ([][$K:ty, $V:ty]) => {
215 <<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache
216 };
217 ([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
218 <$ty as CacheSelector<$K, $V>>::Cache
219 };
220 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
221 query_storage!([$($($modifiers)*)*][$($args)*])
222 };
223 }
224
225 macro_rules! hash_result {
226 ([][$hcx:expr, $result:expr]) => {{
227 dep_graph::hash_result($hcx, &$result)
228 }};
229 ([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
230 None
231 }};
232 ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
233 hash_result!([$($($modifiers)*)*][$($args)*])
234 };
235 }
236
237 macro_rules! define_queries {
238 (<$tcx:tt> $($category:tt {
239 $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*
240 },)*) => {
241 define_queries_inner! { <$tcx>
242 $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)*
243 }
244 }
245 }
246
247 macro_rules! query_helper_param_ty {
248 (DefId) => { impl IntoQueryParam<DefId> };
249 ($K:ty) => { $K };
250 }
251
252 macro_rules! define_queries_inner {
253 (<$tcx:tt>
254 $($(#[$attr:meta])* category<$category:tt>
255 [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
256
257 use std::mem;
258 use crate::{
259 rustc_data_structures::stable_hasher::HashStable,
260 rustc_data_structures::stable_hasher::StableHasher,
261 ich::StableHashingContext
262 };
263 use rustc_data_structures::profiling::ProfileCategory;
264
265 define_queries_struct! {
266 tcx: $tcx,
267 input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
268 }
269
270 #[allow(nonstandard_style)]
271 #[derive(Clone, Debug)]
272 pub enum Query<$tcx> {
273 $($(#[$attr])* $name($($K)*)),*
274 }
275
276 impl<$tcx> Query<$tcx> {
277 pub fn name(&self) -> &'static str {
278 match *self {
279 $(Query::$name(_) => stringify!($name),)*
280 }
281 }
282
283 pub fn describe(&self, tcx: TyCtxt<$tcx>) -> Cow<'static, str> {
284 let (r, name) = match *self {
285 $(Query::$name(key) => {
286 (queries::$name::describe(tcx, key), stringify!($name))
287 })*
288 };
289 if tcx.sess.verbose() {
290 format!("{} [{}]", r, name).into()
291 } else {
292 r
293 }
294 }
295
296 // FIXME(eddyb) Get more valid `Span`s on queries.
297 pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span {
298 if !span.is_dummy() {
299 return span;
300 }
301 // The `def_span` query is used to calculate `default_span`,
302 // so exit to avoid infinite recursion.
303 if let Query::def_span(..) = *self {
304 return span
305 }
306 match *self {
307 $(Query::$name(key) => key.default_span(tcx),)*
308 }
309 }
310 }
311
312 impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
313 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
314 mem::discriminant(self).hash_stable(hcx, hasher);
315 match *self {
316 $(Query::$name(key) => key.hash_stable(hcx, hasher),)*
317 }
318 }
319 }
320
321 #[allow(nonstandard_style)]
322 pub mod queries {
323 use std::marker::PhantomData;
324
325 $(pub struct $name<$tcx> {
326 data: PhantomData<&$tcx ()>
327 })*
328 }
329
330 // HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
331 // below, but using type aliases instead of associated types, to bypass
332 // the limitations around normalizing under HRTB - for example, this:
333 // `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
334 // doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
335 // This is primarily used by the `provide!` macro in `rustc_metadata`.
336 #[allow(nonstandard_style, unused_lifetimes)]
337 pub mod query_keys {
338 use super::*;
339
340 $(pub type $name<$tcx> = $($K)*;)*
341 }
342 #[allow(nonstandard_style, unused_lifetimes)]
343 pub mod query_values {
344 use super::*;
345
346 $(pub type $name<$tcx> = $V;)*
347 }
348
349 $(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
350 type Key = $($K)*;
351 type Value = $V;
352 type Stored = <
353 query_storage!([$($modifiers)*][$($K)*, $V])
354 as QueryStorage
355 >::Stored;
356 const NAME: &'static str = stringify!($name);
357 const CATEGORY: ProfileCategory = $category;
358 }
359
360 impl<$tcx> QueryAccessors<TyCtxt<$tcx>> for queries::$name<$tcx> {
361 const ANON: bool = is_anon!([$($modifiers)*]);
362 const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
363 const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
364
365 type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
366
367 #[inline(always)]
368 fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
369 &tcx.queries.$name
370 }
371
372 #[inline]
373 fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
374 let provider = tcx.queries.providers.get(key.query_crate())
375 // HACK(eddyb) it's possible crates may be loaded after
376 // the query engine is created, and because crate loading
377 // is not yet integrated with the query engine, such crates
378 // would be missing appropriate entries in `providers`.
379 .unwrap_or(&tcx.queries.fallback_extern_providers)
380 .$name;
381 provider(tcx, key)
382 }
383
384 fn hash_result(
385 _hcx: &mut StableHashingContext<'_>,
386 _result: &Self::Value
387 ) -> Option<Fingerprint> {
388 hash_result!([$($modifiers)*][_hcx, _result])
389 }
390
391 fn handle_cycle_error(
392 tcx: TyCtxt<'tcx>,
393 error: CycleError<Query<'tcx>>
394 ) -> Self::Value {
395 handle_cycle_error!([$($modifiers)*][tcx, error])
396 }
397 })*
398
399 #[derive(Copy, Clone)]
400 pub struct TyCtxtEnsure<'tcx> {
401 pub tcx: TyCtxt<'tcx>,
402 }
403
404 impl TyCtxtEnsure<$tcx> {
405 $($(#[$attr])*
406 #[inline(always)]
407 pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
408 ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
409 })*
410 }
411
412 #[derive(Copy, Clone)]
413 pub struct TyCtxtAt<'tcx> {
414 pub tcx: TyCtxt<'tcx>,
415 pub span: Span,
416 }
417
418 impl Deref for TyCtxtAt<'tcx> {
419 type Target = TyCtxt<'tcx>;
420 #[inline(always)]
421 fn deref(&self) -> &Self::Target {
422 &self.tcx
423 }
424 }
425
426 impl TyCtxt<$tcx> {
427 /// Returns a transparent wrapper for `TyCtxt`, which ensures queries
428 /// are executed instead of just returning their results.
429 #[inline(always)]
430 pub fn ensure(self) -> TyCtxtEnsure<$tcx> {
431 TyCtxtEnsure {
432 tcx: self,
433 }
434 }
435
436 /// Returns a transparent wrapper for `TyCtxt` which uses
437 /// `span` as the location of queries performed through it.
438 #[inline(always)]
439 pub fn at(self, span: Span) -> TyCtxtAt<$tcx> {
440 TyCtxtAt {
441 tcx: self,
442 span
443 }
444 }
445
446 $($(#[$attr])*
447 #[inline(always)]
448 #[must_use]
449 pub fn $name(self, key: query_helper_param_ty!($($K)*))
450 -> <queries::$name<$tcx> as QueryConfig<TyCtxt<$tcx>>>::Stored
451 {
452 self.at(DUMMY_SP).$name(key.into_query_param())
453 })*
454
455 /// All self-profiling events generated by the query engine use
456 /// virtual `StringId`s for their `event_id`. This method makes all
457 /// those virtual `StringId`s point to actual strings.
458 ///
459 /// If we are recording only summary data, the ids will point to
460 /// just the query names. If we are recording query keys too, we
461 /// allocate the corresponding strings here.
462 pub fn alloc_self_profile_query_strings(self) {
463 use crate::ty::query::profiling_support::{
464 alloc_self_profile_query_strings_for_query_cache,
465 QueryKeyStringCache,
466 };
467
468 if !self.prof.enabled() {
469 return;
470 }
471
472 let mut string_cache = QueryKeyStringCache::new();
473
474 $({
475 alloc_self_profile_query_strings_for_query_cache(
476 self,
477 stringify!($name),
478 &self.queries.$name,
479 &mut string_cache,
480 );
481 })*
482 }
483 }
484
485 impl TyCtxtAt<$tcx> {
486 $($(#[$attr])*
487 #[inline(always)]
488 pub fn $name(self, key: query_helper_param_ty!($($K)*))
489 -> <queries::$name<$tcx> as QueryConfig<TyCtxt<$tcx>>>::Stored
490 {
491 get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
492 })*
493 }
494
495 define_provider_struct! {
496 tcx: $tcx,
497 input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
498 }
499
500 impl Copy for Providers {}
501 impl Clone for Providers {
502 fn clone(&self) -> Self { *self }
503 }
504 }
505 }
506
507 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
508 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
509 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
510 macro_rules! define_queries_struct {
511 (tcx: $tcx:tt,
512 input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
513 pub struct Queries<$tcx> {
514 /// This provides access to the incrimental comilation on-disk cache for query results.
515 /// Do not access this directly. It is only meant to be used by
516 /// `DepGraph::try_mark_green()` and the query infrastructure.
517 pub(crate) on_disk_cache: OnDiskCache<'tcx>,
518
519 providers: IndexVec<CrateNum, Providers>,
520 fallback_extern_providers: Box<Providers>,
521
522 $($(#[$attr])* $name: QueryState<
523 TyCtxt<$tcx>,
524 <queries::$name<$tcx> as QueryAccessors<TyCtxt<'tcx>>>::Cache,
525 >,)*
526 }
527
528 impl<$tcx> Queries<$tcx> {
529 pub(crate) fn new(
530 providers: IndexVec<CrateNum, Providers>,
531 fallback_extern_providers: Providers,
532 on_disk_cache: OnDiskCache<'tcx>,
533 ) -> Self {
534 Queries {
535 providers,
536 fallback_extern_providers: Box::new(fallback_extern_providers),
537 on_disk_cache,
538 $($name: Default::default()),*
539 }
540 }
541
542 pub(crate) fn try_collect_active_jobs(
543 &self
544 ) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<TyCtxt<'tcx>>>> {
545 let mut jobs = FxHashMap::default();
546
547 $(
548 self.$name.try_collect_active_jobs(
549 <queries::$name<'tcx> as QueryAccessors<TyCtxt<'tcx>>>::DEP_KIND,
550 Query::$name,
551 &mut jobs,
552 )?;
553 )*
554
555 Some(jobs)
556 }
557 }
558 };
559 }
560
561 macro_rules! define_provider_struct {
562 (tcx: $tcx:tt,
563 input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
564 pub struct Providers {
565 $(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
566 }
567
568 impl Default for Providers {
569 fn default() -> Self {
570 $(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
571 bug!("`tcx.{}({:?})` unsupported by its crate",
572 stringify!($name), key);
573 })*
574 Providers { $($name),* }
575 }
576 }
577 };
578 }