]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/_match.rs
Auto merge of #107241 - clubby789:bootstrap-lto-off, r=simulacrum
[rust.git] / compiler / rustc_middle / src / 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<'tcx> 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<'tcx> 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
40     fn intercrate(&self) -> bool {
41         false
42     }
43
44     fn param_env(&self) -> ty::ParamEnv<'tcx> {
45         self.param_env
46     }
47     fn a_is_expected(&self) -> bool {
48         true
49     } // irrelevant
50
51     fn mark_ambiguous(&mut self) {
52         bug!()
53     }
54
55     fn relate_with_variance<T: Relate<'tcx>>(
56         &mut self,
57         _: ty::Variance,
58         _: ty::VarianceDiagInfo<'tcx>,
59         a: T,
60         b: T,
61     ) -> RelateResult<'tcx, T> {
62         self.relate(a, b)
63     }
64
65     #[instrument(skip(self), level = "debug")]
66     fn regions(
67         &mut self,
68         a: ty::Region<'tcx>,
69         b: ty::Region<'tcx>,
70     ) -> RelateResult<'tcx, ty::Region<'tcx>> {
71         Ok(a)
72     }
73
74     #[instrument(skip(self), level = "debug")]
75     fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
76         if a == b {
77             return Ok(a);
78         }
79
80         match (a.kind(), b.kind()) {
81             (
82                 _,
83                 &ty::Infer(ty::FreshTy(_))
84                 | &ty::Infer(ty::FreshIntTy(_))
85                 | &ty::Infer(ty::FreshFloatTy(_)),
86             ) => Ok(a),
87
88             (&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
89                 Err(TypeError::Sorts(relate::expected_found(self, a, b)))
90             }
91
92             (&ty::Error(guar), _) | (_, &ty::Error(guar)) => {
93                 Ok(self.tcx().ty_error_with_guaranteed(guar))
94             }
95
96             _ => relate::super_relate_tys(self, a, b),
97         }
98     }
99
100     fn consts(
101         &mut self,
102         a: ty::Const<'tcx>,
103         b: ty::Const<'tcx>,
104     ) -> RelateResult<'tcx, ty::Const<'tcx>> {
105         debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
106         if a == b {
107             return Ok(a);
108         }
109
110         match (a.kind(), b.kind()) {
111             (_, ty::ConstKind::Infer(InferConst::Fresh(_))) => {
112                 return Ok(a);
113             }
114
115             (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
116                 return Err(TypeError::ConstMismatch(relate::expected_found(self, a, b)));
117             }
118
119             _ => {}
120         }
121
122         relate::super_relate_consts(self, a, b)
123     }
124
125     fn binders<T>(
126         &mut self,
127         a: ty::Binder<'tcx, T>,
128         b: ty::Binder<'tcx, T>,
129     ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
130     where
131         T: Relate<'tcx>,
132     {
133         Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
134     }
135 }