rustc_infer/infer/outlives/
test_type_match.rs1use std::collections::hash_map::Entry;
2
3use rustc_data_structures::fx::FxHashMap;
4use rustc_hir::def_id::DefId;
5use rustc_middle::ty::error::TypeError;
6use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
7use rustc_type_ir::relate::relate_args_with_variances;
8use tracing::instrument;
9
10use crate::infer::region_constraints::VerifyIfEq;
11use crate::infer::relate::{self as relate, Relate, RelateResult, TypeRelation};
12
13#[instrument(level = "debug", skip(tcx))]
39pub fn extract_verify_if_eq<'tcx>(
40 tcx: TyCtxt<'tcx>,
41 verify_if_eq_b: &ty::Binder<'tcx, VerifyIfEq<'tcx>>,
42 test_ty: Ty<'tcx>,
43) -> Option<ty::Region<'tcx>> {
44 assert!(!verify_if_eq_b.has_escaping_bound_vars());
45 let mut m = MatchAgainstHigherRankedOutlives::new(tcx);
46 let verify_if_eq = verify_if_eq_b.skip_binder();
47 m.relate(verify_if_eq.ty, test_ty).ok()?;
48
49 if let ty::RegionKind::ReBound(index_kind, br) = verify_if_eq.bound.kind() {
50 assert!(matches!(index_kind, ty::BoundVarIndexKind::Bound(ty::INNERMOST)));
51 match m.map.get(&br) {
52 Some(&r) => Some(r),
53 None => {
54 Some(tcx.lifetimes.re_static)
57 }
58 }
59 } else {
60 Some(verify_if_eq.bound)
70 }
71}
72
73#[instrument(level = "debug", skip(tcx))]
75pub(super) fn can_match_erased_ty<'tcx>(
76 tcx: TyCtxt<'tcx>,
77 outlives_predicate: ty::Binder<'tcx, ty::TypeOutlivesPredicate<'tcx>>,
78 erased_ty: Ty<'tcx>,
79) -> bool {
80 assert!(!outlives_predicate.has_escaping_bound_vars());
81 let erased_outlives_predicate = tcx.erase_and_anonymize_regions(outlives_predicate);
82 let outlives_ty = erased_outlives_predicate.skip_binder().0;
83 if outlives_ty == erased_ty {
84 true
86 } else {
87 MatchAgainstHigherRankedOutlives::new(tcx).relate(outlives_ty, erased_ty).is_ok()
88 }
89}
90
91struct MatchAgainstHigherRankedOutlives<'tcx> {
92 tcx: TyCtxt<'tcx>,
93 pattern_depth: ty::DebruijnIndex,
94 map: FxHashMap<ty::BoundRegion, ty::Region<'tcx>>,
95}
96
97impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
98 fn new(tcx: TyCtxt<'tcx>) -> MatchAgainstHigherRankedOutlives<'tcx> {
99 MatchAgainstHigherRankedOutlives {
100 tcx,
101 pattern_depth: ty::INNERMOST,
102 map: FxHashMap::default(),
103 }
104 }
105}
106
107impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
108 fn no_match<T>(&self) -> RelateResult<'tcx, T> {
110 Err(TypeError::Mismatch)
111 }
112
113 #[instrument(level = "trace", skip(self))]
116 fn bind(
117 &mut self,
118 br: ty::BoundRegion,
119 value: ty::Region<'tcx>,
120 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
121 match self.map.entry(br) {
122 Entry::Occupied(entry) => {
123 if *entry.get() == value {
124 Ok(value)
125 } else {
126 self.no_match()
127 }
128 }
129 Entry::Vacant(entry) => {
130 entry.insert(value);
131 Ok(value)
132 }
133 }
134 }
135}
136
137impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstHigherRankedOutlives<'tcx> {
138 fn cx(&self) -> TyCtxt<'tcx> {
139 self.tcx
140 }
141
142 fn relate_ty_args(
143 &mut self,
144 a_ty: Ty<'tcx>,
145 _: Ty<'tcx>,
146 def_id: DefId,
147 a_args: ty::GenericArgsRef<'tcx>,
148 b_args: ty::GenericArgsRef<'tcx>,
149 _: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
150 ) -> RelateResult<'tcx, Ty<'tcx>> {
151 let variances = self.cx().variances_of(def_id);
152 relate_args_with_variances(self, variances, a_args, b_args)?;
153 Ok(a_ty)
154 }
155
156 #[instrument(level = "trace", skip(self))]
157 fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
158 &mut self,
159 variance: ty::Variance,
160 _: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
161 a: T,
162 b: T,
163 ) -> RelateResult<'tcx, T> {
164 if variance != ty::Bivariant { self.relate(a, b) } else { Ok(a) }
171 }
172
173 #[instrument(skip(self), level = "trace")]
174 fn regions(
175 &mut self,
176 pattern: ty::Region<'tcx>,
177 value: ty::Region<'tcx>,
178 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
179 if let ty::RegionKind::ReBound(ty::BoundVarIndexKind::Bound(depth), br) = pattern.kind()
180 && depth == self.pattern_depth
181 {
182 self.bind(br, value)
183 } else if pattern == value {
184 Ok(pattern)
185 } else {
186 self.no_match()
187 }
188 }
189
190 #[instrument(skip(self), level = "trace")]
191 fn tys(&mut self, pattern: Ty<'tcx>, value: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
192 if matches!(pattern.kind(), ty::Error(_) | ty::Bound(..)) {
194 self.no_match()
196 } else if pattern == value {
197 Ok(pattern)
198 } else {
199 relate::structurally_relate_tys(self, pattern, value)
200 }
201 }
202
203 #[instrument(skip(self), level = "trace")]
204 fn consts(
205 &mut self,
206 pattern: ty::Const<'tcx>,
207 value: ty::Const<'tcx>,
208 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
209 if pattern == value {
210 Ok(pattern)
211 } else {
212 relate::structurally_relate_consts(self, pattern, value)
213 }
214 }
215
216 #[instrument(skip(self), level = "trace")]
217 fn binders<T>(
218 &mut self,
219 pattern: ty::Binder<'tcx, T>,
220 value: ty::Binder<'tcx, T>,
221 ) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
222 where
223 T: Relate<TyCtxt<'tcx>>,
224 {
225 self.pattern_depth.shift_in(1);
226 let result = Ok(pattern.rebind(self.relate(pattern.skip_binder(), value.skip_binder())?));
227 self.pattern_depth.shift_out(1);
228 result
229 }
230}