]> git.lizzy.rs Git - rust.git/blob - src/librustc/traits/query/type_op/ascribe_user_type.rs
port the relate-types code from NLL type-check into a type-op
[rust.git] / src / librustc / traits / query / type_op / ascribe_user_type.rs
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, CanonicalizedQueryResponse, QueryResponse};
12 use traits::query::Fallible;
13 use hir::def_id::DefId;
14 use ty::{self, ParamEnvAnd, Ty, TyCtxt};
15 use ty::subst::UserSubsts;
16
17 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
18 pub struct AscribeUserType<'tcx> {
19     pub mir_ty: Ty<'tcx>,
20     pub variance: ty::Variance,
21     pub def_id: DefId,
22     pub user_substs: UserSubsts<'tcx>,
23 }
24
25 impl<'tcx> AscribeUserType<'tcx> {
26     pub fn new(
27         mir_ty: Ty<'tcx>,
28         variance: ty::Variance,
29         def_id: DefId,
30         user_substs: UserSubsts<'tcx>,
31     ) -> Self {
32         AscribeUserType { mir_ty, variance, def_id, user_substs }
33     }
34 }
35
36 impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for AscribeUserType<'tcx> {
37     type QueryResponse = ();
38
39     fn try_fast_path(
40         _tcx: TyCtxt<'_, 'gcx, 'tcx>,
41         _key: &ParamEnvAnd<'tcx, Self>,
42     ) -> Option<Self::QueryResponse> {
43         None
44     }
45
46     fn perform_query(
47         tcx: TyCtxt<'_, 'gcx, 'tcx>,
48         canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
49     ) -> Fallible<CanonicalizedQueryResponse<'gcx, ()>> {
50         tcx.type_op_ascribe_user_type(canonicalized)
51     }
52
53     fn shrink_to_tcx_lifetime(
54         v: &'a CanonicalizedQueryResponse<'gcx, ()>,
55     ) -> &'a Canonical<'tcx, QueryResponse<'tcx, ()>> {
56         v
57     }
58 }
59
60 BraceStructTypeFoldableImpl! {
61     impl<'tcx> TypeFoldable<'tcx> for AscribeUserType<'tcx> {
62         mir_ty, variance, def_id, user_substs
63     }
64 }
65
66 BraceStructLiftImpl! {
67     impl<'a, 'tcx> Lift<'tcx> for AscribeUserType<'a> {
68         type Lifted = AscribeUserType<'tcx>;
69         mir_ty, variance, def_id, user_substs
70     }
71 }
72
73 impl_stable_hash_for! {
74     struct AscribeUserType<'tcx> {
75         mir_ty, variance, def_id, user_substs
76     }
77 }