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.
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.
11 //! Defines the set of legal keys that can be used in queries.
13 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
14 use traits::query::{CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal};
15 use ty::{self, Ty, TyCtxt};
16 use ty::subst::Substs;
17 use ty::fast_reject::SimplifiedType;
22 use syntax_pos::{Span, DUMMY_SP};
23 use syntax_pos::symbol::InternedString;
25 /// The `Key` trait controls what types can legally be used as the key
27 pub trait Key: Clone + Hash + Eq + Debug {
28 /// Given an instance of this key, what crate is it referring to?
29 /// This is used to find the provider.
30 fn map_crate(&self) -> CrateNum;
32 /// In the event that a cycle occurs, if no explicit span has been
33 /// given for a query with key `self`, what span should we use?
34 fn default_span(&self, tcx: TyCtxt) -> Span;
37 impl<'tcx> Key for ty::InstanceDef<'tcx> {
38 fn map_crate(&self) -> CrateNum {
42 fn default_span(&self, tcx: TyCtxt) -> Span {
43 tcx.def_span(self.def_id())
47 impl<'tcx> Key for ty::Instance<'tcx> {
48 fn map_crate(&self) -> CrateNum {
52 fn default_span(&self, tcx: TyCtxt) -> Span {
53 tcx.def_span(self.def_id())
57 impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
58 fn map_crate(&self) -> CrateNum {
59 self.instance.map_crate()
62 fn default_span(&self, tcx: TyCtxt) -> Span {
63 self.instance.default_span(tcx)
67 impl Key for CrateNum {
68 fn map_crate(&self) -> CrateNum {
71 fn default_span(&self, _: TyCtxt) -> Span {
76 impl Key for DefIndex {
77 fn map_crate(&self) -> CrateNum {
80 fn default_span(&self, _tcx: TyCtxt) -> Span {
86 fn map_crate(&self) -> CrateNum {
89 fn default_span(&self, tcx: TyCtxt) -> Span {
94 impl Key for (DefId, DefId) {
95 fn map_crate(&self) -> CrateNum {
98 fn default_span(&self, tcx: TyCtxt) -> Span {
99 self.1.default_span(tcx)
103 impl Key for (CrateNum, DefId) {
104 fn map_crate(&self) -> CrateNum {
107 fn default_span(&self, tcx: TyCtxt) -> Span {
108 self.1.default_span(tcx)
112 impl Key for (DefId, SimplifiedType) {
113 fn map_crate(&self) -> CrateNum {
116 fn default_span(&self, tcx: TyCtxt) -> Span {
117 self.0.default_span(tcx)
121 impl<'tcx> Key for (DefId, &'tcx Substs<'tcx>) {
122 fn map_crate(&self) -> CrateNum {
125 fn default_span(&self, tcx: TyCtxt) -> Span {
126 self.0.default_span(tcx)
130 impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
131 fn map_crate(&self) -> CrateNum {
132 self.1.def_id().krate
134 fn default_span(&self, tcx: TyCtxt) -> Span {
135 tcx.def_span(self.1.def_id())
139 impl<'tcx> Key for ty::PolyTraitRef<'tcx>{
140 fn map_crate(&self) -> CrateNum {
143 fn default_span(&self, tcx: TyCtxt) -> Span {
144 tcx.def_span(self.def_id())
148 impl<'tcx> Key for Ty<'tcx> {
149 fn map_crate(&self) -> CrateNum {
152 fn default_span(&self, _: TyCtxt) -> Span {
157 impl<'tcx> Key for ty::ParamEnv<'tcx> {
158 fn map_crate(&self) -> CrateNum {
161 fn default_span(&self, _: TyCtxt) -> Span {
166 impl<'tcx, T: Key> Key for ty::ParamEnvAnd<'tcx, T> {
167 fn map_crate(&self) -> CrateNum {
168 self.value.map_crate()
170 fn default_span(&self, tcx: TyCtxt) -> Span {
171 self.value.default_span(tcx)
175 impl Key for InternedString {
176 fn map_crate(&self) -> CrateNum {
179 fn default_span(&self, _tcx: TyCtxt) -> Span {
184 impl<'tcx> Key for CanonicalProjectionGoal<'tcx> {
185 fn map_crate(&self) -> CrateNum {
189 fn default_span(&self, _tcx: TyCtxt) -> Span {
194 impl<'tcx> Key for CanonicalTyGoal<'tcx> {
195 fn map_crate(&self) -> CrateNum {
199 fn default_span(&self, _tcx: TyCtxt) -> Span {
204 impl<'tcx> Key for CanonicalPredicateGoal<'tcx> {
205 fn map_crate(&self) -> CrateNum {
209 fn default_span(&self, _tcx: TyCtxt) -> Span {