]>
Commit | Line | Data |
---|---|---|
8faf50e0 XL |
1 | // Copyright 2016 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 | use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResult, QueryResult}; | |
12 | use traits::query::Fallible; | |
13 | use ty::{ParamEnvAnd, Ty, TyCtxt}; | |
14 | ||
15 | #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] | |
16 | pub struct Subtype<'tcx> { | |
17 | pub sub: Ty<'tcx>, | |
18 | pub sup: Ty<'tcx>, | |
19 | } | |
20 | ||
21 | impl<'tcx> Subtype<'tcx> { | |
22 | pub fn new(sub: Ty<'tcx>, sup: Ty<'tcx>) -> Self { | |
23 | Self { | |
24 | sub, | |
25 | sup, | |
26 | } | |
27 | } | |
28 | } | |
29 | ||
30 | impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Subtype<'tcx> { | |
31 | type QueryResult = (); | |
32 | ||
33 | fn try_fast_path(_tcx: TyCtxt<'_, 'gcx, 'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<()> { | |
34 | if key.value.sub == key.value.sup { | |
35 | Some(()) | |
36 | } else { | |
37 | None | |
38 | } | |
39 | } | |
40 | ||
41 | fn perform_query( | |
42 | tcx: TyCtxt<'_, 'gcx, 'tcx>, | |
43 | canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>, | |
44 | ) -> Fallible<CanonicalizedQueryResult<'gcx, ()>> { | |
45 | tcx.type_op_subtype(canonicalized) | |
46 | } | |
47 | ||
48 | fn shrink_to_tcx_lifetime( | |
49 | v: &'a CanonicalizedQueryResult<'gcx, ()>, | |
50 | ) -> &'a Canonical<'tcx, QueryResult<'tcx, ()>> { | |
51 | v | |
52 | } | |
53 | } | |
54 | ||
55 | BraceStructTypeFoldableImpl! { | |
56 | impl<'tcx> TypeFoldable<'tcx> for Subtype<'tcx> { | |
57 | sub, | |
58 | sup, | |
59 | } | |
60 | } | |
61 | ||
62 | BraceStructLiftImpl! { | |
63 | impl<'a, 'tcx> Lift<'tcx> for Subtype<'a> { | |
64 | type Lifted = Subtype<'tcx>; | |
65 | sub, | |
66 | sup, | |
67 | } | |
68 | } | |
69 | ||
70 | impl_stable_hash_for! { | |
71 | struct Subtype<'tcx> { sub, sup } | |
72 | } |