1use relate::lattice::{LatticeOp, LatticeOpKind};
29use rustc_middle::ty::relate::solver_relating::RelateExt as NextSolverRelate;
30use rustc_middle::ty::{Const, ParamEnv, TypingMode};
31use rustc_span::bug;
32
33use super::*;
34use crate::infer::relate::type_relating::TypeRelating;
35use crate::infer::relate::{Relate, TypeRelation};
36use crate::traits::Obligation;
37use crate::traits::solve::Goal;
38
39#[derive(#[automatically_derived]impl ::core::fmt::Debug for DefineOpaqueTypes {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
DefineOpaqueTypes::Yes => "Yes",
DefineOpaqueTypes::No => "No",
})
}
}Debug, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
DefineOpaqueTypes {
}
#[automatically_derived]impl ::core::cmp::PartialEq for DefineOpaqueTypes {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other)
}
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for DefineOpaqueTypes { }Eq, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for DefineOpaqueTypes {
}
#[automatically_derived]impl ::core::clone::Clone for DefineOpaqueTypes {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self { *self }
}Clone, #[automatically_derived]impl ::core::marker::Copy for DefineOpaqueTypes { }Copy)]
44pub enum DefineOpaqueTypes {
45 Yes,
46 No,
47}
48
49#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'a, 'tcx>
::core::clone::TrivialClone for At<'a, 'tcx> {
}
#[automatically_derived]impl<'a, 'tcx> ::core::clone::Clone for At<'a, 'tcx> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<&'a InferCtxt<'tcx>>;
let _: ::core::clone::AssertParamIsClone<&'a ObligationCause<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ty::ParamEnv<'tcx>>;
*self
}
}Clone, #[automatically_derived]impl<'a, 'tcx> ::core::marker::Copy for At<'a, 'tcx> {
}Copy)]
50pub struct At<'a, 'tcx> {
51 pub infcx: &'a InferCtxt<'tcx>,
52 pub cause: &'a ObligationCause<'tcx>,
53 pub param_env: ty::ParamEnv<'tcx>,
54}
55
56impl<'tcx> InferCtxt<'tcx> {
57 #[inline]
58 pub fn at<'a>(
59 &'a self,
60 cause: &'a ObligationCause<'tcx>,
61 param_env: ty::ParamEnv<'tcx>,
62 ) -> At<'a, 'tcx> {
63 At { infcx: self, cause, param_env }
64 }
65
66 pub fn fork(&self) -> Self {
70 Self {
71 tcx: self.tcx,
72 typing_mode: self.typing_mode,
73 considering_regions: self.considering_regions,
74 in_hir_typeck: self.in_hir_typeck,
75 skip_leak_check: self.skip_leak_check,
76 inner: self.inner.clone(),
77 lexical_region_resolutions: self.lexical_region_resolutions.clone(),
78 selection_cache: self.selection_cache.clone(),
79 evaluation_cache: self.evaluation_cache.clone(),
80 reported_trait_errors: self.reported_trait_errors.clone(),
81 reported_hir_errors: self.reported_hir_errors.clone(),
82 reported_signature_mismatch: self.reported_signature_mismatch.clone(),
83 tainted_by_errors: self.tainted_by_errors.clone(),
84 universe: self.universe.clone(),
85 placeholder_assumptions_for_next_solver: self
86 .placeholder_assumptions_for_next_solver
87 .clone(),
88 next_trait_solver: self.next_trait_solver,
89 enable_next_solver_overflow_fcw: self.enable_next_solver_overflow_fcw.clone(),
90 obligation_inspector: self.obligation_inspector.clone(),
91 }
92 }
93
94 pub fn fork_with_typing_mode(&self, typing_mode: TypingMode<'tcx>) -> Self {
98 let forked = Self {
101 tcx: self.tcx,
102 typing_mode,
103 considering_regions: self.considering_regions,
104 in_hir_typeck: self.in_hir_typeck,
105 skip_leak_check: self.skip_leak_check,
106 inner: self.inner.clone(),
107 lexical_region_resolutions: self.lexical_region_resolutions.clone(),
108 selection_cache: Default::default(),
109 evaluation_cache: Default::default(),
110 reported_trait_errors: self.reported_trait_errors.clone(),
111 reported_hir_errors: self.reported_hir_errors.clone(),
112 reported_signature_mismatch: self.reported_signature_mismatch.clone(),
113 tainted_by_errors: self.tainted_by_errors.clone(),
114 universe: self.universe.clone(),
115 placeholder_assumptions_for_next_solver: self
116 .placeholder_assumptions_for_next_solver
117 .clone(),
118 next_trait_solver: self.next_trait_solver,
119 enable_next_solver_overflow_fcw: self.enable_next_solver_overflow_fcw.clone(),
120 obligation_inspector: self.obligation_inspector.clone(),
121 };
122 forked.inner.borrow_mut().projection_cache().clear();
123 forked
124 }
125
126 pub fn sup_raw<T>(
134 &self,
135 cause: &ObligationCause<'tcx>,
136 param_env: ParamEnv<'tcx>,
137 define_opaque_types: DefineOpaqueTypes,
138 expected: T,
139 actual: T,
140 ) -> InferResult<'tcx, ()>
141 where
142 T: ToTrace<'tcx>,
143 {
144 if self.next_trait_solver {
145 NextSolverRelate::relate(
146 self,
147 param_env,
148 expected,
149 ty::Contravariant,
150 actual,
151 cause.span,
152 )
153 .map(|goals| self.goals_to_obligations(cause, goals))
154 } else {
155 let mut op = TypeRelating::new(
156 &self,
157 ToTrace::to_trace(cause, expected, actual),
158 param_env,
159 define_opaque_types,
160 ty::Contravariant,
161 );
162 op.relate(expected, actual)?;
163 Ok(InferOk { value: (), obligations: op.into_obligations() })
164 }
165 }
166
167 pub fn sub_raw<T>(
172 &self,
173 cause: &ObligationCause<'tcx>,
174 param_env: ParamEnv<'tcx>,
175 define_opaque_types: DefineOpaqueTypes,
176 expected: T,
177 actual: T,
178 ) -> InferResult<'tcx, ()>
179 where
180 T: ToTrace<'tcx>,
181 {
182 if self.next_trait_solver {
183 NextSolverRelate::relate(self, param_env, expected, ty::Covariant, actual, cause.span)
184 .map(|goals| self.goals_to_obligations(cause, goals))
185 } else {
186 let mut op = TypeRelating::new(
187 self,
188 ToTrace::to_trace(cause, expected, actual),
189 param_env,
190 define_opaque_types,
191 ty::Covariant,
192 );
193 op.relate(expected, actual)?;
194 Ok(InferOk { value: (), obligations: op.into_obligations() })
195 }
196 }
197
198 pub fn eq_raw<T>(
203 &self,
204 cause: &ObligationCause<'tcx>,
205 param_env: ParamEnv<'tcx>,
206 define_opaque_types: DefineOpaqueTypes,
207 expected: T,
208 actual: T,
209 ) -> InferResult<'tcx, ()>
210 where
211 T: ToTrace<'tcx>,
212 {
213 self.eq_trace_raw(
214 cause,
215 param_env,
216 define_opaque_types,
217 ToTrace::to_trace(cause, expected, actual),
218 expected,
219 actual,
220 )
221 }
222
223 pub fn eq_trace_raw<T>(
228 &self,
229 cause: &ObligationCause<'tcx>,
230 param_env: ParamEnv<'tcx>,
231 define_opaque_types: DefineOpaqueTypes,
232 trace: TypeTrace<'tcx>,
233 expected: T,
234 actual: T,
235 ) -> InferResult<'tcx, ()>
236 where
237 T: Relate<TyCtxt<'tcx>>,
238 {
239 if self.next_trait_solver {
240 return NextSolverRelate::relate(
241 self,
242 param_env,
243 expected,
244 ty::Invariant,
245 actual,
246 cause.span,
247 )
248 .map(|goals| self.goals_to_obligations(cause, goals));
249 } else {
250 let mut op =
251 TypeRelating::new(self, trace, param_env, define_opaque_types, ty::Invariant);
252 op.relate(expected, actual)?;
253 return Ok(InferOk { value: (), obligations: op.into_obligations() });
254 }
255 }
256
257 pub fn relate_raw<T>(
260 self,
261 cause: &ObligationCause<'tcx>,
262 param_env: ParamEnv<'tcx>,
263 define_opaque_types: DefineOpaqueTypes,
264 expected: T,
265 variance: ty::Variance,
266 actual: T,
267 ) -> InferResult<'tcx, ()>
268 where
269 T: ToTrace<'tcx>,
270 {
271 self.at(cause, param_env).relate(define_opaque_types, expected, variance, actual)
272 }
273
274 pub fn lub_raw<T>(
283 self,
284 cause: &ObligationCause<'tcx>,
285 param_env: ParamEnv<'tcx>,
286 expected: T,
287 actual: T,
288 ) -> InferResult<'tcx, T>
289 where
290 T: ToTrace<'tcx>,
291 {
292 self.at(cause, param_env).lub(expected, actual)
293 }
294
295 pub fn goals_to_obligations(
296 &self,
297 cause: &ObligationCause<'tcx>,
298 goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
299 ) -> InferOk<'tcx, ()> {
300 InferOk {
301 value: (),
302 obligations: goals
303 .into_iter()
304 .map(|goal| {
305 Obligation::new(self.tcx, cause.clone(), goal.param_env, goal.predicate)
306 })
307 .collect(),
308 }
309 }
310}
311
312pub trait ToTrace<'tcx>: Relate<TyCtxt<'tcx>> + Copy {
313 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx>;
314}
315
316impl<'a, 'tcx> At<'a, 'tcx> {
317 pub fn relate<T>(
319 self,
320 define_opaque_types: DefineOpaqueTypes,
321 expected: T,
322 variance: ty::Variance,
323 actual: T,
324 ) -> InferResult<'tcx, ()>
325 where
326 T: ToTrace<'tcx>,
327 {
328 match variance {
329 ty::Covariant => self.infcx.sub_raw(
330 self.cause,
331 self.param_env,
332 define_opaque_types,
333 expected,
334 actual,
335 ),
336 ty::Invariant => {
337 self.infcx.eq_raw(self.cause, self.param_env, define_opaque_types, expected, actual)
338 }
339 ty::Contravariant => self.infcx.sup_raw(
340 self.cause,
341 self.param_env,
342 define_opaque_types,
343 expected,
344 actual,
345 ),
346
347 ty::Bivariant => {
::core::panicking::panic_fmt(format_args!("Bivariant given to `relate()`"));
}panic!("Bivariant given to `relate()`"),
353 }
354 }
355
356 pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
364 where
365 T: ToTrace<'tcx>,
366 {
367 let mut op = LatticeOp::new(
368 self.infcx,
369 ToTrace::to_trace(self.cause, expected, actual),
370 self.param_env,
371 LatticeOpKind::Lub,
372 );
373 let value = op.relate(expected, actual)?;
374 Ok(InferOk { value, obligations: op.into_obligations() })
375 }
376}
377
378impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
379 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
380 TypeTrace {
381 cause: cause.clone(),
382 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
383 }
384 }
385}
386
387impl<'tcx> ToTrace<'tcx> for ty::Region<'tcx> {
388 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
389 TypeTrace { cause: cause.clone(), values: ValuePairs::Regions(ExpectedFound::new(a, b)) }
390 }
391}
392
393impl<'tcx> ToTrace<'tcx> for Const<'tcx> {
394 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
395 TypeTrace {
396 cause: cause.clone(),
397 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
398 }
399 }
400}
401
402impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
403 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
404 TypeTrace {
405 cause: cause.clone(),
406 values: match (a.kind(), b.kind()) {
407 (GenericArgKind::Lifetime(a), GenericArgKind::Lifetime(b)) => {
408 ValuePairs::Regions(ExpectedFound::new(a, b))
409 }
410 (GenericArgKind::Type(a), GenericArgKind::Type(b)) => {
411 ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
412 }
413 (GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
414 ValuePairs::Terms(ExpectedFound::new(a.into(), b.into()))
415 }
416 _ => ::rustc_span::macros::bug_impl(None,
format_args!("relating different kinds: {0:?} {1:?}", a, b),
Location::caller())bug!("relating different kinds: {a:?} {b:?}"),
417 },
418 }
419 }
420}
421
422impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
423 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
424 TypeTrace { cause: cause.clone(), values: ValuePairs::Terms(ExpectedFound::new(a, b)) }
425 }
426}
427
428impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
429 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
430 TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
431 }
432}
433
434impl<'tcx> ToTrace<'tcx> for ty::AliasTy<'tcx> {
435 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
436 TypeTrace {
437 cause: cause.clone(),
438 values: ValuePairs::Aliases(ExpectedFound::new(a.into(), b.into())),
439 }
440 }
441}
442
443impl<'tcx> ToTrace<'tcx> for ty::AliasTerm<'tcx> {
444 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
445 TypeTrace { cause: cause.clone(), values: ValuePairs::Aliases(ExpectedFound::new(a, b)) }
446 }
447}
448
449impl<'tcx> ToTrace<'tcx> for ty::FnSig<'tcx> {
450 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
451 TypeTrace {
452 cause: cause.clone(),
453 values: ValuePairs::PolySigs(ExpectedFound::new(
454 ty::Binder::dummy(a),
455 ty::Binder::dummy(b),
456 )),
457 }
458 }
459}
460
461impl<'tcx> ToTrace<'tcx> for ty::PolyFnSig<'tcx> {
462 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
463 TypeTrace { cause: cause.clone(), values: ValuePairs::PolySigs(ExpectedFound::new(a, b)) }
464 }
465}
466
467impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialTraitRef<'tcx> {
468 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
469 TypeTrace {
470 cause: cause.clone(),
471 values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(a, b)),
472 }
473 }
474}
475
476impl<'tcx> ToTrace<'tcx> for ty::ExistentialTraitRef<'tcx> {
477 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
478 TypeTrace {
479 cause: cause.clone(),
480 values: ValuePairs::ExistentialTraitRef(ExpectedFound::new(
481 ty::Binder::dummy(a),
482 ty::Binder::dummy(b),
483 )),
484 }
485 }
486}
487
488impl<'tcx> ToTrace<'tcx> for ty::PolyExistentialProjection<'tcx> {
489 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
490 TypeTrace {
491 cause: cause.clone(),
492 values: ValuePairs::ExistentialProjection(ExpectedFound::new(a, b)),
493 }
494 }
495}
496
497impl<'tcx> ToTrace<'tcx> for ty::ExistentialProjection<'tcx> {
498 fn to_trace(cause: &ObligationCause<'tcx>, a: Self, b: Self) -> TypeTrace<'tcx> {
499 TypeTrace {
500 cause: cause.clone(),
501 values: ValuePairs::ExistentialProjection(ExpectedFound::new(
502 ty::Binder::dummy(a),
503 ty::Binder::dummy(b),
504 )),
505 }
506 }
507}