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 infer
::canonical
::Canonical
;
14 use hir
::def_id
::{CrateNum, DefId, LOCAL_CRATE, DefIndex}
;
16 use ty
::{self, Ty, TyCtxt}
;
17 use ty
::subst
::Substs
;
18 use ty
::fast_reject
::SimplifiedType
;
23 use syntax_pos
::{Span, DUMMY_SP}
;
24 use syntax_pos
::symbol
::InternedString
;
26 /// The `Key` trait controls what types can legally be used as the key
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
;
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
;
38 impl<'tcx
> Key
for ty
::InstanceDef
<'tcx
> {
39 fn query_crate(&self) -> CrateNum
{
43 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
44 tcx
.def_span(self.def_id())
48 impl<'tcx
> Key
for ty
::Instance
<'tcx
> {
49 fn query_crate(&self) -> CrateNum
{
53 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
54 tcx
.def_span(self.def_id())
58 impl<'tcx
> Key
for mir
::interpret
::GlobalId
<'tcx
> {
59 fn query_crate(&self) -> CrateNum
{
60 self.instance
.query_crate()
63 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
64 self.instance
.default_span(tcx
)
68 impl Key
for CrateNum
{
69 fn query_crate(&self) -> CrateNum
{
72 fn default_span(&self, _
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
77 impl Key
for DefIndex
{
78 fn query_crate(&self) -> CrateNum
{
81 fn default_span(&self, _tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
87 fn query_crate(&self) -> CrateNum
{
90 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
95 impl Key
for (DefId
, DefId
) {
96 fn query_crate(&self) -> CrateNum
{
99 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
100 self.1.default_span(tcx
)
104 impl Key
for (CrateNum
, DefId
) {
105 fn query_crate(&self) -> CrateNum
{
108 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
109 self.1.default_span(tcx
)
113 impl Key
for (DefId
, SimplifiedType
) {
114 fn query_crate(&self) -> CrateNum
{
117 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
118 self.0.default_span(tcx
)
122 impl<'tcx
> Key
for (DefId
, &'tcx Substs
<'tcx
>) {
123 fn query_crate(&self) -> CrateNum
{
126 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
127 self.0.default_span(tcx
)
131 impl<'tcx
> Key
for (ty
::ParamEnv
<'tcx
>, ty
::PolyTraitRef
<'tcx
>) {
132 fn query_crate(&self) -> CrateNum
{
133 self.1.def_id().krate
135 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
136 tcx
.def_span(self.1.def_id())
140 impl<'tcx
> Key
for ty
::PolyTraitRef
<'tcx
>{
141 fn query_crate(&self) -> CrateNum
{
144 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
145 tcx
.def_span(self.def_id())
149 impl<'tcx
> Key
for &'tcx ty
::Const
<'tcx
> {
150 fn query_crate(&self) -> CrateNum
{
153 fn default_span(&self, _
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
158 impl<'tcx
> Key
for Ty
<'tcx
> {
159 fn query_crate(&self) -> CrateNum
{
162 fn default_span(&self, _
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
167 impl<'tcx
> Key
for ty
::ParamEnv
<'tcx
> {
168 fn query_crate(&self) -> CrateNum
{
171 fn default_span(&self, _
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
176 impl<'tcx
, T
: Key
> Key
for ty
::ParamEnvAnd
<'tcx
, T
> {
177 fn query_crate(&self) -> CrateNum
{
178 self.value
.query_crate()
180 fn default_span(&self, tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
181 self.value
.default_span(tcx
)
185 impl<'tcx
> Key
for traits
::Environment
<'tcx
> {
186 fn query_crate(&self) -> CrateNum
{
189 fn default_span(&self, _
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
194 impl Key
for InternedString
{
195 fn query_crate(&self) -> CrateNum
{
198 fn default_span(&self, _tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{
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
>
207 T
: Debug
+ Hash
+ Clone
+ Eq
,
209 fn query_crate(&self) -> CrateNum
{
213 fn default_span(&self, _tcx
: TyCtxt
<'_
, '_
, '_
>) -> Span
{