1use std::cell::{Cell, RefCell};
2use std::fmt;
3
4pub use BoundRegionConversionTime::*;
5pub use RegionVariableOrigin::*;
6pub use SubregionOrigin::*;
7pub use at::DefineOpaqueTypes;
8use free_regions::RegionRelations;
9pub use freshen::TypeFreshener;
10use lexical_region_resolve::LexicalRegionResolutions;
11pub use lexical_region_resolve::RegionResolutionError;
12pub use opaque_types::{OpaqueTypeStorage, OpaqueTypeStorageEntries, OpaqueTypeTable};
13use region_constraints::{
14 GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
15};
16pub use relate::StructurallyRelateAliases;
17pub use relate::combine::PredicateEmittingRelation;
18use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
19use rustc_data_structures::undo_log::{Rollback, UndoLogs};
20use rustc_data_structures::unify as ut;
21use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed};
22use rustc_hir as hir;
23use rustc_hir::def_id::{DefId, LocalDefId};
24use rustc_macros::extension;
25pub use rustc_macros::{TypeFoldable, TypeVisitable};
26use rustc_middle::bug;
27use rustc_middle::infer::canonical::{CanonicalQueryInput, CanonicalVarValues};
28use rustc_middle::mir::ConstraintCategory;
29use rustc_middle::traits::select;
30use rustc_middle::traits::solve::Goal;
31use rustc_middle::ty::error::{ExpectedFound, TypeError};
32use rustc_middle::ty::{
33 self, BoundVarReplacerDelegate, ConstVid, FloatVid, GenericArg, GenericArgKind, GenericArgs,
34 GenericArgsRef, GenericParamDefKind, InferConst, IntVid, OpaqueHiddenType, OpaqueTypeKey,
35 PseudoCanonicalInput, Term, TermKind, Ty, TyCtxt, TyVid, TypeFoldable, TypeFolder,
36 TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv, TypingMode, fold_regions,
37};
38use rustc_span::{Span, Symbol};
39use snapshot::undo_log::InferCtxtUndoLogs;
40use tracing::{debug, instrument};
41use type_variable::TypeVariableOrigin;
42
43use crate::infer::region_constraints::UndoLog;
44use crate::infer::unify_key::{ConstVariableOrigin, ConstVariableValue, ConstVidKey};
45use crate::traits::{
46 self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine,
47};
48
49pub mod at;
50pub mod canonical;
51mod context;
52mod free_regions;
53mod freshen;
54mod lexical_region_resolve;
55mod opaque_types;
56pub mod outlives;
57mod projection;
58pub mod region_constraints;
59pub mod relate;
60pub mod resolve;
61pub(crate) mod snapshot;
62mod type_variable;
63mod unify_key;
64
65#[must_use]
73#[derive(Debug)]
74pub struct InferOk<'tcx, T> {
75 pub value: T,
76 pub obligations: PredicateObligations<'tcx>,
77}
78pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
79
80pub(crate) type FixupResult<T> = Result<T, FixupError>; pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
83 ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
84>;
85
86#[derive(Clone)]
91pub struct InferCtxtInner<'tcx> {
92 undo_log: InferCtxtUndoLogs<'tcx>,
93
94 projection_cache: traits::ProjectionCacheStorage<'tcx>,
98
99 type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
103
104 const_unification_storage: ut::UnificationTableStorage<ConstVidKey<'tcx>>,
106
107 int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
109
110 float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
112
113 region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
120
121 region_obligations: Vec<TypeOutlivesConstraint<'tcx>>,
154
155 opaque_type_storage: OpaqueTypeStorage<'tcx>,
157}
158
159impl<'tcx> InferCtxtInner<'tcx> {
160 fn new() -> InferCtxtInner<'tcx> {
161 InferCtxtInner {
162 undo_log: InferCtxtUndoLogs::default(),
163
164 projection_cache: Default::default(),
165 type_variable_storage: Default::default(),
166 const_unification_storage: Default::default(),
167 int_unification_storage: Default::default(),
168 float_unification_storage: Default::default(),
169 region_constraint_storage: Some(Default::default()),
170 region_obligations: vec![],
171 opaque_type_storage: Default::default(),
172 }
173 }
174
175 #[inline]
176 pub fn region_obligations(&self) -> &[TypeOutlivesConstraint<'tcx>] {
177 &self.region_obligations
178 }
179
180 #[inline]
181 pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
182 self.projection_cache.with_log(&mut self.undo_log)
183 }
184
185 #[inline]
186 fn try_type_variables_probe_ref(
187 &self,
188 vid: ty::TyVid,
189 ) -> Option<&type_variable::TypeVariableValue<'tcx>> {
190 self.type_variable_storage.eq_relations_ref().try_probe_value(vid)
193 }
194
195 #[inline]
196 fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
197 self.type_variable_storage.with_log(&mut self.undo_log)
198 }
199
200 #[inline]
201 pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
202 self.opaque_type_storage.with_log(&mut self.undo_log)
203 }
204
205 #[inline]
206 fn int_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::IntVid> {
207 self.int_unification_storage.with_log(&mut self.undo_log)
208 }
209
210 #[inline]
211 fn float_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ty::FloatVid> {
212 self.float_unification_storage.with_log(&mut self.undo_log)
213 }
214
215 #[inline]
216 fn const_unification_table(&mut self) -> UnificationTable<'_, 'tcx, ConstVidKey<'tcx>> {
217 self.const_unification_storage.with_log(&mut self.undo_log)
218 }
219
220 #[inline]
221 pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
222 self.region_constraint_storage
223 .as_mut()
224 .expect("region constraints already solved")
225 .with_log(&mut self.undo_log)
226 }
227}
228
229pub struct InferCtxt<'tcx> {
230 pub tcx: TyCtxt<'tcx>,
231
232 typing_mode: TypingMode<'tcx>,
235
236 pub considering_regions: bool,
240
241 skip_leak_check: bool,
246
247 pub inner: RefCell<InferCtxtInner<'tcx>>,
248
249 lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
251
252 pub selection_cache: select::SelectionCache<'tcx, ty::ParamEnv<'tcx>>,
255
256 pub evaluation_cache: select::EvaluationCache<'tcx, ty::ParamEnv<'tcx>>,
259
260 pub reported_trait_errors:
263 RefCell<FxIndexMap<Span, (Vec<Goal<'tcx, ty::Predicate<'tcx>>>, ErrorGuaranteed)>>,
264
265 pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
266
267 tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
275
276 universe: Cell<ty::UniverseIndex>,
286
287 next_trait_solver: bool,
288
289 pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
290}
291
292#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
294pub enum ValuePairs<'tcx> {
295 Regions(ExpectedFound<ty::Region<'tcx>>),
296 Terms(ExpectedFound<ty::Term<'tcx>>),
297 Aliases(ExpectedFound<ty::AliasTerm<'tcx>>),
298 TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
299 PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
300 ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
301 ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
302}
303
304impl<'tcx> ValuePairs<'tcx> {
305 pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
306 if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
307 && let Some(expected) = expected.as_type()
308 && let Some(found) = found.as_type()
309 {
310 Some((expected, found))
311 } else {
312 None
313 }
314 }
315}
316
317#[derive(Clone, Debug)]
322pub struct TypeTrace<'tcx> {
323 pub cause: ObligationCause<'tcx>,
324 pub values: ValuePairs<'tcx>,
325}
326
327#[derive(Clone, Debug)]
331pub enum SubregionOrigin<'tcx> {
332 Subtype(Box<TypeTrace<'tcx>>),
334
335 RelateObjectBound(Span),
338
339 RelateParamBound(Span, Ty<'tcx>, Option<Span>),
342
343 RelateRegionParamBound(Span, Option<Ty<'tcx>>),
346
347 Reborrow(Span),
349
350 ReferenceOutlivesReferent(Ty<'tcx>, Span),
352
353 CompareImplItemObligation {
356 span: Span,
357 impl_item_def_id: LocalDefId,
358 trait_item_def_id: DefId,
359 },
360
361 CheckAssociatedTypeBounds {
363 parent: Box<SubregionOrigin<'tcx>>,
364 impl_item_def_id: LocalDefId,
365 trait_item_def_id: DefId,
366 },
367
368 AscribeUserTypeProvePredicate(Span),
369}
370
371#[cfg(target_pointer_width = "64")]
373rustc_data_structures::static_assert_size!(SubregionOrigin<'_>, 32);
374
375impl<'tcx> SubregionOrigin<'tcx> {
376 pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
377 match self {
378 Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
379 Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
380 _ => ConstraintCategory::BoringNoLocation,
381 }
382 }
383}
384
385#[derive(Clone, Copy, Debug)]
387pub enum BoundRegionConversionTime {
388 FnCall,
390
391 HigherRankedType,
393
394 AssocTypeProjection(DefId),
396}
397
398#[derive(Copy, Clone, Debug)]
402pub enum RegionVariableOrigin {
403 MiscVariable(Span),
407
408 PatternRegion(Span),
410
411 BorrowRegion(Span),
413
414 Autoref(Span),
416
417 Coercion(Span),
419
420 RegionParameterDefinition(Span, Symbol),
425
426 BoundRegion(Span, ty::BoundRegionKind, BoundRegionConversionTime),
429
430 UpvarRegion(ty::UpvarId, Span),
431
432 Nll(NllRegionVariableOrigin),
435}
436
437#[derive(Copy, Clone, Debug)]
438pub enum NllRegionVariableOrigin {
439 FreeRegion,
443
444 Placeholder(ty::PlaceholderRegion),
447
448 Existential {
449 from_forall: bool,
460 },
461}
462
463#[derive(Copy, Clone, Debug)]
464pub struct FixupError {
465 unresolved: TyOrConstInferVar,
466}
467
468impl fmt::Display for FixupError {
469 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470 use TyOrConstInferVar::*;
471
472 match self.unresolved {
473 TyInt(_) => write!(
474 f,
475 "cannot determine the type of this integer; \
476 add a suffix to specify the type explicitly"
477 ),
478 TyFloat(_) => write!(
479 f,
480 "cannot determine the type of this number; \
481 add a suffix to specify the type explicitly"
482 ),
483 Ty(_) => write!(f, "unconstrained type"),
484 Const(_) => write!(f, "unconstrained const value"),
485 }
486 }
487}
488
489#[derive(Clone, Debug)]
491pub struct TypeOutlivesConstraint<'tcx> {
492 pub sub_region: ty::Region<'tcx>,
493 pub sup_type: Ty<'tcx>,
494 pub origin: SubregionOrigin<'tcx>,
495}
496
497pub struct InferCtxtBuilder<'tcx> {
499 tcx: TyCtxt<'tcx>,
500 considering_regions: bool,
501 skip_leak_check: bool,
502 next_trait_solver: bool,
505}
506
507#[extension(pub trait TyCtxtInferExt<'tcx>)]
508impl<'tcx> TyCtxt<'tcx> {
509 fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
510 InferCtxtBuilder {
511 tcx: self,
512 considering_regions: true,
513 skip_leak_check: false,
514 next_trait_solver: self.next_trait_solver_globally(),
515 }
516 }
517}
518
519impl<'tcx> InferCtxtBuilder<'tcx> {
520 pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
521 self.next_trait_solver = next_trait_solver;
522 self
523 }
524
525 pub fn ignoring_regions(mut self) -> Self {
526 self.considering_regions = false;
527 self
528 }
529
530 pub fn skip_leak_check(mut self, skip_leak_check: bool) -> Self {
531 self.skip_leak_check = skip_leak_check;
532 self
533 }
534
535 pub fn build_with_canonical<T>(
543 mut self,
544 span: Span,
545 input: &CanonicalQueryInput<'tcx, T>,
546 ) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
547 where
548 T: TypeFoldable<TyCtxt<'tcx>>,
549 {
550 let infcx = self.build(input.typing_mode);
551 let (value, args) = infcx.instantiate_canonical(span, &input.canonical);
552 (infcx, value, args)
553 }
554
555 pub fn build_with_typing_env(
556 mut self,
557 TypingEnv { typing_mode, param_env }: TypingEnv<'tcx>,
558 ) -> (InferCtxt<'tcx>, ty::ParamEnv<'tcx>) {
559 (self.build(typing_mode), param_env)
560 }
561
562 pub fn build(&mut self, typing_mode: TypingMode<'tcx>) -> InferCtxt<'tcx> {
563 let InferCtxtBuilder { tcx, considering_regions, skip_leak_check, next_trait_solver } =
564 *self;
565 InferCtxt {
566 tcx,
567 typing_mode,
568 considering_regions,
569 skip_leak_check,
570 inner: RefCell::new(InferCtxtInner::new()),
571 lexical_region_resolutions: RefCell::new(None),
572 selection_cache: Default::default(),
573 evaluation_cache: Default::default(),
574 reported_trait_errors: Default::default(),
575 reported_signature_mismatch: Default::default(),
576 tainted_by_errors: Cell::new(None),
577 universe: Cell::new(ty::UniverseIndex::ROOT),
578 next_trait_solver,
579 obligation_inspector: Cell::new(None),
580 }
581 }
582}
583
584impl<'tcx, T> InferOk<'tcx, T> {
585 pub fn into_value_registering_obligations<E: 'tcx>(
587 self,
588 infcx: &InferCtxt<'tcx>,
589 fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
590 ) -> T {
591 let InferOk { value, obligations } = self;
592 fulfill_cx.register_predicate_obligations(infcx, obligations);
593 value
594 }
595}
596
597impl<'tcx> InferOk<'tcx, ()> {
598 pub fn into_obligations(self) -> PredicateObligations<'tcx> {
599 self.obligations
600 }
601}
602
603impl<'tcx> InferCtxt<'tcx> {
604 pub fn dcx(&self) -> DiagCtxtHandle<'_> {
605 self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
606 }
607
608 pub fn next_trait_solver(&self) -> bool {
609 self.next_trait_solver
610 }
611
612 #[inline(always)]
613 pub fn typing_mode(&self) -> TypingMode<'tcx> {
614 self.typing_mode
615 }
616
617 pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
618 t.fold_with(&mut self.freshener())
619 }
620
621 pub fn type_var_origin(&self, vid: TyVid) -> TypeVariableOrigin {
625 self.inner.borrow_mut().type_variables().var_origin(vid)
626 }
627
628 pub fn const_var_origin(&self, vid: ConstVid) -> Option<ConstVariableOrigin> {
632 match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
633 ConstVariableValue::Known { .. } => None,
634 ConstVariableValue::Unknown { origin, .. } => Some(origin),
635 }
636 }
637
638 pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
639 freshen::TypeFreshener::new(self)
640 }
641
642 pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
643 let mut inner = self.inner.borrow_mut();
644 let mut vars: Vec<Ty<'_>> = inner
645 .type_variables()
646 .unresolved_variables()
647 .into_iter()
648 .map(|t| Ty::new_var(self.tcx, t))
649 .collect();
650 vars.extend(
651 (0..inner.int_unification_table().len())
652 .map(|i| ty::IntVid::from_usize(i))
653 .filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
654 .map(|v| Ty::new_int_var(self.tcx, v)),
655 );
656 vars.extend(
657 (0..inner.float_unification_table().len())
658 .map(|i| ty::FloatVid::from_usize(i))
659 .filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
660 .map(|v| Ty::new_float_var(self.tcx, v)),
661 );
662 vars
663 }
664
665 #[instrument(skip(self), level = "debug")]
666 pub fn sub_regions(
667 &self,
668 origin: SubregionOrigin<'tcx>,
669 a: ty::Region<'tcx>,
670 b: ty::Region<'tcx>,
671 ) {
672 self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
673 }
674
675 pub fn coerce_predicate(
691 &self,
692 cause: &ObligationCause<'tcx>,
693 param_env: ty::ParamEnv<'tcx>,
694 predicate: ty::PolyCoercePredicate<'tcx>,
695 ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
696 let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
697 a_is_expected: false, a: p.a,
699 b: p.b,
700 });
701 self.subtype_predicate(cause, param_env, subtype_predicate)
702 }
703
704 pub fn subtype_predicate(
705 &self,
706 cause: &ObligationCause<'tcx>,
707 param_env: ty::ParamEnv<'tcx>,
708 predicate: ty::PolySubtypePredicate<'tcx>,
709 ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
710 let r_a = self.shallow_resolve(predicate.skip_binder().a);
724 let r_b = self.shallow_resolve(predicate.skip_binder().b);
725 match (r_a.kind(), r_b.kind()) {
726 (&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
727 return Err((a_vid, b_vid));
728 }
729 _ => {}
730 }
731
732 self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
733 if a_is_expected {
734 Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::Yes, a, b))
735 } else {
736 Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::Yes, b, a))
737 }
738 })
739 }
740
741 pub fn num_ty_vars(&self) -> usize {
743 self.inner.borrow_mut().type_variables().num_vars()
744 }
745
746 pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
747 self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
748 }
749
750 pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
751 let vid = self.inner.borrow_mut().type_variables().new_var(self.universe(), origin);
752 Ty::new_var(self.tcx, vid)
753 }
754
755 pub fn next_ty_var_id_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
756 let origin = TypeVariableOrigin { span, param_def_id: None };
757 self.inner.borrow_mut().type_variables().new_var(universe, origin)
758 }
759
760 pub fn next_ty_var_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> Ty<'tcx> {
761 let vid = self.next_ty_var_id_in_universe(span, universe);
762 Ty::new_var(self.tcx, vid)
763 }
764
765 pub fn next_const_var(&self, span: Span) -> ty::Const<'tcx> {
766 self.next_const_var_with_origin(ConstVariableOrigin { span, param_def_id: None })
767 }
768
769 pub fn next_const_var_with_origin(&self, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
770 let vid = self
771 .inner
772 .borrow_mut()
773 .const_unification_table()
774 .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
775 .vid;
776 ty::Const::new_var(self.tcx, vid)
777 }
778
779 pub fn next_const_var_in_universe(
780 &self,
781 span: Span,
782 universe: ty::UniverseIndex,
783 ) -> ty::Const<'tcx> {
784 let origin = ConstVariableOrigin { span, param_def_id: None };
785 let vid = self
786 .inner
787 .borrow_mut()
788 .const_unification_table()
789 .new_key(ConstVariableValue::Unknown { origin, universe })
790 .vid;
791 ty::Const::new_var(self.tcx, vid)
792 }
793
794 pub fn next_int_var(&self) -> Ty<'tcx> {
795 let next_int_var_id =
796 self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown);
797 Ty::new_int_var(self.tcx, next_int_var_id)
798 }
799
800 pub fn next_float_var(&self) -> Ty<'tcx> {
801 let next_float_var_id =
802 self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown);
803 Ty::new_float_var(self.tcx, next_float_var_id)
804 }
805
806 pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
810 self.next_region_var_in_universe(origin, self.universe())
811 }
812
813 pub fn next_region_var_in_universe(
817 &self,
818 origin: RegionVariableOrigin,
819 universe: ty::UniverseIndex,
820 ) -> ty::Region<'tcx> {
821 let region_var =
822 self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
823 ty::Region::new_var(self.tcx, region_var)
824 }
825
826 pub fn next_term_var_of_kind(&self, term: ty::Term<'tcx>, span: Span) -> ty::Term<'tcx> {
827 match term.kind() {
828 ty::TermKind::Ty(_) => self.next_ty_var(span).into(),
829 ty::TermKind::Const(_) => self.next_const_var(span).into(),
830 }
831 }
832
833 pub fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
839 self.inner.borrow_mut().unwrap_region_constraints().universe(r)
840 }
841
842 pub fn num_region_vars(&self) -> usize {
844 self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
845 }
846
847 #[instrument(skip(self), level = "debug")]
849 pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
850 self.next_region_var(RegionVariableOrigin::Nll(origin))
851 }
852
853 #[instrument(skip(self), level = "debug")]
855 pub fn next_nll_region_var_in_universe(
856 &self,
857 origin: NllRegionVariableOrigin,
858 universe: ty::UniverseIndex,
859 ) -> ty::Region<'tcx> {
860 self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
861 }
862
863 pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
864 match param.kind {
865 GenericParamDefKind::Lifetime => {
866 self.next_region_var(RegionParameterDefinition(span, param.name)).into()
869 }
870 GenericParamDefKind::Type { .. } => {
871 let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
880 self.universe(),
881 TypeVariableOrigin { param_def_id: Some(param.def_id), span },
882 );
883
884 Ty::new_var(self.tcx, ty_var_id).into()
885 }
886 GenericParamDefKind::Const { .. } => {
887 let origin = ConstVariableOrigin { param_def_id: Some(param.def_id), span };
888 let const_var_id = self
889 .inner
890 .borrow_mut()
891 .const_unification_table()
892 .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
893 .vid;
894 ty::Const::new_var(self.tcx, const_var_id).into()
895 }
896 }
897 }
898
899 pub fn fresh_args_for_item(&self, span: Span, def_id: DefId) -> GenericArgsRef<'tcx> {
902 GenericArgs::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
903 }
904
905 #[must_use = "this method does not have any side effects"]
911 pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
912 self.tainted_by_errors.get()
913 }
914
915 pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
918 debug!("set_tainted_by_errors(ErrorGuaranteed)");
919 self.tainted_by_errors.set(Some(e));
920 }
921
922 pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
923 let mut inner = self.inner.borrow_mut();
924 let inner = &mut *inner;
925 inner.unwrap_region_constraints().var_origin(vid)
926 }
927
928 pub fn get_region_var_infos(&self) -> VarInfos {
931 let inner = self.inner.borrow();
932 assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
933 let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved");
934 assert!(storage.data.is_empty(), "{:#?}", storage.data);
935 storage.var_infos.clone()
939 }
940
941 #[instrument(level = "debug", skip(self), ret)]
942 pub fn take_opaque_types(&self) -> Vec<(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)> {
943 self.inner.borrow_mut().opaque_type_storage.take_opaque_types().collect()
944 }
945
946 #[instrument(level = "debug", skip(self), ret)]
947 pub fn clone_opaque_types(&self) -> Vec<(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)> {
948 self.inner.borrow_mut().opaque_type_storage.iter_opaque_types().collect()
949 }
950
951 #[inline(always)]
952 pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
953 debug_assert!(!self.next_trait_solver());
954 match self.typing_mode() {
955 TypingMode::Analysis {
956 defining_opaque_types_and_generators: defining_opaque_types,
957 }
958 | TypingMode::Borrowck { defining_opaque_types } => {
959 id.into().as_local().is_some_and(|def_id| defining_opaque_types.contains(&def_id))
960 }
961 TypingMode::Coherence
965 | TypingMode::PostBorrowckAnalysis { .. }
966 | TypingMode::PostAnalysis => false,
967 }
968 }
969
970 pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
971 self.resolve_vars_if_possible(t).to_string()
972 }
973
974 pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
977 use self::type_variable::TypeVariableValue;
978
979 match self.inner.borrow_mut().type_variables().probe(vid) {
980 TypeVariableValue::Known { value } => Ok(value),
981 TypeVariableValue::Unknown { universe } => Err(universe),
982 }
983 }
984
985 pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
986 if let ty::Infer(v) = *ty.kind() {
987 match v {
988 ty::TyVar(v) => {
989 let known = self.inner.borrow_mut().type_variables().probe(v).known();
1002 known.map_or(ty, |t| self.shallow_resolve(t))
1003 }
1004
1005 ty::IntVar(v) => {
1006 match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1007 ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1008 ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1009 ty::IntVarValue::Unknown => ty,
1010 }
1011 }
1012
1013 ty::FloatVar(v) => {
1014 match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1015 ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1016 ty::FloatVarValue::Unknown => ty,
1017 }
1018 }
1019
1020 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
1021 }
1022 } else {
1023 ty
1024 }
1025 }
1026
1027 pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1028 match ct.kind() {
1029 ty::ConstKind::Infer(infer_ct) => match infer_ct {
1030 InferConst::Var(vid) => self
1031 .inner
1032 .borrow_mut()
1033 .const_unification_table()
1034 .probe_value(vid)
1035 .known()
1036 .unwrap_or(ct),
1037 InferConst::Fresh(_) => ct,
1038 },
1039 ty::ConstKind::Param(_)
1040 | ty::ConstKind::Bound(_, _)
1041 | ty::ConstKind::Placeholder(_)
1042 | ty::ConstKind::Unevaluated(_)
1043 | ty::ConstKind::Value(_)
1044 | ty::ConstKind::Error(_)
1045 | ty::ConstKind::Expr(_) => ct,
1046 }
1047 }
1048
1049 pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1050 self.inner.borrow_mut().type_variables().root_var(var)
1051 }
1052
1053 pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
1054 self.inner.borrow_mut().const_unification_table().find(var).vid
1055 }
1056
1057 pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
1060 let mut inner = self.inner.borrow_mut();
1061 let value = inner.int_unification_table().probe_value(vid);
1062 match value {
1063 ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1064 ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1065 ty::IntVarValue::Unknown => {
1066 Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1067 }
1068 }
1069 }
1070
1071 pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
1074 let mut inner = self.inner.borrow_mut();
1075 let value = inner.float_unification_table().probe_value(vid);
1076 match value {
1077 ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1078 ty::FloatVarValue::Unknown => {
1079 Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1080 }
1081 }
1082 }
1083
1084 pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
1091 where
1092 T: TypeFoldable<TyCtxt<'tcx>>,
1093 {
1094 if let Err(guar) = value.error_reported() {
1095 self.set_tainted_by_errors(guar);
1096 }
1097 if !value.has_non_region_infer() {
1098 return value;
1099 }
1100 let mut r = resolve::OpportunisticVarResolver::new(self);
1101 value.fold_with(&mut r)
1102 }
1103
1104 pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
1105 where
1106 T: TypeFoldable<TyCtxt<'tcx>>,
1107 {
1108 if !value.has_infer() {
1109 return value; }
1111 let mut r = InferenceLiteralEraser { tcx: self.tcx };
1112 value.fold_with(&mut r)
1113 }
1114
1115 pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1116 match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
1117 ConstVariableValue::Known { value } => Ok(value),
1118 ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
1119 }
1120 }
1121
1122 pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<T> {
1130 match resolve::fully_resolve(self, value) {
1131 Ok(value) => {
1132 if value.has_non_region_infer() {
1133 bug!("`{value:?}` is not fully resolved");
1134 }
1135 if value.has_infer_regions() {
1136 let guar = self.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
1137 Ok(fold_regions(self.tcx, value, |re, _| {
1138 if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }
1139 }))
1140 } else {
1141 Ok(value)
1142 }
1143 }
1144 Err(e) => Err(e),
1145 }
1146 }
1147
1148 pub fn instantiate_binder_with_fresh_vars<T>(
1156 &self,
1157 span: Span,
1158 lbrct: BoundRegionConversionTime,
1159 value: ty::Binder<'tcx, T>,
1160 ) -> T
1161 where
1162 T: TypeFoldable<TyCtxt<'tcx>> + Copy,
1163 {
1164 if let Some(inner) = value.no_bound_vars() {
1165 return inner;
1166 }
1167
1168 let bound_vars = value.bound_vars();
1169 let mut args = Vec::with_capacity(bound_vars.len());
1170
1171 for bound_var_kind in bound_vars {
1172 let arg: ty::GenericArg<'_> = match bound_var_kind {
1173 ty::BoundVariableKind::Ty(_) => self.next_ty_var(span).into(),
1174 ty::BoundVariableKind::Region(br) => {
1175 self.next_region_var(BoundRegion(span, br, lbrct)).into()
1176 }
1177 ty::BoundVariableKind::Const => self.next_const_var(span).into(),
1178 };
1179 args.push(arg);
1180 }
1181
1182 struct ToFreshVars<'tcx> {
1183 args: Vec<ty::GenericArg<'tcx>>,
1184 }
1185
1186 impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'tcx> {
1187 fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
1188 self.args[br.var.index()].expect_region()
1189 }
1190 fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
1191 self.args[bt.var.index()].expect_ty()
1192 }
1193 fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
1194 self.args[bv.index()].expect_const()
1195 }
1196 }
1197 let delegate = ToFreshVars { args };
1198 self.tcx.replace_bound_vars_uncached(value, delegate)
1199 }
1200
1201 pub(crate) fn verify_generic_bound(
1203 &self,
1204 origin: SubregionOrigin<'tcx>,
1205 kind: GenericKind<'tcx>,
1206 a: ty::Region<'tcx>,
1207 bound: VerifyBound<'tcx>,
1208 ) {
1209 debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1210
1211 self.inner
1212 .borrow_mut()
1213 .unwrap_region_constraints()
1214 .verify_generic_bound(origin, kind, a, bound);
1215 }
1216
1217 pub fn closure_kind(&self, closure_ty: Ty<'tcx>) -> Option<ty::ClosureKind> {
1221 let unresolved_kind_ty = match *closure_ty.kind() {
1222 ty::Closure(_, args) => args.as_closure().kind_ty(),
1223 ty::CoroutineClosure(_, args) => args.as_coroutine_closure().kind_ty(),
1224 _ => bug!("unexpected type {closure_ty}"),
1225 };
1226 let closure_kind_ty = self.shallow_resolve(unresolved_kind_ty);
1227 closure_kind_ty.to_opt_closure_kind()
1228 }
1229
1230 pub fn universe(&self) -> ty::UniverseIndex {
1231 self.universe.get()
1232 }
1233
1234 pub fn create_next_universe(&self) -> ty::UniverseIndex {
1237 let u = self.universe.get().next_universe();
1238 debug!("create_next_universe {u:?}");
1239 self.universe.set(u);
1240 u
1241 }
1242
1243 pub fn typing_env(&self, param_env: ty::ParamEnv<'tcx>) -> ty::TypingEnv<'tcx> {
1247 let typing_mode = match self.typing_mode() {
1248 ty::TypingMode::Analysis { defining_opaque_types_and_generators: _ }
1253 | ty::TypingMode::Borrowck { defining_opaque_types: _ } => {
1254 TypingMode::non_body_analysis()
1255 }
1256 mode @ (ty::TypingMode::Coherence
1257 | ty::TypingMode::PostBorrowckAnalysis { .. }
1258 | ty::TypingMode::PostAnalysis) => mode,
1259 };
1260 ty::TypingEnv { typing_mode, param_env }
1261 }
1262
1263 pub fn pseudo_canonicalize_query<V>(
1267 &self,
1268 param_env: ty::ParamEnv<'tcx>,
1269 value: V,
1270 ) -> PseudoCanonicalInput<'tcx, V>
1271 where
1272 V: TypeVisitable<TyCtxt<'tcx>>,
1273 {
1274 debug_assert!(!value.has_infer());
1275 debug_assert!(!value.has_placeholders());
1276 debug_assert!(!param_env.has_infer());
1277 debug_assert!(!param_env.has_placeholders());
1278 self.typing_env(param_env).as_query_input(value)
1279 }
1280
1281 #[inline]
1284 pub fn is_ty_infer_var_definitely_unchanged(&self) -> impl Fn(TyOrConstInferVar) -> bool {
1285 let inner = self.inner.try_borrow();
1287
1288 move |infer_var: TyOrConstInferVar| match (infer_var, &inner) {
1289 (TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
1290 use self::type_variable::TypeVariableValue;
1291
1292 matches!(
1293 inner.try_type_variables_probe_ref(ty_var),
1294 Some(TypeVariableValue::Unknown { .. })
1295 )
1296 }
1297 _ => false,
1298 }
1299 }
1300
1301 #[inline(always)]
1311 pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> bool {
1312 match infer_var {
1313 TyOrConstInferVar::Ty(v) => {
1314 use self::type_variable::TypeVariableValue;
1315
1316 match self.inner.borrow_mut().type_variables().inlined_probe(v) {
1319 TypeVariableValue::Unknown { .. } => false,
1320 TypeVariableValue::Known { .. } => true,
1321 }
1322 }
1323
1324 TyOrConstInferVar::TyInt(v) => {
1325 self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_known()
1329 }
1330
1331 TyOrConstInferVar::TyFloat(v) => {
1332 self.inner.borrow_mut().float_unification_table().probe_value(v).is_known()
1337 }
1338
1339 TyOrConstInferVar::Const(v) => {
1340 match self.inner.borrow_mut().const_unification_table().probe_value(v) {
1345 ConstVariableValue::Unknown { .. } => false,
1346 ConstVariableValue::Known { .. } => true,
1347 }
1348 }
1349 }
1350 }
1351
1352 pub fn attach_obligation_inspector(&self, inspector: ObligationInspector<'tcx>) {
1354 debug_assert!(
1355 self.obligation_inspector.get().is_none(),
1356 "shouldn't override a set obligation inspector"
1357 );
1358 self.obligation_inspector.set(Some(inspector));
1359 }
1360}
1361
1362#[derive(Copy, Clone, Debug)]
1365pub enum TyOrConstInferVar {
1366 Ty(TyVid),
1368 TyInt(IntVid),
1370 TyFloat(FloatVid),
1372
1373 Const(ConstVid),
1375}
1376
1377impl<'tcx> TyOrConstInferVar {
1378 pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1382 match arg.kind() {
1383 GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1384 GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1385 GenericArgKind::Lifetime(_) => None,
1386 }
1387 }
1388
1389 pub fn maybe_from_term(term: Term<'tcx>) -> Option<Self> {
1393 match term.kind() {
1394 TermKind::Ty(ty) => Self::maybe_from_ty(ty),
1395 TermKind::Const(ct) => Self::maybe_from_const(ct),
1396 }
1397 }
1398
1399 fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1402 match *ty.kind() {
1403 ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1404 ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1405 ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1406 _ => None,
1407 }
1408 }
1409
1410 fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
1413 match ct.kind() {
1414 ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1415 _ => None,
1416 }
1417 }
1418}
1419
1420struct InferenceLiteralEraser<'tcx> {
1423 tcx: TyCtxt<'tcx>,
1424}
1425
1426impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
1427 fn cx(&self) -> TyCtxt<'tcx> {
1428 self.tcx
1429 }
1430
1431 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1432 match ty.kind() {
1433 ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1434 ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1435 _ => ty.super_fold_with(self),
1436 }
1437 }
1438}
1439
1440impl<'tcx> TypeTrace<'tcx> {
1441 pub fn span(&self) -> Span {
1442 self.cause.span
1443 }
1444
1445 pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
1446 TypeTrace {
1447 cause: cause.clone(),
1448 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1449 }
1450 }
1451
1452 pub fn trait_refs(
1453 cause: &ObligationCause<'tcx>,
1454 a: ty::TraitRef<'tcx>,
1455 b: ty::TraitRef<'tcx>,
1456 ) -> TypeTrace<'tcx> {
1457 TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
1458 }
1459
1460 pub fn consts(
1461 cause: &ObligationCause<'tcx>,
1462 a: ty::Const<'tcx>,
1463 b: ty::Const<'tcx>,
1464 ) -> TypeTrace<'tcx> {
1465 TypeTrace {
1466 cause: cause.clone(),
1467 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1468 }
1469 }
1470}
1471
1472impl<'tcx> SubregionOrigin<'tcx> {
1473 pub fn span(&self) -> Span {
1474 match *self {
1475 Subtype(ref a) => a.span(),
1476 RelateObjectBound(a) => a,
1477 RelateParamBound(a, ..) => a,
1478 RelateRegionParamBound(a, _) => a,
1479 Reborrow(a) => a,
1480 ReferenceOutlivesReferent(_, a) => a,
1481 CompareImplItemObligation { span, .. } => span,
1482 AscribeUserTypeProvePredicate(span) => span,
1483 CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
1484 }
1485 }
1486
1487 pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1488 where
1489 F: FnOnce() -> Self,
1490 {
1491 match *cause.code() {
1492 traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1493 SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1494 }
1495
1496 traits::ObligationCauseCode::CompareImplItem {
1497 impl_item_def_id,
1498 trait_item_def_id,
1499 kind: _,
1500 } => SubregionOrigin::CompareImplItemObligation {
1501 span: cause.span,
1502 impl_item_def_id,
1503 trait_item_def_id,
1504 },
1505
1506 traits::ObligationCauseCode::CheckAssociatedTypeBounds {
1507 impl_item_def_id,
1508 trait_item_def_id,
1509 } => SubregionOrigin::CheckAssociatedTypeBounds {
1510 impl_item_def_id,
1511 trait_item_def_id,
1512 parent: Box::new(default()),
1513 },
1514
1515 traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
1516 SubregionOrigin::AscribeUserTypeProvePredicate(span)
1517 }
1518
1519 traits::ObligationCauseCode::ObjectTypeBound(ty, _reg) => {
1520 SubregionOrigin::RelateRegionParamBound(cause.span, Some(ty))
1521 }
1522
1523 _ => default(),
1524 }
1525 }
1526}
1527
1528impl RegionVariableOrigin {
1529 pub fn span(&self) -> Span {
1530 match *self {
1531 MiscVariable(a)
1532 | PatternRegion(a)
1533 | BorrowRegion(a)
1534 | Autoref(a)
1535 | Coercion(a)
1536 | RegionParameterDefinition(a, ..)
1537 | BoundRegion(a, ..)
1538 | UpvarRegion(_, a) => a,
1539 Nll(..) => bug!("NLL variable used with `span`"),
1540 }
1541 }
1542}
1543
1544impl<'tcx> InferCtxt<'tcx> {
1545 pub fn find_block_span(&self, block: &'tcx hir::Block<'tcx>) -> Span {
1548 let block = block.innermost_block();
1549 if let Some(expr) = &block.expr {
1550 expr.span
1551 } else if let Some(stmt) = block.stmts.last() {
1552 stmt.span
1554 } else {
1555 block.span
1557 }
1558 }
1559
1560 pub fn find_block_span_from_hir_id(&self, hir_id: hir::HirId) -> Span {
1563 match self.tcx.hir_node(hir_id) {
1564 hir::Node::Block(blk) => self.find_block_span(blk),
1565 hir::Node::Expr(e) => e.span,
1568 _ => rustc_span::DUMMY_SP,
1569 }
1570 }
1571}