|
10 | 10 |
|
11 | 11 | use middle::def_id::DefId;
|
12 | 12 | use middle::infer::InferCtxt;
|
13 |
| -use middle::subst::Substs; |
| 13 | +use middle::subst::{Subst, Substs}; |
14 | 14 | use middle::ty::{self, Ty, TyCtxt, ToPredicate, ToPolyTraitRef};
|
15 | 15 | use syntax::codemap::Span;
|
16 | 16 | use util::common::ErrorReported;
|
17 | 17 | use util::nodemap::FnvHashSet;
|
18 | 18 |
|
19 |
| -use super::{Obligation, ObligationCause, PredicateObligation}; |
| 19 | +use super::{Obligation, ObligationCause, PredicateObligation, SelectionContext, Normalized}; |
20 | 20 |
|
21 | 21 | struct PredicateSet<'a,'tcx:'a> {
|
22 | 22 | tcx: &'a TyCtxt<'tcx>,
|
@@ -299,6 +299,38 @@ impl<'tcx,I:Iterator<Item=ty::Predicate<'tcx>>> Iterator for FilterToTraits<I> {
|
299 | 299 | // Other
|
300 | 300 | ///////////////////////////////////////////////////////////////////////////
|
301 | 301 |
|
| 302 | +/// Instantiate all bound parameters of the impl with the given substs, |
| 303 | +/// returning the resulting trait ref and all obligations that arise. |
| 304 | +/// The obligations are closed under normalization. |
| 305 | +pub fn impl_trait_ref_and_oblig<'a,'tcx>(selcx: &mut SelectionContext<'a,'tcx>, |
| 306 | + impl_def_id: DefId, |
| 307 | + impl_substs: &Substs<'tcx>) |
| 308 | + -> (ty::TraitRef<'tcx>, |
| 309 | + Vec<PredicateObligation<'tcx>>) |
| 310 | +{ |
| 311 | + let impl_trait_ref = |
| 312 | + selcx.tcx().impl_trait_ref(impl_def_id).unwrap(); |
| 313 | + let impl_trait_ref = |
| 314 | + impl_trait_ref.subst(selcx.tcx(), impl_substs); |
| 315 | + let Normalized { value: impl_trait_ref, obligations: normalization_obligations1 } = |
| 316 | + super::normalize(selcx, ObligationCause::dummy(), &impl_trait_ref); |
| 317 | + |
| 318 | + let predicates = selcx.tcx().lookup_predicates(impl_def_id); |
| 319 | + let predicates = predicates.instantiate(selcx.tcx(), impl_substs); |
| 320 | + let Normalized { value: predicates, obligations: normalization_obligations2 } = |
| 321 | + super::normalize(selcx, ObligationCause::dummy(), &predicates); |
| 322 | + let impl_obligations = |
| 323 | + predicates_for_generics(ObligationCause::dummy(), 0, &predicates); |
| 324 | + |
| 325 | + let impl_obligations: Vec<_> = |
| 326 | + impl_obligations.into_iter() |
| 327 | + .chain(normalization_obligations1) |
| 328 | + .chain(normalization_obligations2) |
| 329 | + .collect(); |
| 330 | + |
| 331 | + (impl_trait_ref, impl_obligations) |
| 332 | +} |
| 333 | + |
302 | 334 | // determine the `self` type, using fresh variables for all variables
|
303 | 335 | // declared on the impl declaration e.g., `impl<A,B> for Box<[(A,B)]>`
|
304 | 336 | // would return ($0, $1) where $0 and $1 are freshly instantiated type
|
|
0 commit comments