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;
12use opaque_types::OpaqueTypeStorage;
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, PseudoCanonicalInput, Ty, TyCtxt,
35 TyVid, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv,
36 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<RegionObligation<'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) -> &[RegionObligation<'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 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 pub fn iter_opaque_types(
232 &self,
233 ) -> impl Iterator<Item = (ty::OpaqueTypeKey<'tcx>, ty::OpaqueHiddenType<'tcx>)> {
234 self.opaque_type_storage.opaque_types.iter().map(|(&k, &v)| (k, v))
235 }
236}
237
238pub struct InferCtxt<'tcx> {
239 pub tcx: TyCtxt<'tcx>,
240
241 typing_mode: TypingMode<'tcx>,
244
245 pub considering_regions: bool,
249
250 skip_leak_check: bool,
255
256 pub inner: RefCell<InferCtxtInner<'tcx>>,
257
258 lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
260
261 pub selection_cache: select::SelectionCache<'tcx, ty::ParamEnv<'tcx>>,
264
265 pub evaluation_cache: select::EvaluationCache<'tcx, ty::ParamEnv<'tcx>>,
268
269 pub reported_trait_errors:
272 RefCell<FxIndexMap<Span, (Vec<Goal<'tcx, ty::Predicate<'tcx>>>, ErrorGuaranteed)>>,
273
274 pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
275
276 tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
284
285 universe: Cell<ty::UniverseIndex>,
295
296 next_trait_solver: bool,
297
298 pub obligation_inspector: Cell<Option<ObligationInspector<'tcx>>>,
299}
300
301#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable, TypeVisitable)]
303pub enum ValuePairs<'tcx> {
304 Regions(ExpectedFound<ty::Region<'tcx>>),
305 Terms(ExpectedFound<ty::Term<'tcx>>),
306 Aliases(ExpectedFound<ty::AliasTerm<'tcx>>),
307 TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
308 PolySigs(ExpectedFound<ty::PolyFnSig<'tcx>>),
309 ExistentialTraitRef(ExpectedFound<ty::PolyExistentialTraitRef<'tcx>>),
310 ExistentialProjection(ExpectedFound<ty::PolyExistentialProjection<'tcx>>),
311}
312
313impl<'tcx> ValuePairs<'tcx> {
314 pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
315 if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
316 && let Some(expected) = expected.as_type()
317 && let Some(found) = found.as_type()
318 {
319 Some((expected, found))
320 } else {
321 None
322 }
323 }
324}
325
326#[derive(Clone, Debug)]
331pub struct TypeTrace<'tcx> {
332 pub cause: ObligationCause<'tcx>,
333 pub values: ValuePairs<'tcx>,
334}
335
336#[derive(Clone, Debug)]
340pub enum SubregionOrigin<'tcx> {
341 Subtype(Box<TypeTrace<'tcx>>),
343
344 RelateObjectBound(Span),
347
348 RelateParamBound(Span, Ty<'tcx>, Option<Span>),
351
352 RelateRegionParamBound(Span, Option<Ty<'tcx>>),
355
356 Reborrow(Span),
358
359 ReferenceOutlivesReferent(Ty<'tcx>, Span),
361
362 CompareImplItemObligation {
365 span: Span,
366 impl_item_def_id: LocalDefId,
367 trait_item_def_id: DefId,
368 },
369
370 CheckAssociatedTypeBounds {
372 parent: Box<SubregionOrigin<'tcx>>,
373 impl_item_def_id: LocalDefId,
374 trait_item_def_id: DefId,
375 },
376
377 AscribeUserTypeProvePredicate(Span),
378}
379
380#[cfg(target_pointer_width = "64")]
382rustc_data_structures::static_assert_size!(SubregionOrigin<'_>, 32);
383
384impl<'tcx> SubregionOrigin<'tcx> {
385 pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> {
386 match self {
387 Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(),
388 Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span),
389 _ => ConstraintCategory::BoringNoLocation,
390 }
391 }
392}
393
394#[derive(Clone, Copy, Debug)]
396pub enum BoundRegionConversionTime {
397 FnCall,
399
400 HigherRankedType,
402
403 AssocTypeProjection(DefId),
405}
406
407#[derive(Copy, Clone, Debug)]
411pub enum RegionVariableOrigin {
412 MiscVariable(Span),
416
417 PatternRegion(Span),
419
420 BorrowRegion(Span),
422
423 Autoref(Span),
425
426 Coercion(Span),
428
429 RegionParameterDefinition(Span, Symbol),
434
435 BoundRegion(Span, ty::BoundRegionKind, BoundRegionConversionTime),
438
439 UpvarRegion(ty::UpvarId, Span),
440
441 Nll(NllRegionVariableOrigin),
444}
445
446#[derive(Copy, Clone, Debug)]
447pub enum NllRegionVariableOrigin {
448 FreeRegion,
452
453 Placeholder(ty::PlaceholderRegion),
456
457 Existential {
458 from_forall: bool,
469 },
470}
471
472#[derive(Copy, Clone, Debug)]
473pub struct FixupError {
474 unresolved: TyOrConstInferVar,
475}
476
477impl fmt::Display for FixupError {
478 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
479 use TyOrConstInferVar::*;
480
481 match self.unresolved {
482 TyInt(_) => write!(
483 f,
484 "cannot determine the type of this integer; \
485 add a suffix to specify the type explicitly"
486 ),
487 TyFloat(_) => write!(
488 f,
489 "cannot determine the type of this number; \
490 add a suffix to specify the type explicitly"
491 ),
492 Ty(_) => write!(f, "unconstrained type"),
493 Const(_) => write!(f, "unconstrained const value"),
494 }
495 }
496}
497
498#[derive(Clone, Debug)]
500pub struct RegionObligation<'tcx> {
501 pub sub_region: ty::Region<'tcx>,
502 pub sup_type: Ty<'tcx>,
503 pub origin: SubregionOrigin<'tcx>,
504}
505
506pub struct InferCtxtBuilder<'tcx> {
508 tcx: TyCtxt<'tcx>,
509 considering_regions: bool,
510 skip_leak_check: bool,
511 next_trait_solver: bool,
514}
515
516#[extension(pub trait TyCtxtInferExt<'tcx>)]
517impl<'tcx> TyCtxt<'tcx> {
518 fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
519 InferCtxtBuilder {
520 tcx: self,
521 considering_regions: true,
522 skip_leak_check: false,
523 next_trait_solver: self.next_trait_solver_globally(),
524 }
525 }
526}
527
528impl<'tcx> InferCtxtBuilder<'tcx> {
529 pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
530 self.next_trait_solver = next_trait_solver;
531 self
532 }
533
534 pub fn ignoring_regions(mut self) -> Self {
535 self.considering_regions = false;
536 self
537 }
538
539 pub fn skip_leak_check(mut self, skip_leak_check: bool) -> Self {
540 self.skip_leak_check = skip_leak_check;
541 self
542 }
543
544 pub fn build_with_canonical<T>(
552 mut self,
553 span: Span,
554 input: &CanonicalQueryInput<'tcx, T>,
555 ) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
556 where
557 T: TypeFoldable<TyCtxt<'tcx>>,
558 {
559 let infcx = self.build(input.typing_mode);
560 let (value, args) = infcx.instantiate_canonical(span, &input.canonical);
561 (infcx, value, args)
562 }
563
564 pub fn build_with_typing_env(
565 mut self,
566 TypingEnv { typing_mode, param_env }: TypingEnv<'tcx>,
567 ) -> (InferCtxt<'tcx>, ty::ParamEnv<'tcx>) {
568 (self.build(typing_mode), param_env)
569 }
570
571 pub fn build(&mut self, typing_mode: TypingMode<'tcx>) -> InferCtxt<'tcx> {
572 let InferCtxtBuilder { tcx, considering_regions, skip_leak_check, next_trait_solver } =
573 *self;
574 InferCtxt {
575 tcx,
576 typing_mode,
577 considering_regions,
578 skip_leak_check,
579 inner: RefCell::new(InferCtxtInner::new()),
580 lexical_region_resolutions: RefCell::new(None),
581 selection_cache: Default::default(),
582 evaluation_cache: Default::default(),
583 reported_trait_errors: Default::default(),
584 reported_signature_mismatch: Default::default(),
585 tainted_by_errors: Cell::new(None),
586 universe: Cell::new(ty::UniverseIndex::ROOT),
587 next_trait_solver,
588 obligation_inspector: Cell::new(None),
589 }
590 }
591}
592
593impl<'tcx, T> InferOk<'tcx, T> {
594 pub fn into_value_registering_obligations<E: 'tcx>(
596 self,
597 infcx: &InferCtxt<'tcx>,
598 fulfill_cx: &mut dyn TraitEngine<'tcx, E>,
599 ) -> T {
600 let InferOk { value, obligations } = self;
601 fulfill_cx.register_predicate_obligations(infcx, obligations);
602 value
603 }
604}
605
606impl<'tcx> InferOk<'tcx, ()> {
607 pub fn into_obligations(self) -> PredicateObligations<'tcx> {
608 self.obligations
609 }
610}
611
612impl<'tcx> InferCtxt<'tcx> {
613 pub fn dcx(&self) -> DiagCtxtHandle<'_> {
614 self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
615 }
616
617 pub fn next_trait_solver(&self) -> bool {
618 self.next_trait_solver
619 }
620
621 #[inline(always)]
622 pub fn typing_mode(&self) -> TypingMode<'tcx> {
623 self.typing_mode
624 }
625
626 pub fn freshen<T: TypeFoldable<TyCtxt<'tcx>>>(&self, t: T) -> T {
627 t.fold_with(&mut self.freshener())
628 }
629
630 pub fn type_var_origin(&self, vid: TyVid) -> TypeVariableOrigin {
634 self.inner.borrow_mut().type_variables().var_origin(vid)
635 }
636
637 pub fn const_var_origin(&self, vid: ConstVid) -> Option<ConstVariableOrigin> {
641 match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
642 ConstVariableValue::Known { .. } => None,
643 ConstVariableValue::Unknown { origin, .. } => Some(origin),
644 }
645 }
646
647 pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
648 freshen::TypeFreshener::new(self)
649 }
650
651 pub fn unresolved_variables(&self) -> Vec<Ty<'tcx>> {
652 let mut inner = self.inner.borrow_mut();
653 let mut vars: Vec<Ty<'_>> = inner
654 .type_variables()
655 .unresolved_variables()
656 .into_iter()
657 .map(|t| Ty::new_var(self.tcx, t))
658 .collect();
659 vars.extend(
660 (0..inner.int_unification_table().len())
661 .map(|i| ty::IntVid::from_usize(i))
662 .filter(|&vid| inner.int_unification_table().probe_value(vid).is_unknown())
663 .map(|v| Ty::new_int_var(self.tcx, v)),
664 );
665 vars.extend(
666 (0..inner.float_unification_table().len())
667 .map(|i| ty::FloatVid::from_usize(i))
668 .filter(|&vid| inner.float_unification_table().probe_value(vid).is_unknown())
669 .map(|v| Ty::new_float_var(self.tcx, v)),
670 );
671 vars
672 }
673
674 #[instrument(skip(self), level = "debug")]
675 pub fn sub_regions(
676 &self,
677 origin: SubregionOrigin<'tcx>,
678 a: ty::Region<'tcx>,
679 b: ty::Region<'tcx>,
680 ) {
681 self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
682 }
683
684 pub fn coerce_predicate(
700 &self,
701 cause: &ObligationCause<'tcx>,
702 param_env: ty::ParamEnv<'tcx>,
703 predicate: ty::PolyCoercePredicate<'tcx>,
704 ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
705 let subtype_predicate = predicate.map_bound(|p| ty::SubtypePredicate {
706 a_is_expected: false, a: p.a,
708 b: p.b,
709 });
710 self.subtype_predicate(cause, param_env, subtype_predicate)
711 }
712
713 pub fn subtype_predicate(
714 &self,
715 cause: &ObligationCause<'tcx>,
716 param_env: ty::ParamEnv<'tcx>,
717 predicate: ty::PolySubtypePredicate<'tcx>,
718 ) -> Result<InferResult<'tcx, ()>, (TyVid, TyVid)> {
719 let r_a = self.shallow_resolve(predicate.skip_binder().a);
733 let r_b = self.shallow_resolve(predicate.skip_binder().b);
734 match (r_a.kind(), r_b.kind()) {
735 (&ty::Infer(ty::TyVar(a_vid)), &ty::Infer(ty::TyVar(b_vid))) => {
736 return Err((a_vid, b_vid));
737 }
738 _ => {}
739 }
740
741 self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
742 if a_is_expected {
743 Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::Yes, a, b))
744 } else {
745 Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::Yes, b, a))
746 }
747 })
748 }
749
750 pub fn region_outlives_predicate(
751 &self,
752 cause: &traits::ObligationCause<'tcx>,
753 predicate: ty::PolyRegionOutlivesPredicate<'tcx>,
754 ) {
755 self.enter_forall(predicate, |ty::OutlivesPredicate(r_a, r_b)| {
756 let origin = SubregionOrigin::from_obligation_cause(cause, || {
757 RelateRegionParamBound(cause.span, None)
758 });
759 self.sub_regions(origin, r_b, r_a); })
761 }
762
763 pub fn num_ty_vars(&self) -> usize {
765 self.inner.borrow_mut().type_variables().num_vars()
766 }
767
768 pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
769 self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
770 }
771
772 pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
773 let vid = self.inner.borrow_mut().type_variables().new_var(self.universe(), origin);
774 Ty::new_var(self.tcx, vid)
775 }
776
777 pub fn next_ty_var_id_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
778 let origin = TypeVariableOrigin { span, param_def_id: None };
779 self.inner.borrow_mut().type_variables().new_var(universe, origin)
780 }
781
782 pub fn next_ty_var_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> Ty<'tcx> {
783 let vid = self.next_ty_var_id_in_universe(span, universe);
784 Ty::new_var(self.tcx, vid)
785 }
786
787 pub fn next_const_var(&self, span: Span) -> ty::Const<'tcx> {
788 self.next_const_var_with_origin(ConstVariableOrigin { span, param_def_id: None })
789 }
790
791 pub fn next_const_var_with_origin(&self, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
792 let vid = self
793 .inner
794 .borrow_mut()
795 .const_unification_table()
796 .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
797 .vid;
798 ty::Const::new_var(self.tcx, vid)
799 }
800
801 pub fn next_const_var_in_universe(
802 &self,
803 span: Span,
804 universe: ty::UniverseIndex,
805 ) -> ty::Const<'tcx> {
806 let origin = ConstVariableOrigin { span, param_def_id: None };
807 let vid = self
808 .inner
809 .borrow_mut()
810 .const_unification_table()
811 .new_key(ConstVariableValue::Unknown { origin, universe })
812 .vid;
813 ty::Const::new_var(self.tcx, vid)
814 }
815
816 pub fn next_int_var(&self) -> Ty<'tcx> {
817 let next_int_var_id =
818 self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown);
819 Ty::new_int_var(self.tcx, next_int_var_id)
820 }
821
822 pub fn next_float_var(&self) -> Ty<'tcx> {
823 let next_float_var_id =
824 self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown);
825 Ty::new_float_var(self.tcx, next_float_var_id)
826 }
827
828 pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
832 self.next_region_var_in_universe(origin, self.universe())
833 }
834
835 pub fn next_region_var_in_universe(
839 &self,
840 origin: RegionVariableOrigin,
841 universe: ty::UniverseIndex,
842 ) -> ty::Region<'tcx> {
843 let region_var =
844 self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
845 ty::Region::new_var(self.tcx, region_var)
846 }
847
848 pub fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
854 self.inner.borrow_mut().unwrap_region_constraints().universe(r)
855 }
856
857 pub fn num_region_vars(&self) -> usize {
859 self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
860 }
861
862 #[instrument(skip(self), level = "debug")]
864 pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> {
865 self.next_region_var(RegionVariableOrigin::Nll(origin))
866 }
867
868 #[instrument(skip(self), level = "debug")]
870 pub fn next_nll_region_var_in_universe(
871 &self,
872 origin: NllRegionVariableOrigin,
873 universe: ty::UniverseIndex,
874 ) -> ty::Region<'tcx> {
875 self.next_region_var_in_universe(RegionVariableOrigin::Nll(origin), universe)
876 }
877
878 pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
879 match param.kind {
880 GenericParamDefKind::Lifetime => {
881 self.next_region_var(RegionParameterDefinition(span, param.name)).into()
884 }
885 GenericParamDefKind::Type { .. } => {
886 let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
895 self.universe(),
896 TypeVariableOrigin { param_def_id: Some(param.def_id), span },
897 );
898
899 Ty::new_var(self.tcx, ty_var_id).into()
900 }
901 GenericParamDefKind::Const { .. } => {
902 let origin = ConstVariableOrigin { param_def_id: Some(param.def_id), span };
903 let const_var_id = self
904 .inner
905 .borrow_mut()
906 .const_unification_table()
907 .new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
908 .vid;
909 ty::Const::new_var(self.tcx, const_var_id).into()
910 }
911 }
912 }
913
914 pub fn fresh_args_for_item(&self, span: Span, def_id: DefId) -> GenericArgsRef<'tcx> {
917 GenericArgs::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
918 }
919
920 #[must_use = "this method does not have any side effects"]
926 pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
927 self.tainted_by_errors.get()
928 }
929
930 pub fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
933 debug!("set_tainted_by_errors(ErrorGuaranteed)");
934 self.tainted_by_errors.set(Some(e));
935 }
936
937 pub fn region_var_origin(&self, vid: ty::RegionVid) -> RegionVariableOrigin {
938 let mut inner = self.inner.borrow_mut();
939 let inner = &mut *inner;
940 inner.unwrap_region_constraints().var_origin(vid)
941 }
942
943 pub fn get_region_var_infos(&self) -> VarInfos {
946 let inner = self.inner.borrow();
947 assert!(!UndoLogs::<UndoLog<'_>>::in_snapshot(&inner.undo_log));
948 let storage = inner.region_constraint_storage.as_ref().expect("regions already resolved");
949 assert!(storage.data.is_empty(), "{:#?}", storage.data);
950 storage.var_infos.clone()
954 }
955
956 #[instrument(level = "debug", skip(self), ret)]
957 pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
958 std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
959 }
960
961 #[instrument(level = "debug", skip(self), ret)]
962 pub fn clone_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
963 self.inner.borrow().opaque_type_storage.opaque_types.clone()
964 }
965
966 #[inline(always)]
967 pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
968 debug_assert!(!self.next_trait_solver());
969 match self.typing_mode() {
970 TypingMode::Analysis { defining_opaque_types }
971 | TypingMode::Borrowck { defining_opaque_types } => {
972 id.into().as_local().is_some_and(|def_id| defining_opaque_types.contains(&def_id))
973 }
974 TypingMode::Coherence
978 | TypingMode::PostBorrowckAnalysis { .. }
979 | TypingMode::PostAnalysis => false,
980 }
981 }
982
983 pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
984 self.resolve_vars_if_possible(t).to_string()
985 }
986
987 pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
990 use self::type_variable::TypeVariableValue;
991
992 match self.inner.borrow_mut().type_variables().probe(vid) {
993 TypeVariableValue::Known { value } => Ok(value),
994 TypeVariableValue::Unknown { universe } => Err(universe),
995 }
996 }
997
998 pub fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
999 if let ty::Infer(v) = *ty.kind() {
1000 match v {
1001 ty::TyVar(v) => {
1002 let known = self.inner.borrow_mut().type_variables().probe(v).known();
1015 known.map_or(ty, |t| self.shallow_resolve(t))
1016 }
1017
1018 ty::IntVar(v) => {
1019 match self.inner.borrow_mut().int_unification_table().probe_value(v) {
1020 ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1021 ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1022 ty::IntVarValue::Unknown => ty,
1023 }
1024 }
1025
1026 ty::FloatVar(v) => {
1027 match self.inner.borrow_mut().float_unification_table().probe_value(v) {
1028 ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1029 ty::FloatVarValue::Unknown => ty,
1030 }
1031 }
1032
1033 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => ty,
1034 }
1035 } else {
1036 ty
1037 }
1038 }
1039
1040 pub fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
1041 match ct.kind() {
1042 ty::ConstKind::Infer(infer_ct) => match infer_ct {
1043 InferConst::Var(vid) => self
1044 .inner
1045 .borrow_mut()
1046 .const_unification_table()
1047 .probe_value(vid)
1048 .known()
1049 .unwrap_or(ct),
1050 InferConst::Fresh(_) => ct,
1051 },
1052 ty::ConstKind::Param(_)
1053 | ty::ConstKind::Bound(_, _)
1054 | ty::ConstKind::Placeholder(_)
1055 | ty::ConstKind::Unevaluated(_)
1056 | ty::ConstKind::Value(_)
1057 | ty::ConstKind::Error(_)
1058 | ty::ConstKind::Expr(_) => ct,
1059 }
1060 }
1061
1062 pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1063 self.inner.borrow_mut().type_variables().root_var(var)
1064 }
1065
1066 pub fn root_const_var(&self, var: ty::ConstVid) -> ty::ConstVid {
1067 self.inner.borrow_mut().const_unification_table().find(var).vid
1068 }
1069
1070 pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {
1073 let mut inner = self.inner.borrow_mut();
1074 let value = inner.int_unification_table().probe_value(vid);
1075 match value {
1076 ty::IntVarValue::IntType(ty) => Ty::new_int(self.tcx, ty),
1077 ty::IntVarValue::UintType(ty) => Ty::new_uint(self.tcx, ty),
1078 ty::IntVarValue::Unknown => {
1079 Ty::new_int_var(self.tcx, inner.int_unification_table().find(vid))
1080 }
1081 }
1082 }
1083
1084 pub fn opportunistic_resolve_float_var(&self, vid: ty::FloatVid) -> Ty<'tcx> {
1087 let mut inner = self.inner.borrow_mut();
1088 let value = inner.float_unification_table().probe_value(vid);
1089 match value {
1090 ty::FloatVarValue::Known(ty) => Ty::new_float(self.tcx, ty),
1091 ty::FloatVarValue::Unknown => {
1092 Ty::new_float_var(self.tcx, inner.float_unification_table().find(vid))
1093 }
1094 }
1095 }
1096
1097 pub fn resolve_vars_if_possible<T>(&self, value: T) -> T
1104 where
1105 T: TypeFoldable<TyCtxt<'tcx>>,
1106 {
1107 if let Err(guar) = value.error_reported() {
1108 self.set_tainted_by_errors(guar);
1109 }
1110 if !value.has_non_region_infer() {
1111 return value;
1112 }
1113 let mut r = resolve::OpportunisticVarResolver::new(self);
1114 value.fold_with(&mut r)
1115 }
1116
1117 pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
1118 where
1119 T: TypeFoldable<TyCtxt<'tcx>>,
1120 {
1121 if !value.has_infer() {
1122 return value; }
1124 let mut r = InferenceLiteralEraser { tcx: self.tcx };
1125 value.fold_with(&mut r)
1126 }
1127
1128 pub fn probe_const_var(&self, vid: ty::ConstVid) -> Result<ty::Const<'tcx>, ty::UniverseIndex> {
1129 match self.inner.borrow_mut().const_unification_table().probe_value(vid) {
1130 ConstVariableValue::Known { value } => Ok(value),
1131 ConstVariableValue::Unknown { origin: _, universe } => Err(universe),
1132 }
1133 }
1134
1135 pub fn fully_resolve<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> FixupResult<T> {
1143 match resolve::fully_resolve(self, value) {
1144 Ok(value) => {
1145 if value.has_non_region_infer() {
1146 bug!("`{value:?}` is not fully resolved");
1147 }
1148 if value.has_infer_regions() {
1149 let guar = self.dcx().delayed_bug(format!("`{value:?}` is not fully resolved"));
1150 Ok(fold_regions(self.tcx, value, |re, _| {
1151 if re.is_var() { ty::Region::new_error(self.tcx, guar) } else { re }
1152 }))
1153 } else {
1154 Ok(value)
1155 }
1156 }
1157 Err(e) => Err(e),
1158 }
1159 }
1160
1161 pub fn instantiate_binder_with_fresh_vars<T>(
1169 &self,
1170 span: Span,
1171 lbrct: BoundRegionConversionTime,
1172 value: ty::Binder<'tcx, T>,
1173 ) -> T
1174 where
1175 T: TypeFoldable<TyCtxt<'tcx>> + Copy,
1176 {
1177 if let Some(inner) = value.no_bound_vars() {
1178 return inner;
1179 }
1180
1181 let bound_vars = value.bound_vars();
1182 let mut args = Vec::with_capacity(bound_vars.len());
1183
1184 for bound_var_kind in bound_vars {
1185 let arg: ty::GenericArg<'_> = match bound_var_kind {
1186 ty::BoundVariableKind::Ty(_) => self.next_ty_var(span).into(),
1187 ty::BoundVariableKind::Region(br) => {
1188 self.next_region_var(BoundRegion(span, br, lbrct)).into()
1189 }
1190 ty::BoundVariableKind::Const => self.next_const_var(span).into(),
1191 };
1192 args.push(arg);
1193 }
1194
1195 struct ToFreshVars<'tcx> {
1196 args: Vec<ty::GenericArg<'tcx>>,
1197 }
1198
1199 impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'tcx> {
1200 fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
1201 self.args[br.var.index()].expect_region()
1202 }
1203 fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
1204 self.args[bt.var.index()].expect_ty()
1205 }
1206 fn replace_const(&mut self, bv: ty::BoundVar) -> ty::Const<'tcx> {
1207 self.args[bv.index()].expect_const()
1208 }
1209 }
1210 let delegate = ToFreshVars { args };
1211 self.tcx.replace_bound_vars_uncached(value, delegate)
1212 }
1213
1214 pub(crate) fn verify_generic_bound(
1216 &self,
1217 origin: SubregionOrigin<'tcx>,
1218 kind: GenericKind<'tcx>,
1219 a: ty::Region<'tcx>,
1220 bound: VerifyBound<'tcx>,
1221 ) {
1222 debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1223
1224 self.inner
1225 .borrow_mut()
1226 .unwrap_region_constraints()
1227 .verify_generic_bound(origin, kind, a, bound);
1228 }
1229
1230 pub fn closure_kind(&self, closure_ty: Ty<'tcx>) -> Option<ty::ClosureKind> {
1234 let unresolved_kind_ty = match *closure_ty.kind() {
1235 ty::Closure(_, args) => args.as_closure().kind_ty(),
1236 ty::CoroutineClosure(_, args) => args.as_coroutine_closure().kind_ty(),
1237 _ => bug!("unexpected type {closure_ty}"),
1238 };
1239 let closure_kind_ty = self.shallow_resolve(unresolved_kind_ty);
1240 closure_kind_ty.to_opt_closure_kind()
1241 }
1242
1243 pub fn universe(&self) -> ty::UniverseIndex {
1244 self.universe.get()
1245 }
1246
1247 pub fn create_next_universe(&self) -> ty::UniverseIndex {
1250 let u = self.universe.get().next_universe();
1251 debug!("create_next_universe {u:?}");
1252 self.universe.set(u);
1253 u
1254 }
1255
1256 pub fn typing_env(&self, param_env: ty::ParamEnv<'tcx>) -> ty::TypingEnv<'tcx> {
1260 let typing_mode = match self.typing_mode() {
1261 ty::TypingMode::Analysis { defining_opaque_types: _ }
1266 | ty::TypingMode::Borrowck { defining_opaque_types: _ } => {
1267 TypingMode::non_body_analysis()
1268 }
1269 mode @ (ty::TypingMode::Coherence
1270 | ty::TypingMode::PostBorrowckAnalysis { .. }
1271 | ty::TypingMode::PostAnalysis) => mode,
1272 };
1273 ty::TypingEnv { typing_mode, param_env }
1274 }
1275
1276 pub fn pseudo_canonicalize_query<V>(
1280 &self,
1281 param_env: ty::ParamEnv<'tcx>,
1282 value: V,
1283 ) -> PseudoCanonicalInput<'tcx, V>
1284 where
1285 V: TypeVisitable<TyCtxt<'tcx>>,
1286 {
1287 debug_assert!(!value.has_infer());
1288 debug_assert!(!value.has_placeholders());
1289 debug_assert!(!param_env.has_infer());
1290 debug_assert!(!param_env.has_placeholders());
1291 self.typing_env(param_env).as_query_input(value)
1292 }
1293
1294 #[inline]
1297 pub fn is_ty_infer_var_definitely_unchanged(&self) -> impl Fn(TyOrConstInferVar) -> bool {
1298 let inner = self.inner.try_borrow();
1300
1301 move |infer_var: TyOrConstInferVar| match (infer_var, &inner) {
1302 (TyOrConstInferVar::Ty(ty_var), Ok(inner)) => {
1303 use self::type_variable::TypeVariableValue;
1304
1305 matches!(
1306 inner.try_type_variables_probe_ref(ty_var),
1307 Some(TypeVariableValue::Unknown { .. })
1308 )
1309 }
1310 _ => false,
1311 }
1312 }
1313
1314 #[inline(always)]
1324 pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar) -> bool {
1325 match infer_var {
1326 TyOrConstInferVar::Ty(v) => {
1327 use self::type_variable::TypeVariableValue;
1328
1329 match self.inner.borrow_mut().type_variables().inlined_probe(v) {
1332 TypeVariableValue::Unknown { .. } => false,
1333 TypeVariableValue::Known { .. } => true,
1334 }
1335 }
1336
1337 TyOrConstInferVar::TyInt(v) => {
1338 self.inner.borrow_mut().int_unification_table().inlined_probe_value(v).is_known()
1342 }
1343
1344 TyOrConstInferVar::TyFloat(v) => {
1345 self.inner.borrow_mut().float_unification_table().probe_value(v).is_known()
1350 }
1351
1352 TyOrConstInferVar::Const(v) => {
1353 match self.inner.borrow_mut().const_unification_table().probe_value(v) {
1358 ConstVariableValue::Unknown { .. } => false,
1359 ConstVariableValue::Known { .. } => true,
1360 }
1361 }
1362 }
1363 }
1364
1365 pub fn attach_obligation_inspector(&self, inspector: ObligationInspector<'tcx>) {
1367 debug_assert!(
1368 self.obligation_inspector.get().is_none(),
1369 "shouldn't override a set obligation inspector"
1370 );
1371 self.obligation_inspector.set(Some(inspector));
1372 }
1373}
1374
1375#[derive(Copy, Clone, Debug)]
1378pub enum TyOrConstInferVar {
1379 Ty(TyVid),
1381 TyInt(IntVid),
1383 TyFloat(FloatVid),
1385
1386 Const(ConstVid),
1388}
1389
1390impl<'tcx> TyOrConstInferVar {
1391 pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1395 match arg.unpack() {
1396 GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1397 GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1398 GenericArgKind::Lifetime(_) => None,
1399 }
1400 }
1401
1402 fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1405 match *ty.kind() {
1406 ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1407 ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1408 ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1409 _ => None,
1410 }
1411 }
1412
1413 fn maybe_from_const(ct: ty::Const<'tcx>) -> Option<Self> {
1416 match ct.kind() {
1417 ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1418 _ => None,
1419 }
1420 }
1421}
1422
1423struct InferenceLiteralEraser<'tcx> {
1426 tcx: TyCtxt<'tcx>,
1427}
1428
1429impl<'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceLiteralEraser<'tcx> {
1430 fn cx(&self) -> TyCtxt<'tcx> {
1431 self.tcx
1432 }
1433
1434 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1435 match ty.kind() {
1436 ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1437 ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1438 _ => ty.super_fold_with(self),
1439 }
1440 }
1441}
1442
1443impl<'tcx> TypeTrace<'tcx> {
1444 pub fn span(&self) -> Span {
1445 self.cause.span
1446 }
1447
1448 pub fn types(cause: &ObligationCause<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> TypeTrace<'tcx> {
1449 TypeTrace {
1450 cause: cause.clone(),
1451 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1452 }
1453 }
1454
1455 pub fn trait_refs(
1456 cause: &ObligationCause<'tcx>,
1457 a: ty::TraitRef<'tcx>,
1458 b: ty::TraitRef<'tcx>,
1459 ) -> TypeTrace<'tcx> {
1460 TypeTrace { cause: cause.clone(), values: ValuePairs::TraitRefs(ExpectedFound::new(a, b)) }
1461 }
1462
1463 pub fn consts(
1464 cause: &ObligationCause<'tcx>,
1465 a: ty::Const<'tcx>,
1466 b: ty::Const<'tcx>,
1467 ) -> TypeTrace<'tcx> {
1468 TypeTrace {
1469 cause: cause.clone(),
1470 values: ValuePairs::Terms(ExpectedFound::new(a.into(), b.into())),
1471 }
1472 }
1473}
1474
1475impl<'tcx> SubregionOrigin<'tcx> {
1476 pub fn span(&self) -> Span {
1477 match *self {
1478 Subtype(ref a) => a.span(),
1479 RelateObjectBound(a) => a,
1480 RelateParamBound(a, ..) => a,
1481 RelateRegionParamBound(a, _) => a,
1482 Reborrow(a) => a,
1483 ReferenceOutlivesReferent(_, a) => a,
1484 CompareImplItemObligation { span, .. } => span,
1485 AscribeUserTypeProvePredicate(span) => span,
1486 CheckAssociatedTypeBounds { ref parent, .. } => parent.span(),
1487 }
1488 }
1489
1490 pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1491 where
1492 F: FnOnce() -> Self,
1493 {
1494 match *cause.code() {
1495 traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1496 SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1497 }
1498
1499 traits::ObligationCauseCode::CompareImplItem {
1500 impl_item_def_id,
1501 trait_item_def_id,
1502 kind: _,
1503 } => SubregionOrigin::CompareImplItemObligation {
1504 span: cause.span,
1505 impl_item_def_id,
1506 trait_item_def_id,
1507 },
1508
1509 traits::ObligationCauseCode::CheckAssociatedTypeBounds {
1510 impl_item_def_id,
1511 trait_item_def_id,
1512 } => SubregionOrigin::CheckAssociatedTypeBounds {
1513 impl_item_def_id,
1514 trait_item_def_id,
1515 parent: Box::new(default()),
1516 },
1517
1518 traits::ObligationCauseCode::AscribeUserTypeProvePredicate(span) => {
1519 SubregionOrigin::AscribeUserTypeProvePredicate(span)
1520 }
1521
1522 traits::ObligationCauseCode::ObjectTypeBound(ty, _reg) => {
1523 SubregionOrigin::RelateRegionParamBound(cause.span, Some(ty))
1524 }
1525
1526 _ => default(),
1527 }
1528 }
1529}
1530
1531impl RegionVariableOrigin {
1532 pub fn span(&self) -> Span {
1533 match *self {
1534 MiscVariable(a)
1535 | PatternRegion(a)
1536 | BorrowRegion(a)
1537 | Autoref(a)
1538 | Coercion(a)
1539 | RegionParameterDefinition(a, ..)
1540 | BoundRegion(a, ..)
1541 | UpvarRegion(_, a) => a,
1542 Nll(..) => bug!("NLL variable used with `span`"),
1543 }
1544 }
1545}
1546
1547impl<'tcx> InferCtxt<'tcx> {
1548 pub fn find_block_span(&self, block: &'tcx hir::Block<'tcx>) -> Span {
1551 let block = block.innermost_block();
1552 if let Some(expr) = &block.expr {
1553 expr.span
1554 } else if let Some(stmt) = block.stmts.last() {
1555 stmt.span
1557 } else {
1558 block.span
1560 }
1561 }
1562
1563 pub fn find_block_span_from_hir_id(&self, hir_id: hir::HirId) -> Span {
1566 match self.tcx.hir_node(hir_id) {
1567 hir::Node::Block(blk) => self.find_block_span(blk),
1568 hir::Node::Expr(e) => e.span,
1571 _ => rustc_span::DUMMY_SP,
1572 }
1573 }
1574}