|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -use middle::ty::{self}; |
| 11 | +use middle::subst; |
| 12 | +use middle::ty::{self, Ty}; |
12 | 13 |
|
13 | 14 | use std::collections::HashSet;
|
14 | 15 | use std::rc::Rc;
|
15 | 16 |
|
| 17 | +#[derive(Clone, PartialEq, Eq, Hash, Debug)] |
| 18 | +pub enum Parameter { |
| 19 | + Type(ty::ParamTy), |
| 20 | + Region(ty::EarlyBoundRegion), |
| 21 | +} |
| 22 | + |
| 23 | +pub fn parameters_for_type<'tcx>(ty: Ty<'tcx>) -> Vec<Parameter> { |
| 24 | + ty.walk() |
| 25 | + .flat_map(|ty| parameters_for_type_shallow(ty).into_iter()) |
| 26 | + .collect() |
| 27 | +} |
| 28 | + |
| 29 | +pub fn parameters_for_trait_ref<'tcx>(trait_ref: &Rc<ty::TraitRef<'tcx>>) -> Vec<Parameter> { |
| 30 | + let mut region_parameters = |
| 31 | + parameters_for_regions_in_substs(&trait_ref.substs); |
| 32 | + |
| 33 | + let type_parameters = |
| 34 | + trait_ref.substs.types.iter() |
| 35 | + .flat_map(|ty| parameters_for_type(ty).into_iter()); |
| 36 | + |
| 37 | + region_parameters.extend(type_parameters); |
| 38 | + |
| 39 | + region_parameters |
| 40 | +} |
| 41 | + |
| 42 | +fn parameters_for_type_shallow<'tcx>(ty: Ty<'tcx>) -> Vec<Parameter> { |
| 43 | + match ty.sty { |
| 44 | + ty::ty_param(ref d) => |
| 45 | + vec![Parameter::Type(d.clone())], |
| 46 | + ty::ty_rptr(region, _) => |
| 47 | + parameters_for_region(region).into_iter().collect(), |
| 48 | + ty::ty_struct(_, substs) | |
| 49 | + ty::ty_enum(_, substs) => |
| 50 | + parameters_for_regions_in_substs(substs), |
| 51 | + ty::ty_trait(ref data) => |
| 52 | + parameters_for_regions_in_substs(&data.principal.skip_binder().substs), |
| 53 | + _ => |
| 54 | + vec![], |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn parameters_for_regions_in_substs(substs: &subst::Substs) -> Vec<Parameter> { |
| 59 | + substs.regions() |
| 60 | + .iter() |
| 61 | + .filter_map(|r| parameters_for_region(r)) |
| 62 | + .collect() |
| 63 | +} |
| 64 | + |
| 65 | +fn parameters_for_region(region: &ty::Region) -> Option<Parameter> { |
| 66 | + match *region { |
| 67 | + ty::ReEarlyBound(data) => Some(Parameter::Region(data)), |
| 68 | + _ => None, |
| 69 | + } |
| 70 | +} |
| 71 | + |
16 | 72 | pub fn identify_constrained_type_params<'tcx>(_tcx: &ty::ctxt<'tcx>,
|
17 | 73 | predicates: &[ty::Predicate<'tcx>],
|
18 | 74 | impl_trait_ref: Option<Rc<ty::TraitRef<'tcx>>>,
|
19 |
| - input_parameters: &mut HashSet<ty::ParamTy>) |
| 75 | + input_parameters: &mut HashSet<Parameter>) |
20 | 76 | {
|
21 | 77 | loop {
|
22 | 78 | let num_inputs = input_parameters.len();
|
23 | 79 |
|
24 |
| - let projection_predicates = |
| 80 | + let poly_projection_predicates = // : iterator over PolyProjectionPredicate |
25 | 81 | predicates.iter()
|
26 | 82 | .filter_map(|predicate| {
|
27 | 83 | match *predicate {
|
28 |
| - // Ignore higher-ranked binders. For the purposes |
29 |
| - // of this check, they don't matter because they |
30 |
| - // only affect named regions, and we're just |
31 |
| - // concerned about type parameters here. |
32 |
| - ty::Predicate::Projection(ref data) => Some(data.0.clone()), |
| 84 | + ty::Predicate::Projection(ref data) => Some(data.clone()), |
33 | 85 | _ => None,
|
34 | 86 | }
|
35 | 87 | });
|
36 | 88 |
|
37 |
| - for projection in projection_predicates { |
| 89 | + for poly_projection in poly_projection_predicates { |
| 90 | + // Note that we can skip binder here because the impl |
| 91 | + // trait ref never contains any late-bound regions. |
| 92 | + let projection = poly_projection.skip_binder(); |
| 93 | + |
38 | 94 | // Special case: watch out for some kind of sneaky attempt
|
39 |
| - // to project out an associated type defined by this very trait. |
40 |
| - if Some(projection.projection_ty.trait_ref.clone()) == impl_trait_ref { |
| 95 | + // to project out an associated type defined by this very |
| 96 | + // trait. |
| 97 | + let unbound_trait_ref = &projection.projection_ty.trait_ref; |
| 98 | + if Some(unbound_trait_ref.clone()) == impl_trait_ref { |
41 | 99 | continue;
|
42 | 100 | }
|
43 | 101 |
|
44 |
| - let relies_only_on_inputs = |
45 |
| - projection.projection_ty.trait_ref.input_types() |
46 |
| - .iter() |
47 |
| - .flat_map(|t| t.walk()) |
48 |
| - .filter_map(|t| t.as_opt_param_ty()) |
49 |
| - .all(|t| input_parameters.contains(&t)); |
50 |
| - |
| 102 | + let inputs = parameters_for_trait_ref(&projection.projection_ty.trait_ref); |
| 103 | + let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(&p)); |
51 | 104 | if relies_only_on_inputs {
|
52 |
| - input_parameters.extend( |
53 |
| - projection.ty.walk().filter_map(|t| t.as_opt_param_ty())); |
| 105 | + input_parameters.extend(parameters_for_type(projection.ty)); |
54 | 106 | }
|
55 | 107 | }
|
56 | 108 |
|
|
0 commit comments