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::SubstsRef;
8 use crate::ty::{self, Ty, TyCtxt};
9 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
10 use rustc_span::symbol::Symbol;
11 use rustc_span::{Span, DUMMY_SP};
13 /// The `Key` trait controls what types can legally be used as the key
15 pub(super) trait Key {
16 /// Given an instance of this key, what crate is it referring to?
17 /// This is used to find the provider.
18 fn query_crate(&self) -> CrateNum;
20 /// In the event that a cycle occurs, if no explicit span has been
21 /// given for a query with key `self`, what span should we use?
22 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
25 impl<'tcx> Key for ty::InstanceDef<'tcx> {
26 fn query_crate(&self) -> CrateNum {
30 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
31 tcx.def_span(self.def_id())
35 impl<'tcx> Key for ty::Instance<'tcx> {
36 fn query_crate(&self) -> CrateNum {
40 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
41 tcx.def_span(self.def_id())
45 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
46 fn query_crate(&self) -> CrateNum {
47 self.instance.query_crate()
50 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
51 self.instance.default_span(tcx)
55 impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
56 fn query_crate(&self) -> CrateNum {
60 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
65 impl Key for CrateNum {
66 fn query_crate(&self) -> CrateNum {
69 fn default_span(&self, _: TyCtxt<'_>) -> Span {
74 impl Key for DefIndex {
75 fn query_crate(&self) -> CrateNum {
78 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
84 fn query_crate(&self) -> CrateNum {
87 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
92 impl Key for (DefId, DefId) {
93 fn query_crate(&self) -> CrateNum {
96 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
97 self.1.default_span(tcx)
101 impl Key for (CrateNum, DefId) {
102 fn query_crate(&self) -> CrateNum {
105 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
106 self.1.default_span(tcx)
110 impl Key for (DefId, SimplifiedType) {
111 fn query_crate(&self) -> CrateNum {
114 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
115 self.0.default_span(tcx)
119 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
120 fn query_crate(&self) -> CrateNum {
123 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124 self.0.default_span(tcx)
128 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
129 fn query_crate(&self) -> CrateNum {
130 self.1.def_id().krate
132 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
133 tcx.def_span(self.1.def_id())
137 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
138 fn query_crate(&self) -> CrateNum {
141 fn default_span(&self, _: TyCtxt<'_>) -> Span {
146 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
147 fn query_crate(&self) -> CrateNum {
150 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
151 tcx.def_span(self.def_id())
155 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
156 fn query_crate(&self) -> CrateNum {
159 fn default_span(&self, _: TyCtxt<'_>) -> Span {
164 impl<'tcx> Key for Ty<'tcx> {
165 fn query_crate(&self) -> CrateNum {
168 fn default_span(&self, _: TyCtxt<'_>) -> Span {
173 impl<'tcx> Key for ty::ParamEnv<'tcx> {
174 fn query_crate(&self) -> CrateNum {
177 fn default_span(&self, _: TyCtxt<'_>) -> Span {
182 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
183 fn query_crate(&self) -> CrateNum {
184 self.value.query_crate()
186 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
187 self.value.default_span(tcx)
191 impl<'tcx> Key for traits::Environment<'tcx> {
192 fn query_crate(&self) -> CrateNum {
195 fn default_span(&self, _: TyCtxt<'_>) -> Span {
200 impl Key for Symbol {
201 fn query_crate(&self) -> CrateNum {
204 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
209 /// Canonical query goals correspond to abstract trait operations that
210 /// are not tied to any crate in particular.
211 impl<'tcx, T> Key for Canonical<'tcx, T> {
212 fn query_crate(&self) -> CrateNum {
216 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
221 impl Key for (Symbol, u32, u32) {
222 fn query_crate(&self) -> CrateNum {
226 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {