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 Key for CrateNum {
56 fn query_crate(&self) -> CrateNum {
59 fn default_span(&self, _: TyCtxt<'_>) -> Span {
64 impl Key for DefIndex {
65 fn query_crate(&self) -> CrateNum {
68 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
74 fn query_crate(&self) -> CrateNum {
77 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
82 impl Key for (DefId, DefId) {
83 fn query_crate(&self) -> CrateNum {
86 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
87 self.1.default_span(tcx)
91 impl Key for (CrateNum, DefId) {
92 fn query_crate(&self) -> CrateNum {
95 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
96 self.1.default_span(tcx)
100 impl Key for (DefId, SimplifiedType) {
101 fn query_crate(&self) -> CrateNum {
104 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
105 self.0.default_span(tcx)
109 impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
110 fn query_crate(&self) -> CrateNum {
113 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
114 self.0.default_span(tcx)
118 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
119 fn query_crate(&self) -> CrateNum {
120 self.1.def_id().krate
122 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
123 tcx.def_span(self.1.def_id())
127 impl<'tcx> Key for (&'tcx ty::Const<'tcx>, mir::Field) {
128 fn query_crate(&self) -> CrateNum {
131 fn default_span(&self, _: TyCtxt<'_>) -> Span {
136 impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
137 fn query_crate(&self) -> CrateNum {
140 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141 tcx.def_span(self.def_id())
145 impl<'tcx> Key for ty::Const<'tcx> {
146 fn query_crate(&self) -> CrateNum {
149 fn default_span(&self, _: TyCtxt<'_>) -> Span {
154 impl<'tcx> Key for Ty<'tcx> {
155 fn query_crate(&self) -> CrateNum {
158 fn default_span(&self, _: TyCtxt<'_>) -> Span {
163 impl<'tcx> Key for ty::ParamEnv<'tcx> {
164 fn query_crate(&self) -> CrateNum {
167 fn default_span(&self, _: TyCtxt<'_>) -> Span {
172 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
173 fn query_crate(&self) -> CrateNum {
174 self.value.query_crate()
176 fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
177 self.value.default_span(tcx)
181 impl<'tcx> Key for traits::Environment<'tcx> {
182 fn query_crate(&self) -> CrateNum {
185 fn default_span(&self, _: TyCtxt<'_>) -> Span {
190 impl Key for Symbol {
191 fn query_crate(&self) -> CrateNum {
194 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
199 /// Canonical query goals correspond to abstract trait operations that
200 /// are not tied to any crate in particular.
201 impl<'tcx, T> Key for Canonical<'tcx, T> {
202 fn query_crate(&self) -> CrateNum {
206 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
211 impl Key for (Symbol, u32, u32) {
212 fn query_crate(&self) -> CrateNum {
216 fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {