1 //! Defines the set of legal keys that can be used in queries.
3 use crate::infer::canonical::Canonical;
6 use crate::ty::fast_reject::SimplifiedType;
7 use crate::ty::subst::{GenericArg, SubstsRef};
8 use crate::ty::{self, Ty, TyCtxt};
9 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
10 use rustc_query_system::query::DefaultCacheSelector;
11 use rustc_span::symbol::Symbol;
12 use rustc_span::{Span, DUMMY_SP};
14 /// The `Key` trait controls what types can legally be used as the key
19 /// Given an instance of this key, what crate is it referring to?
20 /// This is used to find the provider.
21 fn query_crate(&self) -> CrateNum;
23 /// In the event that a cycle occurs, if no explicit span has been
24 /// given for a query with key `self`, what span should we use?
25 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
28 impl<'tcx> Key for ty::InstanceDef<'tcx> {
29 type CacheSelector = DefaultCacheSelector;
31 fn query_crate(&self) -> CrateNum {
35 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
36 tcx.def_span(self.def_id())
40 impl<'tcx> Key for ty::Instance<'tcx> {
41 type CacheSelector = DefaultCacheSelector;
43 fn query_crate(&self) -> CrateNum {
47 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
48 tcx.def_span(self.def_id())
52 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
53 type CacheSelector = DefaultCacheSelector;
55 fn query_crate(&self) -> CrateNum {
56 self.instance.query_crate()
59 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
60 self.instance.default_span(tcx)
64 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
65 type CacheSelector = DefaultCacheSelector;
67 fn query_crate(&self) -> CrateNum {
71 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
76 impl Key for CrateNum {
77 type CacheSelector = DefaultCacheSelector;
79 fn query_crate(&self) -> CrateNum {
82 fn default_span(&self, _: TyCtxt<'_>) -> Span {
87 impl Key for LocalDefId {
88 type CacheSelector = DefaultCacheSelector;
90 fn query_crate(&self) -> CrateNum {
91 self.to_def_id().query_crate()
93 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
94 self.to_def_id().default_span(tcx)
99 type CacheSelector = DefaultCacheSelector;
101 fn query_crate(&self) -> CrateNum {
104 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
109 impl Key for (DefId, DefId) {
110 type CacheSelector = DefaultCacheSelector;
112 fn query_crate(&self) -> CrateNum {
115 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
116 self.1.default_span(tcx)
120 impl Key for (CrateNum, DefId) {
121 type CacheSelector = DefaultCacheSelector;
123 fn query_crate(&self) -> CrateNum {
126 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
127 self.1.default_span(tcx)
131 impl Key for (DefId, SimplifiedType) {
132 type CacheSelector = DefaultCacheSelector;
134 fn query_crate(&self) -> CrateNum {
137 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
138 self.0.default_span(tcx)
142 impl<'tcx> Key for SubstsRef<'tcx> {
143 type CacheSelector = DefaultCacheSelector;
145 fn query_crate(&self) -> CrateNum {
148 fn default_span(&self, _: TyCtxt<'_>) -> Span {
153 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
154 type CacheSelector = DefaultCacheSelector;
156 fn query_crate(&self) -> CrateNum {
159 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
160 self.0.default_span(tcx)
164 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
165 type CacheSelector = DefaultCacheSelector;
167 fn query_crate(&self) -> CrateNum {
168 self.1.def_id().krate
170 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
171 tcx.def_span(self.1.def_id())
175 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
176 type CacheSelector = DefaultCacheSelector;
178 fn query_crate(&self) -> CrateNum {
181 fn default_span(&self, _: TyCtxt<'_>) -> Span {
186 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
187 type CacheSelector = DefaultCacheSelector;
189 fn query_crate(&self) -> CrateNum {
192 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
193 tcx.def_span(self.def_id())
197 impl<'tcx> Key for GenericArg<'tcx> {
198 type CacheSelector = DefaultCacheSelector;
200 fn query_crate(&self) -> CrateNum {
203 fn default_span(&self, _: TyCtxt<'_>) -> Span {
208 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
209 type CacheSelector = DefaultCacheSelector;
211 fn query_crate(&self) -> CrateNum {
214 fn default_span(&self, _: TyCtxt<'_>) -> Span {
219 impl<'tcx> Key for Ty<'tcx> {
220 type CacheSelector = DefaultCacheSelector;
222 fn query_crate(&self) -> CrateNum {
225 fn default_span(&self, _: TyCtxt<'_>) -> Span {
230 impl<'tcx> Key for ty::ParamEnv<'tcx> {
231 type CacheSelector = DefaultCacheSelector;
233 fn query_crate(&self) -> CrateNum {
236 fn default_span(&self, _: TyCtxt<'_>) -> Span {
241 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
242 type CacheSelector = DefaultCacheSelector;
244 fn query_crate(&self) -> CrateNum {
245 self.value.query_crate()
247 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
248 self.value.default_span(tcx)
252 impl<'tcx> Key for traits::Environment<'tcx> {
253 type CacheSelector = DefaultCacheSelector;
255 fn query_crate(&self) -> CrateNum {
258 fn default_span(&self, _: TyCtxt<'_>) -> Span {
263 impl Key for Symbol {
264 type CacheSelector = DefaultCacheSelector;
266 fn query_crate(&self) -> CrateNum {
269 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
274 /// Canonical query goals correspond to abstract trait operations that
275 /// are not tied to any crate in particular.
276 impl<'tcx, T> Key for Canonical<'tcx, T> {
277 type CacheSelector = DefaultCacheSelector;
279 fn query_crate(&self) -> CrateNum {
283 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
288 impl Key for (Symbol, u32, u32) {
289 type CacheSelector = DefaultCacheSelector;
291 fn query_crate(&self) -> CrateNum {
295 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {