]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/query.rs
Rollup merge of #90277 - pierwill:fix-70258-inference-terms, r=jackh726
[rust.git] / compiler / rustc_middle / src / ty / query.rs
1 use crate::dep_graph;
2 use crate::infer::canonical::{self, Canonical};
3 use crate::lint::LintLevelMap;
4 use crate::metadata::ModChild;
5 use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
6 use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
7 use crate::middle::lib_features::LibFeatures;
8 use crate::middle::privacy::AccessLevels;
9 use crate::middle::region;
10 use crate::middle::resolve_lifetime::{
11     LifetimeScopeForPath, ObjectLifetimeDefault, Region, ResolveLifetimes,
12 };
13 use crate::middle::stability::{self, DeprecationEntry};
14 use crate::mir;
15 use crate::mir::interpret::GlobalId;
16 use crate::mir::interpret::{ConstAlloc, LitToConstError, LitToConstInput};
17 use crate::mir::interpret::{ConstValue, EvalToAllocationRawResult, EvalToConstValueResult};
18 use crate::mir::mono::CodegenUnit;
19 use crate::thir;
20 use crate::traits::query::{
21     CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
22     CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
23     CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal, NoSolution,
24 };
25 use crate::traits::query::{
26     DropckOutlivesResult, DtorckConstraint, MethodAutoderefStepsResult, NormalizationResult,
27     OutlivesBound,
28 };
29 use crate::traits::specialization_graph;
30 use crate::traits::{self, ImplSource};
31 use crate::ty::fast_reject::SimplifiedType;
32 use crate::ty::subst::{GenericArg, SubstsRef};
33 use crate::ty::util::AlwaysRequiresDrop;
34 use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
35 use rustc_ast::expand::allocator::AllocatorKind;
36 use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
37 use rustc_data_structures::steal::Steal;
38 use rustc_data_structures::svh::Svh;
39 use rustc_data_structures::sync::Lrc;
40 use rustc_errors::ErrorReported;
41 use rustc_hir as hir;
42 use rustc_hir::def::DefKind;
43 use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
44 use rustc_hir::lang_items::{LangItem, LanguageItems};
45 use rustc_hir::{Crate, ItemLocalId, TraitCandidate};
46 use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
47 use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
48 use rustc_session::cstore::{CrateDepKind, CrateSource};
49 use rustc_session::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLib};
50 use rustc_session::utils::NativeLibKind;
51 use rustc_session::Limits;
52 use rustc_target::abi;
53 use rustc_target::spec::PanicStrategy;
54
55 use rustc_ast as ast;
56 use rustc_attr as attr;
57 use rustc_span::symbol::Symbol;
58 use rustc_span::{Span, DUMMY_SP};
59 use std::collections::BTreeMap;
60 use std::ops::Deref;
61 use std::path::PathBuf;
62 use std::sync::Arc;
63
64 pub(crate) use rustc_query_system::query::QueryJobId;
65 use rustc_query_system::query::*;
66
67 #[derive(Copy, Clone)]
68 pub struct TyCtxtAt<'tcx> {
69     pub tcx: TyCtxt<'tcx>,
70     pub span: Span,
71 }
72
73 impl<'tcx> Deref for TyCtxtAt<'tcx> {
74     type Target = TyCtxt<'tcx>;
75     #[inline(always)]
76     fn deref(&self) -> &Self::Target {
77         &self.tcx
78     }
79 }
80
81 #[derive(Copy, Clone)]
82 pub struct TyCtxtEnsure<'tcx> {
83     pub tcx: TyCtxt<'tcx>,
84 }
85
86 impl<'tcx> TyCtxt<'tcx> {
87     /// Returns a transparent wrapper for `TyCtxt`, which ensures queries
88     /// are executed instead of just returning their results.
89     #[inline(always)]
90     pub fn ensure(self) -> TyCtxtEnsure<'tcx> {
91         TyCtxtEnsure { tcx: self }
92     }
93
94     /// Returns a transparent wrapper for `TyCtxt` which uses
95     /// `span` as the location of queries performed through it.
96     #[inline(always)]
97     pub fn at(self, span: Span) -> TyCtxtAt<'tcx> {
98         TyCtxtAt { tcx: self, span }
99     }
100
101     pub fn try_mark_green(self, dep_node: &dep_graph::DepNode) -> bool {
102         self.queries.try_mark_green(self, dep_node)
103     }
104 }
105
106 /// Helper for `TyCtxtEnsure` to avoid a closure.
107 #[inline(always)]
108 fn noop<T>(_: &T) {}
109
110 macro_rules! query_helper_param_ty {
111     (DefId) => { impl IntoQueryParam<DefId> };
112     ($K:ty) => { $K };
113 }
114
115 macro_rules! query_storage {
116     ([][$K:ty, $V:ty]) => {
117         <DefaultCacheSelector as CacheSelector<$K, $V>>::Cache
118     };
119     ([(storage $ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
120         <$ty as CacheSelector<$K, $V>>::Cache
121     };
122     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
123         query_storage!([$($modifiers)*][$($args)*])
124     };
125 }
126
127 macro_rules! separate_provide_extern_decl {
128     ([][$name:ident]) => {
129         ()
130     };
131     ([(separate_provide_extern) $($rest:tt)*][$name:ident]) => {
132         for<'tcx> fn(
133             TyCtxt<'tcx>,
134             query_keys::$name<'tcx>,
135         ) -> query_values::$name<'tcx>
136     };
137     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
138         separate_provide_extern_decl!([$($modifiers)*][$($args)*])
139     };
140 }
141
142 macro_rules! separate_provide_extern_default {
143     ([][$name:ident]) => {
144         ()
145     };
146     ([(separate_provide_extern) $($rest:tt)*][$name:ident]) => {
147         |_, key| bug!(
148             "`tcx.{}({:?})` unsupported by its crate; \
149              perhaps the `{}` query was never assigned a provider function",
150             stringify!($name),
151             key,
152             stringify!($name),
153         )
154     };
155     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
156         separate_provide_extern_default!([$($modifiers)*][$($args)*])
157     };
158 }
159
160 macro_rules! opt_remap_env_constness {
161     ([][$name:ident]) => {};
162     ([(remap_env_constness) $($rest:tt)*][$name:ident]) => {
163         let $name = $name.without_const();
164     };
165     ([$other:tt $($modifiers:tt)*][$name:ident]) => {
166         opt_remap_env_constness!([$($modifiers)*][$name])
167     };
168 }
169
170 macro_rules! define_callbacks {
171     (<$tcx:tt>
172      $($(#[$attr:meta])*
173         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
174
175         // HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
176         // below, but using type aliases instead of associated types, to bypass
177         // the limitations around normalizing under HRTB - for example, this:
178         // `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
179         // doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
180         // This is primarily used by the `provide!` macro in `rustc_metadata`.
181         #[allow(nonstandard_style, unused_lifetimes)]
182         pub mod query_keys {
183             use super::*;
184
185             $(pub type $name<$tcx> = $($K)*;)*
186         }
187         #[allow(nonstandard_style, unused_lifetimes)]
188         pub mod query_values {
189             use super::*;
190
191             $(pub type $name<$tcx> = $V;)*
192         }
193         #[allow(nonstandard_style, unused_lifetimes)]
194         pub mod query_storage {
195             use super::*;
196
197             $(pub type $name<$tcx> = query_storage!([$($modifiers)*][$($K)*, $V]);)*
198         }
199         #[allow(nonstandard_style, unused_lifetimes)]
200         pub mod query_stored {
201             use super::*;
202
203             $(pub type $name<$tcx> = <query_storage::$name<$tcx> as QueryStorage>::Stored;)*
204         }
205
206         #[derive(Default)]
207         pub struct QueryCaches<$tcx> {
208             $($(#[$attr])* pub $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
209         }
210
211         impl<$tcx> TyCtxtEnsure<$tcx> {
212             $($(#[$attr])*
213             #[inline(always)]
214             pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
215                 let key = key.into_query_param();
216                 opt_remap_env_constness!([$($modifiers)*][key]);
217
218                 let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, noop);
219
220                 let lookup = match cached {
221                     Ok(()) => return,
222                     Err(lookup) => lookup,
223                 };
224
225                 self.tcx.queries.$name(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
226             })*
227         }
228
229         impl<$tcx> TyCtxt<$tcx> {
230             $($(#[$attr])*
231             #[inline(always)]
232             #[must_use]
233             pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
234             {
235                 self.at(DUMMY_SP).$name(key)
236             })*
237         }
238
239         impl<$tcx> TyCtxtAt<$tcx> {
240             $($(#[$attr])*
241             #[inline(always)]
242             pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
243             {
244                 let key = key.into_query_param();
245                 opt_remap_env_constness!([$($modifiers)*][key]);
246
247                 let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, Clone::clone);
248
249                 let lookup = match cached {
250                     Ok(value) => return value,
251                     Err(lookup) => lookup,
252                 };
253
254                 self.tcx.queries.$name(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
255             })*
256         }
257
258         pub struct Providers {
259             $(pub $name: for<'tcx> fn(
260                 TyCtxt<'tcx>,
261                 query_keys::$name<'tcx>,
262             ) -> query_values::$name<'tcx>,)*
263         }
264
265         pub struct ExternProviders {
266             $(pub $name: separate_provide_extern_decl!([$($modifiers)*][$name]),)*
267         }
268
269         impl Default for Providers {
270             fn default() -> Self {
271                 Providers {
272                     $($name: |_, key| bug!(
273                         "`tcx.{}({:?})` unsupported by its crate; \
274                          perhaps the `{}` query was never assigned a provider function",
275                         stringify!($name),
276                         key,
277                         stringify!($name),
278                     ),)*
279                 }
280             }
281         }
282
283         impl Default for ExternProviders {
284             fn default() -> Self {
285                 ExternProviders {
286                     $($name: separate_provide_extern_default!([$($modifiers)*][$name]),)*
287                 }
288             }
289         }
290
291         impl Copy for Providers {}
292         impl Clone for Providers {
293             fn clone(&self) -> Self { *self }
294         }
295
296         impl Copy for ExternProviders {}
297         impl Clone for ExternProviders {
298             fn clone(&self) -> Self { *self }
299         }
300
301         pub trait QueryEngine<'tcx>: rustc_data_structures::sync::Sync {
302             fn as_any(&'tcx self) -> &'tcx dyn std::any::Any;
303
304             fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool;
305
306             $($(#[$attr])*
307             fn $name(
308                 &'tcx self,
309                 tcx: TyCtxt<$tcx>,
310                 span: Span,
311                 key: query_keys::$name<$tcx>,
312                 lookup: QueryLookup,
313                 mode: QueryMode,
314             ) -> Option<query_stored::$name<$tcx>>;)*
315         }
316     };
317 }
318
319 // Each of these queries corresponds to a function pointer field in the
320 // `Providers` struct for requesting a value of that type, and a method
321 // on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
322 // which memoizes and does dep-graph tracking, wrapping around the actual
323 // `Providers` that the driver creates (using several `rustc_*` crates).
324 //
325 // The result type of each query must implement `Clone`, and additionally
326 // `ty::query::values::Value`, which produces an appropriate placeholder
327 // (error) value if the query resulted in a query cycle.
328 // Queries marked with `fatal_cycle` do not need the latter implementation,
329 // as they will raise an fatal error on query cycles instead.
330
331 rustc_query_append! { [define_callbacks!][<'tcx>] }
332
333 mod sealed {
334     use super::{DefId, LocalDefId};
335
336     /// An analogue of the `Into` trait that's intended only for query paramaters.
337     ///
338     /// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
339     /// user call `to_def_id` to convert between them everywhere else.
340     pub trait IntoQueryParam<P> {
341         fn into_query_param(self) -> P;
342     }
343
344     impl<P> IntoQueryParam<P> for P {
345         #[inline(always)]
346         fn into_query_param(self) -> P {
347             self
348         }
349     }
350
351     impl IntoQueryParam<DefId> for LocalDefId {
352         #[inline(always)]
353         fn into_query_param(self) -> DefId {
354             self.to_def_id()
355         }
356     }
357 }
358
359 use sealed::IntoQueryParam;
360
361 impl<'tcx> TyCtxt<'tcx> {
362     pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> DefKind {
363         let def_id = def_id.into_query_param();
364         self.opt_def_kind(def_id)
365             .unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
366     }
367 }
368
369 impl<'tcx> TyCtxtAt<'tcx> {
370     pub fn def_kind(self, def_id: impl IntoQueryParam<DefId>) -> DefKind {
371         let def_id = def_id.into_query_param();
372         self.opt_def_kind(def_id)
373             .unwrap_or_else(|| bug!("def_kind: unsupported node: {:?}", def_id))
374     }
375 }