Skip to content

Commit 8f20cbf

Browse files
committed
Add more commentary for subst translation
1 parent 940adda commit 8f20cbf

File tree

1 file changed

+29
-3
lines changed
  • src/librustc/middle/traits/specialize

1 file changed

+29
-3
lines changed

src/librustc/middle/traits/specialize/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,35 @@ pub fn translate_substs<'tcx>(tcx: &ty::ctxt<'tcx>,
6464
/// When we have selected one impl, but are actually using item definitions from
6565
/// a parent impl providing a default, we need a way to translate between the
6666
/// type parameters of the two impls. Here the `source_impl` is the one we've
67-
/// selected, and `source_substs` is a substitution of its generics (and possibly
68-
/// some relevant `FnSpace` variables as well). And `target_impl` is the impl
69-
/// we're actually going to get the definition from.
67+
/// selected, and `source_substs` is a substitution of its generics (and
68+
/// possibly some relevant `FnSpace` variables as well). And `target_impl` is
69+
/// the impl we're actually going to get the definition from. The resulting
70+
/// substitution will map from `target_impl`'s generics to `source_impl`'s
71+
/// generics as instantiated by `source_subst`.
72+
///
73+
/// For example, consider the following scenario:
74+
///
75+
/// ```rust
76+
/// trait Foo { ... }
77+
/// impl<T, U> Foo for (T, U) { ... } // target impl
78+
/// impl<V> Foo for (V, V) { ... } // source impl
79+
/// ```
80+
///
81+
/// Suppose we have selected "source impl" with `V` instantiated with `u32`.
82+
/// This function will produce a substitution with `T` and `U` both mapping to `u32`.
83+
///
84+
/// Where clauses add some trickiness here, because they can be used to "define"
85+
/// an argument indirectly:
86+
///
87+
/// ```rust
88+
/// impl<'a, I, T: 'a> Iterator for Cloned<I>
89+
/// where I: Iterator<Item=&'a T>, T: Clone
90+
/// ```
91+
///
92+
/// In a case like this, the substitution for `T` is determined indirectly,
93+
/// through associated type projection. We deal with such cases by using
94+
/// *fulfillment* to relate the two impls, requiring that all projections are
95+
/// resolved.
7096
fn translate_substs_between_impls<'tcx>(tcx: &ty::ctxt<'tcx>,
7197
source_impl: DefId,
7298
source_substs: Substs<'tcx>,

0 commit comments

Comments
 (0)