]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 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 | //! Trait Resolution. See the Book for more. |
1a4d82fc JJ |
12 | |
13 | pub use self::SelectionError::*; | |
14 | pub use self::FulfillmentErrorCode::*; | |
15 | pub use self::Vtable::*; | |
16 | pub use self::ObligationCauseCode::*; | |
17 | ||
bd371182 | 18 | use middle::free_region::FreeRegionMap; |
1a4d82fc | 19 | use middle::subst; |
c1a9b12d | 20 | use middle::ty::{self, HasTypeFlags, Ty}; |
85aaf69f | 21 | use middle::ty_fold::TypeFoldable; |
c34b1796 | 22 | use middle::infer::{self, fixup_err_to_string, InferCtxt}; |
1a4d82fc JJ |
23 | use std::rc::Rc; |
24 | use syntax::ast; | |
25 | use syntax::codemap::{Span, DUMMY_SP}; | |
1a4d82fc JJ |
26 | |
27 | pub use self::error_reporting::report_fulfillment_errors; | |
c34b1796 | 28 | pub use self::error_reporting::report_overflow_error; |
d9579d0f | 29 | pub use self::error_reporting::report_selection_error; |
1a4d82fc JJ |
30 | pub use self::error_reporting::suggest_new_overflow_limit; |
31 | pub use self::coherence::orphan_check; | |
85aaf69f | 32 | pub use self::coherence::overlapping_impls; |
1a4d82fc | 33 | pub use self::coherence::OrphanCheckErr; |
62682a34 | 34 | pub use self::fulfill::{FulfillmentContext, FulfilledPredicates, RegionObligation}; |
1a4d82fc JJ |
35 | pub use self::project::MismatchedProjectionTypes; |
36 | pub use self::project::normalize; | |
37 | pub use self::project::Normalized; | |
38 | pub use self::object_safety::is_object_safe; | |
39 | pub use self::object_safety::object_safety_violations; | |
40 | pub use self::object_safety::ObjectSafetyViolation; | |
41 | pub use self::object_safety::MethodViolationCode; | |
c34b1796 | 42 | pub use self::object_safety::is_vtable_safe_method; |
1a4d82fc JJ |
43 | pub use self::select::SelectionContext; |
44 | pub use self::select::SelectionCache; | |
45 | pub use self::select::{MethodMatchResult, MethodMatched, MethodAmbiguous, MethodDidNotMatch}; | |
46 | pub use self::select::{MethodMatchedData}; // intentionally don't export variants | |
47 | pub use self::util::elaborate_predicates; | |
48 | pub use self::util::get_vtable_index_of_object_method; | |
49 | pub use self::util::trait_ref_for_builtin_bound; | |
d9579d0f | 50 | pub use self::util::predicate_for_trait_def; |
1a4d82fc JJ |
51 | pub use self::util::supertraits; |
52 | pub use self::util::Supertraits; | |
c34b1796 AL |
53 | pub use self::util::supertrait_def_ids; |
54 | pub use self::util::SupertraitDefIds; | |
1a4d82fc JJ |
55 | pub use self::util::transitive_bounds; |
56 | pub use self::util::upcast; | |
57 | ||
58 | mod coherence; | |
59 | mod error_reporting; | |
60 | mod fulfill; | |
61 | mod project; | |
62 | mod object_safety; | |
63 | mod select; | |
64 | mod util; | |
65 | ||
66 | /// An `Obligation` represents some trait reference (e.g. `int:Eq`) for | |
67 | /// which the vtable must be found. The process of finding a vtable is | |
68 | /// called "resolving" the `Obligation`. This process consists of | |
69 | /// either identifying an `impl` (e.g., `impl Eq for int`) that | |
70 | /// provides the required vtable, or else finding a bound that is in | |
71 | /// scope. The eventual result is usually a `Selection` (defined below). | |
85aaf69f | 72 | #[derive(Clone, PartialEq, Eq)] |
1a4d82fc JJ |
73 | pub struct Obligation<'tcx, T> { |
74 | pub cause: ObligationCause<'tcx>, | |
c34b1796 | 75 | pub recursion_depth: usize, |
1a4d82fc JJ |
76 | pub predicate: T, |
77 | } | |
78 | ||
79 | pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>; | |
80 | pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>; | |
81 | ||
82 | /// Why did we incur this obligation? Used for error reporting. | |
85aaf69f | 83 | #[derive(Clone, PartialEq, Eq)] |
1a4d82fc JJ |
84 | pub struct ObligationCause<'tcx> { |
85 | pub span: Span, | |
86 | ||
87 | // The id of the fn body that triggered this obligation. This is | |
88 | // used for region obligations to determine the precise | |
89 | // environment in which the region obligation should be evaluated | |
90 | // (in particular, closures can add new assumptions). See the | |
91 | // field `region_obligations` of the `FulfillmentContext` for more | |
92 | // information. | |
93 | pub body_id: ast::NodeId, | |
94 | ||
95 | pub code: ObligationCauseCode<'tcx> | |
96 | } | |
97 | ||
85aaf69f | 98 | #[derive(Clone, PartialEq, Eq)] |
1a4d82fc JJ |
99 | pub enum ObligationCauseCode<'tcx> { |
100 | /// Not well classified or should be obvious from span. | |
101 | MiscObligation, | |
102 | ||
103 | /// In an impl of trait X for type Y, type Y must | |
104 | /// also implement all supertraits of X. | |
105 | ItemObligation(ast::DefId), | |
106 | ||
107 | /// Obligation incurred due to an object cast. | |
108 | ObjectCastObligation(/* Object type */ Ty<'tcx>), | |
109 | ||
110 | /// Various cases where expressions must be sized/copy/etc: | |
111 | AssignmentLhsSized, // L = X implies that L is Sized | |
112 | StructInitializerSized, // S { ... } must be Sized | |
113 | VariableType(ast::NodeId), // Type of each variable must be Sized | |
114 | ReturnType, // Return type must be Sized | |
115 | RepeatVec, // [T,..n] --> T must be Copy | |
116 | ||
117 | // Captures of variable the given id by a closure (span is the | |
118 | // span of the closure) | |
119 | ClosureCapture(ast::NodeId, Span, ty::BuiltinBound), | |
120 | ||
121 | // Types of fields (other than the last) in a struct must be sized. | |
122 | FieldSized, | |
123 | ||
1a4d82fc JJ |
124 | // static items must have `Sync` type |
125 | SharedStatic, | |
126 | ||
85aaf69f | 127 | |
1a4d82fc JJ |
128 | BuiltinDerivedObligation(DerivedObligationCause<'tcx>), |
129 | ||
130 | ImplDerivedObligation(DerivedObligationCause<'tcx>), | |
85aaf69f SL |
131 | |
132 | CompareImplMethodObligation, | |
1a4d82fc JJ |
133 | } |
134 | ||
85aaf69f | 135 | #[derive(Clone, PartialEq, Eq)] |
1a4d82fc JJ |
136 | pub struct DerivedObligationCause<'tcx> { |
137 | /// The trait reference of the parent obligation that led to the | |
138 | /// current obligation. Note that only trait obligations lead to | |
139 | /// derived obligations, so we just store the trait reference here | |
140 | /// directly. | |
141 | parent_trait_ref: ty::PolyTraitRef<'tcx>, | |
142 | ||
143 | /// The parent trait had this cause | |
144 | parent_code: Rc<ObligationCauseCode<'tcx>> | |
145 | } | |
146 | ||
62682a34 SL |
147 | pub type Obligations<'tcx, O> = Vec<Obligation<'tcx, O>>; |
148 | pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>; | |
149 | pub type TraitObligations<'tcx> = Vec<TraitObligation<'tcx>>; | |
1a4d82fc JJ |
150 | |
151 | pub type Selection<'tcx> = Vtable<'tcx, PredicateObligation<'tcx>>; | |
152 | ||
85aaf69f | 153 | #[derive(Clone,Debug)] |
1a4d82fc JJ |
154 | pub enum SelectionError<'tcx> { |
155 | Unimplemented, | |
1a4d82fc JJ |
156 | OutputTypeParameterMismatch(ty::PolyTraitRef<'tcx>, |
157 | ty::PolyTraitRef<'tcx>, | |
c1a9b12d | 158 | ty::TypeError<'tcx>), |
d9579d0f | 159 | TraitNotObjectSafe(ast::DefId), |
1a4d82fc JJ |
160 | } |
161 | ||
162 | pub struct FulfillmentError<'tcx> { | |
163 | pub obligation: PredicateObligation<'tcx>, | |
164 | pub code: FulfillmentErrorCode<'tcx> | |
165 | } | |
166 | ||
167 | #[derive(Clone)] | |
168 | pub enum FulfillmentErrorCode<'tcx> { | |
169 | CodeSelectionError(SelectionError<'tcx>), | |
170 | CodeProjectionError(MismatchedProjectionTypes<'tcx>), | |
171 | CodeAmbiguity, | |
172 | } | |
173 | ||
174 | /// When performing resolution, it is typically the case that there | |
175 | /// can be one of three outcomes: | |
176 | /// | |
177 | /// - `Ok(Some(r))`: success occurred with result `r` | |
178 | /// - `Ok(None)`: could not definitely determine anything, usually due | |
179 | /// to inconclusive type inference. | |
180 | /// - `Err(e)`: error `e` occurred | |
181 | pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>; | |
182 | ||
183 | /// Given the successful resolution of an obligation, the `Vtable` | |
184 | /// indicates where the vtable comes from. Note that while we call this | |
185 | /// a "vtable", it does not necessarily indicate dynamic dispatch at | |
186 | /// runtime. `Vtable` instances just tell the compiler where to find | |
187 | /// methods, but in generic code those methods are typically statically | |
188 | /// dispatched -- only when an object is constructed is a `Vtable` | |
189 | /// instance reified into an actual vtable. | |
190 | /// | |
191 | /// For example, the vtable may be tied to a specific impl (case A), | |
192 | /// or it may be relative to some bound that is in scope (case B). | |
193 | /// | |
194 | /// | |
195 | /// ``` | |
196 | /// impl<T:Clone> Clone<T> for Option<T> { ... } // Impl_1 | |
197 | /// impl<T:Clone> Clone<T> for Box<T> { ... } // Impl_2 | |
198 | /// impl Clone for int { ... } // Impl_3 | |
199 | /// | |
200 | /// fn foo<T:Clone>(concrete: Option<Box<int>>, | |
201 | /// param: T, | |
202 | /// mixed: Option<T>) { | |
203 | /// | |
204 | /// // Case A: Vtable points at a specific impl. Only possible when | |
205 | /// // type is concretely known. If the impl itself has bounded | |
206 | /// // type parameters, Vtable will carry resolutions for those as well: | |
207 | /// concrete.clone(); // Vtable(Impl_1, [Vtable(Impl_2, [Vtable(Impl_3)])]) | |
208 | /// | |
209 | /// // Case B: Vtable must be provided by caller. This applies when | |
210 | /// // type is a type parameter. | |
211 | /// param.clone(); // VtableParam | |
212 | /// | |
213 | /// // Case C: A mix of cases A and B. | |
214 | /// mixed.clone(); // Vtable(Impl_1, [VtableParam]) | |
215 | /// } | |
216 | /// ``` | |
217 | /// | |
218 | /// ### The type parameter `N` | |
219 | /// | |
220 | /// See explanation on `VtableImplData`. | |
62682a34 | 221 | #[derive(Clone)] |
1a4d82fc JJ |
222 | pub enum Vtable<'tcx, N> { |
223 | /// Vtable identifying a particular impl. | |
224 | VtableImpl(VtableImplData<'tcx, N>), | |
225 | ||
c34b1796 AL |
226 | /// Vtable for default trait implementations |
227 | /// This carries the information and nested obligations with regards | |
228 | /// to a default implementation for a trait `Trait`. The nested obligations | |
229 | /// ensure the trait implementation holds for all the constituent types. | |
230 | VtableDefaultImpl(VtableDefaultImplData<N>), | |
231 | ||
1a4d82fc | 232 | /// Successful resolution to an obligation provided by the caller |
85aaf69f SL |
233 | /// for some type parameter. The `Vec<N>` represents the |
234 | /// obligations incurred from normalizing the where-clause (if | |
235 | /// any). | |
236 | VtableParam(Vec<N>), | |
1a4d82fc JJ |
237 | |
238 | /// Virtual calls through an object | |
239 | VtableObject(VtableObjectData<'tcx>), | |
240 | ||
241 | /// Successful resolution for a builtin trait. | |
242 | VtableBuiltin(VtableBuiltinData<N>), | |
243 | ||
85aaf69f SL |
244 | /// Vtable automatically generated for a closure. The def ID is the ID |
245 | /// of the closure expression. This is a `VtableImpl` in spirit, but the | |
246 | /// impl is generated by the compiler and does not appear in the source. | |
62682a34 | 247 | VtableClosure(VtableClosureData<'tcx, N>), |
1a4d82fc JJ |
248 | |
249 | /// Same as above, but for a fn pointer type with the given signature. | |
250 | VtableFnPointer(ty::Ty<'tcx>), | |
251 | } | |
252 | ||
253 | /// Identifies a particular impl in the source, along with a set of | |
254 | /// substitutions from the impl's type/lifetime parameters. The | |
255 | /// `nested` vector corresponds to the nested obligations attached to | |
256 | /// the impl's type parameters. | |
257 | /// | |
258 | /// The type parameter `N` indicates the type used for "nested | |
259 | /// obligations" that are required by the impl. During type check, this | |
260 | /// is `Obligation`, as one might expect. During trans, however, this | |
261 | /// is `()`, because trans only requires a shallow resolution of an | |
262 | /// impl, and nested obligations are satisfied later. | |
85aaf69f | 263 | #[derive(Clone, PartialEq, Eq)] |
1a4d82fc JJ |
264 | pub struct VtableImplData<'tcx, N> { |
265 | pub impl_def_id: ast::DefId, | |
266 | pub substs: subst::Substs<'tcx>, | |
62682a34 SL |
267 | pub nested: Vec<N> |
268 | } | |
269 | ||
270 | #[derive(Clone, PartialEq, Eq)] | |
271 | pub struct VtableClosureData<'tcx, N> { | |
272 | pub closure_def_id: ast::DefId, | |
c1a9b12d | 273 | pub substs: ty::ClosureSubsts<'tcx>, |
62682a34 SL |
274 | /// Nested obligations. This can be non-empty if the closure |
275 | /// signature contains associated types. | |
276 | pub nested: Vec<N> | |
1a4d82fc JJ |
277 | } |
278 | ||
62682a34 | 279 | #[derive(Clone)] |
c34b1796 AL |
280 | pub struct VtableDefaultImplData<N> { |
281 | pub trait_def_id: ast::DefId, | |
282 | pub nested: Vec<N> | |
283 | } | |
284 | ||
62682a34 | 285 | #[derive(Clone)] |
1a4d82fc | 286 | pub struct VtableBuiltinData<N> { |
62682a34 | 287 | pub nested: Vec<N> |
1a4d82fc JJ |
288 | } |
289 | ||
290 | /// A vtable for some object-safe trait `Foo` automatically derived | |
291 | /// for the object type `Foo`. | |
292 | #[derive(PartialEq,Eq,Clone)] | |
293 | pub struct VtableObjectData<'tcx> { | |
c34b1796 AL |
294 | /// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`. |
295 | pub upcast_trait_ref: ty::PolyTraitRef<'tcx>, | |
c1a9b12d SL |
296 | |
297 | /// The vtable is formed by concatenating together the method lists of | |
298 | /// the base object trait and all supertraits; this is the start of | |
299 | /// `upcast_trait_ref`'s methods in that vtable. | |
300 | pub vtable_base: usize | |
1a4d82fc JJ |
301 | } |
302 | ||
1a4d82fc | 303 | /// Creates predicate obligations from the generic bounds. |
62682a34 | 304 | pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>, |
85aaf69f | 305 | generic_bounds: &ty::InstantiatedPredicates<'tcx>) |
1a4d82fc JJ |
306 | -> PredicateObligations<'tcx> |
307 | { | |
62682a34 | 308 | util::predicates_for_generics(cause, 0, generic_bounds) |
1a4d82fc JJ |
309 | } |
310 | ||
311 | /// Determines whether the type `ty` is known to meet `bound` and | |
312 | /// returns true if so. Returns false if `ty` either does not meet | |
313 | /// `bound` or is not known to meet bound (note that this is | |
314 | /// conservative towards *no impl*, which is the opposite of the | |
315 | /// `evaluate` methods). | |
62682a34 | 316 | pub fn type_known_to_meet_builtin_bound<'a,'tcx>(infcx: &InferCtxt<'a,'tcx>, |
62682a34 SL |
317 | ty: Ty<'tcx>, |
318 | bound: ty::BuiltinBound, | |
319 | span: Span) | |
320 | -> bool | |
1a4d82fc | 321 | { |
62682a34 SL |
322 | debug!("type_known_to_meet_builtin_bound(ty={:?}, bound={:?})", |
323 | ty, | |
1a4d82fc JJ |
324 | bound); |
325 | ||
62682a34 | 326 | let mut fulfill_cx = FulfillmentContext::new(false); |
1a4d82fc JJ |
327 | |
328 | // We can use a dummy node-id here because we won't pay any mind | |
329 | // to region obligations that arise (there shouldn't really be any | |
330 | // anyhow). | |
331 | let cause = ObligationCause::misc(span, ast::DUMMY_NODE_ID); | |
332 | ||
333 | fulfill_cx.register_builtin_bound(infcx, ty, bound, cause); | |
334 | ||
335 | // Note: we only assume something is `Copy` if we can | |
336 | // *definitively* show that it implements `Copy`. Otherwise, | |
337 | // assume it is move; linear is always ok. | |
c1a9b12d | 338 | match fulfill_cx.select_all_or_error(infcx) { |
62682a34 SL |
339 | Ok(()) => { |
340 | debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} success", | |
341 | ty, | |
342 | bound); | |
1a4d82fc JJ |
343 | true |
344 | } | |
62682a34 SL |
345 | Err(e) => { |
346 | debug!("type_known_to_meet_builtin_bound: ty={:?} bound={:?} errors={:?}", | |
347 | ty, | |
348 | bound, | |
349 | e); | |
1a4d82fc JJ |
350 | false |
351 | } | |
352 | } | |
353 | } | |
354 | ||
c1a9b12d | 355 | // FIXME: this is gonna need to be removed ... |
c34b1796 | 356 | /// Normalizes the parameter environment, reporting errors if they occur. |
85aaf69f SL |
357 | pub fn normalize_param_env_or_error<'a,'tcx>(unnormalized_env: ty::ParameterEnvironment<'a,'tcx>, |
358 | cause: ObligationCause<'tcx>) | |
359 | -> ty::ParameterEnvironment<'a,'tcx> | |
360 | { | |
c34b1796 AL |
361 | // I'm not wild about reporting errors here; I'd prefer to |
362 | // have the errors get reported at a defined place (e.g., | |
363 | // during typeck). Instead I have all parameter | |
364 | // environments, in effect, going through this function | |
365 | // and hence potentially reporting errors. This ensurse of | |
366 | // course that we never forget to normalize (the | |
367 | // alternative seemed like it would involve a lot of | |
368 | // manual invocations of this fn -- and then we'd have to | |
369 | // deal with the errors at each of those sites). | |
370 | // | |
371 | // In any case, in practice, typeck constructs all the | |
372 | // parameter environments once for every fn as it goes, | |
373 | // and errors will get reported then; so after typeck we | |
374 | // can be sure that no errors should occur. | |
375 | ||
376 | let tcx = unnormalized_env.tcx; | |
377 | let span = cause.span; | |
378 | let body_id = cause.body_id; | |
379 | ||
62682a34 SL |
380 | debug!("normalize_param_env_or_error(unnormalized_env={:?})", |
381 | unnormalized_env); | |
382 | ||
383 | let predicates: Vec<_> = | |
384 | util::elaborate_predicates(tcx, unnormalized_env.caller_bounds.clone()) | |
385 | .filter(|p| !p.is_global()) // (*) | |
386 | .collect(); | |
387 | ||
388 | // (*) Any predicate like `i32: Trait<u32>` or whatever doesn't | |
389 | // need to be in the *environment* to be proven, so screen those | |
390 | // out. This is important for the soundness of inter-fn | |
391 | // caching. Note though that we should probably check that these | |
392 | // predicates hold at the point where the environment is | |
393 | // constructed, but I am not currently doing so out of laziness. | |
394 | // -nmatsakis | |
395 | ||
396 | debug!("normalize_param_env_or_error: elaborated-predicates={:?}", | |
397 | predicates); | |
398 | ||
399 | let elaborated_env = unnormalized_env.with_caller_bounds(predicates); | |
c34b1796 | 400 | |
c1a9b12d SL |
401 | let infcx = infer::new_infer_ctxt(tcx, &tcx.tables, Some(elaborated_env), false); |
402 | let predicates = match fully_normalize(&infcx, cause, | |
403 | &infcx.parameter_environment.caller_bounds) { | |
c34b1796 | 404 | Ok(predicates) => predicates, |
85aaf69f | 405 | Err(errors) => { |
85aaf69f | 406 | report_fulfillment_errors(&infcx, &errors); |
c1a9b12d | 407 | return infcx.parameter_environment; // an unnormalized env is better than nothing |
85aaf69f | 408 | } |
c34b1796 | 409 | }; |
85aaf69f | 410 | |
bd371182 AL |
411 | let free_regions = FreeRegionMap::new(); |
412 | infcx.resolve_regions_and_report_errors(&free_regions, body_id); | |
c34b1796 AL |
413 | let predicates = match infcx.fully_resolve(&predicates) { |
414 | Ok(predicates) => predicates, | |
415 | Err(fixup_err) => { | |
416 | // If we encounter a fixup error, it means that some type | |
417 | // variable wound up unconstrained. I actually don't know | |
418 | // if this can happen, and I certainly don't expect it to | |
419 | // happen often, but if it did happen it probably | |
420 | // represents a legitimate failure due to some kind of | |
421 | // unconstrained variable, and it seems better not to ICE, | |
422 | // all things considered. | |
423 | let err_msg = fixup_err_to_string(fixup_err); | |
424 | tcx.sess.span_err(span, &err_msg); | |
c1a9b12d | 425 | return infcx.parameter_environment; // an unnormalized env is better than nothing |
c34b1796 AL |
426 | } |
427 | }; | |
85aaf69f | 428 | |
c1a9b12d | 429 | infcx.parameter_environment.with_caller_bounds(predicates) |
85aaf69f SL |
430 | } |
431 | ||
432 | pub fn fully_normalize<'a,'tcx,T>(infcx: &InferCtxt<'a,'tcx>, | |
85aaf69f SL |
433 | cause: ObligationCause<'tcx>, |
434 | value: &T) | |
435 | -> Result<T, Vec<FulfillmentError<'tcx>>> | |
c1a9b12d | 436 | where T : TypeFoldable<'tcx> + HasTypeFlags |
85aaf69f | 437 | { |
62682a34 | 438 | debug!("normalize_param_env(value={:?})", value); |
85aaf69f | 439 | |
c1a9b12d SL |
440 | let mut selcx = &mut SelectionContext::new(infcx); |
441 | // FIXME (@jroesch) ISSUE 26721 | |
442 | // I'm not sure if this is a bug or not, needs further investigation. | |
443 | // It appears that by reusing the fulfillment_cx here we incur more | |
444 | // obligations and later trip an asssertion on regionck.rs line 337. | |
445 | // | |
446 | // The two possibilities I see is: | |
447 | // - normalization is not actually fully happening and we | |
448 | // have a bug else where | |
449 | // - we are adding a duplicate bound into the list causing | |
450 | // its size to change. | |
451 | // | |
452 | // I think we should probably land this refactor and then come | |
453 | // back to this is a follow-up patch. | |
62682a34 | 454 | let mut fulfill_cx = FulfillmentContext::new(false); |
c1a9b12d | 455 | |
85aaf69f SL |
456 | let Normalized { value: normalized_value, obligations } = |
457 | project::normalize(selcx, cause, value); | |
62682a34 SL |
458 | debug!("normalize_param_env: normalized_value={:?} obligations={:?}", |
459 | normalized_value, | |
460 | obligations); | |
85aaf69f SL |
461 | for obligation in obligations { |
462 | fulfill_cx.register_predicate_obligation(selcx.infcx(), obligation); | |
463 | } | |
c1a9b12d SL |
464 | |
465 | try!(fulfill_cx.select_all_or_error(infcx)); | |
85aaf69f | 466 | let resolved_value = infcx.resolve_type_vars_if_possible(&normalized_value); |
62682a34 | 467 | debug!("normalize_param_env: resolved_value={:?}", resolved_value); |
85aaf69f SL |
468 | Ok(resolved_value) |
469 | } | |
470 | ||
1a4d82fc JJ |
471 | impl<'tcx,O> Obligation<'tcx,O> { |
472 | pub fn new(cause: ObligationCause<'tcx>, | |
473 | trait_ref: O) | |
474 | -> Obligation<'tcx, O> | |
475 | { | |
476 | Obligation { cause: cause, | |
477 | recursion_depth: 0, | |
478 | predicate: trait_ref } | |
479 | } | |
480 | ||
481 | fn with_depth(cause: ObligationCause<'tcx>, | |
c34b1796 | 482 | recursion_depth: usize, |
1a4d82fc JJ |
483 | trait_ref: O) |
484 | -> Obligation<'tcx, O> | |
485 | { | |
486 | Obligation { cause: cause, | |
487 | recursion_depth: recursion_depth, | |
488 | predicate: trait_ref } | |
489 | } | |
490 | ||
491 | pub fn misc(span: Span, body_id: ast::NodeId, trait_ref: O) -> Obligation<'tcx, O> { | |
492 | Obligation::new(ObligationCause::misc(span, body_id), trait_ref) | |
493 | } | |
494 | ||
495 | pub fn with<P>(&self, value: P) -> Obligation<'tcx,P> { | |
496 | Obligation { cause: self.cause.clone(), | |
497 | recursion_depth: self.recursion_depth, | |
498 | predicate: value } | |
499 | } | |
500 | } | |
501 | ||
502 | impl<'tcx> ObligationCause<'tcx> { | |
503 | pub fn new(span: Span, | |
504 | body_id: ast::NodeId, | |
505 | code: ObligationCauseCode<'tcx>) | |
506 | -> ObligationCause<'tcx> { | |
507 | ObligationCause { span: span, body_id: body_id, code: code } | |
508 | } | |
509 | ||
510 | pub fn misc(span: Span, body_id: ast::NodeId) -> ObligationCause<'tcx> { | |
511 | ObligationCause { span: span, body_id: body_id, code: MiscObligation } | |
512 | } | |
513 | ||
514 | pub fn dummy() -> ObligationCause<'tcx> { | |
515 | ObligationCause { span: DUMMY_SP, body_id: 0, code: MiscObligation } | |
516 | } | |
517 | } | |
518 | ||
519 | impl<'tcx, N> Vtable<'tcx, N> { | |
62682a34 | 520 | pub fn nested_obligations(self) -> Vec<N> { |
1a4d82fc | 521 | match self { |
62682a34 SL |
522 | VtableImpl(i) => i.nested, |
523 | VtableParam(n) => n, | |
524 | VtableBuiltin(i) => i.nested, | |
525 | VtableDefaultImpl(d) => d.nested, | |
526 | VtableClosure(c) => c.nested, | |
527 | VtableObject(_) | VtableFnPointer(..) => vec![] | |
c34b1796 AL |
528 | } |
529 | } | |
530 | ||
62682a34 SL |
531 | pub fn map<M, F>(self, f: F) -> Vtable<'tcx, M> where F: FnMut(N) -> M { |
532 | match self { | |
533 | VtableImpl(i) => VtableImpl(VtableImplData { | |
534 | impl_def_id: i.impl_def_id, | |
535 | substs: i.substs, | |
536 | nested: i.nested.into_iter().map(f).collect() | |
537 | }), | |
538 | VtableParam(n) => VtableParam(n.into_iter().map(f).collect()), | |
539 | VtableBuiltin(i) => VtableBuiltin(VtableBuiltinData { | |
540 | nested: i.nested.into_iter().map(f).collect() | |
541 | }), | |
542 | VtableObject(o) => VtableObject(o), | |
543 | VtableDefaultImpl(d) => VtableDefaultImpl(VtableDefaultImplData { | |
544 | trait_def_id: d.trait_def_id, | |
545 | nested: d.nested.into_iter().map(f).collect() | |
546 | }), | |
547 | VtableFnPointer(f) => VtableFnPointer(f), | |
548 | VtableClosure(c) => VtableClosure(VtableClosureData { | |
549 | closure_def_id: c.closure_def_id, | |
550 | substs: c.substs, | |
c1a9b12d | 551 | nested: c.nested.into_iter().map(f).collect(), |
62682a34 | 552 | }) |
1a4d82fc JJ |
553 | } |
554 | } | |
555 | } | |
556 | ||
557 | impl<'tcx> FulfillmentError<'tcx> { | |
558 | fn new(obligation: PredicateObligation<'tcx>, | |
559 | code: FulfillmentErrorCode<'tcx>) | |
560 | -> FulfillmentError<'tcx> | |
561 | { | |
562 | FulfillmentError { obligation: obligation, code: code } | |
563 | } | |
1a4d82fc JJ |
564 | } |
565 | ||
566 | impl<'tcx> TraitObligation<'tcx> { | |
c34b1796 AL |
567 | fn self_ty(&self) -> ty::Binder<Ty<'tcx>> { |
568 | ty::Binder(self.predicate.skip_binder().self_ty()) | |
1a4d82fc JJ |
569 | } |
570 | } |