]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/_match.rs
Rollup merge of #66325 - BartMassey:master, r=joshtriplett
[rust.git] / src / librustc / ty / _match.rs
1 use crate::ty::{self, Ty, TyCtxt, InferConst};
2 use crate::ty::error::TypeError;
3 use crate::ty::relate::{self, Relate, TypeRelation, RelateResult};
4
5 /// A type "A" *matches* "B" if the fresh types in B could be
6 /// substituted with values so as to make it equal to A. Matching is
7 /// intended to be used only on freshened types, and it basically
8 /// indicates if the non-freshened versions of A and B could have been
9 /// unified.
10 ///
11 /// It is only an approximation. If it yields false, unification would
12 /// definitely fail, but a true result doesn't mean unification would
13 /// succeed. This is because we don't track the "side-constraints" on
14 /// type variables, nor do we track if the same freshened type appears
15 /// more than once. To some extent these approximations could be
16 /// fixed, given effort.
17 ///
18 /// Like subtyping, matching is really a binary relation, so the only
19 /// important thing about the result is Ok/Err. Also, matching never
20 /// affects any type variables or unification state.
21 pub struct Match<'tcx> {
22     tcx: TyCtxt<'tcx>,
23     param_env: ty::ParamEnv<'tcx>,
24 }
25
26 impl Match<'tcx> {
27     pub fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Match<'tcx> {
28         Match { tcx, param_env }
29     }
30 }
31
32 impl TypeRelation<'tcx> for Match<'tcx> {
33     fn tag(&self) -> &'static str { "Match" }
34     fn tcx(&self) -> TyCtxt<'tcx> { self.tcx }
35     fn param_env(&self) -> ty::ParamEnv<'tcx> { self.param_env }
36     fn a_is_expected(&self) -> bool { true } // irrelevant
37
38     fn relate_with_variance<T: Relate<'tcx>>(&mut self,
39                                              _: ty::Variance,
40                                              a: &T,
41                                              b: &T)
42                                              -> RelateResult<'tcx, T>
43     {
44         self.relate(a, b)
45     }
46
47     fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
48                -> RelateResult<'tcx, ty::Region<'tcx>> {
49         debug!("{}.regions({:?}, {:?})",
50                self.tag(),
51                a,
52                b);
53         Ok(a)
54     }
55
56     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
57         debug!("{}.tys({:?}, {:?})", self.tag(),
58                a, b);
59         if a == b { return Ok(a); }
60
61         match (&a.kind, &b.kind) {
62             (_, &ty::Infer(ty::FreshTy(_))) |
63             (_, &ty::Infer(ty::FreshIntTy(_))) |
64             (_, &ty::Infer(ty::FreshFloatTy(_))) => {
65                 Ok(a)
66             }
67
68             (&ty::Infer(_), _) |
69             (_, &ty::Infer(_)) => {
70                 Err(TypeError::Sorts(relate::expected_found(self, &a, &b)))
71             }
72
73             (&ty::Error, _) | (_, &ty::Error) => {
74                 Ok(self.tcx().types.err)
75             }
76
77             _ => {
78                 relate::super_relate_tys(self, a, b)
79             }
80         }
81     }
82
83     fn consts(
84         &mut self,
85         a: &'tcx ty::Const<'tcx>,
86         b: &'tcx ty::Const<'tcx>,
87     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
88         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
89         if a == b {
90             return Ok(a);
91         }
92
93         match (a.val, b.val) {
94             (_, ty::ConstKind::Infer(InferConst::Fresh(_))) => {
95                 return Ok(a);
96             }
97
98             (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
99                 return Err(TypeError::ConstMismatch(relate::expected_found(self, &a, &b)));
100             }
101
102             _ => {}
103         }
104
105         relate::super_relate_consts(self, a, b)
106     }
107
108     fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
109                   -> RelateResult<'tcx, ty::Binder<T>>
110         where T: Relate<'tcx>
111     {
112         Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
113     }
114 }