1 //! Defines the set of legal keys that can be used in queries.
3 use crate::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
4 use crate::infer::canonical::Canonical;
7 use crate::ty::fast_reject::SimplifiedType;
8 use crate::ty::subst::SubstsRef;
9 use crate::ty::{self, Ty, TyCtxt};
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
16 pub(super) trait Key {
17 /// Given an instance of this key, what crate is it referring to?
18 /// This is used to find the provider.
19 fn query_crate(&self) -> CrateNum;
21 /// In the event that a cycle occurs, if no explicit span has been
22 /// given for a query with key `self`, what span should we use?
23 fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
26 impl<'tcx> Key for ty::InstanceDef<'tcx> {
27 fn query_crate(&self) -> CrateNum {
31 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
32 tcx.def_span(self.def_id())
36 impl<'tcx> Key for ty::Instance<'tcx> {
37 fn query_crate(&self) -> CrateNum {
41 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
42 tcx.def_span(self.def_id())
46 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
47 fn query_crate(&self) -> CrateNum {
48 self.instance.query_crate()
51 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
52 self.instance.default_span(tcx)
56 impl Key for CrateNum {
57 fn query_crate(&self) -> CrateNum {
60 fn default_span(&self, _: TyCtxt<'_>) -> Span {
65 impl Key for DefIndex {
66 fn query_crate(&self) -> CrateNum {
69 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
75 fn query_crate(&self) -> CrateNum {
78 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
83 impl Key for (DefId, DefId) {
84 fn query_crate(&self) -> CrateNum {
87 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
88 self.1.default_span(tcx)
92 impl Key for (CrateNum, 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 (DefId, SimplifiedType) {
102 fn query_crate(&self) -> CrateNum {
105 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
106 self.0.default_span(tcx)
110 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
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 (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
120 fn query_crate(&self) -> CrateNum {
121 self.1.def_id().krate
123 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
124 tcx.def_span(self.1.def_id())
128 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
129 fn query_crate(&self) -> CrateNum {
132 fn default_span(&self, _: TyCtxt<'_>) -> Span {
137 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
138 fn query_crate(&self) -> CrateNum {
141 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
142 tcx.def_span(self.def_id())
146 impl<'tcx> Key for ty::Const<'tcx> {
147 fn query_crate(&self) -> CrateNum {
150 fn default_span(&self, _: TyCtxt<'_>) -> Span {
155 impl<'tcx> Key for Ty<'tcx> {
156 fn query_crate(&self) -> CrateNum {
159 fn default_span(&self, _: TyCtxt<'_>) -> Span {
164 impl<'tcx> Key for ty::ParamEnv<'tcx> {
165 fn query_crate(&self) -> CrateNum {
168 fn default_span(&self, _: TyCtxt<'_>) -> Span {
173 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
174 fn query_crate(&self) -> CrateNum {
175 self.value.query_crate()
177 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
178 self.value.default_span(tcx)
182 impl<'tcx> Key for traits::Environment<'tcx> {
183 fn query_crate(&self) -> CrateNum {
186 fn default_span(&self, _: TyCtxt<'_>) -> Span {
191 impl Key for Symbol {
192 fn query_crate(&self) -> CrateNum {
195 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
200 /// Canonical query goals correspond to abstract trait operations that
201 /// are not tied to any crate in particular.
202 impl<'tcx, T> Key for Canonical<'tcx, T> {
203 fn query_crate(&self) -> CrateNum {
207 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
212 impl Key for (Symbol, u32, u32) {
213 fn query_crate(&self) -> CrateNum {
217 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {