1 //! Defines the set of legal keys that can be used in queries.
3 use crate::infer::canonical::Canonical;
4 use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
6 use crate::ty::{self, Ty, TyCtxt};
7 use crate::ty::subst::SubstsRef;
8 use crate::ty::fast_reject::SimplifiedType;
13 use syntax_pos::{Span, DUMMY_SP};
14 use syntax_pos::symbol::InternedString;
16 /// The `Key` trait controls what types can legally be used as the key
18 pub(super) trait Key: Clone + Hash + Eq + Debug {
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 fn query_crate(&self) -> CrateNum {
33 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
34 tcx.def_span(self.def_id())
38 impl<'tcx> Key for ty::Instance<'tcx> {
39 fn query_crate(&self) -> CrateNum {
43 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
44 tcx.def_span(self.def_id())
48 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
49 fn query_crate(&self) -> CrateNum {
50 self.instance.query_crate()
53 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
54 self.instance.default_span(tcx)
58 impl Key for CrateNum {
59 fn query_crate(&self) -> CrateNum {
62 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
67 impl Key for DefIndex {
68 fn query_crate(&self) -> CrateNum {
71 fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {
77 fn query_crate(&self) -> CrateNum {
80 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
85 impl Key for (DefId, DefId) {
86 fn query_crate(&self) -> CrateNum {
89 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
90 self.1.default_span(tcx)
94 impl Key for (CrateNum, DefId) {
95 fn query_crate(&self) -> CrateNum {
98 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
99 self.1.default_span(tcx)
103 impl Key for (DefId, SimplifiedType) {
104 fn query_crate(&self) -> CrateNum {
107 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
108 self.0.default_span(tcx)
112 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
113 fn query_crate(&self) -> CrateNum {
116 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
117 self.0.default_span(tcx)
121 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
122 fn query_crate(&self) -> CrateNum {
123 self.1.def_id().krate
125 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
126 tcx.def_span(self.1.def_id())
130 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
131 fn query_crate(&self) -> CrateNum {
134 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
139 impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
140 fn query_crate(&self) -> CrateNum {
143 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
144 tcx.def_span(self.def_id())
148 impl<'tcx> Key for ty::Const<'tcx> {
149 fn query_crate(&self) -> CrateNum {
152 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
157 impl<'tcx> Key for Ty<'tcx> {
158 fn query_crate(&self) -> CrateNum {
161 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
166 impl<'tcx> Key for ty::ParamEnv<'tcx> {
167 fn query_crate(&self) -> CrateNum {
170 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
175 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
176 fn query_crate(&self) -> CrateNum {
177 self.value.query_crate()
179 fn default_span(&self, tcx: TyCtxt<'_, '_>) -> Span {
180 self.value.default_span(tcx)
184 impl<'tcx> Key for traits::Environment<'tcx> {
185 fn query_crate(&self) -> CrateNum {
188 fn default_span(&self, _: TyCtxt<'_, '_>) -> Span {
193 impl Key for InternedString {
194 fn query_crate(&self) -> CrateNum {
197 fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {
202 /// Canonical query goals correspond to abstract trait operations that
203 /// are not tied to any crate in particular.
204 impl<'tcx, T> Key for Canonical<'tcx, T>
206 T: Debug + Hash + Clone + Eq,
208 fn query_crate(&self) -> CrateNum {
212 fn default_span(&self, _tcx: TyCtxt<'_, '_>) -> Span {