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 mir
::transform
::{MirSuite, MirPassIndex}
;
15 use ty
::{self, Ty, TyCtxt}
;
16 use ty
::subst
::Substs
;
17 use ty
::fast_reject
::SimplifiedType
;
21 use syntax_pos
::{Span, DUMMY_SP}
;
22 use syntax_pos
::symbol
::InternedString
;
24 /// The `Key` trait controls what types can legally be used as the key
26 pub trait Key
: Clone
+ Hash
+ Eq
+ Debug
{
27 /// Given an instance of this key, what crate is it referring to?
28 /// This is used to find the provider.
29 fn map_crate(&self) -> CrateNum
;
31 /// In the event that a cycle occurs, if no explicit span has been
32 /// given for a query with key `self`, what span should we use?
33 fn default_span(&self, tcx
: TyCtxt
) -> Span
;
36 impl<'tcx
> Key
for ty
::InstanceDef
<'tcx
> {
37 fn map_crate(&self) -> CrateNum
{
41 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
42 tcx
.def_span(self.def_id())
46 impl<'tcx
> Key
for ty
::Instance
<'tcx
> {
47 fn map_crate(&self) -> CrateNum
{
51 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
52 tcx
.def_span(self.def_id())
56 impl Key
for CrateNum
{
57 fn map_crate(&self) -> CrateNum
{
60 fn default_span(&self, _
: TyCtxt
) -> Span
{
65 impl Key
for DefIndex
{
66 fn map_crate(&self) -> CrateNum
{
69 fn default_span(&self, _tcx
: TyCtxt
) -> Span
{
75 fn map_crate(&self) -> CrateNum
{
78 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
83 impl Key
for (DefId
, DefId
) {
84 fn map_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 map_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 map_crate(&self) -> CrateNum
{
105 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
106 self.0.default_span(tcx
)
110 impl<'tcx
> Key
for (DefId
, &'tcx Substs
<'tcx
>) {
111 fn map_crate(&self) -> CrateNum
{
114 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
115 self.0.default_span(tcx
)
119 impl Key
for (MirSuite
, DefId
) {
120 fn map_crate(&self) -> CrateNum
{
123 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
124 self.1.default_span(tcx
)
128 impl Key
for (MirSuite
, MirPassIndex
, DefId
) {
129 fn map_crate(&self) -> CrateNum
{
132 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
133 self.2.default_span(tcx
)
137 impl<'tcx
> Key
for Ty
<'tcx
> {
138 fn map_crate(&self) -> CrateNum
{
141 fn default_span(&self, _
: TyCtxt
) -> Span
{
146 impl<'tcx
, T
: Key
> Key
for ty
::ParamEnvAnd
<'tcx
, T
> {
147 fn map_crate(&self) -> CrateNum
{
148 self.value
.map_crate()
150 fn default_span(&self, tcx
: TyCtxt
) -> Span
{
151 self.value
.default_span(tcx
)
155 impl Key
for InternedString
{
156 fn map_crate(&self) -> CrateNum
{
159 fn default_span(&self, _tcx
: TyCtxt
) -> Span
{