]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/mod.rs
fdd0c5b2ff368825cee5f9eaedc8c26844e31db5
[rust.git] / src / librustc_infer / infer / mod.rs
1 //! See the Book for more information.
2
3 pub use self::freshen::TypeFreshener;
4 pub use self::LateBoundRegionConversionTime::*;
5 pub use self::RegionVariableOrigin::*;
6 pub use self::SubregionOrigin::*;
7 pub use self::ValuePairs::*;
8
9 pub(crate) use self::undo_log::{InferCtxtUndoLogs, Snapshot, UndoLog};
10
11 use crate::traits::{self, ObligationCause, PredicateObligations, TraitEngine};
12
13 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
14 use rustc_data_structures::sync::Lrc;
15 use rustc_data_structures::undo_log::Rollback;
16 use rustc_data_structures::unify as ut;
17 use rustc_errors::DiagnosticBuilder;
18 use rustc_hir as hir;
19 use rustc_hir::def_id::{DefId, LocalDefId};
20 use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
21 use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
22 use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
23 use rustc_middle::middle::region;
24 use rustc_middle::mir;
25 use rustc_middle::mir::interpret::ConstEvalResult;
26 use rustc_middle::traits::select;
27 use rustc_middle::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
28 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
29 use rustc_middle::ty::relate::RelateResult;
30 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
31 pub use rustc_middle::ty::IntVarValue;
32 use rustc_middle::ty::{self, GenericParamDefKind, InferConst, Ty, TyCtxt};
33 use rustc_middle::ty::{ConstVid, FloatVid, IntVid, TyVid};
34 use rustc_session::config::BorrowckMode;
35 use rustc_span::symbol::Symbol;
36 use rustc_span::Span;
37
38 use std::cell::{Cell, Ref, RefCell};
39 use std::collections::BTreeMap;
40 use std::fmt;
41
42 use self::combine::CombineFields;
43 use self::free_regions::RegionRelations;
44 use self::lexical_region_resolve::LexicalRegionResolutions;
45 use self::outlives::env::OutlivesEnvironment;
46 use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
47 use self::region_constraints::{
48     RegionConstraintCollector, RegionConstraintStorage, RegionSnapshot,
49 };
50 use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
51
52 pub mod at;
53 pub mod canonical;
54 mod combine;
55 mod equate;
56 pub mod error_reporting;
57 pub mod free_regions;
58 mod freshen;
59 mod fudge;
60 mod glb;
61 mod higher_ranked;
62 pub mod lattice;
63 mod lexical_region_resolve;
64 mod lub;
65 pub mod nll_relate;
66 pub mod outlives;
67 pub mod region_constraints;
68 pub mod resolve;
69 mod sub;
70 pub mod type_variable;
71 mod undo_log;
72
73 use crate::infer::canonical::OriginalQueryValues;
74 pub use rustc_middle::infer::unify_key;
75
76 #[must_use]
77 #[derive(Debug)]
78 pub struct InferOk<'tcx, T> {
79     pub value: T,
80     pub obligations: PredicateObligations<'tcx>,
81 }
82 pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
83
84 pub type Bound<T> = Option<T>;
85 pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
86 pub type FixupResult<'tcx, T> = Result<T, FixupError<'tcx>>; // "fixup result"
87
88 pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
89     ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
90 >;
91
92 /// How we should handle region solving.
93 ///
94 /// This is used so that the region values inferred by HIR region solving are
95 /// not exposed, and so that we can avoid doing work in HIR typeck that MIR
96 /// typeck will also do.
97 #[derive(Copy, Clone, Debug)]
98 pub enum RegionckMode {
99     /// The default mode: report region errors, don't erase regions.
100     Solve,
101     /// Erase the results of region after solving.
102     Erase {
103         /// A flag that is used to suppress region errors, when we are doing
104         /// region checks that the NLL borrow checker will also do -- it might
105         /// be set to true.
106         suppress_errors: bool,
107     },
108 }
109
110 impl Default for RegionckMode {
111     fn default() -> Self {
112         RegionckMode::Solve
113     }
114 }
115
116 impl RegionckMode {
117     pub fn suppressed(self) -> bool {
118         match self {
119             Self::Solve => false,
120             Self::Erase { suppress_errors } => suppress_errors,
121         }
122     }
123
124     /// Indicates that the MIR borrowck will repeat these region
125     /// checks, so we should ignore errors if NLL is (unconditionally)
126     /// enabled.
127     pub fn for_item_body(tcx: TyCtxt<'_>) -> Self {
128         // FIXME(Centril): Once we actually remove `::Migrate` also make
129         // this always `true` and then proceed to eliminate the dead code.
130         match tcx.borrowck_mode() {
131             // If we're on Migrate mode, report AST region errors
132             BorrowckMode::Migrate => RegionckMode::Erase { suppress_errors: false },
133
134             // If we're on MIR, don't report AST region errors as they should be reported by NLL
135             BorrowckMode::Mir => RegionckMode::Erase { suppress_errors: true },
136         }
137     }
138 }
139
140 /// This type contains all the things within `InferCtxt` that sit within a
141 /// `RefCell` and are involved with taking/rolling back snapshots. Snapshot
142 /// operations are hot enough that we want only one call to `borrow_mut` per
143 /// call to `start_snapshot` and `rollback_to`.
144 pub struct InferCtxtInner<'tcx> {
145     /// Cache for projections. This cache is snapshotted along with the infcx.
146     ///
147     /// Public so that `traits::project` can use it.
148     pub projection_cache: traits::ProjectionCacheStorage<'tcx>,
149
150     /// We instantiate `UnificationTable` with `bounds<Ty>` because the types
151     /// that might instantiate a general type variable have an order,
152     /// represented by its upper and lower bounds.
153     type_variable_storage: type_variable::TypeVariableStorage<'tcx>,
154
155     /// Map from const parameter variable to the kind of const it represents.
156     const_unification_storage: ut::UnificationTableStorage<ty::ConstVid<'tcx>>,
157
158     /// Map from integral variable to the kind of integer it represents.
159     int_unification_storage: ut::UnificationTableStorage<ty::IntVid>,
160
161     /// Map from floating variable to the kind of float it represents.
162     float_unification_storage: ut::UnificationTableStorage<ty::FloatVid>,
163
164     /// Tracks the set of region variables and the constraints between them.
165     /// This is initially `Some(_)` but when
166     /// `resolve_regions_and_report_errors` is invoked, this gets set to `None`
167     /// -- further attempts to perform unification, etc., may fail if new
168     /// region constraints would've been added.
169     region_constraint_storage: Option<RegionConstraintStorage<'tcx>>,
170
171     /// A set of constraints that regionck must validate. Each
172     /// constraint has the form `T:'a`, meaning "some type `T` must
173     /// outlive the lifetime 'a". These constraints derive from
174     /// instantiated type parameters. So if you had a struct defined
175     /// like
176     ///
177     ///     struct Foo<T:'static> { ... }
178     ///
179     /// then in some expression `let x = Foo { ... }` it will
180     /// instantiate the type parameter `T` with a fresh type `$0`. At
181     /// the same time, it will record a region obligation of
182     /// `$0:'static`. This will get checked later by regionck. (We
183     /// can't generally check these things right away because we have
184     /// to wait until types are resolved.)
185     ///
186     /// These are stored in a map keyed to the id of the innermost
187     /// enclosing fn body / static initializer expression. This is
188     /// because the location where the obligation was incurred can be
189     /// relevant with respect to which sublifetime assumptions are in
190     /// place. The reason that we store under the fn-id, and not
191     /// something more fine-grained, is so that it is easier for
192     /// regionck to be sure that it has found *all* the region
193     /// obligations (otherwise, it's easy to fail to walk to a
194     /// particular node-id).
195     ///
196     /// Before running `resolve_regions_and_report_errors`, the creator
197     /// of the inference context is expected to invoke
198     /// `process_region_obligations` (defined in `self::region_obligations`)
199     /// for each body-id in this map, which will process the
200     /// obligations within. This is expected to be done 'late enough'
201     /// that all type inference variables have been bound and so forth.
202     region_obligations: Vec<(hir::HirId, RegionObligation<'tcx>)>,
203
204     undo_log: InferCtxtUndoLogs<'tcx>,
205 }
206
207 impl<'tcx> InferCtxtInner<'tcx> {
208     fn new() -> InferCtxtInner<'tcx> {
209         InferCtxtInner {
210             projection_cache: Default::default(),
211             type_variable_storage: type_variable::TypeVariableStorage::new(),
212             undo_log: InferCtxtUndoLogs::default(),
213             const_unification_storage: ut::UnificationTableStorage::new(),
214             int_unification_storage: ut::UnificationTableStorage::new(),
215             float_unification_storage: ut::UnificationTableStorage::new(),
216             region_constraint_storage: Some(RegionConstraintStorage::new()),
217             region_obligations: vec![],
218         }
219     }
220
221     pub fn region_obligations(&self) -> &[(hir::HirId, RegionObligation<'tcx>)] {
222         &self.region_obligations
223     }
224
225     pub fn projection_cache(&mut self) -> traits::ProjectionCache<'_, 'tcx> {
226         self.projection_cache.with_log(&mut self.undo_log)
227     }
228
229     fn type_variables(&mut self) -> type_variable::TypeVariableTable<'_, 'tcx> {
230         self.type_variable_storage.with_log(&mut self.undo_log)
231     }
232
233     fn int_unification_table(
234         &mut self,
235     ) -> ut::UnificationTable<
236         ut::InPlace<
237             ty::IntVid,
238             &mut ut::UnificationStorage<ty::IntVid>,
239             &mut InferCtxtUndoLogs<'tcx>,
240         >,
241     > {
242         self.int_unification_storage.with_log(&mut self.undo_log)
243     }
244
245     fn float_unification_table(
246         &mut self,
247     ) -> ut::UnificationTable<
248         ut::InPlace<
249             ty::FloatVid,
250             &mut ut::UnificationStorage<ty::FloatVid>,
251             &mut InferCtxtUndoLogs<'tcx>,
252         >,
253     > {
254         self.float_unification_storage.with_log(&mut self.undo_log)
255     }
256
257     fn const_unification_table(
258         &mut self,
259     ) -> ut::UnificationTable<
260         ut::InPlace<
261             ty::ConstVid<'tcx>,
262             &mut ut::UnificationStorage<ty::ConstVid<'tcx>>,
263             &mut InferCtxtUndoLogs<'tcx>,
264         >,
265     > {
266         self.const_unification_storage.with_log(&mut self.undo_log)
267     }
268
269     pub fn unwrap_region_constraints(&mut self) -> RegionConstraintCollector<'_, 'tcx> {
270         self.region_constraint_storage
271             .as_mut()
272             .expect("region constraints already solved")
273             .with_log(&mut self.undo_log)
274     }
275 }
276
277 pub struct InferCtxt<'a, 'tcx> {
278     pub tcx: TyCtxt<'tcx>,
279
280     /// During type-checking/inference of a body, `in_progress_tables`
281     /// contains a reference to the tables being built up, which are
282     /// used for reading closure kinds/signatures as they are inferred,
283     /// and for error reporting logic to read arbitrary node types.
284     pub in_progress_tables: Option<&'a RefCell<ty::TypeckTables<'tcx>>>,
285
286     pub inner: RefCell<InferCtxtInner<'tcx>>,
287
288     /// If set, this flag causes us to skip the 'leak check' during
289     /// higher-ranked subtyping operations. This flag is a temporary one used
290     /// to manage the removal of the leak-check: for the time being, we still run the
291     /// leak-check, but we issue warnings. This flag can only be set to true
292     /// when entering a snapshot.
293     skip_leak_check: Cell<bool>,
294
295     /// Once region inference is done, the values for each variable.
296     lexical_region_resolutions: RefCell<Option<LexicalRegionResolutions<'tcx>>>,
297
298     /// Caches the results of trait selection. This cache is used
299     /// for things that have to do with the parameters in scope.
300     pub selection_cache: select::SelectionCache<'tcx>,
301
302     /// Caches the results of trait evaluation.
303     pub evaluation_cache: select::EvaluationCache<'tcx>,
304
305     /// the set of predicates on which errors have been reported, to
306     /// avoid reporting the same error twice.
307     pub reported_trait_errors: RefCell<FxHashMap<Span, Vec<ty::Predicate<'tcx>>>>,
308
309     pub reported_closure_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,
310
311     /// When an error occurs, we want to avoid reporting "derived"
312     /// errors that are due to this original failure. Normally, we
313     /// handle this with the `err_count_on_creation` count, which
314     /// basically just tracks how many errors were reported when we
315     /// started type-checking a fn and checks to see if any new errors
316     /// have been reported since then. Not great, but it works.
317     ///
318     /// However, when errors originated in other passes -- notably
319     /// resolve -- this heuristic breaks down. Therefore, we have this
320     /// auxiliary flag that one can set whenever one creates a
321     /// type-error that is due to an error in a prior pass.
322     ///
323     /// Don't read this flag directly, call `is_tainted_by_errors()`
324     /// and `set_tainted_by_errors()`.
325     tainted_by_errors_flag: Cell<bool>,
326
327     /// Track how many errors were reported when this infcx is created.
328     /// If the number of errors increases, that's also a sign (line
329     /// `tained_by_errors`) to avoid reporting certain kinds of errors.
330     // FIXME(matthewjasper) Merge into `tainted_by_errors_flag`
331     err_count_on_creation: usize,
332
333     /// This flag is true while there is an active snapshot.
334     in_snapshot: Cell<bool>,
335
336     /// What is the innermost universe we have created? Starts out as
337     /// `UniverseIndex::root()` but grows from there as we enter
338     /// universal quantifiers.
339     ///
340     /// N.B., at present, we exclude the universal quantifiers on the
341     /// item we are type-checking, and just consider those names as
342     /// part of the root universe. So this would only get incremented
343     /// when we enter into a higher-ranked (`for<..>`) type or trait
344     /// bound.
345     universe: Cell<ty::UniverseIndex>,
346 }
347
348 /// A map returned by `replace_bound_vars_with_placeholders()`
349 /// indicating the placeholder region that each late-bound region was
350 /// replaced with.
351 pub type PlaceholderMap<'tcx> = BTreeMap<ty::BoundRegion, ty::Region<'tcx>>;
352
353 /// See the `error_reporting` module for more details.
354 #[derive(Clone, Debug, PartialEq, Eq, TypeFoldable)]
355 pub enum ValuePairs<'tcx> {
356     Types(ExpectedFound<Ty<'tcx>>),
357     Regions(ExpectedFound<ty::Region<'tcx>>),
358     Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
359     TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
360     PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
361 }
362
363 /// The trace designates the path through inference that we took to
364 /// encounter an error or subtyping constraint.
365 ///
366 /// See the `error_reporting` module for more details.
367 #[derive(Clone, Debug)]
368 pub struct TypeTrace<'tcx> {
369     cause: ObligationCause<'tcx>,
370     values: ValuePairs<'tcx>,
371 }
372
373 /// The origin of a `r1 <= r2` constraint.
374 ///
375 /// See `error_reporting` module for more details
376 #[derive(Clone, Debug)]
377 pub enum SubregionOrigin<'tcx> {
378     /// Arose from a subtyping relation
379     Subtype(Box<TypeTrace<'tcx>>),
380
381     /// When casting `&'a T` to an `&'b Trait` object,
382     /// relating `'a` to `'b`
383     RelateObjectBound(Span),
384
385     /// Some type parameter was instantiated with the given type,
386     /// and that type must outlive some region.
387     RelateParamBound(Span, Ty<'tcx>),
388
389     /// The given region parameter was instantiated with a region
390     /// that must outlive some other region.
391     RelateRegionParamBound(Span),
392
393     /// Creating a pointer `b` to contents of another reference
394     Reborrow(Span),
395
396     /// Creating a pointer `b` to contents of an upvar
397     ReborrowUpvar(Span, ty::UpvarId),
398
399     /// Data with type `Ty<'tcx>` was borrowed
400     DataBorrowed(Ty<'tcx>, Span),
401
402     /// (&'a &'b T) where a >= b
403     ReferenceOutlivesReferent(Ty<'tcx>, Span),
404
405     /// Region in return type of invoked fn must enclose call
406     CallReturn(Span),
407
408     /// Comparing the signature and requirements of an impl method against
409     /// the containing trait.
410     CompareImplMethodObligation {
411         span: Span,
412         item_name: Symbol,
413         impl_item_def_id: DefId,
414         trait_item_def_id: DefId,
415     },
416 }
417
418 // `SubregionOrigin` is used a lot. Make sure it doesn't unintentionally get bigger.
419 #[cfg(target_arch = "x86_64")]
420 static_assert_size!(SubregionOrigin<'_>, 32);
421
422 /// Places that type/region parameters can appear.
423 #[derive(Clone, Copy, Debug)]
424 pub enum ParameterOrigin {
425     Path,               // foo::bar
426     MethodCall,         // foo.bar() <-- parameters on impl providing bar()
427     OverloadedOperator, // a + b when overloaded
428     OverloadedDeref,    // *a when overloaded
429 }
430
431 /// Times when we replace late-bound regions with variables:
432 #[derive(Clone, Copy, Debug)]
433 pub enum LateBoundRegionConversionTime {
434     /// when a fn is called
435     FnCall,
436
437     /// when two higher-ranked types are compared
438     HigherRankedType,
439
440     /// when projecting an associated type
441     AssocTypeProjection(DefId),
442 }
443
444 /// Reasons to create a region inference variable
445 ///
446 /// See `error_reporting` module for more details
447 #[derive(Copy, Clone, Debug)]
448 pub enum RegionVariableOrigin {
449     /// Region variables created for ill-categorized reasons,
450     /// mostly indicates places in need of refactoring
451     MiscVariable(Span),
452
453     /// Regions created by a `&P` or `[...]` pattern
454     PatternRegion(Span),
455
456     /// Regions created by `&` operator
457     AddrOfRegion(Span),
458
459     /// Regions created as part of an autoref of a method receiver
460     Autoref(Span),
461
462     /// Regions created as part of an automatic coercion
463     Coercion(Span),
464
465     /// Region variables created as the values for early-bound regions
466     EarlyBoundRegion(Span, Symbol),
467
468     /// Region variables created for bound regions
469     /// in a function or method that is called
470     LateBoundRegion(Span, ty::BoundRegion, LateBoundRegionConversionTime),
471
472     UpvarRegion(ty::UpvarId, Span),
473
474     BoundRegionInCoherence(Symbol),
475
476     /// This origin is used for the inference variables that we create
477     /// during NLL region processing.
478     NLL(NLLRegionVariableOrigin),
479 }
480
481 #[derive(Copy, Clone, Debug)]
482 pub enum NLLRegionVariableOrigin {
483     /// During NLL region processing, we create variables for free
484     /// regions that we encounter in the function signature and
485     /// elsewhere. This origin indices we've got one of those.
486     FreeRegion,
487
488     /// "Universal" instantiation of a higher-ranked region (e.g.,
489     /// from a `for<'a> T` binder). Meant to represent "any region".
490     Placeholder(ty::PlaceholderRegion),
491
492     /// The variable we create to represent `'empty(U0)`.
493     RootEmptyRegion,
494
495     Existential {
496         /// If this is true, then this variable was created to represent a lifetime
497         /// bound in a `for` binder. For example, it might have been created to
498         /// represent the lifetime `'a` in a type like `for<'a> fn(&'a u32)`.
499         /// Such variables are created when we are trying to figure out if there
500         /// is any valid instantiation of `'a` that could fit into some scenario.
501         ///
502         /// This is used to inform error reporting: in the case that we are trying to
503         /// determine whether there is any valid instantiation of a `'a` variable that meets
504         /// some constraint C, we want to blame the "source" of that `for` type,
505         /// rather than blaming the source of the constraint C.
506         from_forall: bool,
507     },
508 }
509
510 impl NLLRegionVariableOrigin {
511     pub fn is_universal(self) -> bool {
512         match self {
513             NLLRegionVariableOrigin::FreeRegion => true,
514             NLLRegionVariableOrigin::Placeholder(..) => true,
515             NLLRegionVariableOrigin::Existential { .. } => false,
516             NLLRegionVariableOrigin::RootEmptyRegion => false,
517         }
518     }
519
520     pub fn is_existential(self) -> bool {
521         !self.is_universal()
522     }
523 }
524
525 // FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
526 #[derive(Copy, Clone, Debug)]
527 pub enum FixupError<'tcx> {
528     UnresolvedIntTy(IntVid),
529     UnresolvedFloatTy(FloatVid),
530     UnresolvedTy(TyVid),
531     UnresolvedConst(ConstVid<'tcx>),
532 }
533
534 /// See the `region_obligations` field for more information.
535 #[derive(Clone)]
536 pub struct RegionObligation<'tcx> {
537     pub sub_region: ty::Region<'tcx>,
538     pub sup_type: Ty<'tcx>,
539     pub origin: SubregionOrigin<'tcx>,
540 }
541
542 impl<'tcx> fmt::Display for FixupError<'tcx> {
543     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544         use self::FixupError::*;
545
546         match *self {
547             UnresolvedIntTy(_) => write!(
548                 f,
549                 "cannot determine the type of this integer; \
550                  add a suffix to specify the type explicitly"
551             ),
552             UnresolvedFloatTy(_) => write!(
553                 f,
554                 "cannot determine the type of this number; \
555                  add a suffix to specify the type explicitly"
556             ),
557             UnresolvedTy(_) => write!(f, "unconstrained type"),
558             UnresolvedConst(_) => write!(f, "unconstrained const value"),
559         }
560     }
561 }
562
563 /// Helper type of a temporary returned by `tcx.infer_ctxt()`.
564 /// Necessary because we can't write the following bound:
565 /// `F: for<'b, 'tcx> where 'tcx FnOnce(InferCtxt<'b, 'tcx>)`.
566 pub struct InferCtxtBuilder<'tcx> {
567     global_tcx: TyCtxt<'tcx>,
568     fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
569 }
570
571 pub trait TyCtxtInferExt<'tcx> {
572     fn infer_ctxt(self) -> InferCtxtBuilder<'tcx>;
573 }
574
575 impl TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
576     fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
577         InferCtxtBuilder { global_tcx: self, fresh_tables: None }
578     }
579 }
580
581 impl<'tcx> InferCtxtBuilder<'tcx> {
582     /// Used only by `rustc_typeck` during body type-checking/inference,
583     /// will initialize `in_progress_tables` with fresh `TypeckTables`.
584     pub fn with_fresh_in_progress_tables(mut self, table_owner: LocalDefId) -> Self {
585         self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(Some(table_owner))));
586         self
587     }
588
589     /// Given a canonical value `C` as a starting point, create an
590     /// inference context that contains each of the bound values
591     /// within instantiated as a fresh variable. The `f` closure is
592     /// invoked with the new infcx, along with the instantiated value
593     /// `V` and a substitution `S`. This substitution `S` maps from
594     /// the bound values in `C` to their instantiated values in `V`
595     /// (in other words, `S(C) = V`).
596     pub fn enter_with_canonical<T, R>(
597         &mut self,
598         span: Span,
599         canonical: &Canonical<'tcx, T>,
600         f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>, T, CanonicalVarValues<'tcx>) -> R,
601     ) -> R
602     where
603         T: TypeFoldable<'tcx>,
604     {
605         self.enter(|infcx| {
606             let (value, subst) =
607                 infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
608             f(infcx, value, subst)
609         })
610     }
611
612     pub fn enter<R>(&mut self, f: impl for<'a> FnOnce(InferCtxt<'a, 'tcx>) -> R) -> R {
613         let InferCtxtBuilder { global_tcx, ref fresh_tables } = *self;
614         let in_progress_tables = fresh_tables.as_ref();
615         global_tcx.enter_local(|tcx| {
616             f(InferCtxt {
617                 tcx,
618                 in_progress_tables,
619                 inner: RefCell::new(InferCtxtInner::new()),
620                 lexical_region_resolutions: RefCell::new(None),
621                 selection_cache: Default::default(),
622                 evaluation_cache: Default::default(),
623                 reported_trait_errors: Default::default(),
624                 reported_closure_mismatch: Default::default(),
625                 tainted_by_errors_flag: Cell::new(false),
626                 err_count_on_creation: tcx.sess.err_count(),
627                 in_snapshot: Cell::new(false),
628                 skip_leak_check: Cell::new(false),
629                 universe: Cell::new(ty::UniverseIndex::ROOT),
630             })
631         })
632     }
633 }
634
635 impl<'tcx, T> InferOk<'tcx, T> {
636     pub fn unit(self) -> InferOk<'tcx, ()> {
637         InferOk { value: (), obligations: self.obligations }
638     }
639
640     /// Extracts `value`, registering any obligations into `fulfill_cx`.
641     pub fn into_value_registering_obligations(
642         self,
643         infcx: &InferCtxt<'_, 'tcx>,
644         fulfill_cx: &mut dyn TraitEngine<'tcx>,
645     ) -> T {
646         let InferOk { value, obligations } = self;
647         for obligation in obligations {
648             fulfill_cx.register_predicate_obligation(infcx, obligation);
649         }
650         value
651     }
652 }
653
654 impl<'tcx> InferOk<'tcx, ()> {
655     pub fn into_obligations(self) -> PredicateObligations<'tcx> {
656         self.obligations
657     }
658 }
659
660 #[must_use = "once you start a snapshot, you should always consume it"]
661 pub struct CombinedSnapshot<'a, 'tcx> {
662     undo_snapshot: Snapshot<'tcx>,
663     region_constraints_snapshot: RegionSnapshot,
664     universe: ty::UniverseIndex,
665     was_in_snapshot: bool,
666     _in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
667 }
668
669 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
670     pub fn is_in_snapshot(&self) -> bool {
671         self.in_snapshot.get()
672     }
673
674     pub fn freshen<T: TypeFoldable<'tcx>>(&self, t: T) -> T {
675         t.fold_with(&mut self.freshener())
676     }
677
678     pub fn type_var_diverges(&'a self, ty: Ty<'_>) -> bool {
679         match ty.kind {
680             ty::Infer(ty::TyVar(vid)) => self.inner.borrow_mut().type_variables().var_diverges(vid),
681             _ => false,
682         }
683     }
684
685     pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'tcx> {
686         freshen::TypeFreshener::new(self)
687     }
688
689     pub fn type_is_unconstrained_numeric(&'a self, ty: Ty<'_>) -> UnconstrainedNumeric {
690         use rustc_middle::ty::error::UnconstrainedNumeric::Neither;
691         use rustc_middle::ty::error::UnconstrainedNumeric::{UnconstrainedFloat, UnconstrainedInt};
692         match ty.kind {
693             ty::Infer(ty::IntVar(vid)) => {
694                 if self.inner.borrow_mut().int_unification_table().probe_value(vid).is_some() {
695                     Neither
696                 } else {
697                     UnconstrainedInt
698                 }
699             }
700             ty::Infer(ty::FloatVar(vid)) => {
701                 if self.inner.borrow_mut().float_unification_table().probe_value(vid).is_some() {
702                     Neither
703                 } else {
704                     UnconstrainedFloat
705                 }
706             }
707             _ => Neither,
708         }
709     }
710
711     pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
712         let mut inner = self.inner.borrow_mut();
713         // FIXME(const_generics): should there be an equivalent function for const variables?
714
715         let mut vars: Vec<Ty<'_>> = inner
716             .type_variables()
717             .unsolved_variables()
718             .into_iter()
719             .map(|t| self.tcx.mk_ty_var(t))
720             .collect();
721         vars.extend(
722             (0..inner.int_unification_table().len())
723                 .map(|i| ty::IntVid { index: i as u32 })
724                 .filter(|&vid| inner.int_unification_table().probe_value(vid).is_none())
725                 .map(|v| self.tcx.mk_int_var(v)),
726         );
727         vars.extend(
728             (0..inner.float_unification_table().len())
729                 .map(|i| ty::FloatVid { index: i as u32 })
730                 .filter(|&vid| inner.float_unification_table().probe_value(vid).is_none())
731                 .map(|v| self.tcx.mk_float_var(v)),
732         );
733         vars
734     }
735
736     fn combine_fields(
737         &'a self,
738         trace: TypeTrace<'tcx>,
739         param_env: ty::ParamEnv<'tcx>,
740     ) -> CombineFields<'a, 'tcx> {
741         CombineFields {
742             infcx: self,
743             trace,
744             cause: None,
745             param_env,
746             obligations: PredicateObligations::new(),
747         }
748     }
749
750     /// Clear the "currently in a snapshot" flag, invoke the closure,
751     /// then restore the flag to its original value. This flag is a
752     /// debugging measure designed to detect cases where we start a
753     /// snapshot, create type variables, and register obligations
754     /// which may involve those type variables in the fulfillment cx,
755     /// potentially leaving "dangling type variables" behind.
756     /// In such cases, an assertion will fail when attempting to
757     /// register obligations, within a snapshot. Very useful, much
758     /// better than grovelling through megabytes of `RUSTC_LOG` output.
759     ///
760     /// HOWEVER, in some cases the flag is unhelpful. In particular, we
761     /// sometimes create a "mini-fulfilment-cx" in which we enroll
762     /// obligations. As long as this fulfillment cx is fully drained
763     /// before we return, this is not a problem, as there won't be any
764     /// escaping obligations in the main cx. In those cases, you can
765     /// use this function.
766     pub fn save_and_restore_in_snapshot_flag<F, R>(&self, func: F) -> R
767     where
768         F: FnOnce(&Self) -> R,
769     {
770         let flag = self.in_snapshot.replace(false);
771         let result = func(self);
772         self.in_snapshot.set(flag);
773         result
774     }
775
776     fn start_snapshot(&self) -> CombinedSnapshot<'a, 'tcx> {
777         debug!("start_snapshot()");
778
779         let in_snapshot = self.in_snapshot.replace(true);
780
781         let mut inner = self.inner.borrow_mut();
782
783         CombinedSnapshot {
784             undo_snapshot: inner.undo_log.start_snapshot(),
785             region_constraints_snapshot: inner.unwrap_region_constraints().start_snapshot(),
786             universe: self.universe(),
787             was_in_snapshot: in_snapshot,
788             // Borrow tables "in progress" (i.e., during typeck)
789             // to ban writes from within a snapshot to them.
790             _in_progress_tables: self.in_progress_tables.map(|tables| tables.borrow()),
791         }
792     }
793
794     fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot<'a, 'tcx>) {
795         debug!("rollback_to(cause={})", cause);
796         let CombinedSnapshot {
797             undo_snapshot,
798             region_constraints_snapshot,
799             universe,
800             was_in_snapshot,
801             _in_progress_tables,
802         } = snapshot;
803
804         self.in_snapshot.set(was_in_snapshot);
805         self.universe.set(universe);
806
807         let mut inner = self.inner.borrow_mut();
808         inner.rollback_to(undo_snapshot);
809         inner.unwrap_region_constraints().rollback_to(region_constraints_snapshot);
810     }
811
812     fn commit_from(&self, snapshot: CombinedSnapshot<'a, 'tcx>) {
813         debug!("commit_from()");
814         let CombinedSnapshot {
815             undo_snapshot,
816             region_constraints_snapshot: _,
817             universe: _,
818             was_in_snapshot,
819             _in_progress_tables,
820         } = snapshot;
821
822         self.in_snapshot.set(was_in_snapshot);
823
824         self.inner.borrow_mut().commit(undo_snapshot);
825     }
826
827     /// Executes `f` and commit the bindings.
828     pub fn commit_unconditionally<R, F>(&self, f: F) -> R
829     where
830         F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
831     {
832         debug!("commit_unconditionally()");
833         let snapshot = self.start_snapshot();
834         let r = f(&snapshot);
835         self.commit_from(snapshot);
836         r
837     }
838
839     /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)`.
840     pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E>
841     where
842         F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> Result<T, E>,
843     {
844         debug!("commit_if_ok()");
845         let snapshot = self.start_snapshot();
846         let r = f(&snapshot);
847         debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok());
848         match r {
849             Ok(_) => {
850                 self.commit_from(snapshot);
851             }
852             Err(_) => {
853                 self.rollback_to("commit_if_ok -- error", snapshot);
854             }
855         }
856         r
857     }
858
859     /// Execute `f` then unroll any bindings it creates.
860     pub fn probe<R, F>(&self, f: F) -> R
861     where
862         F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
863     {
864         debug!("probe()");
865         let snapshot = self.start_snapshot();
866         let r = f(&snapshot);
867         self.rollback_to("probe", snapshot);
868         r
869     }
870
871     /// If `should_skip` is true, then execute `f` then unroll any bindings it creates.
872     pub fn probe_maybe_skip_leak_check<R, F>(&self, should_skip: bool, f: F) -> R
873     where
874         F: FnOnce(&CombinedSnapshot<'a, 'tcx>) -> R,
875     {
876         debug!("probe()");
877         let snapshot = self.start_snapshot();
878         let was_skip_leak_check = self.skip_leak_check.get();
879         if should_skip {
880             self.skip_leak_check.set(true);
881         }
882         let r = f(&snapshot);
883         self.rollback_to("probe", snapshot);
884         self.skip_leak_check.set(was_skip_leak_check);
885         r
886     }
887
888     /// Scan the constraints produced since `snapshot` began and returns:
889     ///
890     /// - `None` -- if none of them involve "region outlives" constraints
891     /// - `Some(true)` -- if there are `'a: 'b` constraints where `'a` or `'b` is a placeholder
892     /// - `Some(false)` -- if there are `'a: 'b` constraints but none involve placeholders
893     pub fn region_constraints_added_in_snapshot(
894         &self,
895         snapshot: &CombinedSnapshot<'a, 'tcx>,
896     ) -> Option<bool> {
897         self.inner
898             .borrow_mut()
899             .unwrap_region_constraints()
900             .region_constraints_added_in_snapshot(&snapshot.undo_snapshot)
901     }
902
903     pub fn add_given(&self, sub: ty::Region<'tcx>, sup: ty::RegionVid) {
904         self.inner.borrow_mut().unwrap_region_constraints().add_given(sub, sup);
905     }
906
907     pub fn can_sub<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
908     where
909         T: at::ToTrace<'tcx>,
910     {
911         let origin = &ObligationCause::dummy();
912         self.probe(|_| {
913             self.at(origin, param_env).sub(a, b).map(|InferOk { obligations: _, .. }| {
914                 // Ignore obligations, since we are unrolling
915                 // everything anyway.
916             })
917         })
918     }
919
920     pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> UnitResult<'tcx>
921     where
922         T: at::ToTrace<'tcx>,
923     {
924         let origin = &ObligationCause::dummy();
925         self.probe(|_| {
926             self.at(origin, param_env).eq(a, b).map(|InferOk { obligations: _, .. }| {
927                 // Ignore obligations, since we are unrolling
928                 // everything anyway.
929             })
930         })
931     }
932
933     pub fn sub_regions(
934         &self,
935         origin: SubregionOrigin<'tcx>,
936         a: ty::Region<'tcx>,
937         b: ty::Region<'tcx>,
938     ) {
939         debug!("sub_regions({:?} <: {:?})", a, b);
940         self.inner.borrow_mut().unwrap_region_constraints().make_subregion(origin, a, b);
941     }
942
943     /// Require that the region `r` be equal to one of the regions in
944     /// the set `regions`.
945     pub fn member_constraint(
946         &self,
947         opaque_type_def_id: DefId,
948         definition_span: Span,
949         hidden_ty: Ty<'tcx>,
950         region: ty::Region<'tcx>,
951         in_regions: &Lrc<Vec<ty::Region<'tcx>>>,
952     ) {
953         debug!("member_constraint({:?} <: {:?})", region, in_regions);
954         self.inner.borrow_mut().unwrap_region_constraints().member_constraint(
955             opaque_type_def_id,
956             definition_span,
957             hidden_ty,
958             region,
959             in_regions,
960         );
961     }
962
963     pub fn subtype_predicate(
964         &self,
965         cause: &ObligationCause<'tcx>,
966         param_env: ty::ParamEnv<'tcx>,
967         predicate: &ty::PolySubtypePredicate<'tcx>,
968     ) -> Option<InferResult<'tcx, ()>> {
969         // Subtle: it's ok to skip the binder here and resolve because
970         // `shallow_resolve` just ignores anything that is not a type
971         // variable, and because type variable's can't (at present, at
972         // least) capture any of the things bound by this binder.
973         //
974         // NOTE(nmatsakis): really, there is no *particular* reason to do this
975         // `shallow_resolve` here except as a micro-optimization.
976         // Naturally I could not resist.
977         let two_unbound_type_vars = {
978             let a = self.shallow_resolve(predicate.skip_binder().a);
979             let b = self.shallow_resolve(predicate.skip_binder().b);
980             a.is_ty_var() && b.is_ty_var()
981         };
982
983         if two_unbound_type_vars {
984             // Two unbound type variables? Can't make progress.
985             return None;
986         }
987
988         Some(self.commit_if_ok(|snapshot| {
989             let (ty::SubtypePredicate { a_is_expected, a, b }, placeholder_map) =
990                 self.replace_bound_vars_with_placeholders(predicate);
991
992             let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
993
994             self.leak_check(false, &placeholder_map, snapshot)?;
995
996             Ok(ok.unit())
997         }))
998     }
999
1000     pub fn region_outlives_predicate(
1001         &self,
1002         cause: &traits::ObligationCause<'tcx>,
1003         predicate: &ty::PolyRegionOutlivesPredicate<'tcx>,
1004     ) -> UnitResult<'tcx> {
1005         self.commit_if_ok(|snapshot| {
1006             let (ty::OutlivesPredicate(r_a, r_b), placeholder_map) =
1007                 self.replace_bound_vars_with_placeholders(predicate);
1008             let origin = SubregionOrigin::from_obligation_cause(cause, || {
1009                 RelateRegionParamBound(cause.span)
1010             });
1011             self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b`
1012             self.leak_check(false, &placeholder_map, snapshot)?;
1013             Ok(())
1014         })
1015     }
1016
1017     pub fn next_ty_var_id(&self, diverging: bool, origin: TypeVariableOrigin) -> TyVid {
1018         self.inner.borrow_mut().type_variables().new_var(self.universe(), diverging, origin)
1019     }
1020
1021     pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
1022         self.tcx.mk_ty_var(self.next_ty_var_id(false, origin))
1023     }
1024
1025     pub fn next_ty_var_in_universe(
1026         &self,
1027         origin: TypeVariableOrigin,
1028         universe: ty::UniverseIndex,
1029     ) -> Ty<'tcx> {
1030         let vid = self.inner.borrow_mut().type_variables().new_var(universe, false, origin);
1031         self.tcx.mk_ty_var(vid)
1032     }
1033
1034     pub fn next_diverging_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
1035         self.tcx.mk_ty_var(self.next_ty_var_id(true, origin))
1036     }
1037
1038     pub fn next_const_var(
1039         &self,
1040         ty: Ty<'tcx>,
1041         origin: ConstVariableOrigin,
1042     ) -> &'tcx ty::Const<'tcx> {
1043         self.tcx.mk_const_var(self.next_const_var_id(origin), ty)
1044     }
1045
1046     pub fn next_const_var_in_universe(
1047         &self,
1048         ty: Ty<'tcx>,
1049         origin: ConstVariableOrigin,
1050         universe: ty::UniverseIndex,
1051     ) -> &'tcx ty::Const<'tcx> {
1052         let vid = self
1053             .inner
1054             .borrow_mut()
1055             .const_unification_table()
1056             .new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1057         self.tcx.mk_const_var(vid, ty)
1058     }
1059
1060     pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
1061         self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1062             origin,
1063             val: ConstVariableValue::Unknown { universe: self.universe() },
1064         })
1065     }
1066
1067     fn next_int_var_id(&self) -> IntVid {
1068         self.inner.borrow_mut().int_unification_table().new_key(None)
1069     }
1070
1071     pub fn next_int_var(&self) -> Ty<'tcx> {
1072         self.tcx.mk_int_var(self.next_int_var_id())
1073     }
1074
1075     fn next_float_var_id(&self) -> FloatVid {
1076         self.inner.borrow_mut().float_unification_table().new_key(None)
1077     }
1078
1079     pub fn next_float_var(&self) -> Ty<'tcx> {
1080         self.tcx.mk_float_var(self.next_float_var_id())
1081     }
1082
1083     /// Creates a fresh region variable with the next available index.
1084     /// The variable will be created in the maximum universe created
1085     /// thus far, allowing it to name any region created thus far.
1086     pub fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> {
1087         self.next_region_var_in_universe(origin, self.universe())
1088     }
1089
1090     /// Creates a fresh region variable with the next available index
1091     /// in the given universe; typically, you can use
1092     /// `next_region_var` and just use the maximal universe.
1093     pub fn next_region_var_in_universe(
1094         &self,
1095         origin: RegionVariableOrigin,
1096         universe: ty::UniverseIndex,
1097     ) -> ty::Region<'tcx> {
1098         let region_var =
1099             self.inner.borrow_mut().unwrap_region_constraints().new_region_var(universe, origin);
1100         self.tcx.mk_region(ty::ReVar(region_var))
1101     }
1102
1103     /// Return the universe that the region `r` was created in.  For
1104     /// most regions (e.g., `'static`, named regions from the user,
1105     /// etc) this is the root universe U0. For inference variables or
1106     /// placeholders, however, it will return the universe which which
1107     /// they are associated.
1108     fn universe_of_region(&self, r: ty::Region<'tcx>) -> ty::UniverseIndex {
1109         self.inner.borrow_mut().unwrap_region_constraints().universe(r)
1110     }
1111
1112     /// Number of region variables created so far.
1113     pub fn num_region_vars(&self) -> usize {
1114         self.inner.borrow_mut().unwrap_region_constraints().num_region_vars()
1115     }
1116
1117     /// Just a convenient wrapper of `next_region_var` for using during NLL.
1118     pub fn next_nll_region_var(&self, origin: NLLRegionVariableOrigin) -> ty::Region<'tcx> {
1119         self.next_region_var(RegionVariableOrigin::NLL(origin))
1120     }
1121
1122     /// Just a convenient wrapper of `next_region_var` for using during NLL.
1123     pub fn next_nll_region_var_in_universe(
1124         &self,
1125         origin: NLLRegionVariableOrigin,
1126         universe: ty::UniverseIndex,
1127     ) -> ty::Region<'tcx> {
1128         self.next_region_var_in_universe(RegionVariableOrigin::NLL(origin), universe)
1129     }
1130
1131     pub fn var_for_def(&self, span: Span, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
1132         match param.kind {
1133             GenericParamDefKind::Lifetime => {
1134                 // Create a region inference variable for the given
1135                 // region parameter definition.
1136                 self.next_region_var(EarlyBoundRegion(span, param.name)).into()
1137             }
1138             GenericParamDefKind::Type { .. } => {
1139                 // Create a type inference variable for the given
1140                 // type parameter definition. The substitutions are
1141                 // for actual parameters that may be referred to by
1142                 // the default of this type parameter, if it exists.
1143                 // e.g., `struct Foo<A, B, C = (A, B)>(...);` when
1144                 // used in a path such as `Foo::<T, U>::new()` will
1145                 // use an inference variable for `C` with `[T, U]`
1146                 // as the substitutions for the default, `(T, U)`.
1147                 let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
1148                     self.universe(),
1149                     false,
1150                     TypeVariableOrigin {
1151                         kind: TypeVariableOriginKind::TypeParameterDefinition(
1152                             param.name,
1153                             Some(param.def_id),
1154                         ),
1155                         span,
1156                     },
1157                 );
1158
1159                 self.tcx.mk_ty_var(ty_var_id).into()
1160             }
1161             GenericParamDefKind::Const { .. } => {
1162                 let origin = ConstVariableOrigin {
1163                     kind: ConstVariableOriginKind::ConstParameterDefinition(param.name),
1164                     span,
1165                 };
1166                 let const_var_id =
1167                     self.inner.borrow_mut().const_unification_table().new_key(ConstVarValue {
1168                         origin,
1169                         val: ConstVariableValue::Unknown { universe: self.universe() },
1170                     });
1171                 self.tcx.mk_const_var(const_var_id, self.tcx.type_of(param.def_id)).into()
1172             }
1173         }
1174     }
1175
1176     /// Given a set of generics defined on a type or impl, returns a substitution mapping each
1177     /// type/region parameter to a fresh inference variable.
1178     pub fn fresh_substs_for_item(&self, span: Span, def_id: DefId) -> SubstsRef<'tcx> {
1179         InternalSubsts::for_item(self.tcx, def_id, |param, _| self.var_for_def(span, param))
1180     }
1181
1182     /// Returns `true` if errors have been reported since this infcx was
1183     /// created. This is sometimes used as a heuristic to skip
1184     /// reporting errors that often occur as a result of earlier
1185     /// errors, but where it's hard to be 100% sure (e.g., unresolved
1186     /// inference variables, regionck errors).
1187     pub fn is_tainted_by_errors(&self) -> bool {
1188         debug!(
1189             "is_tainted_by_errors(err_count={}, err_count_on_creation={}, \
1190              tainted_by_errors_flag={})",
1191             self.tcx.sess.err_count(),
1192             self.err_count_on_creation,
1193             self.tainted_by_errors_flag.get()
1194         );
1195
1196         if self.tcx.sess.err_count() > self.err_count_on_creation {
1197             return true; // errors reported since this infcx was made
1198         }
1199         self.tainted_by_errors_flag.get()
1200     }
1201
1202     /// Set the "tainted by errors" flag to true. We call this when we
1203     /// observe an error from a prior pass.
1204     pub fn set_tainted_by_errors(&self) {
1205         debug!("set_tainted_by_errors()");
1206         self.tainted_by_errors_flag.set(true)
1207     }
1208
1209     /// Process the region constraints and report any errors that
1210     /// result. After this, no more unification operations should be
1211     /// done -- or the compiler will panic -- but it is legal to use
1212     /// `resolve_vars_if_possible` as well as `fully_resolve`.
1213     pub fn resolve_regions_and_report_errors(
1214         &self,
1215         region_context: DefId,
1216         region_map: &region::ScopeTree,
1217         outlives_env: &OutlivesEnvironment<'tcx>,
1218         mode: RegionckMode,
1219     ) {
1220         let (var_infos, data) = {
1221             let mut inner = self.inner.borrow_mut();
1222             let inner = &mut *inner;
1223             assert!(
1224                 self.is_tainted_by_errors() || inner.region_obligations.is_empty(),
1225                 "region_obligations not empty: {:#?}",
1226                 inner.region_obligations
1227             );
1228             inner
1229                 .region_constraint_storage
1230                 .take()
1231                 .expect("regions already resolved")
1232                 .with_log(&mut inner.undo_log)
1233                 .into_infos_and_data()
1234         };
1235
1236         let region_rels = &RegionRelations::new(
1237             self.tcx,
1238             region_context,
1239             region_map,
1240             outlives_env.free_region_map(),
1241         );
1242
1243         let (lexical_region_resolutions, errors) =
1244             lexical_region_resolve::resolve(region_rels, var_infos, data, mode);
1245
1246         let old_value = self.lexical_region_resolutions.replace(Some(lexical_region_resolutions));
1247         assert!(old_value.is_none());
1248
1249         if !self.is_tainted_by_errors() {
1250             // As a heuristic, just skip reporting region errors
1251             // altogether if other errors have been reported while
1252             // this infcx was in use.  This is totally hokey but
1253             // otherwise we have a hard time separating legit region
1254             // errors from silly ones.
1255             self.report_region_errors(region_map, &errors);
1256         }
1257     }
1258
1259     /// Obtains (and clears) the current set of region
1260     /// constraints. The inference context is still usable: further
1261     /// unifications will simply add new constraints.
1262     ///
1263     /// This method is not meant to be used with normal lexical region
1264     /// resolution. Rather, it is used in the NLL mode as a kind of
1265     /// interim hack: basically we run normal type-check and generate
1266     /// region constraints as normal, but then we take them and
1267     /// translate them into the form that the NLL solver
1268     /// understands. See the NLL module for mode details.
1269     pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
1270         assert!(
1271             self.inner.borrow().region_obligations.is_empty(),
1272             "region_obligations not empty: {:#?}",
1273             self.inner.borrow().region_obligations
1274         );
1275
1276         self.inner.borrow_mut().unwrap_region_constraints().take_and_reset_data()
1277     }
1278
1279     /// Gives temporary access to the region constraint data.
1280     #[allow(non_camel_case_types)] // bug with impl trait
1281     pub fn with_region_constraints<R>(
1282         &self,
1283         op: impl FnOnce(&RegionConstraintData<'tcx>) -> R,
1284     ) -> R {
1285         let mut inner = self.inner.borrow_mut();
1286         op(inner.unwrap_region_constraints().data())
1287     }
1288
1289     /// Takes ownership of the list of variable regions. This implies
1290     /// that all the region constraints have already been taken, and
1291     /// hence that `resolve_regions_and_report_errors` can never be
1292     /// called. This is used only during NLL processing to "hand off" ownership
1293     /// of the set of region variables into the NLL region context.
1294     pub fn take_region_var_origins(&self) -> VarInfos {
1295         let mut inner = self.inner.borrow_mut();
1296         let (var_infos, data) = inner
1297             .region_constraint_storage
1298             .take()
1299             .expect("regions already resolved")
1300             .with_log(&mut inner.undo_log)
1301             .into_infos_and_data();
1302         assert!(data.is_empty());
1303         var_infos
1304     }
1305
1306     pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {
1307         self.resolve_vars_if_possible(&t).to_string()
1308     }
1309
1310     pub fn tys_to_string(&self, ts: &[Ty<'tcx>]) -> String {
1311         let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect();
1312         format!("({})", tstrs.join(", "))
1313     }
1314
1315     pub fn trait_ref_to_string(&self, t: &ty::TraitRef<'tcx>) -> String {
1316         self.resolve_vars_if_possible(t).print_only_trait_path().to_string()
1317     }
1318
1319     /// If `TyVar(vid)` resolves to a type, return that type. Else, return the
1320     /// universe index of `TyVar(vid)`.
1321     pub fn probe_ty_var(&self, vid: TyVid) -> Result<Ty<'tcx>, ty::UniverseIndex> {
1322         use self::type_variable::TypeVariableValue;
1323
1324         match self.inner.borrow_mut().type_variables().probe(vid) {
1325             TypeVariableValue::Known { value } => Ok(value),
1326             TypeVariableValue::Unknown { universe } => Err(universe),
1327         }
1328     }
1329
1330     /// Resolve any type variables found in `value` -- but only one
1331     /// level.  So, if the variable `?X` is bound to some type
1332     /// `Foo<?Y>`, then this would return `Foo<?Y>` (but `?Y` may
1333     /// itself be bound to a type).
1334     ///
1335     /// Useful when you only need to inspect the outermost level of
1336     /// the type and don't care about nested types (or perhaps you
1337     /// will be resolving them as well, e.g. in a loop).
1338     pub fn shallow_resolve<T>(&self, value: T) -> T
1339     where
1340         T: TypeFoldable<'tcx>,
1341     {
1342         value.fold_with(&mut ShallowResolver { infcx: self })
1343     }
1344
1345     pub fn root_var(&self, var: ty::TyVid) -> ty::TyVid {
1346         self.inner.borrow_mut().type_variables().root_var(var)
1347     }
1348
1349     /// Where possible, replaces type/const variables in
1350     /// `value` with their final value. Note that region variables
1351     /// are unaffected. If a type/const variable has not been unified, it
1352     /// is left as is. This is an idempotent operation that does
1353     /// not affect inference state in any way and so you can do it
1354     /// at will.
1355     pub fn resolve_vars_if_possible<T>(&self, value: &T) -> T
1356     where
1357         T: TypeFoldable<'tcx>,
1358     {
1359         if !value.needs_infer() {
1360             return value.clone(); // Avoid duplicated subst-folding.
1361         }
1362         let mut r = resolve::OpportunisticVarResolver::new(self);
1363         value.fold_with(&mut r)
1364     }
1365
1366     /// Returns the first unresolved variable contained in `T`. In the
1367     /// process of visiting `T`, this will resolve (where possible)
1368     /// type variables in `T`, but it never constructs the final,
1369     /// resolved type, so it's more efficient than
1370     /// `resolve_vars_if_possible()`.
1371     pub fn unresolved_type_vars<T>(&self, value: &T) -> Option<(Ty<'tcx>, Option<Span>)>
1372     where
1373         T: TypeFoldable<'tcx>,
1374     {
1375         let mut r = resolve::UnresolvedTypeFinder::new(self);
1376         value.visit_with(&mut r);
1377         r.first_unresolved
1378     }
1379
1380     pub fn probe_const_var(
1381         &self,
1382         vid: ty::ConstVid<'tcx>,
1383     ) -> Result<&'tcx ty::Const<'tcx>, ty::UniverseIndex> {
1384         match self.inner.borrow_mut().const_unification_table().probe_value(vid).val {
1385             ConstVariableValue::Known { value } => Ok(value),
1386             ConstVariableValue::Unknown { universe } => Err(universe),
1387         }
1388     }
1389
1390     pub fn fully_resolve<T: TypeFoldable<'tcx>>(&self, value: &T) -> FixupResult<'tcx, T> {
1391         /*!
1392          * Attempts to resolve all type/region/const variables in
1393          * `value`. Region inference must have been run already (e.g.,
1394          * by calling `resolve_regions_and_report_errors`). If some
1395          * variable was never unified, an `Err` results.
1396          *
1397          * This method is idempotent, but it not typically not invoked
1398          * except during the writeback phase.
1399          */
1400
1401         resolve::fully_resolve(self, value)
1402     }
1403
1404     // [Note-Type-error-reporting]
1405     // An invariant is that anytime the expected or actual type is Error (the special
1406     // error type, meaning that an error occurred when typechecking this expression),
1407     // this is a derived error. The error cascaded from another error (that was already
1408     // reported), so it's not useful to display it to the user.
1409     // The following methods implement this logic.
1410     // They check if either the actual or expected type is Error, and don't print the error
1411     // in this case. The typechecker should only ever report type errors involving mismatched
1412     // types using one of these methods, and should not call span_err directly for such
1413     // errors.
1414
1415     pub fn type_error_struct_with_diag<M>(
1416         &self,
1417         sp: Span,
1418         mk_diag: M,
1419         actual_ty: Ty<'tcx>,
1420     ) -> DiagnosticBuilder<'tcx>
1421     where
1422         M: FnOnce(String) -> DiagnosticBuilder<'tcx>,
1423     {
1424         let actual_ty = self.resolve_vars_if_possible(&actual_ty);
1425         debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
1426
1427         // Don't report an error if actual type is `Error`.
1428         if actual_ty.references_error() {
1429             return self.tcx.sess.diagnostic().struct_dummy();
1430         }
1431
1432         mk_diag(self.ty_to_string(actual_ty))
1433     }
1434
1435     pub fn report_mismatched_types(
1436         &self,
1437         cause: &ObligationCause<'tcx>,
1438         expected: Ty<'tcx>,
1439         actual: Ty<'tcx>,
1440         err: TypeError<'tcx>,
1441     ) -> DiagnosticBuilder<'tcx> {
1442         let trace = TypeTrace::types(cause, true, expected, actual);
1443         self.report_and_explain_type_error(trace, &err)
1444     }
1445
1446     pub fn report_mismatched_consts(
1447         &self,
1448         cause: &ObligationCause<'tcx>,
1449         expected: &'tcx ty::Const<'tcx>,
1450         actual: &'tcx ty::Const<'tcx>,
1451         err: TypeError<'tcx>,
1452     ) -> DiagnosticBuilder<'tcx> {
1453         let trace = TypeTrace::consts(cause, true, expected, actual);
1454         self.report_and_explain_type_error(trace, &err)
1455     }
1456
1457     pub fn replace_bound_vars_with_fresh_vars<T>(
1458         &self,
1459         span: Span,
1460         lbrct: LateBoundRegionConversionTime,
1461         value: &ty::Binder<T>,
1462     ) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
1463     where
1464         T: TypeFoldable<'tcx>,
1465     {
1466         let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
1467         let fld_t = |_| {
1468             self.next_ty_var(TypeVariableOrigin {
1469                 kind: TypeVariableOriginKind::MiscVariable,
1470                 span,
1471             })
1472         };
1473         let fld_c = |_, ty| {
1474             self.next_const_var(
1475                 ty,
1476                 ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
1477             )
1478         };
1479         self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
1480     }
1481
1482     /// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
1483     pub fn verify_generic_bound(
1484         &self,
1485         origin: SubregionOrigin<'tcx>,
1486         kind: GenericKind<'tcx>,
1487         a: ty::Region<'tcx>,
1488         bound: VerifyBound<'tcx>,
1489     ) {
1490         debug!("verify_generic_bound({:?}, {:?} <: {:?})", kind, a, bound);
1491
1492         self.inner
1493             .borrow_mut()
1494             .unwrap_region_constraints()
1495             .verify_generic_bound(origin, kind, a, bound);
1496     }
1497
1498     /// Obtains the latest type of the given closure; this may be a
1499     /// closure in the current function, in which case its
1500     /// `ClosureKind` may not yet be known.
1501     pub fn closure_kind(&self, closure_substs: SubstsRef<'tcx>) -> Option<ty::ClosureKind> {
1502         let closure_kind_ty = closure_substs.as_closure().kind_ty();
1503         let closure_kind_ty = self.shallow_resolve(closure_kind_ty);
1504         closure_kind_ty.to_opt_closure_kind()
1505     }
1506
1507     /// Clears the selection, evaluation, and projection caches. This is useful when
1508     /// repeatedly attempting to select an `Obligation` while changing only
1509     /// its `ParamEnv`, since `FulfillmentContext` doesn't use probing.
1510     pub fn clear_caches(&self) {
1511         self.selection_cache.clear();
1512         self.evaluation_cache.clear();
1513         self.inner.borrow_mut().projection_cache().clear();
1514     }
1515
1516     fn universe(&self) -> ty::UniverseIndex {
1517         self.universe.get()
1518     }
1519
1520     /// Creates and return a fresh universe that extends all previous
1521     /// universes. Updates `self.universe` to that new universe.
1522     pub fn create_next_universe(&self) -> ty::UniverseIndex {
1523         let u = self.universe.get().next_universe();
1524         self.universe.set(u);
1525         u
1526     }
1527
1528     /// Resolves and evaluates a constant.
1529     ///
1530     /// The constant can be located on a trait like `<A as B>::C`, in which case the given
1531     /// substitutions and environment are used to resolve the constant. Alternatively if the
1532     /// constant has generic parameters in scope the substitutions are used to evaluate the value of
1533     /// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
1534     /// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
1535     /// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
1536     /// returned.
1537     ///
1538     /// This handles inferences variables within both `param_env` and `substs` by
1539     /// performing the operation on their respective canonical forms.
1540     pub fn const_eval_resolve(
1541         &self,
1542         param_env: ty::ParamEnv<'tcx>,
1543         def_id: DefId,
1544         substs: SubstsRef<'tcx>,
1545         promoted: Option<mir::Promoted>,
1546         span: Option<Span>,
1547     ) -> ConstEvalResult<'tcx> {
1548         let mut original_values = OriginalQueryValues::default();
1549         let canonical = self.canonicalize_query(&(param_env, substs), &mut original_values);
1550
1551         let (param_env, substs) = canonical.value;
1552         // The return value is the evaluated value which doesn't contain any reference to inference
1553         // variables, thus we don't need to substitute back the original values.
1554         self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, span)
1555     }
1556
1557     /// If `typ` is a type variable of some kind, resolve it one level
1558     /// (but do not resolve types found in the result). If `typ` is
1559     /// not a type variable, just return it unmodified.
1560     // FIXME(eddyb) inline into `ShallowResolver::visit_ty`.
1561     fn shallow_resolve_ty(&self, typ: Ty<'tcx>) -> Ty<'tcx> {
1562         match typ.kind {
1563             ty::Infer(ty::TyVar(v)) => {
1564                 // Not entirely obvious: if `typ` is a type variable,
1565                 // it can be resolved to an int/float variable, which
1566                 // can then be recursively resolved, hence the
1567                 // recursion. Note though that we prevent type
1568                 // variables from unifying to other type variables
1569                 // directly (though they may be embedded
1570                 // structurally), and we prevent cycles in any case,
1571                 // so this recursion should always be of very limited
1572                 // depth.
1573                 //
1574                 // Note: if these two lines are combined into one we get
1575                 // dynamic borrow errors on `self.inner`.
1576                 let known = self.inner.borrow_mut().type_variables().probe(v).known();
1577                 known.map(|t| self.shallow_resolve_ty(t)).unwrap_or(typ)
1578             }
1579
1580             ty::Infer(ty::IntVar(v)) => self
1581                 .inner
1582                 .borrow_mut()
1583                 .int_unification_table()
1584                 .probe_value(v)
1585                 .map(|v| v.to_type(self.tcx))
1586                 .unwrap_or(typ),
1587
1588             ty::Infer(ty::FloatVar(v)) => self
1589                 .inner
1590                 .borrow_mut()
1591                 .float_unification_table()
1592                 .probe_value(v)
1593                 .map(|v| v.to_type(self.tcx))
1594                 .unwrap_or(typ),
1595
1596             _ => typ,
1597         }
1598     }
1599
1600     /// `ty_or_const_infer_var_changed` is equivalent to one of these two:
1601     ///   * `shallow_resolve(ty) != ty` (where `ty.kind = ty::Infer(_)`)
1602     ///   * `shallow_resolve(ct) != ct` (where `ct.kind = ty::ConstKind::Infer(_)`)
1603     ///
1604     /// However, `ty_or_const_infer_var_changed` is more efficient. It's always
1605     /// inlined, despite being large, because it has only two call sites that
1606     /// are extremely hot (both in `traits::fulfill`'s checking of `stalled_on`
1607     /// inference variables), and it handles both `Ty` and `ty::Const` without
1608     /// having to resort to storing full `GenericArg`s in `stalled_on`.
1609     #[inline(always)]
1610     pub fn ty_or_const_infer_var_changed(&self, infer_var: TyOrConstInferVar<'tcx>) -> bool {
1611         let mut inner = self.inner.borrow_mut();
1612         match infer_var {
1613             TyOrConstInferVar::Ty(v) => {
1614                 use self::type_variable::TypeVariableValue;
1615
1616                 // If `inlined_probe` returns a `Known` value, it never equals
1617                 // `ty::Infer(ty::TyVar(v))`.
1618                 match inner.type_variables().inlined_probe(v) {
1619                     TypeVariableValue::Unknown { .. } => false,
1620                     TypeVariableValue::Known { .. } => true,
1621                 }
1622             }
1623
1624             TyOrConstInferVar::TyInt(v) => {
1625                 // If `inlined_probe_value` returns a value it's always a
1626                 // `ty::Int(_)` or `ty::UInt(_)`, which never matches a
1627                 // `ty::Infer(_)`.
1628                 inner.int_unification_table().inlined_probe_value(v).is_some()
1629             }
1630
1631             TyOrConstInferVar::TyFloat(v) => {
1632                 // If `probe_value` returns a value it's always a
1633                 // `ty::Float(_)`, which never matches a `ty::Infer(_)`.
1634                 //
1635                 // Not `inlined_probe_value(v)` because this call site is colder.
1636                 inner.float_unification_table().probe_value(v).is_some()
1637             }
1638
1639             TyOrConstInferVar::Const(v) => {
1640                 // If `probe_value` returns a `Known` value, it never equals
1641                 // `ty::ConstKind::Infer(ty::InferConst::Var(v))`.
1642                 //
1643                 // Not `inlined_probe_value(v)` because this call site is colder.
1644                 match inner.const_unification_table().probe_value(v).val {
1645                     ConstVariableValue::Unknown { .. } => false,
1646                     ConstVariableValue::Known { .. } => true,
1647                 }
1648             }
1649         }
1650     }
1651 }
1652
1653 /// Helper for `ty_or_const_infer_var_changed` (see comment on that), currently
1654 /// used only for `traits::fulfill`'s list of `stalled_on` inference variables.
1655 #[derive(Copy, Clone, Debug)]
1656 pub enum TyOrConstInferVar<'tcx> {
1657     /// Equivalent to `ty::Infer(ty::TyVar(_))`.
1658     Ty(TyVid),
1659     /// Equivalent to `ty::Infer(ty::IntVar(_))`.
1660     TyInt(IntVid),
1661     /// Equivalent to `ty::Infer(ty::FloatVar(_))`.
1662     TyFloat(FloatVid),
1663
1664     /// Equivalent to `ty::ConstKind::Infer(ty::InferConst::Var(_))`.
1665     Const(ConstVid<'tcx>),
1666 }
1667
1668 impl TyOrConstInferVar<'tcx> {
1669     /// Tries to extract an inference variable from a type or a constant, returns `None`
1670     /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1671     /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1672     pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option<Self> {
1673         match arg.unpack() {
1674             GenericArgKind::Type(ty) => Self::maybe_from_ty(ty),
1675             GenericArgKind::Const(ct) => Self::maybe_from_const(ct),
1676             GenericArgKind::Lifetime(_) => None,
1677         }
1678     }
1679
1680     /// Tries to extract an inference variable from a type, returns `None`
1681     /// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
1682     pub fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {
1683         match ty.kind {
1684             ty::Infer(ty::TyVar(v)) => Some(TyOrConstInferVar::Ty(v)),
1685             ty::Infer(ty::IntVar(v)) => Some(TyOrConstInferVar::TyInt(v)),
1686             ty::Infer(ty::FloatVar(v)) => Some(TyOrConstInferVar::TyFloat(v)),
1687             _ => None,
1688         }
1689     }
1690
1691     /// Tries to extract an inference variable from a constant, returns `None`
1692     /// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1693     pub fn maybe_from_const(ct: &'tcx ty::Const<'tcx>) -> Option<Self> {
1694         match ct.val {
1695             ty::ConstKind::Infer(InferConst::Var(v)) => Some(TyOrConstInferVar::Const(v)),
1696             _ => None,
1697         }
1698     }
1699 }
1700
1701 struct ShallowResolver<'a, 'tcx> {
1702     infcx: &'a InferCtxt<'a, 'tcx>,
1703 }
1704
1705 impl<'a, 'tcx> TypeFolder<'tcx> for ShallowResolver<'a, 'tcx> {
1706     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1707         self.infcx.tcx
1708     }
1709
1710     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1711         self.infcx.shallow_resolve_ty(ty)
1712     }
1713
1714     fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
1715         if let ty::Const { val: ty::ConstKind::Infer(InferConst::Var(vid)), .. } = ct {
1716             self.infcx
1717                 .inner
1718                 .borrow_mut()
1719                 .const_unification_table()
1720                 .probe_value(*vid)
1721                 .val
1722                 .known()
1723                 .unwrap_or(ct)
1724         } else {
1725             ct
1726         }
1727     }
1728 }
1729
1730 impl<'tcx> TypeTrace<'tcx> {
1731     pub fn span(&self) -> Span {
1732         self.cause.span
1733     }
1734
1735     pub fn types(
1736         cause: &ObligationCause<'tcx>,
1737         a_is_expected: bool,
1738         a: Ty<'tcx>,
1739         b: Ty<'tcx>,
1740     ) -> TypeTrace<'tcx> {
1741         TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
1742     }
1743
1744     pub fn consts(
1745         cause: &ObligationCause<'tcx>,
1746         a_is_expected: bool,
1747         a: &'tcx ty::Const<'tcx>,
1748         b: &'tcx ty::Const<'tcx>,
1749     ) -> TypeTrace<'tcx> {
1750         TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
1751     }
1752
1753     pub fn dummy(tcx: TyCtxt<'tcx>) -> TypeTrace<'tcx> {
1754         TypeTrace {
1755             cause: ObligationCause::dummy(),
1756             values: Types(ExpectedFound { expected: tcx.types.err, found: tcx.types.err }),
1757         }
1758     }
1759 }
1760
1761 impl<'tcx> SubregionOrigin<'tcx> {
1762     pub fn span(&self) -> Span {
1763         match *self {
1764             Subtype(ref a) => a.span(),
1765             RelateObjectBound(a) => a,
1766             RelateParamBound(a, _) => a,
1767             RelateRegionParamBound(a) => a,
1768             Reborrow(a) => a,
1769             ReborrowUpvar(a, _) => a,
1770             DataBorrowed(_, a) => a,
1771             ReferenceOutlivesReferent(_, a) => a,
1772             CallReturn(a) => a,
1773             CompareImplMethodObligation { span, .. } => span,
1774         }
1775     }
1776
1777     pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, default: F) -> Self
1778     where
1779         F: FnOnce() -> Self,
1780     {
1781         match cause.code {
1782             traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => {
1783                 SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span)
1784             }
1785
1786             traits::ObligationCauseCode::CompareImplMethodObligation {
1787                 item_name,
1788                 impl_item_def_id,
1789                 trait_item_def_id,
1790             } => SubregionOrigin::CompareImplMethodObligation {
1791                 span: cause.span,
1792                 item_name,
1793                 impl_item_def_id,
1794                 trait_item_def_id,
1795             },
1796
1797             _ => default(),
1798         }
1799     }
1800 }
1801
1802 impl RegionVariableOrigin {
1803     pub fn span(&self) -> Span {
1804         match *self {
1805             MiscVariable(a) => a,
1806             PatternRegion(a) => a,
1807             AddrOfRegion(a) => a,
1808             Autoref(a) => a,
1809             Coercion(a) => a,
1810             EarlyBoundRegion(a, ..) => a,
1811             LateBoundRegion(a, ..) => a,
1812             BoundRegionInCoherence(_) => rustc_span::DUMMY_SP,
1813             UpvarRegion(_, a) => a,
1814             NLL(..) => bug!("NLL variable used with `span`"),
1815         }
1816     }
1817 }
1818
1819 impl<'tcx> fmt::Debug for RegionObligation<'tcx> {
1820     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1821         write!(
1822             f,
1823             "RegionObligation(sub_region={:?}, sup_type={:?})",
1824             self.sub_region, self.sup_type
1825         )
1826     }
1827 }