]> git.proxmox.com Git - rustc.git/blob - src/librustc/ty/query/keys.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / librustc / ty / query / keys.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Defines the set of legal keys that can be used in queries.
12
13 use infer::canonical::Canonical;
14 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
15 use traits;
16 use ty::{self, Ty, TyCtxt};
17 use ty::subst::Substs;
18 use ty::fast_reject::SimplifiedType;
19 use mir;
20
21 use std::fmt::Debug;
22 use std::hash::Hash;
23 use syntax_pos::{Span, DUMMY_SP};
24 use syntax_pos::symbol::InternedString;
25
26 /// The `Key` trait controls what types can legally be used as the key
27 /// for a query.
28 pub(super) trait Key: Clone + Hash + Eq + Debug {
29 /// Given an instance of this key, what crate is it referring to?
30 /// This is used to find the provider.
31 fn query_crate(&self) -> CrateNum;
32
33 /// In the event that a cycle occurs, if no explicit span has been
34 /// given for a query with key `self`, what span should we use?
35 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span;
36 }
37
38 impl<'tcx> Key for ty::InstanceDef<'tcx> {
39 fn query_crate(&self) -> CrateNum {
40 LOCAL_CRATE
41 }
42
43 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
44 tcx.def_span(self.def_id())
45 }
46 }
47
48 impl<'tcx> Key for ty::Instance<'tcx> {
49 fn query_crate(&self) -> CrateNum {
50 LOCAL_CRATE
51 }
52
53 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
54 tcx.def_span(self.def_id())
55 }
56 }
57
58 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
59 fn query_crate(&self) -> CrateNum {
60 self.instance.query_crate()
61 }
62
63 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
64 self.instance.default_span(tcx)
65 }
66 }
67
68 impl Key for CrateNum {
69 fn query_crate(&self) -> CrateNum {
70 *self
71 }
72 fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
73 DUMMY_SP
74 }
75 }
76
77 impl Key for DefIndex {
78 fn query_crate(&self) -> CrateNum {
79 LOCAL_CRATE
80 }
81 fn default_span(&self, _tcx: TyCtxt<'_, '_, '_>) -> Span {
82 DUMMY_SP
83 }
84 }
85
86 impl Key for DefId {
87 fn query_crate(&self) -> CrateNum {
88 self.krate
89 }
90 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
91 tcx.def_span(*self)
92 }
93 }
94
95 impl Key for (DefId, DefId) {
96 fn query_crate(&self) -> CrateNum {
97 self.0.krate
98 }
99 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
100 self.1.default_span(tcx)
101 }
102 }
103
104 impl Key for (CrateNum, DefId) {
105 fn query_crate(&self) -> CrateNum {
106 self.0
107 }
108 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
109 self.1.default_span(tcx)
110 }
111 }
112
113 impl Key for (DefId, SimplifiedType) {
114 fn query_crate(&self) -> CrateNum {
115 self.0.krate
116 }
117 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
118 self.0.default_span(tcx)
119 }
120 }
121
122 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
123 fn query_crate(&self) -> CrateNum {
124 self.0.krate
125 }
126 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
127 self.0.default_span(tcx)
128 }
129 }
130
131 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
132 fn query_crate(&self) -> CrateNum {
133 self.1.def_id().krate
134 }
135 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
136 tcx.def_span(self.1.def_id())
137 }
138 }
139
140 impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
141 fn query_crate(&self) -> CrateNum {
142 self.def_id().krate
143 }
144 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
145 tcx.def_span(self.def_id())
146 }
147 }
148
149 impl<'tcx> Key for &'tcx ty::Const<'tcx> {
150 fn query_crate(&self) -> CrateNum {
151 LOCAL_CRATE
152 }
153 fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
154 DUMMY_SP
155 }
156 }
157
158 impl<'tcx> Key for Ty<'tcx> {
159 fn query_crate(&self) -> CrateNum {
160 LOCAL_CRATE
161 }
162 fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
163 DUMMY_SP
164 }
165 }
166
167 impl<'tcx> Key for ty::ParamEnv<'tcx> {
168 fn query_crate(&self) -> CrateNum {
169 LOCAL_CRATE
170 }
171 fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
172 DUMMY_SP
173 }
174 }
175
176 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
177 fn query_crate(&self) -> CrateNum {
178 self.value.query_crate()
179 }
180 fn default_span(&self, tcx: TyCtxt<'_, '_, '_>) -> Span {
181 self.value.default_span(tcx)
182 }
183 }
184
185 impl<'tcx> Key for traits::Environment<'tcx> {
186 fn query_crate(&self) -> CrateNum {
187 LOCAL_CRATE
188 }
189 fn default_span(&self, _: TyCtxt<'_, '_, '_>) -> Span {
190 DUMMY_SP
191 }
192 }
193
194 impl Key for InternedString {
195 fn query_crate(&self) -> CrateNum {
196 LOCAL_CRATE
197 }
198 fn default_span(&self, _tcx: TyCtxt<'_, '_, '_>) -> Span {
199 DUMMY_SP
200 }
201 }
202
203 /// Canonical query goals correspond to abstract trait operations that
204 /// are not tied to any crate in particular.
205 impl<'tcx, T> Key for Canonical<'tcx, T>
206 where
207 T: Debug + Hash + Clone + Eq,
208 {
209 fn query_crate(&self) -> CrateNum {
210 LOCAL_CRATE
211 }
212
213 fn default_span(&self, _tcx: TyCtxt<'_, '_, '_>) -> Span {
214 DUMMY_SP
215 }
216 }