]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
85aaf69f | 11 | //! See the Book for more information. |
1a4d82fc | 12 | |
1a4d82fc JJ |
13 | pub use self::LateBoundRegionConversionTime::*; |
14 | pub use self::RegionVariableOrigin::*; | |
15 | pub use self::SubregionOrigin::*; | |
1a4d82fc | 16 | pub use self::ValuePairs::*; |
54a0048b | 17 | pub use ty::IntVarValue; |
1a4d82fc | 18 | pub use self::freshen::TypeFreshener; |
e9174d1e | 19 | pub use self::region_inference::{GenericKind, VerifyBound}; |
1a4d82fc | 20 | |
54a0048b SL |
21 | use hir::def_id::DefId; |
22 | use hir; | |
bd371182 | 23 | use middle::free_region::FreeRegionMap; |
c1a9b12d SL |
24 | use middle::mem_categorization as mc; |
25 | use middle::mem_categorization::McResult; | |
26 | use middle::region::CodeExtent; | |
a7813a04 | 27 | use mir::tcx::LvalueTy; |
c30ab7b3 | 28 | use ty::subst::{Kind, Subst, Substs}; |
54a0048b SL |
29 | use ty::adjustment; |
30 | use ty::{TyVid, IntVid, FloatVid}; | |
31 | use ty::{self, Ty, TyCtxt}; | |
32 | use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric}; | |
5bcae85e | 33 | use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; |
54a0048b | 34 | use ty::relate::{Relate, RelateResult, TypeRelation}; |
5bcae85e | 35 | use traits::{self, PredicateObligations, Reveal}; |
d9579d0f | 36 | use rustc_data_structures::unify::{self, UnificationTable}; |
a7813a04 | 37 | use std::cell::{Cell, RefCell, Ref, RefMut}; |
c34b1796 | 38 | use std::fmt; |
1a4d82fc | 39 | use syntax::ast; |
3157f602 XL |
40 | use errors::DiagnosticBuilder; |
41 | use syntax_pos::{self, Span, DUMMY_SP}; | |
e9174d1e | 42 | use util::nodemap::{FnvHashMap, FnvHashSet, NodeMap}; |
1a4d82fc | 43 | |
c34b1796 | 44 | use self::combine::CombineFields; |
3157f602 | 45 | use self::higher_ranked::HrMatchResult; |
1a4d82fc | 46 | use self::region_inference::{RegionVarBindings, RegionSnapshot}; |
d9579d0f | 47 | use self::unify_key::ToType; |
1a4d82fc | 48 | |
5bcae85e SL |
49 | mod bivariate; |
50 | mod combine; | |
51 | mod equate; | |
1a4d82fc | 52 | pub mod error_reporting; |
5bcae85e | 53 | mod glb; |
1a4d82fc JJ |
54 | mod higher_ranked; |
55 | pub mod lattice; | |
5bcae85e | 56 | mod lub; |
1a4d82fc JJ |
57 | pub mod region_inference; |
58 | pub mod resolve; | |
59 | mod freshen; | |
5bcae85e | 60 | mod sub; |
1a4d82fc | 61 | pub mod type_variable; |
d9579d0f | 62 | pub mod unify_key; |
1a4d82fc | 63 | |
3157f602 | 64 | #[must_use] |
54a0048b SL |
65 | pub struct InferOk<'tcx, T> { |
66 | pub value: T, | |
67 | pub obligations: PredicateObligations<'tcx>, | |
68 | } | |
69 | pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>; | |
70 | ||
1a4d82fc | 71 | pub type Bound<T> = Option<T>; |
c34b1796 | 72 | pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result" |
c1a9b12d | 73 | pub type FixupResult<T> = Result<T, FixupError>; // "fixup result" |
1a4d82fc | 74 | |
a7813a04 XL |
75 | /// A version of &ty::Tables which can be global or local. |
76 | /// Only the local version supports borrow_mut. | |
77 | #[derive(Copy, Clone)] | |
78 | pub enum InferTables<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { | |
79 | Global(&'a RefCell<ty::Tables<'gcx>>), | |
80 | Local(&'a RefCell<ty::Tables<'tcx>>) | |
81 | } | |
82 | ||
83 | impl<'a, 'gcx, 'tcx> InferTables<'a, 'gcx, 'tcx> { | |
84 | pub fn borrow(self) -> Ref<'a, ty::Tables<'tcx>> { | |
85 | match self { | |
86 | InferTables::Global(tables) => tables.borrow(), | |
87 | InferTables::Local(tables) => tables.borrow() | |
88 | } | |
89 | } | |
90 | ||
91 | pub fn borrow_mut(self) -> RefMut<'a, ty::Tables<'tcx>> { | |
92 | match self { | |
93 | InferTables::Global(_) => { | |
94 | bug!("InferTables: infcx.tables.borrow_mut() outside of type-checking"); | |
95 | } | |
96 | InferTables::Local(tables) => tables.borrow_mut() | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { | |
102 | pub tcx: TyCtxt<'a, 'gcx, 'tcx>, | |
1a4d82fc | 103 | |
a7813a04 | 104 | pub tables: InferTables<'a, 'gcx, 'tcx>, |
c1a9b12d | 105 | |
3157f602 XL |
106 | // Cache for projections. This cache is snapshotted along with the |
107 | // infcx. | |
108 | // | |
109 | // Public so that `traits::project` can use it. | |
110 | pub projection_cache: RefCell<traits::ProjectionCache<'tcx>>, | |
111 | ||
1a4d82fc JJ |
112 | // We instantiate UnificationTable with bounds<Ty> because the |
113 | // types that might instantiate a general type variable have an | |
114 | // order, represented by its upper and lower bounds. | |
115 | type_variables: RefCell<type_variable::TypeVariableTable<'tcx>>, | |
116 | ||
117 | // Map from integral variable to the kind of integer it represents | |
85aaf69f | 118 | int_unification_table: RefCell<UnificationTable<ty::IntVid>>, |
1a4d82fc JJ |
119 | |
120 | // Map from floating variable to the kind of float it represents | |
85aaf69f | 121 | float_unification_table: RefCell<UnificationTable<ty::FloatVid>>, |
1a4d82fc JJ |
122 | |
123 | // For region variables. | |
a7813a04 XL |
124 | region_vars: RegionVarBindings<'a, 'gcx, 'tcx>, |
125 | ||
126 | pub parameter_environment: ty::ParameterEnvironment<'gcx>, | |
127 | ||
128 | /// Caches the results of trait selection. This cache is used | |
129 | /// for things that have to do with the parameters in scope. | |
130 | pub selection_cache: traits::SelectionCache<'tcx>, | |
c1a9b12d | 131 | |
a7813a04 XL |
132 | /// Caches the results of trait evaluation. |
133 | pub evaluation_cache: traits::EvaluationCache<'tcx>, | |
c1a9b12d | 134 | |
e9174d1e SL |
135 | // the set of predicates on which errors have been reported, to |
136 | // avoid reporting the same error twice. | |
137 | pub reported_trait_errors: RefCell<FnvHashSet<traits::TraitErrorKey<'tcx>>>, | |
138 | ||
54a0048b SL |
139 | // Sadly, the behavior of projection varies a bit depending on the |
140 | // stage of compilation. The specifics are given in the | |
5bcae85e SL |
141 | // documentation for `Reveal`. |
142 | projection_mode: Reveal, | |
54a0048b | 143 | |
a7813a04 XL |
144 | // When an error occurs, we want to avoid reporting "derived" |
145 | // errors that are due to this original failure. Normally, we | |
146 | // handle this with the `err_count_on_creation` count, which | |
147 | // basically just tracks how many errors were reported when we | |
148 | // started type-checking a fn and checks to see if any new errors | |
149 | // have been reported since then. Not great, but it works. | |
150 | // | |
151 | // However, when errors originated in other passes -- notably | |
152 | // resolve -- this heuristic breaks down. Therefore, we have this | |
153 | // auxiliary flag that one can set whenever one creates a | |
154 | // type-error that is due to an error in a prior pass. | |
155 | // | |
156 | // Don't read this flag directly, call `is_tainted_by_errors()` | |
157 | // and `set_tainted_by_errors()`. | |
158 | tainted_by_errors_flag: Cell<bool>, | |
159 | ||
160 | // Track how many errors were reported when this infcx is created. | |
161 | // If the number of errors increases, that's also a sign (line | |
162 | // `tained_by_errors`) to avoid reporting certain kinds of errors. | |
c1a9b12d | 163 | err_count_on_creation: usize, |
3157f602 XL |
164 | |
165 | // This flag is used for debugging, and is set to true if there are | |
166 | // any obligations set during the current snapshot. In that case, the | |
167 | // snapshot can't be rolled back. | |
168 | pub obligations_in_snapshot: Cell<bool>, | |
1a4d82fc JJ |
169 | } |
170 | ||
171 | /// A map returned by `skolemize_late_bound_regions()` indicating the skolemized | |
172 | /// region that each late-bound region was replaced with. | |
9e0c209e | 173 | pub type SkolemizationMap<'tcx> = FnvHashMap<ty::BoundRegion, &'tcx ty::Region>; |
1a4d82fc JJ |
174 | |
175 | /// Why did we require that the two types be related? | |
176 | /// | |
177 | /// See `error_reporting.rs` for more details | |
85aaf69f | 178 | #[derive(Clone, Copy, Debug)] |
1a4d82fc JJ |
179 | pub enum TypeOrigin { |
180 | // Not yet categorized in a better way | |
181 | Misc(Span), | |
182 | ||
183 | // Checking that method of impl is compatible with trait | |
184 | MethodCompatCheck(Span), | |
185 | ||
186 | // Checking that this expression can be assigned where it needs to be | |
187 | // FIXME(eddyb) #11161 is the original Expr required? | |
188 | ExprAssignable(Span), | |
189 | ||
1a4d82fc JJ |
190 | // Relating trait type parameters to those found in impl etc |
191 | RelateOutputImplTypes(Span), | |
192 | ||
193 | // Computing common supertype in the arms of a match expression | |
92a42be0 | 194 | MatchExpressionArm(Span, Span, hir::MatchSource), |
1a4d82fc JJ |
195 | |
196 | // Computing common supertype in an if expression | |
197 | IfExpression(Span), | |
198 | ||
199 | // Computing common supertype of an if expression with no else counter-part | |
200 | IfExpressionWithNoElse(Span), | |
201 | ||
1a4d82fc JJ |
202 | // `where a == b` |
203 | EquatePredicate(Span), | |
5bcae85e SL |
204 | |
205 | // `main` has wrong type | |
206 | MainFunctionType(Span), | |
207 | ||
208 | // `start` has wrong type | |
209 | StartFunctionType(Span), | |
210 | ||
211 | // intrinsic has wrong type | |
212 | IntrinsicType(Span), | |
213 | ||
214 | // method receiver | |
215 | MethodReceiver(Span), | |
1a4d82fc JJ |
216 | } |
217 | ||
c34b1796 | 218 | impl TypeOrigin { |
5bcae85e | 219 | fn as_failure_str(&self) -> &'static str { |
c34b1796 AL |
220 | match self { |
221 | &TypeOrigin::Misc(_) | | |
c34b1796 AL |
222 | &TypeOrigin::RelateOutputImplTypes(_) | |
223 | &TypeOrigin::ExprAssignable(_) => "mismatched types", | |
c34b1796 | 224 | &TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait", |
9e0c209e | 225 | &TypeOrigin::MatchExpressionArm(.., source) => match source { |
92a42be0 SL |
226 | hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types", |
227 | _ => "match arms have incompatible types", | |
228 | }, | |
c34b1796 AL |
229 | &TypeOrigin::IfExpression(_) => "if and else have incompatible types", |
230 | &TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause", | |
c34b1796 | 231 | &TypeOrigin::EquatePredicate(_) => "equality predicate not satisfied", |
5bcae85e SL |
232 | &TypeOrigin::MainFunctionType(_) => "main function has wrong type", |
233 | &TypeOrigin::StartFunctionType(_) => "start function has wrong type", | |
234 | &TypeOrigin::IntrinsicType(_) => "intrinsic has wrong type", | |
235 | &TypeOrigin::MethodReceiver(_) => "mismatched method receiver", | |
c34b1796 AL |
236 | } |
237 | } | |
c34b1796 | 238 | |
5bcae85e SL |
239 | fn as_requirement_str(&self) -> &'static str { |
240 | match self { | |
241 | &TypeOrigin::Misc(_) => "types are compatible", | |
242 | &TypeOrigin::MethodCompatCheck(_) => "method type is compatible with trait", | |
243 | &TypeOrigin::ExprAssignable(_) => "expression is assignable", | |
244 | &TypeOrigin::RelateOutputImplTypes(_) => { | |
245 | "trait type parameters matches those specified on the impl" | |
246 | } | |
9e0c209e | 247 | &TypeOrigin::MatchExpressionArm(..) => "match arms have compatible types", |
5bcae85e SL |
248 | &TypeOrigin::IfExpression(_) => "if and else have compatible types", |
249 | &TypeOrigin::IfExpressionWithNoElse(_) => "if missing an else returns ()", | |
5bcae85e SL |
250 | &TypeOrigin::EquatePredicate(_) => "equality where clause is satisfied", |
251 | &TypeOrigin::MainFunctionType(_) => "`main` function has the correct type", | |
252 | &TypeOrigin::StartFunctionType(_) => "`start` function has the correct type", | |
253 | &TypeOrigin::IntrinsicType(_) => "intrinsic has the correct type", | |
254 | &TypeOrigin::MethodReceiver(_) => "method receiver has the correct type", | |
255 | } | |
c34b1796 AL |
256 | } |
257 | } | |
258 | ||
1a4d82fc | 259 | /// See `error_reporting.rs` for more details |
85aaf69f | 260 | #[derive(Clone, Debug)] |
1a4d82fc | 261 | pub enum ValuePairs<'tcx> { |
e9174d1e SL |
262 | Types(ExpectedFound<Ty<'tcx>>), |
263 | TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>), | |
264 | PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>), | |
1a4d82fc JJ |
265 | } |
266 | ||
267 | /// The trace designates the path through inference that we took to | |
268 | /// encounter an error or subtyping constraint. | |
269 | /// | |
270 | /// See `error_reporting.rs` for more details. | |
62682a34 | 271 | #[derive(Clone)] |
1a4d82fc JJ |
272 | pub struct TypeTrace<'tcx> { |
273 | origin: TypeOrigin, | |
274 | values: ValuePairs<'tcx>, | |
275 | } | |
276 | ||
277 | /// The origin of a `r1 <= r2` constraint. | |
278 | /// | |
279 | /// See `error_reporting.rs` for more details | |
85aaf69f | 280 | #[derive(Clone, Debug)] |
1a4d82fc JJ |
281 | pub enum SubregionOrigin<'tcx> { |
282 | // Arose from a subtyping relation | |
283 | Subtype(TypeTrace<'tcx>), | |
284 | ||
285 | // Stack-allocated closures cannot outlive innermost loop | |
286 | // or function so as to ensure we only require finite stack | |
287 | InfStackClosure(Span), | |
288 | ||
289 | // Invocation of closure must be within its lifetime | |
290 | InvokeClosure(Span), | |
291 | ||
292 | // Dereference of reference must be within its lifetime | |
293 | DerefPointer(Span), | |
294 | ||
295 | // Closure bound must not outlive captured free variables | |
296 | FreeVariable(Span, ast::NodeId), | |
297 | ||
298 | // Index into slice must be within its lifetime | |
299 | IndexSlice(Span), | |
300 | ||
301 | // When casting `&'a T` to an `&'b Trait` object, | |
302 | // relating `'a` to `'b` | |
303 | RelateObjectBound(Span), | |
304 | ||
305 | // Some type parameter was instantiated with the given type, | |
306 | // and that type must outlive some region. | |
307 | RelateParamBound(Span, Ty<'tcx>), | |
308 | ||
309 | // The given region parameter was instantiated with a region | |
310 | // that must outlive some other region. | |
311 | RelateRegionParamBound(Span), | |
312 | ||
313 | // A bound placed on type parameters that states that must outlive | |
314 | // the moment of their instantiation. | |
315 | RelateDefaultParamBound(Span, Ty<'tcx>), | |
316 | ||
317 | // Creating a pointer `b` to contents of another reference | |
318 | Reborrow(Span), | |
319 | ||
320 | // Creating a pointer `b` to contents of an upvar | |
321 | ReborrowUpvar(Span, ty::UpvarId), | |
322 | ||
e9174d1e SL |
323 | // Data with type `Ty<'tcx>` was borrowed |
324 | DataBorrowed(Ty<'tcx>, Span), | |
325 | ||
1a4d82fc JJ |
326 | // (&'a &'b T) where a >= b |
327 | ReferenceOutlivesReferent(Ty<'tcx>, Span), | |
328 | ||
e9174d1e SL |
329 | // Type or region parameters must be in scope. |
330 | ParameterInScope(ParameterOrigin, Span), | |
331 | ||
1a4d82fc JJ |
332 | // The type T of an expression E must outlive the lifetime for E. |
333 | ExprTypeIsNotInScope(Ty<'tcx>, Span), | |
334 | ||
335 | // A `ref b` whose region does not enclose the decl site | |
336 | BindingTypeIsNotValidAtDecl(Span), | |
337 | ||
338 | // Regions appearing in a method receiver must outlive method call | |
339 | CallRcvr(Span), | |
340 | ||
341 | // Regions appearing in a function argument must outlive func call | |
342 | CallArg(Span), | |
343 | ||
344 | // Region in return type of invoked fn must enclose call | |
345 | CallReturn(Span), | |
346 | ||
85aaf69f SL |
347 | // Operands must be in scope |
348 | Operand(Span), | |
349 | ||
1a4d82fc JJ |
350 | // Region resulting from a `&` expr must enclose the `&` expr |
351 | AddrOf(Span), | |
352 | ||
353 | // An auto-borrow that does not enclose the expr where it occurs | |
354 | AutoBorrow(Span), | |
85aaf69f SL |
355 | |
356 | // Region constraint arriving from destructor safety | |
357 | SafeDestructor(Span), | |
c30ab7b3 SL |
358 | |
359 | // Comparing the signature and requirements of an impl method against | |
360 | // the containing trait. | |
361 | CompareImplMethodObligation { | |
362 | span: Span, | |
363 | item_name: ast::Name, | |
364 | impl_item_def_id: DefId, | |
365 | trait_item_def_id: DefId, | |
366 | ||
367 | // this is `Some(_)` if this error arises from the bug fix for | |
368 | // #18937. This is a temporary measure. | |
369 | lint_id: Option<ast::NodeId>, | |
370 | }, | |
1a4d82fc JJ |
371 | } |
372 | ||
e9174d1e SL |
373 | /// Places that type/region parameters can appear. |
374 | #[derive(Clone, Copy, Debug)] | |
375 | pub enum ParameterOrigin { | |
376 | Path, // foo::bar | |
377 | MethodCall, // foo.bar() <-- parameters on impl providing bar() | |
378 | OverloadedOperator, // a + b when overloaded | |
379 | OverloadedDeref, // *a when overloaded | |
380 | } | |
381 | ||
1a4d82fc | 382 | /// Times when we replace late-bound regions with variables: |
85aaf69f | 383 | #[derive(Clone, Copy, Debug)] |
1a4d82fc JJ |
384 | pub enum LateBoundRegionConversionTime { |
385 | /// when a fn is called | |
386 | FnCall, | |
387 | ||
388 | /// when two higher-ranked types are compared | |
389 | HigherRankedType, | |
390 | ||
391 | /// when projecting an associated type | |
392 | AssocTypeProjection(ast::Name), | |
393 | } | |
394 | ||
395 | /// Reasons to create a region inference variable | |
396 | /// | |
397 | /// See `error_reporting.rs` for more details | |
85aaf69f | 398 | #[derive(Clone, Debug)] |
c34b1796 | 399 | pub enum RegionVariableOrigin { |
1a4d82fc JJ |
400 | // Region variables created for ill-categorized reasons, |
401 | // mostly indicates places in need of refactoring | |
402 | MiscVariable(Span), | |
403 | ||
404 | // Regions created by a `&P` or `[...]` pattern | |
405 | PatternRegion(Span), | |
406 | ||
407 | // Regions created by `&` operator | |
408 | AddrOfRegion(Span), | |
409 | ||
1a4d82fc JJ |
410 | // Regions created as part of an autoref of a method receiver |
411 | Autoref(Span), | |
412 | ||
413 | // Regions created as part of an automatic coercion | |
c34b1796 | 414 | Coercion(Span), |
1a4d82fc JJ |
415 | |
416 | // Region variables created as the values for early-bound regions | |
417 | EarlyBoundRegion(Span, ast::Name), | |
418 | ||
419 | // Region variables created for bound regions | |
420 | // in a function or method that is called | |
421 | LateBoundRegion(Span, ty::BoundRegion, LateBoundRegionConversionTime), | |
422 | ||
423 | UpvarRegion(ty::UpvarId, Span), | |
424 | ||
425 | BoundRegionInCoherence(ast::Name), | |
426 | } | |
427 | ||
c34b1796 | 428 | #[derive(Copy, Clone, Debug)] |
c1a9b12d SL |
429 | pub enum FixupError { |
430 | UnresolvedIntTy(IntVid), | |
431 | UnresolvedFloatTy(FloatVid), | |
432 | UnresolvedTy(TyVid) | |
1a4d82fc JJ |
433 | } |
434 | ||
a7813a04 XL |
435 | impl fmt::Display for FixupError { |
436 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
437 | use self::FixupError::*; | |
1a4d82fc | 438 | |
a7813a04 XL |
439 | match *self { |
440 | UnresolvedIntTy(_) => { | |
441 | write!(f, "cannot determine the type of this integer; \ | |
442 | add a suffix to specify the type explicitly") | |
443 | } | |
444 | UnresolvedFloatTy(_) => { | |
445 | write!(f, "cannot determine the type of this number; \ | |
446 | add a suffix to specify the type explicitly") | |
447 | } | |
448 | UnresolvedTy(_) => write!(f, "unconstrained type") | |
449 | } | |
1a4d82fc JJ |
450 | } |
451 | } | |
452 | ||
a7813a04 XL |
453 | /// Helper type of a temporary returned by tcx.infer_ctxt(...). |
454 | /// Necessary because we can't write the following bound: | |
455 | /// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(InferCtxt<'b, 'gcx, 'tcx>). | |
456 | pub struct InferCtxtBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { | |
457 | global_tcx: TyCtxt<'a, 'gcx, 'gcx>, | |
458 | arenas: ty::CtxtArenas<'tcx>, | |
459 | tables: Option<RefCell<ty::Tables<'tcx>>>, | |
460 | param_env: Option<ty::ParameterEnvironment<'gcx>>, | |
5bcae85e | 461 | projection_mode: Reveal, |
1a4d82fc JJ |
462 | } |
463 | ||
a7813a04 XL |
464 | impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> { |
465 | pub fn infer_ctxt(self, | |
466 | tables: Option<ty::Tables<'tcx>>, | |
467 | param_env: Option<ty::ParameterEnvironment<'gcx>>, | |
5bcae85e | 468 | projection_mode: Reveal) |
a7813a04 XL |
469 | -> InferCtxtBuilder<'a, 'gcx, 'tcx> { |
470 | InferCtxtBuilder { | |
471 | global_tcx: self, | |
472 | arenas: ty::CtxtArenas::new(), | |
473 | tables: tables.map(RefCell::new), | |
474 | param_env: param_env, | |
475 | projection_mode: projection_mode, | |
a7813a04 XL |
476 | } |
477 | } | |
1a4d82fc | 478 | |
a7813a04 XL |
479 | /// Fake InferCtxt with the global tcx. Used by pre-MIR borrowck |
480 | /// for MemCategorizationContext/ExprUseVisitor. | |
481 | /// If any inference functionality is used, ICEs will occur. | |
482 | pub fn borrowck_fake_infer_ctxt(self, param_env: ty::ParameterEnvironment<'gcx>) | |
483 | -> InferCtxt<'a, 'gcx, 'gcx> { | |
484 | InferCtxt { | |
485 | tcx: self, | |
486 | tables: InferTables::Global(&self.tables), | |
487 | type_variables: RefCell::new(type_variable::TypeVariableTable::new()), | |
488 | int_unification_table: RefCell::new(UnificationTable::new()), | |
489 | float_unification_table: RefCell::new(UnificationTable::new()), | |
490 | region_vars: RegionVarBindings::new(self), | |
491 | parameter_environment: param_env, | |
492 | selection_cache: traits::SelectionCache::new(), | |
493 | evaluation_cache: traits::EvaluationCache::new(), | |
3157f602 | 494 | projection_cache: RefCell::new(traits::ProjectionCache::new()), |
a7813a04 | 495 | reported_trait_errors: RefCell::new(FnvHashSet()), |
5bcae85e | 496 | projection_mode: Reveal::NotSpecializable, |
a7813a04 | 497 | tainted_by_errors_flag: Cell::new(false), |
3157f602 XL |
498 | err_count_on_creation: self.sess.err_count(), |
499 | obligations_in_snapshot: Cell::new(false), | |
a7813a04 XL |
500 | } |
501 | } | |
92a42be0 SL |
502 | } |
503 | ||
a7813a04 XL |
504 | impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> { |
505 | pub fn enter<F, R>(&'tcx mut self, f: F) -> R | |
506 | where F: for<'b> FnOnce(InferCtxt<'b, 'gcx, 'tcx>) -> R | |
507 | { | |
508 | let InferCtxtBuilder { | |
509 | global_tcx, | |
510 | ref arenas, | |
511 | ref tables, | |
512 | ref mut param_env, | |
513 | projection_mode, | |
a7813a04 XL |
514 | } = *self; |
515 | let tables = if let Some(ref tables) = *tables { | |
516 | InferTables::Local(tables) | |
517 | } else { | |
518 | InferTables::Global(&global_tcx.tables) | |
519 | }; | |
520 | let param_env = param_env.take().unwrap_or_else(|| { | |
521 | global_tcx.empty_parameter_environment() | |
522 | }); | |
523 | global_tcx.enter_local(arenas, |tcx| f(InferCtxt { | |
524 | tcx: tcx, | |
525 | tables: tables, | |
3157f602 | 526 | projection_cache: RefCell::new(traits::ProjectionCache::new()), |
a7813a04 XL |
527 | type_variables: RefCell::new(type_variable::TypeVariableTable::new()), |
528 | int_unification_table: RefCell::new(UnificationTable::new()), | |
529 | float_unification_table: RefCell::new(UnificationTable::new()), | |
530 | region_vars: RegionVarBindings::new(tcx), | |
531 | parameter_environment: param_env, | |
532 | selection_cache: traits::SelectionCache::new(), | |
533 | evaluation_cache: traits::EvaluationCache::new(), | |
534 | reported_trait_errors: RefCell::new(FnvHashSet()), | |
a7813a04 XL |
535 | projection_mode: projection_mode, |
536 | tainted_by_errors_flag: Cell::new(false), | |
3157f602 XL |
537 | err_count_on_creation: tcx.sess.err_count(), |
538 | obligations_in_snapshot: Cell::new(false), | |
a7813a04 XL |
539 | })) |
540 | } | |
54a0048b SL |
541 | } |
542 | ||
a7813a04 XL |
543 | impl<T> ExpectedFound<T> { |
544 | fn new(a_is_expected: bool, a: T, b: T) -> Self { | |
545 | if a_is_expected { | |
546 | ExpectedFound {expected: a, found: b} | |
547 | } else { | |
548 | ExpectedFound {expected: b, found: a} | |
549 | } | |
54a0048b | 550 | } |
1a4d82fc JJ |
551 | } |
552 | ||
a7813a04 | 553 | impl<'tcx, T> InferOk<'tcx, T> { |
3157f602 | 554 | pub fn unit(self) -> InferOk<'tcx, ()> { |
a7813a04 | 555 | InferOk { value: (), obligations: self.obligations } |
1a4d82fc JJ |
556 | } |
557 | } | |
558 | ||
1a4d82fc JJ |
559 | #[must_use = "once you start a snapshot, you should always consume it"] |
560 | pub struct CombinedSnapshot { | |
3157f602 | 561 | projection_cache_snapshot: traits::ProjectionCacheSnapshot, |
1a4d82fc JJ |
562 | type_snapshot: type_variable::Snapshot, |
563 | int_snapshot: unify::Snapshot<ty::IntVid>, | |
564 | float_snapshot: unify::Snapshot<ty::FloatVid>, | |
565 | region_vars_snapshot: RegionSnapshot, | |
3157f602 | 566 | obligations_in_snapshot: bool, |
1a4d82fc JJ |
567 | } |
568 | ||
a7813a04 XL |
569 | /// Helper trait for shortening the lifetimes inside a |
570 | /// value for post-type-checking normalization. | |
571 | pub trait TransNormalize<'gcx>: TypeFoldable<'gcx> { | |
572 | fn trans_normalize<'a, 'tcx>(&self, infcx: &InferCtxt<'a, 'gcx, 'tcx>) -> Self; | |
573 | } | |
c1a9b12d | 574 | |
a7813a04 XL |
575 | macro_rules! items { ($($item:item)+) => ($($item)+) } |
576 | macro_rules! impl_trans_normalize { | |
577 | ($lt_gcx:tt, $($ty:ty),+) => { | |
578 | items!($(impl<$lt_gcx> TransNormalize<$lt_gcx> for $ty { | |
579 | fn trans_normalize<'a, 'tcx>(&self, | |
580 | infcx: &InferCtxt<'a, $lt_gcx, 'tcx>) | |
581 | -> Self { | |
582 | infcx.normalize_projections_in(self) | |
583 | } | |
584 | })+); | |
585 | } | |
586 | } | |
c1a9b12d | 587 | |
a7813a04 XL |
588 | impl_trans_normalize!('gcx, |
589 | Ty<'gcx>, | |
590 | &'gcx Substs<'gcx>, | |
591 | ty::FnSig<'gcx>, | |
a7813a04 XL |
592 | &'gcx ty::BareFnTy<'gcx>, |
593 | ty::ClosureSubsts<'gcx>, | |
9e0c209e SL |
594 | ty::PolyTraitRef<'gcx>, |
595 | ty::ExistentialTraitRef<'gcx> | |
a7813a04 XL |
596 | ); |
597 | ||
598 | impl<'gcx> TransNormalize<'gcx> for LvalueTy<'gcx> { | |
599 | fn trans_normalize<'a, 'tcx>(&self, infcx: &InferCtxt<'a, 'gcx, 'tcx>) -> Self { | |
600 | match *self { | |
601 | LvalueTy::Ty { ty } => LvalueTy::Ty { ty: ty.trans_normalize(infcx) }, | |
602 | LvalueTy::Downcast { adt_def, substs, variant_index } => { | |
603 | LvalueTy::Downcast { | |
604 | adt_def: adt_def, | |
605 | substs: substs.trans_normalize(infcx), | |
606 | variant_index: variant_index | |
607 | } | |
608 | } | |
609 | } | |
c1a9b12d | 610 | } |
a7813a04 | 611 | } |
c1a9b12d | 612 | |
a7813a04 XL |
613 | // NOTE: Callable from trans only! |
614 | impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { | |
9e0c209e SL |
615 | /// Currently, higher-ranked type bounds inhibit normalization. Therefore, |
616 | /// each time we erase them in translation, we need to normalize | |
617 | /// the contents. | |
618 | pub fn erase_late_bound_regions_and_normalize<T>(self, value: &ty::Binder<T>) | |
619 | -> T | |
620 | where T: TransNormalize<'tcx> | |
621 | { | |
622 | assert!(!value.needs_subst()); | |
623 | let value = self.erase_late_bound_regions(value); | |
624 | self.normalize_associated_type(&value) | |
625 | } | |
626 | ||
a7813a04 XL |
627 | pub fn normalize_associated_type<T>(self, value: &T) -> T |
628 | where T: TransNormalize<'tcx> | |
629 | { | |
630 | debug!("normalize_associated_type(t={:?})", value); | |
c1a9b12d | 631 | |
a7813a04 | 632 | let value = self.erase_regions(value); |
c1a9b12d | 633 | |
a7813a04 XL |
634 | if !value.has_projection_types() { |
635 | return value; | |
636 | } | |
c1a9b12d | 637 | |
5bcae85e | 638 | self.infer_ctxt(None, None, Reveal::All).enter(|infcx| { |
a7813a04 XL |
639 | value.trans_normalize(&infcx) |
640 | }) | |
c1a9b12d | 641 | } |
3157f602 XL |
642 | |
643 | pub fn normalize_associated_type_in_env<T>( | |
644 | self, value: &T, env: &'a ty::ParameterEnvironment<'tcx> | |
645 | ) -> T | |
646 | where T: TransNormalize<'tcx> | |
647 | { | |
648 | debug!("normalize_associated_type_in_env(t={:?})", value); | |
649 | ||
650 | let value = self.erase_regions(value); | |
651 | ||
652 | if !value.has_projection_types() { | |
653 | return value; | |
654 | } | |
655 | ||
5bcae85e | 656 | self.infer_ctxt(None, Some(env.clone()), Reveal::All).enter(|infcx| { |
3157f602 XL |
657 | value.trans_normalize(&infcx) |
658 | }) | |
659 | } | |
c1a9b12d SL |
660 | } |
661 | ||
a7813a04 XL |
662 | impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { |
663 | fn normalize_projections_in<T>(&self, value: &T) -> T::Lifted | |
664 | where T: TypeFoldable<'tcx> + ty::Lift<'gcx> | |
665 | { | |
666 | let mut selcx = traits::SelectionContext::new(self); | |
667 | let cause = traits::ObligationCause::dummy(); | |
668 | let traits::Normalized { value: result, obligations } = | |
669 | traits::normalize(&mut selcx, cause, value); | |
670 | ||
671 | debug!("normalize_projections_in: result={:?} obligations={:?}", | |
672 | result, obligations); | |
673 | ||
674 | let mut fulfill_cx = traits::FulfillmentContext::new(); | |
675 | ||
676 | for obligation in obligations { | |
677 | fulfill_cx.register_predicate_obligation(self, obligation); | |
c1a9b12d | 678 | } |
a7813a04 XL |
679 | |
680 | self.drain_fulfillment_cx_or_panic(DUMMY_SP, &mut fulfill_cx, &result) | |
c1a9b12d | 681 | } |
c1a9b12d | 682 | |
9e0c209e SL |
683 | /// Finishes processes any obligations that remain in the |
684 | /// fulfillment context, and then returns the result with all type | |
685 | /// variables removed and regions erased. Because this is intended | |
686 | /// for use after type-check has completed, if any errors occur, | |
687 | /// it will panic. It is used during normalization and other cases | |
688 | /// where processing the obligations in `fulfill_cx` may cause | |
689 | /// type inference variables that appear in `result` to be | |
690 | /// unified, and hence we need to process those obligations to get | |
691 | /// the complete picture of the type. | |
a7813a04 XL |
692 | pub fn drain_fulfillment_cx_or_panic<T>(&self, |
693 | span: Span, | |
694 | fulfill_cx: &mut traits::FulfillmentContext<'tcx>, | |
695 | result: &T) | |
696 | -> T::Lifted | |
697 | where T: TypeFoldable<'tcx> + ty::Lift<'gcx> | |
698 | { | |
3157f602 XL |
699 | debug!("drain_fulfillment_cx_or_panic()"); |
700 | ||
9e0c209e SL |
701 | // In principle, we only need to do this so long as `result` |
702 | // contains unbound type parameters. It could be a slight | |
703 | // optimization to stop iterating early. | |
704 | match fulfill_cx.select_all_or_error(self) { | |
705 | Ok(()) => { } | |
a7813a04 | 706 | Err(errors) => { |
9e0c209e SL |
707 | span_bug!(span, "Encountered errors `{:?}` resolving bounds after type-checking", |
708 | errors); | |
a7813a04 | 709 | } |
9e0c209e SL |
710 | } |
711 | ||
712 | let result = self.resolve_type_vars_if_possible(result); | |
713 | let result = self.tcx.erase_regions(&result); | |
a7813a04 | 714 | |
9e0c209e SL |
715 | match self.tcx.lift_to_global(&result) { |
716 | Some(result) => result, | |
a7813a04 | 717 | None => { |
9e0c209e | 718 | span_bug!(span, "Uninferred types/regions in `{:?}`", result); |
a7813a04 | 719 | } |
c1a9b12d SL |
720 | } |
721 | } | |
722 | ||
5bcae85e | 723 | pub fn projection_mode(&self) -> Reveal { |
54a0048b SL |
724 | self.projection_mode |
725 | } | |
726 | ||
1a4d82fc JJ |
727 | pub fn freshen<T:TypeFoldable<'tcx>>(&self, t: T) -> T { |
728 | t.fold_with(&mut self.freshener()) | |
729 | } | |
730 | ||
731 | pub fn type_var_diverges(&'a self, ty: Ty) -> bool { | |
732 | match ty.sty { | |
62682a34 | 733 | ty::TyInfer(ty::TyVar(vid)) => self.type_variables.borrow().var_diverges(vid), |
1a4d82fc JJ |
734 | _ => false |
735 | } | |
736 | } | |
737 | ||
a7813a04 | 738 | pub fn freshener<'b>(&'b self) -> TypeFreshener<'b, 'gcx, 'tcx> { |
1a4d82fc JJ |
739 | freshen::TypeFreshener::new(self) |
740 | } | |
741 | ||
742 | pub fn type_is_unconstrained_numeric(&'a self, ty: Ty) -> UnconstrainedNumeric { | |
54a0048b SL |
743 | use ty::error::UnconstrainedNumeric::Neither; |
744 | use ty::error::UnconstrainedNumeric::{UnconstrainedInt, UnconstrainedFloat}; | |
1a4d82fc | 745 | match ty.sty { |
62682a34 | 746 | ty::TyInfer(ty::IntVar(vid)) => { |
c34b1796 AL |
747 | if self.int_unification_table.borrow_mut().has_value(vid) { |
748 | Neither | |
749 | } else { | |
750 | UnconstrainedInt | |
1a4d82fc JJ |
751 | } |
752 | }, | |
62682a34 | 753 | ty::TyInfer(ty::FloatVar(vid)) => { |
c34b1796 AL |
754 | if self.float_unification_table.borrow_mut().has_value(vid) { |
755 | Neither | |
756 | } else { | |
757 | UnconstrainedFloat | |
1a4d82fc JJ |
758 | } |
759 | }, | |
760 | _ => Neither, | |
761 | } | |
762 | } | |
763 | ||
c1a9b12d SL |
764 | /// Returns a type variable's default fallback if any exists. A default |
765 | /// must be attached to the variable when created, if it is created | |
766 | /// without a default, this will return None. | |
767 | /// | |
768 | /// This code does not apply to integral or floating point variables, | |
769 | /// only to use declared defaults. | |
770 | /// | |
771 | /// See `new_ty_var_with_default` to create a type variable with a default. | |
772 | /// See `type_variable::Default` for details about what a default entails. | |
773 | pub fn default(&self, ty: Ty<'tcx>) -> Option<type_variable::Default<'tcx>> { | |
774 | match ty.sty { | |
775 | ty::TyInfer(ty::TyVar(vid)) => self.type_variables.borrow().default(vid), | |
776 | _ => None | |
777 | } | |
778 | } | |
779 | ||
780 | pub fn unsolved_variables(&self) -> Vec<ty::Ty<'tcx>> { | |
781 | let mut variables = Vec::new(); | |
782 | ||
783 | let unbound_ty_vars = self.type_variables | |
54a0048b | 784 | .borrow_mut() |
c1a9b12d SL |
785 | .unsolved_variables() |
786 | .into_iter() | |
787 | .map(|t| self.tcx.mk_var(t)); | |
788 | ||
789 | let unbound_int_vars = self.int_unification_table | |
790 | .borrow_mut() | |
791 | .unsolved_variables() | |
792 | .into_iter() | |
793 | .map(|v| self.tcx.mk_int_var(v)); | |
794 | ||
795 | let unbound_float_vars = self.float_unification_table | |
796 | .borrow_mut() | |
797 | .unsolved_variables() | |
798 | .into_iter() | |
799 | .map(|v| self.tcx.mk_float_var(v)); | |
800 | ||
801 | variables.extend(unbound_ty_vars); | |
802 | variables.extend(unbound_int_vars); | |
803 | variables.extend(unbound_float_vars); | |
804 | ||
805 | return variables; | |
806 | } | |
807 | ||
5bcae85e | 808 | fn combine_fields(&'a self, trace: TypeTrace<'tcx>) |
a7813a04 | 809 | -> CombineFields<'a, 'gcx, 'tcx> { |
54a0048b SL |
810 | CombineFields { |
811 | infcx: self, | |
54a0048b SL |
812 | trace: trace, |
813 | cause: None, | |
814 | obligations: PredicateObligations::new(), | |
815 | } | |
1a4d82fc JJ |
816 | } |
817 | ||
54a0048b SL |
818 | pub fn equate<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) |
819 | -> InferResult<'tcx, T> | |
a7813a04 | 820 | where T: Relate<'tcx> |
c34b1796 | 821 | { |
5bcae85e SL |
822 | let mut fields = self.combine_fields(trace); |
823 | let result = fields.equate(a_is_expected).relate(a, b); | |
824 | result.map(move |t| InferOk { value: t, obligations: fields.obligations }) | |
c34b1796 AL |
825 | } |
826 | ||
54a0048b SL |
827 | pub fn sub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) |
828 | -> InferResult<'tcx, T> | |
a7813a04 | 829 | where T: Relate<'tcx> |
c34b1796 | 830 | { |
5bcae85e SL |
831 | let mut fields = self.combine_fields(trace); |
832 | let result = fields.sub(a_is_expected).relate(a, b); | |
833 | result.map(move |t| InferOk { value: t, obligations: fields.obligations }) | |
1a4d82fc JJ |
834 | } |
835 | ||
54a0048b SL |
836 | pub fn lub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) |
837 | -> InferResult<'tcx, T> | |
a7813a04 | 838 | where T: Relate<'tcx> |
c34b1796 | 839 | { |
5bcae85e SL |
840 | let mut fields = self.combine_fields(trace); |
841 | let result = fields.lub(a_is_expected).relate(a, b); | |
842 | result.map(move |t| InferOk { value: t, obligations: fields.obligations }) | |
1a4d82fc JJ |
843 | } |
844 | ||
54a0048b SL |
845 | pub fn glb<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T) |
846 | -> InferResult<'tcx, T> | |
a7813a04 | 847 | where T: Relate<'tcx> |
c34b1796 | 848 | { |
5bcae85e SL |
849 | let mut fields = self.combine_fields(trace); |
850 | let result = fields.glb(a_is_expected).relate(a, b); | |
851 | result.map(move |t| InferOk { value: t, obligations: fields.obligations }) | |
1a4d82fc JJ |
852 | } |
853 | ||
9e0c209e SL |
854 | // Clear the "obligations in snapshot" flag, invoke the closure, |
855 | // then restore the flag to its original value. This flag is a | |
856 | // debugging measure designed to detect cases where we start a | |
857 | // snapshot, create type variables, register obligations involving | |
858 | // those type variables in the fulfillment cx, and then have to | |
859 | // unroll the snapshot, leaving "dangling type variables" behind. | |
860 | // In such cases, the flag will be set by the fulfillment cx, and | |
861 | // an assertion will fail when rolling the snapshot back. Very | |
862 | // useful, much better than grovelling through megabytes of | |
863 | // RUST_LOG output. | |
864 | // | |
865 | // HOWEVER, in some cases the flag is wrong. In particular, we | |
866 | // sometimes create a "mini-fulfilment-cx" in which we enroll | |
867 | // obligations. As long as this fulfillment cx is fully drained | |
868 | // before we return, this is not a problem, as there won't be any | |
869 | // escaping obligations in the main cx. In those cases, you can | |
870 | // use this function. | |
871 | pub fn save_and_restore_obligations_in_snapshot_flag<F, R>(&self, func: F) -> R | |
872 | where F: FnOnce(&Self) -> R | |
873 | { | |
874 | let flag = self.obligations_in_snapshot.get(); | |
875 | self.obligations_in_snapshot.set(false); | |
876 | let result = func(self); | |
877 | self.obligations_in_snapshot.set(flag); | |
878 | result | |
879 | } | |
880 | ||
1a4d82fc | 881 | fn start_snapshot(&self) -> CombinedSnapshot { |
3157f602 XL |
882 | debug!("start_snapshot()"); |
883 | ||
884 | let obligations_in_snapshot = self.obligations_in_snapshot.get(); | |
885 | self.obligations_in_snapshot.set(false); | |
886 | ||
1a4d82fc | 887 | CombinedSnapshot { |
3157f602 | 888 | projection_cache_snapshot: self.projection_cache.borrow_mut().snapshot(), |
1a4d82fc JJ |
889 | type_snapshot: self.type_variables.borrow_mut().snapshot(), |
890 | int_snapshot: self.int_unification_table.borrow_mut().snapshot(), | |
891 | float_snapshot: self.float_unification_table.borrow_mut().snapshot(), | |
892 | region_vars_snapshot: self.region_vars.start_snapshot(), | |
3157f602 | 893 | obligations_in_snapshot: obligations_in_snapshot, |
1a4d82fc JJ |
894 | } |
895 | } | |
896 | ||
c1a9b12d SL |
897 | fn rollback_to(&self, cause: &str, snapshot: CombinedSnapshot) { |
898 | debug!("rollback_to(cause={})", cause); | |
3157f602 XL |
899 | let CombinedSnapshot { projection_cache_snapshot, |
900 | type_snapshot, | |
1a4d82fc JJ |
901 | int_snapshot, |
902 | float_snapshot, | |
3157f602 XL |
903 | region_vars_snapshot, |
904 | obligations_in_snapshot } = snapshot; | |
1a4d82fc | 905 | |
3157f602 XL |
906 | assert!(!self.obligations_in_snapshot.get()); |
907 | self.obligations_in_snapshot.set(obligations_in_snapshot); | |
908 | ||
909 | self.projection_cache | |
910 | .borrow_mut() | |
911 | .rollback_to(projection_cache_snapshot); | |
1a4d82fc JJ |
912 | self.type_variables |
913 | .borrow_mut() | |
914 | .rollback_to(type_snapshot); | |
915 | self.int_unification_table | |
916 | .borrow_mut() | |
917 | .rollback_to(int_snapshot); | |
918 | self.float_unification_table | |
919 | .borrow_mut() | |
920 | .rollback_to(float_snapshot); | |
921 | self.region_vars | |
922 | .rollback_to(region_vars_snapshot); | |
923 | } | |
924 | ||
925 | fn commit_from(&self, snapshot: CombinedSnapshot) { | |
3157f602 XL |
926 | debug!("commit_from()"); |
927 | let CombinedSnapshot { projection_cache_snapshot, | |
928 | type_snapshot, | |
1a4d82fc JJ |
929 | int_snapshot, |
930 | float_snapshot, | |
3157f602 XL |
931 | region_vars_snapshot, |
932 | obligations_in_snapshot } = snapshot; | |
933 | ||
934 | self.obligations_in_snapshot.set(obligations_in_snapshot); | |
1a4d82fc | 935 | |
3157f602 XL |
936 | self.projection_cache |
937 | .borrow_mut() | |
938 | .commit(projection_cache_snapshot); | |
1a4d82fc JJ |
939 | self.type_variables |
940 | .borrow_mut() | |
941 | .commit(type_snapshot); | |
942 | self.int_unification_table | |
943 | .borrow_mut() | |
944 | .commit(int_snapshot); | |
945 | self.float_unification_table | |
946 | .borrow_mut() | |
947 | .commit(float_snapshot); | |
948 | self.region_vars | |
949 | .commit(region_vars_snapshot); | |
950 | } | |
951 | ||
952 | /// Execute `f` and commit the bindings | |
953 | pub fn commit_unconditionally<R, F>(&self, f: F) -> R where | |
954 | F: FnOnce() -> R, | |
955 | { | |
956 | debug!("commit()"); | |
957 | let snapshot = self.start_snapshot(); | |
958 | let r = f(); | |
959 | self.commit_from(snapshot); | |
960 | r | |
961 | } | |
962 | ||
c34b1796 | 963 | /// Execute `f` and commit the bindings if closure `f` returns `Ok(_)` |
1a4d82fc | 964 | pub fn commit_if_ok<T, E, F>(&self, f: F) -> Result<T, E> where |
c34b1796 | 965 | F: FnOnce(&CombinedSnapshot) -> Result<T, E> |
1a4d82fc | 966 | { |
c34b1796 AL |
967 | debug!("commit_if_ok()"); |
968 | let snapshot = self.start_snapshot(); | |
969 | let r = f(&snapshot); | |
970 | debug!("commit_if_ok() -- r.is_ok() = {}", r.is_ok()); | |
971 | match r { | |
972 | Ok(_) => { self.commit_from(snapshot); } | |
c1a9b12d | 973 | Err(_) => { self.rollback_to("commit_if_ok -- error", snapshot); } |
c34b1796 AL |
974 | } |
975 | r | |
1a4d82fc JJ |
976 | } |
977 | ||
a7813a04 XL |
978 | // Execute `f` in a snapshot, and commit the bindings it creates |
979 | pub fn in_snapshot<T, F>(&self, f: F) -> T where | |
980 | F: FnOnce(&CombinedSnapshot) -> T | |
981 | { | |
982 | debug!("in_snapshot()"); | |
983 | let snapshot = self.start_snapshot(); | |
984 | let r = f(&snapshot); | |
985 | self.commit_from(snapshot); | |
986 | r | |
987 | } | |
988 | ||
85aaf69f SL |
989 | /// Execute `f` and commit only the region bindings if successful. |
990 | /// The function f must be very careful not to leak any non-region | |
991 | /// variables that get created. | |
992 | pub fn commit_regions_if_ok<T, E, F>(&self, f: F) -> Result<T, E> where | |
993 | F: FnOnce() -> Result<T, E> | |
994 | { | |
995 | debug!("commit_regions_if_ok()"); | |
3157f602 XL |
996 | let CombinedSnapshot { projection_cache_snapshot, |
997 | type_snapshot, | |
85aaf69f SL |
998 | int_snapshot, |
999 | float_snapshot, | |
3157f602 XL |
1000 | region_vars_snapshot, |
1001 | obligations_in_snapshot } = self.start_snapshot(); | |
85aaf69f | 1002 | |
c34b1796 | 1003 | let r = self.commit_if_ok(|_| f()); |
85aaf69f | 1004 | |
c1a9b12d SL |
1005 | debug!("commit_regions_if_ok: rolling back everything but regions"); |
1006 | ||
3157f602 XL |
1007 | assert!(!self.obligations_in_snapshot.get()); |
1008 | self.obligations_in_snapshot.set(obligations_in_snapshot); | |
1009 | ||
85aaf69f SL |
1010 | // Roll back any non-region bindings - they should be resolved |
1011 | // inside `f`, with, e.g. `resolve_type_vars_if_possible`. | |
3157f602 XL |
1012 | self.projection_cache |
1013 | .borrow_mut() | |
1014 | .rollback_to(projection_cache_snapshot); | |
85aaf69f SL |
1015 | self.type_variables |
1016 | .borrow_mut() | |
1017 | .rollback_to(type_snapshot); | |
1018 | self.int_unification_table | |
1019 | .borrow_mut() | |
1020 | .rollback_to(int_snapshot); | |
1021 | self.float_unification_table | |
1022 | .borrow_mut() | |
1023 | .rollback_to(float_snapshot); | |
1024 | ||
1025 | // Commit region vars that may escape through resolved types. | |
1026 | self.region_vars | |
1027 | .commit(region_vars_snapshot); | |
1028 | ||
1029 | r | |
1030 | } | |
1031 | ||
1a4d82fc JJ |
1032 | /// Execute `f` then unroll any bindings it creates |
1033 | pub fn probe<R, F>(&self, f: F) -> R where | |
1034 | F: FnOnce(&CombinedSnapshot) -> R, | |
1035 | { | |
1036 | debug!("probe()"); | |
1037 | let snapshot = self.start_snapshot(); | |
1038 | let r = f(&snapshot); | |
c1a9b12d | 1039 | self.rollback_to("probe", snapshot); |
1a4d82fc JJ |
1040 | r |
1041 | } | |
1042 | ||
1043 | pub fn add_given(&self, | |
1044 | sub: ty::FreeRegion, | |
1045 | sup: ty::RegionVid) | |
1046 | { | |
1047 | self.region_vars.add_given(sub, sup); | |
1048 | } | |
1049 | ||
1050 | pub fn sub_types(&self, | |
1051 | a_is_expected: bool, | |
1052 | origin: TypeOrigin, | |
1053 | a: Ty<'tcx>, | |
1054 | b: Ty<'tcx>) | |
54a0048b | 1055 | -> InferResult<'tcx, ()> |
1a4d82fc | 1056 | { |
62682a34 | 1057 | debug!("sub_types({:?} <: {:?})", a, b); |
c34b1796 | 1058 | self.commit_if_ok(|_| { |
85aaf69f | 1059 | let trace = TypeTrace::types(origin, a_is_expected, a, b); |
54a0048b | 1060 | self.sub(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) |
1a4d82fc JJ |
1061 | }) |
1062 | } | |
1063 | ||
a7813a04 XL |
1064 | pub fn can_sub_types(&self, |
1065 | a: Ty<'tcx>, | |
1066 | b: Ty<'tcx>) | |
1067 | -> UnitResult<'tcx> | |
1068 | { | |
1069 | self.probe(|_| { | |
3157f602 | 1070 | let origin = TypeOrigin::Misc(syntax_pos::DUMMY_SP); |
a7813a04 | 1071 | let trace = TypeTrace::types(origin, true, a, b); |
c30ab7b3 SL |
1072 | self.sub(true, trace, &a, &b).map(|InferOk { obligations, .. }| { |
1073 | // FIXME(#32730) propagate obligations | |
1074 | assert!(obligations.is_empty()); | |
1075 | }) | |
a7813a04 XL |
1076 | }) |
1077 | } | |
1078 | ||
1a4d82fc JJ |
1079 | pub fn eq_types(&self, |
1080 | a_is_expected: bool, | |
1081 | origin: TypeOrigin, | |
1082 | a: Ty<'tcx>, | |
1083 | b: Ty<'tcx>) | |
54a0048b | 1084 | -> InferResult<'tcx, ()> |
1a4d82fc | 1085 | { |
c34b1796 | 1086 | self.commit_if_ok(|_| { |
85aaf69f | 1087 | let trace = TypeTrace::types(origin, a_is_expected, a, b); |
54a0048b | 1088 | self.equate(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) |
1a4d82fc JJ |
1089 | }) |
1090 | } | |
1091 | ||
92a42be0 | 1092 | pub fn eq_trait_refs(&self, |
1a4d82fc JJ |
1093 | a_is_expected: bool, |
1094 | origin: TypeOrigin, | |
d9579d0f AL |
1095 | a: ty::TraitRef<'tcx>, |
1096 | b: ty::TraitRef<'tcx>) | |
54a0048b | 1097 | -> InferResult<'tcx, ()> |
1a4d82fc | 1098 | { |
a7813a04 | 1099 | debug!("eq_trait_refs({:?} = {:?})", a, b); |
c34b1796 | 1100 | self.commit_if_ok(|_| { |
1a4d82fc JJ |
1101 | let trace = TypeTrace { |
1102 | origin: origin, | |
a7813a04 | 1103 | values: TraitRefs(ExpectedFound::new(a_is_expected, a, b)) |
1a4d82fc | 1104 | }; |
54a0048b | 1105 | self.equate(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) |
1a4d82fc JJ |
1106 | }) |
1107 | } | |
1108 | ||
a7813a04 XL |
1109 | pub fn eq_impl_headers(&self, |
1110 | a_is_expected: bool, | |
1111 | origin: TypeOrigin, | |
1112 | a: &ty::ImplHeader<'tcx>, | |
1113 | b: &ty::ImplHeader<'tcx>) | |
1114 | -> InferResult<'tcx, ()> | |
1115 | { | |
1116 | debug!("eq_impl_header({:?} = {:?})", a, b); | |
1117 | match (a.trait_ref, b.trait_ref) { | |
1118 | (Some(a_ref), Some(b_ref)) => self.eq_trait_refs(a_is_expected, origin, a_ref, b_ref), | |
1119 | (None, None) => self.eq_types(a_is_expected, origin, a.self_ty, b.self_ty), | |
1120 | _ => bug!("mk_eq_impl_headers given mismatched impl kinds"), | |
1121 | } | |
1122 | } | |
1123 | ||
1a4d82fc JJ |
1124 | pub fn sub_poly_trait_refs(&self, |
1125 | a_is_expected: bool, | |
1126 | origin: TypeOrigin, | |
1127 | a: ty::PolyTraitRef<'tcx>, | |
1128 | b: ty::PolyTraitRef<'tcx>) | |
54a0048b | 1129 | -> InferResult<'tcx, ()> |
1a4d82fc | 1130 | { |
a7813a04 | 1131 | debug!("sub_poly_trait_refs({:?} <: {:?})", a, b); |
c34b1796 | 1132 | self.commit_if_ok(|_| { |
1a4d82fc JJ |
1133 | let trace = TypeTrace { |
1134 | origin: origin, | |
a7813a04 | 1135 | values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a, b)) |
1a4d82fc | 1136 | }; |
54a0048b | 1137 | self.sub(a_is_expected, trace, &a, &b).map(|ok| ok.unit()) |
1a4d82fc JJ |
1138 | }) |
1139 | } | |
1140 | ||
a7813a04 XL |
1141 | pub fn sub_regions(&self, |
1142 | origin: SubregionOrigin<'tcx>, | |
9e0c209e SL |
1143 | a: &'tcx ty::Region, |
1144 | b: &'tcx ty::Region) { | |
a7813a04 XL |
1145 | debug!("sub_regions({:?} <: {:?})", a, b); |
1146 | self.region_vars.make_subregion(origin, a, b); | |
1a4d82fc JJ |
1147 | } |
1148 | ||
1149 | pub fn equality_predicate(&self, | |
1150 | span: Span, | |
1151 | predicate: &ty::PolyEquatePredicate<'tcx>) | |
54a0048b SL |
1152 | -> InferResult<'tcx, ()> |
1153 | { | |
c34b1796 | 1154 | self.commit_if_ok(|snapshot| { |
1a4d82fc JJ |
1155 | let (ty::EquatePredicate(a, b), skol_map) = |
1156 | self.skolemize_late_bound_regions(predicate, snapshot); | |
92a42be0 | 1157 | let origin = TypeOrigin::EquatePredicate(span); |
a7813a04 | 1158 | let eqty_ok = self.eq_types(false, origin, a, b)?; |
3157f602 XL |
1159 | self.leak_check(false, span, &skol_map, snapshot)?; |
1160 | self.pop_skolemized(skol_map, snapshot); | |
1161 | Ok(eqty_ok.unit()) | |
1a4d82fc JJ |
1162 | }) |
1163 | } | |
1164 | ||
1165 | pub fn region_outlives_predicate(&self, | |
c30ab7b3 | 1166 | cause: &traits::ObligationCause<'tcx>, |
9e0c209e | 1167 | predicate: &ty::PolyRegionOutlivesPredicate<'tcx>) |
54a0048b SL |
1168 | -> UnitResult<'tcx> |
1169 | { | |
c34b1796 | 1170 | self.commit_if_ok(|snapshot| { |
1a4d82fc JJ |
1171 | let (ty::OutlivesPredicate(r_a, r_b), skol_map) = |
1172 | self.skolemize_late_bound_regions(predicate, snapshot); | |
c30ab7b3 SL |
1173 | let origin = |
1174 | SubregionOrigin::from_obligation_cause(cause, | |
1175 | || RelateRegionParamBound(cause.span)); | |
a7813a04 | 1176 | self.sub_regions(origin, r_b, r_a); // `b : a` ==> `a <= b` |
c30ab7b3 | 1177 | self.leak_check(false, cause.span, &skol_map, snapshot)?; |
3157f602 | 1178 | Ok(self.pop_skolemized(skol_map, snapshot)) |
1a4d82fc JJ |
1179 | }) |
1180 | } | |
1181 | ||
1182 | pub fn next_ty_var_id(&self, diverging: bool) -> TyVid { | |
1183 | self.type_variables | |
1184 | .borrow_mut() | |
c1a9b12d | 1185 | .new_var(diverging, None) |
1a4d82fc JJ |
1186 | } |
1187 | ||
1188 | pub fn next_ty_var(&self) -> Ty<'tcx> { | |
c1a9b12d SL |
1189 | self.tcx.mk_var(self.next_ty_var_id(false)) |
1190 | } | |
1191 | ||
1a4d82fc | 1192 | pub fn next_diverging_ty_var(&self) -> Ty<'tcx> { |
c1a9b12d | 1193 | self.tcx.mk_var(self.next_ty_var_id(true)) |
1a4d82fc JJ |
1194 | } |
1195 | ||
c34b1796 | 1196 | pub fn next_ty_vars(&self, n: usize) -> Vec<Ty<'tcx>> { |
85aaf69f | 1197 | (0..n).map(|_i| self.next_ty_var()).collect() |
1a4d82fc JJ |
1198 | } |
1199 | ||
1200 | pub fn next_int_var_id(&self) -> IntVid { | |
1201 | self.int_unification_table | |
1202 | .borrow_mut() | |
1203 | .new_key(None) | |
1204 | } | |
1205 | ||
1206 | pub fn next_float_var_id(&self) -> FloatVid { | |
1207 | self.float_unification_table | |
1208 | .borrow_mut() | |
1209 | .new_key(None) | |
1210 | } | |
1211 | ||
9e0c209e SL |
1212 | pub fn next_region_var(&self, origin: RegionVariableOrigin) |
1213 | -> &'tcx ty::Region { | |
1214 | self.tcx.mk_region(ty::ReVar(self.region_vars.new_region_var(origin))) | |
1a4d82fc JJ |
1215 | } |
1216 | ||
9e0c209e SL |
1217 | /// Create a region inference variable for the given |
1218 | /// region parameter definition. | |
1219 | pub fn region_var_for_def(&self, | |
c1a9b12d | 1220 | span: Span, |
9e0c209e SL |
1221 | def: &ty::RegionParameterDef) |
1222 | -> &'tcx ty::Region { | |
1223 | self.next_region_var(EarlyBoundRegion(span, def.name)) | |
1224 | } | |
1225 | ||
1226 | /// Create a type inference variable for the given | |
1227 | /// type parameter definition. The substitutions are | |
1228 | /// for actual parameters that may be referred to by | |
1229 | /// the default of this type parameter, if it exists. | |
1230 | /// E.g. `struct Foo<A, B, C = (A, B)>(...);` when | |
1231 | /// used in a path such as `Foo::<T, U>::new()` will | |
1232 | /// use an inference variable for `C` with `[T, U]` | |
1233 | /// as the substitutions for the default, `(T, U)`. | |
1234 | pub fn type_var_for_def(&self, | |
1235 | span: Span, | |
1236 | def: &ty::TypeParameterDef<'tcx>, | |
c30ab7b3 | 1237 | substs: &[Kind<'tcx>]) |
9e0c209e SL |
1238 | -> Ty<'tcx> { |
1239 | let default = def.default.map(|default| { | |
1240 | type_variable::Default { | |
1241 | ty: default.subst_spanned(self.tcx, substs, Some(span)), | |
1242 | origin_span: span, | |
1243 | def_id: def.default_def_id | |
1244 | } | |
1245 | }); | |
c1a9b12d | 1246 | |
c1a9b12d | 1247 | |
9e0c209e SL |
1248 | let ty_var_id = self.type_variables |
1249 | .borrow_mut() | |
1250 | .new_var(false, default); | |
c1a9b12d | 1251 | |
9e0c209e | 1252 | self.tcx.mk_var(ty_var_id) |
1a4d82fc JJ |
1253 | } |
1254 | ||
9e0c209e SL |
1255 | /// Given a set of generics defined on a type or impl, returns a substitution mapping each |
1256 | /// type/region parameter to a fresh inference variable. | |
1257 | pub fn fresh_substs_for_item(&self, | |
1258 | span: Span, | |
1259 | def_id: DefId) | |
1260 | -> &'tcx Substs<'tcx> { | |
1261 | Substs::for_item(self.tcx, def_id, |def, _| { | |
1262 | self.region_var_for_def(span, def) | |
1263 | }, |def, substs| { | |
1264 | self.type_var_for_def(span, def, substs) | |
1265 | }) | |
1a4d82fc JJ |
1266 | } |
1267 | ||
9e0c209e | 1268 | pub fn fresh_bound_region(&self, debruijn: ty::DebruijnIndex) -> &'tcx ty::Region { |
1a4d82fc JJ |
1269 | self.region_vars.new_bound(debruijn) |
1270 | } | |
1271 | ||
a7813a04 XL |
1272 | /// True if errors have been reported since this infcx was |
1273 | /// created. This is sometimes used as a heuristic to skip | |
1274 | /// reporting errors that often occur as a result of earlier | |
1275 | /// errors, but where it's hard to be 100% sure (e.g., unresolved | |
1276 | /// inference variables, regionck errors). | |
1277 | pub fn is_tainted_by_errors(&self) -> bool { | |
1278 | debug!("is_tainted_by_errors(err_count={}, err_count_on_creation={}, \ | |
1279 | tainted_by_errors_flag={})", | |
1280 | self.tcx.sess.err_count(), | |
1281 | self.err_count_on_creation, | |
1282 | self.tainted_by_errors_flag.get()); | |
1283 | ||
1284 | if self.tcx.sess.err_count() > self.err_count_on_creation { | |
1285 | return true; // errors reported since this infcx was made | |
1286 | } | |
1287 | self.tainted_by_errors_flag.get() | |
1288 | } | |
1289 | ||
1290 | /// Set the "tainted by errors" flag to true. We call this when we | |
1291 | /// observe an error from a prior pass. | |
1292 | pub fn set_tainted_by_errors(&self) { | |
1293 | debug!("set_tainted_by_errors()"); | |
1294 | self.tainted_by_errors_flag.set(true) | |
7453a54e SL |
1295 | } |
1296 | ||
c1a9b12d SL |
1297 | pub fn node_type(&self, id: ast::NodeId) -> Ty<'tcx> { |
1298 | match self.tables.borrow().node_types.get(&id) { | |
1299 | Some(&t) => t, | |
1300 | // FIXME | |
a7813a04 | 1301 | None if self.is_tainted_by_errors() => |
c1a9b12d SL |
1302 | self.tcx.types.err, |
1303 | None => { | |
54a0048b SL |
1304 | bug!("no type for node {}: {} in fcx", |
1305 | id, self.tcx.map.node_to_string(id)); | |
c1a9b12d SL |
1306 | } |
1307 | } | |
1308 | } | |
1309 | ||
e9174d1e | 1310 | pub fn expr_ty(&self, ex: &hir::Expr) -> Ty<'tcx> { |
c1a9b12d SL |
1311 | match self.tables.borrow().node_types.get(&ex.id) { |
1312 | Some(&t) => t, | |
1313 | None => { | |
54a0048b | 1314 | bug!("no type for expr in fcx"); |
c1a9b12d SL |
1315 | } |
1316 | } | |
1317 | } | |
1318 | ||
bd371182 AL |
1319 | pub fn resolve_regions_and_report_errors(&self, |
1320 | free_regions: &FreeRegionMap, | |
1321 | subject_node_id: ast::NodeId) { | |
1322 | let errors = self.region_vars.resolve_regions(free_regions, subject_node_id); | |
a7813a04 | 1323 | if !self.is_tainted_by_errors() { |
7453a54e SL |
1324 | // As a heuristic, just skip reporting region errors |
1325 | // altogether if other errors have been reported while | |
1326 | // this infcx was in use. This is totally hokey but | |
1327 | // otherwise we have a hard time separating legit region | |
1328 | // errors from silly ones. | |
1329 | self.report_region_errors(&errors); // see error_reporting.rs | |
1330 | } | |
1a4d82fc JJ |
1331 | } |
1332 | ||
1333 | pub fn ty_to_string(&self, t: Ty<'tcx>) -> String { | |
62682a34 | 1334 | self.resolve_type_vars_if_possible(&t).to_string() |
1a4d82fc JJ |
1335 | } |
1336 | ||
1337 | pub fn tys_to_string(&self, ts: &[Ty<'tcx>]) -> String { | |
1338 | let tstrs: Vec<String> = ts.iter().map(|t| self.ty_to_string(*t)).collect(); | |
c1a9b12d | 1339 | format!("({})", tstrs.join(", ")) |
1a4d82fc JJ |
1340 | } |
1341 | ||
d9579d0f | 1342 | pub fn trait_ref_to_string(&self, t: &ty::TraitRef<'tcx>) -> String { |
62682a34 | 1343 | self.resolve_type_vars_if_possible(t).to_string() |
1a4d82fc JJ |
1344 | } |
1345 | ||
1346 | pub fn shallow_resolve(&self, typ: Ty<'tcx>) -> Ty<'tcx> { | |
1347 | match typ.sty { | |
62682a34 | 1348 | ty::TyInfer(ty::TyVar(v)) => { |
1a4d82fc JJ |
1349 | // Not entirely obvious: if `typ` is a type variable, |
1350 | // it can be resolved to an int/float variable, which | |
1351 | // can then be recursively resolved, hence the | |
1352 | // recursion. Note though that we prevent type | |
1353 | // variables from unifying to other type variables | |
1354 | // directly (though they may be embedded | |
1355 | // structurally), and we prevent cycles in any case, | |
1356 | // so this recursion should always be of very limited | |
1357 | // depth. | |
54a0048b | 1358 | self.type_variables.borrow_mut() |
1a4d82fc JJ |
1359 | .probe(v) |
1360 | .map(|t| self.shallow_resolve(t)) | |
1361 | .unwrap_or(typ) | |
1362 | } | |
1363 | ||
62682a34 | 1364 | ty::TyInfer(ty::IntVar(v)) => { |
c34b1796 AL |
1365 | self.int_unification_table |
1366 | .borrow_mut() | |
1367 | .probe(v) | |
1368 | .map(|v| v.to_type(self.tcx)) | |
1a4d82fc JJ |
1369 | .unwrap_or(typ) |
1370 | } | |
1371 | ||
62682a34 | 1372 | ty::TyInfer(ty::FloatVar(v)) => { |
c34b1796 AL |
1373 | self.float_unification_table |
1374 | .borrow_mut() | |
1375 | .probe(v) | |
1376 | .map(|v| v.to_type(self.tcx)) | |
1a4d82fc JJ |
1377 | .unwrap_or(typ) |
1378 | } | |
1379 | ||
1380 | _ => { | |
1381 | typ | |
1382 | } | |
1383 | } | |
1384 | } | |
1385 | ||
e9174d1e | 1386 | pub fn resolve_type_vars_if_possible<T>(&self, value: &T) -> T |
9cc50fc6 | 1387 | where T: TypeFoldable<'tcx> |
e9174d1e | 1388 | { |
1a4d82fc JJ |
1389 | /*! |
1390 | * Where possible, replaces type/int/float variables in | |
1391 | * `value` with their final value. Note that region variables | |
1392 | * are unaffected. If a type variable has not been unified, it | |
1393 | * is left as is. This is an idempotent operation that does | |
1394 | * not affect inference state in any way and so you can do it | |
1395 | * at will. | |
1396 | */ | |
1397 | ||
e9174d1e SL |
1398 | if !value.needs_infer() { |
1399 | return value.clone(); // avoid duplicated subst-folding | |
1400 | } | |
1a4d82fc JJ |
1401 | let mut r = resolve::OpportunisticTypeResolver::new(self); |
1402 | value.fold_with(&mut r) | |
1403 | } | |
1404 | ||
9cc50fc6 SL |
1405 | pub fn resolve_type_and_region_vars_if_possible<T>(&self, value: &T) -> T |
1406 | where T: TypeFoldable<'tcx> | |
1407 | { | |
1408 | let mut r = resolve::OpportunisticTypeAndRegionResolver::new(self); | |
1409 | value.fold_with(&mut r) | |
1410 | } | |
1411 | ||
c1a9b12d SL |
1412 | /// Resolves all type variables in `t` and then, if any were left |
1413 | /// unresolved, substitutes an error type. This is used after the | |
1414 | /// main checking when doing a second pass before writeback. The | |
1415 | /// justification is that writeback will produce an error for | |
1416 | /// these unconstrained type variables. | |
1417 | fn resolve_type_vars_or_error(&self, t: &Ty<'tcx>) -> mc::McResult<Ty<'tcx>> { | |
1418 | let ty = self.resolve_type_vars_if_possible(t); | |
1419 | if ty.references_error() || ty.is_ty_var() { | |
1420 | debug!("resolve_type_vars_or_error: error from {:?}", ty); | |
1421 | Err(()) | |
1422 | } else { | |
1423 | Ok(ty) | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | pub fn fully_resolve<T:TypeFoldable<'tcx>>(&self, value: &T) -> FixupResult<T> { | |
1a4d82fc JJ |
1428 | /*! |
1429 | * Attempts to resolve all type/region variables in | |
1430 | * `value`. Region inference must have been run already (e.g., | |
1431 | * by calling `resolve_regions_and_report_errors`). If some | |
1432 | * variable was never unified, an `Err` results. | |
1433 | * | |
1434 | * This method is idempotent, but it not typically not invoked | |
1435 | * except during the writeback phase. | |
1436 | */ | |
1437 | ||
1438 | resolve::fully_resolve(self, value) | |
1439 | } | |
1440 | ||
1441 | // [Note-Type-error-reporting] | |
62682a34 | 1442 | // An invariant is that anytime the expected or actual type is TyError (the special |
1a4d82fc JJ |
1443 | // error type, meaning that an error occurred when typechecking this expression), |
1444 | // this is a derived error. The error cascaded from another error (that was already | |
1445 | // reported), so it's not useful to display it to the user. | |
5bcae85e | 1446 | // The following methods implement this logic. |
62682a34 | 1447 | // They check if either the actual or expected type is TyError, and don't print the error |
1a4d82fc | 1448 | // in this case. The typechecker should only ever report type errors involving mismatched |
5bcae85e | 1449 | // types using one of these methods, and should not call span_err directly for such |
1a4d82fc | 1450 | // errors. |
1a4d82fc JJ |
1451 | |
1452 | pub fn type_error_message<M>(&self, | |
1453 | sp: Span, | |
1454 | mk_msg: M, | |
5bcae85e | 1455 | actual_ty: Ty<'tcx>) |
9cc50fc6 SL |
1456 | where M: FnOnce(String) -> String, |
1457 | { | |
5bcae85e | 1458 | self.type_error_struct(sp, mk_msg, actual_ty).emit(); |
9cc50fc6 SL |
1459 | } |
1460 | ||
5bcae85e | 1461 | // FIXME: this results in errors without an error code. Deprecate? |
9cc50fc6 SL |
1462 | pub fn type_error_struct<M>(&self, |
1463 | sp: Span, | |
1464 | mk_msg: M, | |
5bcae85e | 1465 | actual_ty: Ty<'tcx>) |
9cc50fc6 SL |
1466 | -> DiagnosticBuilder<'tcx> |
1467 | where M: FnOnce(String) -> String, | |
5bcae85e SL |
1468 | { |
1469 | self.type_error_struct_with_diag(sp, |actual_ty| { | |
1470 | self.tcx.sess.struct_span_err(sp, &mk_msg(actual_ty)) | |
1471 | }, actual_ty) | |
1472 | } | |
1473 | ||
1474 | pub fn type_error_struct_with_diag<M>(&self, | |
1475 | sp: Span, | |
1476 | mk_diag: M, | |
1477 | actual_ty: Ty<'tcx>) | |
1478 | -> DiagnosticBuilder<'tcx> | |
1479 | where M: FnOnce(String) -> DiagnosticBuilder<'tcx>, | |
1a4d82fc JJ |
1480 | { |
1481 | let actual_ty = self.resolve_type_vars_if_possible(&actual_ty); | |
5bcae85e | 1482 | debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty); |
1a4d82fc | 1483 | |
62682a34 | 1484 | // Don't report an error if actual type is TyError. |
c1a9b12d | 1485 | if actual_ty.references_error() { |
9cc50fc6 | 1486 | return self.tcx.sess.diagnostic().struct_dummy(); |
1a4d82fc JJ |
1487 | } |
1488 | ||
5bcae85e | 1489 | mk_diag(self.ty_to_string(actual_ty)) |
1a4d82fc JJ |
1490 | } |
1491 | ||
1492 | pub fn report_mismatched_types(&self, | |
54a0048b | 1493 | origin: TypeOrigin, |
1a4d82fc JJ |
1494 | expected: Ty<'tcx>, |
1495 | actual: Ty<'tcx>, | |
54a0048b | 1496 | err: TypeError<'tcx>) { |
1a4d82fc | 1497 | let trace = TypeTrace { |
54a0048b | 1498 | origin: origin, |
e9174d1e | 1499 | values: Types(ExpectedFound { |
1a4d82fc JJ |
1500 | expected: expected, |
1501 | found: actual | |
1502 | }) | |
1503 | }; | |
54a0048b | 1504 | self.report_and_explain_type_error(trace, &err).emit(); |
1a4d82fc JJ |
1505 | } |
1506 | ||
c1a9b12d SL |
1507 | pub fn report_conflicting_default_types(&self, |
1508 | span: Span, | |
1509 | expected: type_variable::Default<'tcx>, | |
1510 | actual: type_variable::Default<'tcx>) { | |
1511 | let trace = TypeTrace { | |
92a42be0 | 1512 | origin: TypeOrigin::Misc(span), |
e9174d1e | 1513 | values: Types(ExpectedFound { |
c1a9b12d SL |
1514 | expected: expected.ty, |
1515 | found: actual.ty | |
1516 | }) | |
1517 | }; | |
1518 | ||
54a0048b SL |
1519 | self.report_and_explain_type_error( |
1520 | trace, | |
e9174d1e | 1521 | &TypeError::TyParamDefaultMismatch(ExpectedFound { |
c1a9b12d SL |
1522 | expected: expected, |
1523 | found: actual | |
54a0048b SL |
1524 | })) |
1525 | .emit(); | |
c1a9b12d SL |
1526 | } |
1527 | ||
1a4d82fc JJ |
1528 | pub fn replace_late_bound_regions_with_fresh_var<T>( |
1529 | &self, | |
1530 | span: Span, | |
1531 | lbrct: LateBoundRegionConversionTime, | |
1532 | value: &ty::Binder<T>) | |
9e0c209e | 1533 | -> (T, FnvHashMap<ty::BoundRegion, &'tcx ty::Region>) |
62682a34 | 1534 | where T : TypeFoldable<'tcx> |
1a4d82fc | 1535 | { |
e9174d1e | 1536 | self.tcx.replace_late_bound_regions( |
1a4d82fc JJ |
1537 | value, |
1538 | |br| self.next_region_var(LateBoundRegion(span, br, lbrct))) | |
1539 | } | |
1540 | ||
3157f602 XL |
1541 | /// Given a higher-ranked projection predicate like: |
1542 | /// | |
1543 | /// for<'a> <T as Fn<&'a u32>>::Output = &'a u32 | |
1544 | /// | |
1545 | /// and a target trait-ref like: | |
1546 | /// | |
1547 | /// <T as Fn<&'x u32>> | |
1548 | /// | |
1549 | /// find a substitution `S` for the higher-ranked regions (here, | |
1550 | /// `['a => 'x]`) such that the predicate matches the trait-ref, | |
1551 | /// and then return the value (here, `&'a u32`) but with the | |
1552 | /// substitution applied (hence, `&'x u32`). | |
1553 | /// | |
1554 | /// See `higher_ranked_match` in `higher_ranked/mod.rs` for more | |
1555 | /// details. | |
1556 | pub fn match_poly_projection_predicate(&self, | |
1557 | origin: TypeOrigin, | |
1558 | match_a: ty::PolyProjectionPredicate<'tcx>, | |
1559 | match_b: ty::TraitRef<'tcx>) | |
1560 | -> InferResult<'tcx, HrMatchResult<Ty<'tcx>>> | |
1561 | { | |
1562 | let span = origin.span(); | |
1563 | let match_trait_ref = match_a.skip_binder().projection_ty.trait_ref; | |
1564 | let trace = TypeTrace { | |
1565 | origin: origin, | |
1566 | values: TraitRefs(ExpectedFound::new(true, match_trait_ref, match_b)) | |
1567 | }; | |
1568 | ||
1569 | let match_pair = match_a.map_bound(|p| (p.projection_ty.trait_ref, p.ty)); | |
5bcae85e SL |
1570 | let mut combine = self.combine_fields(trace); |
1571 | let result = combine.higher_ranked_match(span, &match_pair, &match_b, true)?; | |
3157f602 XL |
1572 | Ok(InferOk { value: result, obligations: combine.obligations }) |
1573 | } | |
1574 | ||
1a4d82fc JJ |
1575 | /// See `verify_generic_bound` method in `region_inference` |
1576 | pub fn verify_generic_bound(&self, | |
1577 | origin: SubregionOrigin<'tcx>, | |
1578 | kind: GenericKind<'tcx>, | |
9e0c209e SL |
1579 | a: &'tcx ty::Region, |
1580 | bound: VerifyBound<'tcx>) { | |
62682a34 SL |
1581 | debug!("verify_generic_bound({:?}, {:?} <: {:?})", |
1582 | kind, | |
1583 | a, | |
e9174d1e | 1584 | bound); |
1a4d82fc | 1585 | |
e9174d1e | 1586 | self.region_vars.verify_generic_bound(origin, kind, a, bound); |
1a4d82fc JJ |
1587 | } |
1588 | ||
a7813a04 XL |
1589 | pub fn can_equate<T>(&self, a: &T, b: &T) -> UnitResult<'tcx> |
1590 | where T: Relate<'tcx> + fmt::Debug | |
1a4d82fc | 1591 | { |
62682a34 | 1592 | debug!("can_equate({:?}, {:?})", a, b); |
1a4d82fc JJ |
1593 | self.probe(|_| { |
1594 | // Gin up a dummy trace, since this won't be committed | |
1595 | // anyhow. We should make this typetrace stuff more | |
1596 | // generic so we don't have to do anything quite this | |
1597 | // terrible. | |
c30ab7b3 SL |
1598 | let trace = TypeTrace::dummy(self.tcx); |
1599 | self.equate(true, trace, a, b).map(|InferOk { obligations, .. }| { | |
1600 | // FIXME(#32730) propagate obligations | |
1601 | assert!(obligations.is_empty()); | |
1602 | }) | |
1603 | }) | |
1a4d82fc | 1604 | } |
c1a9b12d SL |
1605 | |
1606 | pub fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> { | |
1607 | let ty = self.node_type(id); | |
1608 | self.resolve_type_vars_or_error(&ty) | |
1609 | } | |
1610 | ||
e9174d1e | 1611 | pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> { |
c30ab7b3 | 1612 | let ty = self.tables.borrow().expr_ty_adjusted(expr); |
c1a9b12d SL |
1613 | self.resolve_type_vars_or_error(&ty) |
1614 | } | |
1615 | ||
1616 | pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool { | |
1617 | let ty = self.resolve_type_vars_if_possible(&ty); | |
a7813a04 XL |
1618 | if let Some(ty) = self.tcx.lift_to_global(&ty) { |
1619 | // Even if the type may have no inference variables, during | |
1620 | // type-checking closure types are in local tables only. | |
1621 | let local_closures = match self.tables { | |
1622 | InferTables::Local(_) => ty.has_closure_types(), | |
1623 | InferTables::Global(_) => false | |
1624 | }; | |
1625 | if !local_closures { | |
1626 | return ty.moves_by_default(self.tcx.global_tcx(), self.param_env(), span); | |
1627 | } | |
e9174d1e | 1628 | } |
a7813a04 XL |
1629 | |
1630 | // this can get called from typeck (by euv), and moves_by_default | |
1631 | // rightly refuses to work with inference variables, but | |
1632 | // moves_by_default has a cache, which we want to use in other | |
1633 | // cases. | |
1634 | !traits::type_known_to_meet_builtin_bound(self, ty, ty::BoundCopy, span) | |
c1a9b12d SL |
1635 | } |
1636 | ||
1637 | pub fn node_method_ty(&self, method_call: ty::MethodCall) | |
1638 | -> Option<Ty<'tcx>> { | |
1639 | self.tables | |
1640 | .borrow() | |
1641 | .method_map | |
1642 | .get(&method_call) | |
1643 | .map(|method| method.ty) | |
1644 | .map(|ty| self.resolve_type_vars_if_possible(&ty)) | |
1645 | } | |
1646 | ||
1647 | pub fn node_method_id(&self, method_call: ty::MethodCall) | |
e9174d1e | 1648 | -> Option<DefId> { |
c1a9b12d SL |
1649 | self.tables |
1650 | .borrow() | |
1651 | .method_map | |
1652 | .get(&method_call) | |
1653 | .map(|method| method.def_id) | |
1654 | } | |
1655 | ||
c30ab7b3 | 1656 | pub fn adjustments(&self) -> Ref<NodeMap<adjustment::Adjustment<'tcx>>> { |
c1a9b12d | 1657 | fn project_adjustments<'a, 'tcx>(tables: &'a ty::Tables<'tcx>) |
c30ab7b3 | 1658 | -> &'a NodeMap<adjustment::Adjustment<'tcx>> { |
c1a9b12d SL |
1659 | &tables.adjustments |
1660 | } | |
1661 | ||
1662 | Ref::map(self.tables.borrow(), project_adjustments) | |
1663 | } | |
1664 | ||
1665 | pub fn is_method_call(&self, id: ast::NodeId) -> bool { | |
1666 | self.tables.borrow().method_map.contains_key(&ty::MethodCall::expr(id)) | |
1667 | } | |
1668 | ||
1669 | pub fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<CodeExtent> { | |
1670 | self.tcx.region_maps.temporary_scope(rvalue_id) | |
1671 | } | |
1672 | ||
9e0c209e | 1673 | pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> { |
c1a9b12d SL |
1674 | self.tables.borrow().upvar_capture_map.get(&upvar_id).cloned() |
1675 | } | |
1676 | ||
a7813a04 | 1677 | pub fn param_env(&self) -> &ty::ParameterEnvironment<'gcx> { |
c1a9b12d SL |
1678 | &self.parameter_environment |
1679 | } | |
1680 | ||
1681 | pub fn closure_kind(&self, | |
e9174d1e | 1682 | def_id: DefId) |
c1a9b12d SL |
1683 | -> Option<ty::ClosureKind> |
1684 | { | |
b039eaaf SL |
1685 | if def_id.is_local() { |
1686 | self.tables.borrow().closure_kinds.get(&def_id).cloned() | |
1687 | } else { | |
1688 | // During typeck, ALL closures are local. But afterwards, | |
1689 | // during trans, we see closure ids from other traits. | |
1690 | // That may require loading the closure data out of the | |
1691 | // cstore. | |
a7813a04 | 1692 | Some(self.tcx.closure_kind(def_id)) |
b039eaaf | 1693 | } |
c1a9b12d SL |
1694 | } |
1695 | ||
1696 | pub fn closure_type(&self, | |
e9174d1e | 1697 | def_id: DefId, |
a7813a04 | 1698 | substs: ty::ClosureSubsts<'tcx>) |
c1a9b12d SL |
1699 | -> ty::ClosureTy<'tcx> |
1700 | { | |
a7813a04 XL |
1701 | if let InferTables::Local(tables) = self.tables { |
1702 | if let Some(ty) = tables.borrow().closure_tys.get(&def_id) { | |
1703 | return ty.subst(self.tcx, substs.func_substs); | |
1704 | } | |
1705 | } | |
c1a9b12d | 1706 | |
a7813a04 | 1707 | let closure_ty = self.tcx.closure_type(def_id, substs); |
9e0c209e | 1708 | closure_ty |
c1a9b12d | 1709 | } |
1a4d82fc JJ |
1710 | } |
1711 | ||
a7813a04 | 1712 | impl<'a, 'gcx, 'tcx> TypeTrace<'tcx> { |
1a4d82fc JJ |
1713 | pub fn span(&self) -> Span { |
1714 | self.origin.span() | |
1715 | } | |
1716 | ||
85aaf69f SL |
1717 | pub fn types(origin: TypeOrigin, |
1718 | a_is_expected: bool, | |
1719 | a: Ty<'tcx>, | |
1720 | b: Ty<'tcx>) | |
1721 | -> TypeTrace<'tcx> { | |
1722 | TypeTrace { | |
1723 | origin: origin, | |
a7813a04 | 1724 | values: Types(ExpectedFound::new(a_is_expected, a, b)) |
85aaf69f SL |
1725 | } |
1726 | } | |
1727 | ||
a7813a04 | 1728 | pub fn dummy(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> TypeTrace<'tcx> { |
1a4d82fc | 1729 | TypeTrace { |
3157f602 | 1730 | origin: TypeOrigin::Misc(syntax_pos::DUMMY_SP), |
e9174d1e | 1731 | values: Types(ExpectedFound { |
1a4d82fc JJ |
1732 | expected: tcx.types.err, |
1733 | found: tcx.types.err, | |
1734 | }) | |
1735 | } | |
1736 | } | |
1737 | } | |
1738 | ||
62682a34 SL |
1739 | impl<'tcx> fmt::Debug for TypeTrace<'tcx> { |
1740 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
1741 | write!(f, "TypeTrace({:?})", self.origin) | |
1a4d82fc JJ |
1742 | } |
1743 | } | |
1744 | ||
1745 | impl TypeOrigin { | |
1746 | pub fn span(&self) -> Span { | |
1747 | match *self { | |
92a42be0 SL |
1748 | TypeOrigin::MethodCompatCheck(span) => span, |
1749 | TypeOrigin::ExprAssignable(span) => span, | |
1750 | TypeOrigin::Misc(span) => span, | |
92a42be0 | 1751 | TypeOrigin::RelateOutputImplTypes(span) => span, |
9e0c209e | 1752 | TypeOrigin::MatchExpressionArm(match_span, ..) => match_span, |
92a42be0 SL |
1753 | TypeOrigin::IfExpression(span) => span, |
1754 | TypeOrigin::IfExpressionWithNoElse(span) => span, | |
92a42be0 | 1755 | TypeOrigin::EquatePredicate(span) => span, |
5bcae85e SL |
1756 | TypeOrigin::MainFunctionType(span) => span, |
1757 | TypeOrigin::StartFunctionType(span) => span, | |
1758 | TypeOrigin::IntrinsicType(span) => span, | |
1759 | TypeOrigin::MethodReceiver(span) => span, | |
1a4d82fc JJ |
1760 | } |
1761 | } | |
1762 | } | |
1763 | ||
1a4d82fc JJ |
1764 | impl<'tcx> SubregionOrigin<'tcx> { |
1765 | pub fn span(&self) -> Span { | |
1766 | match *self { | |
1767 | Subtype(ref a) => a.span(), | |
1768 | InfStackClosure(a) => a, | |
1769 | InvokeClosure(a) => a, | |
1770 | DerefPointer(a) => a, | |
1771 | FreeVariable(a, _) => a, | |
1772 | IndexSlice(a) => a, | |
1773 | RelateObjectBound(a) => a, | |
1774 | RelateParamBound(a, _) => a, | |
1775 | RelateRegionParamBound(a) => a, | |
1776 | RelateDefaultParamBound(a, _) => a, | |
1777 | Reborrow(a) => a, | |
1778 | ReborrowUpvar(a, _) => a, | |
e9174d1e | 1779 | DataBorrowed(_, a) => a, |
1a4d82fc | 1780 | ReferenceOutlivesReferent(_, a) => a, |
e9174d1e | 1781 | ParameterInScope(_, a) => a, |
1a4d82fc JJ |
1782 | ExprTypeIsNotInScope(_, a) => a, |
1783 | BindingTypeIsNotValidAtDecl(a) => a, | |
1784 | CallRcvr(a) => a, | |
1785 | CallArg(a) => a, | |
1786 | CallReturn(a) => a, | |
85aaf69f | 1787 | Operand(a) => a, |
1a4d82fc JJ |
1788 | AddrOf(a) => a, |
1789 | AutoBorrow(a) => a, | |
85aaf69f | 1790 | SafeDestructor(a) => a, |
c30ab7b3 SL |
1791 | CompareImplMethodObligation { span, .. } => span, |
1792 | } | |
1793 | } | |
1794 | ||
1795 | pub fn from_obligation_cause<F>(cause: &traits::ObligationCause<'tcx>, | |
1796 | default: F) | |
1797 | -> Self | |
1798 | where F: FnOnce() -> Self | |
1799 | { | |
1800 | match cause.code { | |
1801 | traits::ObligationCauseCode::ReferenceOutlivesReferent(ref_type) => | |
1802 | SubregionOrigin::ReferenceOutlivesReferent(ref_type, cause.span), | |
1803 | ||
1804 | traits::ObligationCauseCode::CompareImplMethodObligation { item_name, | |
1805 | impl_item_def_id, | |
1806 | trait_item_def_id, | |
1807 | lint_id } => | |
1808 | SubregionOrigin::CompareImplMethodObligation { | |
1809 | span: cause.span, | |
1810 | item_name: item_name, | |
1811 | impl_item_def_id: impl_item_def_id, | |
1812 | trait_item_def_id: trait_item_def_id, | |
1813 | lint_id: lint_id, | |
1814 | }, | |
1815 | ||
1816 | _ => default(), | |
1a4d82fc JJ |
1817 | } |
1818 | } | |
1819 | } | |
1820 | ||
c34b1796 | 1821 | impl RegionVariableOrigin { |
1a4d82fc JJ |
1822 | pub fn span(&self) -> Span { |
1823 | match *self { | |
1824 | MiscVariable(a) => a, | |
1825 | PatternRegion(a) => a, | |
1826 | AddrOfRegion(a) => a, | |
1a4d82fc | 1827 | Autoref(a) => a, |
c34b1796 | 1828 | Coercion(a) => a, |
1a4d82fc | 1829 | EarlyBoundRegion(a, _) => a, |
9e0c209e | 1830 | LateBoundRegion(a, ..) => a, |
3157f602 | 1831 | BoundRegionInCoherence(_) => syntax_pos::DUMMY_SP, |
1a4d82fc JJ |
1832 | UpvarRegion(_, a) => a |
1833 | } | |
1834 | } | |
1835 | } | |
5bcae85e SL |
1836 | |
1837 | impl<'tcx> TypeFoldable<'tcx> for TypeOrigin { | |
1838 | fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, _folder: &mut F) -> Self { | |
1839 | self.clone() | |
1840 | } | |
1841 | ||
1842 | fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _visitor: &mut V) -> bool { | |
1843 | false | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | impl<'tcx> TypeFoldable<'tcx> for ValuePairs<'tcx> { | |
1848 | fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { | |
1849 | match *self { | |
1850 | ValuePairs::Types(ref ef) => { | |
1851 | ValuePairs::Types(ef.fold_with(folder)) | |
1852 | } | |
1853 | ValuePairs::TraitRefs(ref ef) => { | |
1854 | ValuePairs::TraitRefs(ef.fold_with(folder)) | |
1855 | } | |
1856 | ValuePairs::PolyTraitRefs(ref ef) => { | |
1857 | ValuePairs::PolyTraitRefs(ef.fold_with(folder)) | |
1858 | } | |
1859 | } | |
1860 | } | |
1861 | ||
1862 | fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool { | |
1863 | match *self { | |
1864 | ValuePairs::Types(ref ef) => ef.visit_with(visitor), | |
1865 | ValuePairs::TraitRefs(ref ef) => ef.visit_with(visitor), | |
1866 | ValuePairs::PolyTraitRefs(ref ef) => ef.visit_with(visitor), | |
1867 | } | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | impl<'tcx> TypeFoldable<'tcx> for TypeTrace<'tcx> { | |
1872 | fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self { | |
1873 | TypeTrace { | |
1874 | origin: self.origin.fold_with(folder), | |
1875 | values: self.values.fold_with(folder) | |
1876 | } | |
1877 | } | |
1878 | ||
1879 | fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool { | |
1880 | self.origin.visit_with(visitor) || self.values.visit_with(visitor) | |
1881 | } | |
1882 | } |