First stab at desugaring bounds for APIT

This commit is contained in:
Florian Diebold 2020-01-31 15:17:48 +01:00
parent a9430865b3
commit dbc14f9d57
3 changed files with 56 additions and 10 deletions

View file

@ -53,10 +53,17 @@ pub struct GenericParams {
/// associated type bindings like `Iterator<Item = u32>`.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct WherePredicate {
pub type_ref: TypeRef,
pub target: WherePredicateTarget,
pub bound: TypeBound,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum WherePredicateTarget {
TypeRef(TypeRef),
/// For desugared where predicates that can directly refer to a type param.
TypeParam(LocalTypeParamId)
}
type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>;
impl GenericParams {
@ -190,18 +197,24 @@ impl GenericParams {
return;
}
let bound = TypeBound::from_ast(bound);
self.where_predicates.push(WherePredicate { type_ref, bound });
self.where_predicates.push(WherePredicate { target: WherePredicateTarget::TypeRef(type_ref), bound });
}
fn fill_implicit_impl_trait_args(&mut self, type_ref: &TypeRef) {
type_ref.walk(&mut |type_ref| {
if let TypeRef::ImplTrait(_) = type_ref {
if let TypeRef::ImplTrait(bounds) = type_ref {
let param = TypeParamData {
name: None,
default: None,
provenance: TypeParamProvenance::ArgumentImplTrait,
};
let _param_id = self.types.alloc(param);
let param_id = self.types.alloc(param);
for bound in bounds {
self.where_predicates.push(WherePredicate {
target: WherePredicateTarget::TypeParam(param_id),
bound: bound.clone()
});
}
}
});
}
@ -211,6 +224,12 @@ impl GenericParams {
.iter()
.find_map(|(id, p)| if p.name.as_ref() == Some(name) { Some(id) } else { None })
}
pub fn find_trait_self_param(&self) -> Option<LocalTypeParamId> {
self.types
.iter()
.find_map(|(id, p)| if p.provenance == TypeParamProvenance::TraitSelf { Some(id) } else { None })
}
}
impl HasChildSource for GenericDefId {