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 ty
::{self, Ty, TyCtxt}
;
15 use ty
::subst
::Substs
;
16 use ty
::fast_reject
::SimplifiedType
;
20 use syntax_pos
::{Span, DUMMY_SP}
;
21 use syntax_pos
::symbol
::InternedString
;
23 /// The `Key` trait controls what types can legally be used as the key
25 pub trait Key
: Clone
+ Hash
+ Eq
+ Debug
{
26 /// Given an instance of this key, what crate is it referring to?
27 /// This is used to find the provider.
28 fn map_crate(&self) -> CrateNum
;
30 /// In the event that a cycle occurs, if no explicit span has been
31 /// given for a query with key `self`, what span should we use?
32 fn default_span(&self, tcx
: TyCtxt
) -> Span
;
35 impl<'tcx
> Key
for ty
::InstanceDef
<'tcx
> {
36 fn map_crate(&self) -> CrateNum
{
40 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
41 tcx
.def_span(self.def_id())
45 impl<'tcx
> Key
for ty
::Instance
<'tcx
> {
46 fn map_crate(&self) -> CrateNum
{
50 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
51 tcx
.def_span(self.def_id())
55 impl Key
for CrateNum
{
56 fn map_crate(&self) -> CrateNum
{
59 fn default_span(&self, _
: TyCtxt
) -> Span
{
64 impl Key
for DefIndex
{
65 fn map_crate(&self) -> CrateNum
{
68 fn default_span(&self, _tcx
: TyCtxt
) -> Span
{
74 fn map_crate(&self) -> CrateNum
{
77 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
82 impl Key
for (DefId
, DefId
) {
83 fn map_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 map_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 map_crate(&self) -> CrateNum
{
104 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
105 self.0.default_span(tcx
)
109 impl<'tcx
> Key
for (DefId
, &'tcx Substs
<'tcx
>) {
110 fn map_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 map_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 ty
::PolyTraitRef
<'tcx
>{
128 fn map_crate(&self) -> CrateNum
{
131 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
132 tcx
.def_span(self.def_id())
136 impl<'tcx
> Key
for Ty
<'tcx
> {
137 fn map_crate(&self) -> CrateNum
{
140 fn default_span(&self, _
: TyCtxt
) -> Span
{
145 impl<'tcx
, T
: Key
> Key
for ty
::ParamEnvAnd
<'tcx
, T
> {
146 fn map_crate(&self) -> CrateNum
{
147 self.value
.map_crate()
149 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
150 self.value
.default_span(tcx
)
154 impl Key
for InternedString
{
155 fn map_crate(&self) -> CrateNum
{
158 fn default_span(&self, _tcx
: TyCtxt
) -> Span
{