]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/_match.rs
Rollup merge of #69766 - skade:make-point-copy-in-add-documentation, r=shepmaster
[rust.git] / src / librustc / ty / _match.rs
1 use crate::ty::error::TypeError;
2 use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
3 use crate::ty::{self, InferConst, Ty, TyCtxt};
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 {
34         "Match"
35     }
36     fn tcx(&self) -> TyCtxt<'tcx> {
37         self.tcx
38     }
39     fn param_env(&self) -> ty::ParamEnv<'tcx> {
40         self.param_env
41     }
42     fn a_is_expected(&self) -> bool {
43         true
44     } // irrelevant
45
46     fn relate_with_variance<T: Relate<'tcx>>(
47         &mut self,
48         _: ty::Variance,
49         a: &T,
50         b: &T,
51     ) -> RelateResult<'tcx, T> {
52         self.relate(a, b)
53     }
54
55     fn regions(
56         &mut self,
57         a: ty::Region<'tcx>,
58         b: ty::Region<'tcx>,
59     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
60         debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
61         Ok(a)
62     }
63
64     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
65         debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
66         if a == b {
67             return Ok(a);
68         }
69
70         match (&a.kind, &b.kind) {
71             (_, &ty::Infer(ty::FreshTy(_)))
72             | (_, &ty::Infer(ty::FreshIntTy(_)))
73             | (_, &ty::Infer(ty::FreshFloatTy(_))) => Ok(a),
74
75             (&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
76                 Err(TypeError::Sorts(relate::expected_found(self, &a, &b)))
77             }
78
79             (&ty::Error, _) | (_, &ty::Error) => Ok(self.tcx().types.err),
80
81             _ => relate::super_relate_tys(self, a, b),
82         }
83     }
84
85     fn consts(
86         &mut self,
87         a: &'tcx ty::Const<'tcx>,
88         b: &'tcx ty::Const<'tcx>,
89     ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
90         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
91         if a == b {
92             return Ok(a);
93         }
94
95         match (a.val, b.val) {
96             (_, ty::ConstKind::Infer(InferConst::Fresh(_))) => {
97                 return Ok(a);
98             }
99
100             (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
101                 return Err(TypeError::ConstMismatch(relate::expected_found(self, &a, &b)));
102             }
103
104             _ => {}
105         }
106
107         relate::super_relate_consts(self, a, b)
108     }
109
110     fn binders<T>(
111         &mut self,
112         a: &ty::Binder<T>,
113         b: &ty::Binder<T>,
114     ) -> RelateResult<'tcx, ty::Binder<T>>
115     where
116         T: Relate<'tcx>,
117     {
118         Ok(ty::Binder::bind(self.relate(a.skip_binder(), b.skip_binder())?))
119     }
120 }