Simplify type_parameter_bounds_in_generics

This commit is contained in:
Michael Goulet 2023-04-18 20:57:16 +00:00
parent b7d8c88b64
commit 0bcfff48a5
2 changed files with 33 additions and 52 deletions

View File

@ -1007,7 +1007,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
param_ty: Ty<'tcx>, param_ty: Ty<'tcx>,
ast_bounds: &[hir::GenericBound<'_>], ast_bounds: &[hir::GenericBound<'_>],
) -> Bounds<'tcx> { ) -> Bounds<'tcx> {
self.compute_bounds_inner(param_ty, ast_bounds) let mut bounds = Bounds::default();
self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty());
debug!(?bounds);
bounds
} }
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type /// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@ -1029,17 +1033,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
} }
self.compute_bounds_inner(param_ty, &result)
}
fn compute_bounds_inner(
&self,
param_ty: Ty<'tcx>,
ast_bounds: &[hir::GenericBound<'_>],
) -> Bounds<'tcx> {
let mut bounds = Bounds::default(); let mut bounds = Bounds::default();
self.add_bounds(param_ty, result.iter(), &mut bounds, ty::List::empty());
self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty());
debug!(?bounds); debug!(?bounds);
bounds bounds

View File

@ -774,32 +774,34 @@ impl<'tcx> ItemCtxt<'tcx> {
only_self_bounds: OnlySelfBounds, only_self_bounds: OnlySelfBounds,
assoc_name: Option<Ident>, assoc_name: Option<Ident>,
) -> Vec<(ty::Predicate<'tcx>, Span)> { ) -> Vec<(ty::Predicate<'tcx>, Span)> {
ast_generics let mut bounds = Bounds::default();
.predicates
.iter()
.filter_map(|wp| match wp {
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
_ => None,
})
.flat_map(|bp| {
let bt = if bp.is_param_bound(param_def_id.to_def_id()) {
Some(ty)
} else if !only_self_bounds.0 {
Some(self.to_ty(bp.bounded_ty))
} else {
None
};
let bvars = self.tcx.late_bound_vars(bp.hir_id);
bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b, bvars))).filter( for predicate in ast_generics.predicates {
|(_, b, _)| match assoc_name { let hir::WherePredicate::BoundPredicate(predicate) = predicate else {
Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name), continue;
None => true, };
},
) let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
}) ty
.flat_map(|(bt, b, bvars)| predicates_from_bound(self, bt, b, bvars)) } else if !only_self_bounds.0 {
.collect() self.to_ty(predicate.bounded_ty)
} else {
continue;
};
let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
self.astconv().add_bounds(
bound_ty,
predicate.bounds.iter().filter(|bound| {
assoc_name
.map_or(true, |assoc_name| self.bound_defines_assoc_item(bound, assoc_name))
}),
&mut bounds,
bound_vars,
);
}
bounds.predicates().collect()
} }
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
@ -817,19 +819,3 @@ impl<'tcx> ItemCtxt<'tcx> {
} }
} }
} }
/// Converts a specific `GenericBound` from the AST into a set of
/// predicates that apply to the self type. A vector is returned
/// because this can be anywhere from zero predicates (`T: ?Sized` adds no
/// predicates) to one (`T: Foo`) to many (`T: Bar<X = i32>` adds `T: Bar`
/// and `<T as Bar>::X == i32`).
fn predicates_from_bound<'tcx>(
astconv: &dyn AstConv<'tcx>,
param_ty: Ty<'tcx>,
bound: &'tcx hir::GenericBound<'tcx>,
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
) -> Vec<(ty::Predicate<'tcx>, Span)> {
let mut bounds = Bounds::default();
astconv.add_bounds(param_ty, [bound].into_iter(), &mut bounds, bound_vars);
bounds.predicates().collect()
}