Skip to content

Commit 0bcfff4

Browse files
Simplify type_parameter_bounds_in_generics
1 parent b7d8c88 commit 0bcfff4

File tree

2 files changed

+34
-53
lines changed

2 files changed

+34
-53
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10071007
param_ty: Ty<'tcx>,
10081008
ast_bounds: &[hir::GenericBound<'_>],
10091009
) -> Bounds<'tcx> {
1010-
self.compute_bounds_inner(param_ty, ast_bounds)
1010+
let mut bounds = Bounds::default();
1011+
self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty());
1012+
debug!(?bounds);
1013+
1014+
bounds
10111015
}
10121016

10131017
/// 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 {
10291033
}
10301034
}
10311035

1032-
self.compute_bounds_inner(param_ty, &result)
1033-
}
1034-
1035-
fn compute_bounds_inner(
1036-
&self,
1037-
param_ty: Ty<'tcx>,
1038-
ast_bounds: &[hir::GenericBound<'_>],
1039-
) -> Bounds<'tcx> {
10401036
let mut bounds = Bounds::default();
1041-
1042-
self.add_bounds(param_ty, ast_bounds.iter(), &mut bounds, ty::List::empty());
1037+
self.add_bounds(param_ty, result.iter(), &mut bounds, ty::List::empty());
10431038
debug!(?bounds);
10441039

10451040
bounds

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+28-42
Original file line numberDiff line numberDiff line change
@@ -774,32 +774,34 @@ impl<'tcx> ItemCtxt<'tcx> {
774774
only_self_bounds: OnlySelfBounds,
775775
assoc_name: Option<Ident>,
776776
) -> Vec<(ty::Predicate<'tcx>, Span)> {
777-
ast_generics
778-
.predicates
779-
.iter()
780-
.filter_map(|wp| match wp {
781-
hir::WherePredicate::BoundPredicate(bp) => Some(bp),
782-
_ => None,
783-
})
784-
.flat_map(|bp| {
785-
let bt = if bp.is_param_bound(param_def_id.to_def_id()) {
786-
Some(ty)
787-
} else if !only_self_bounds.0 {
788-
Some(self.to_ty(bp.bounded_ty))
789-
} else {
790-
None
791-
};
792-
let bvars = self.tcx.late_bound_vars(bp.hir_id);
793-
794-
bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b, bvars))).filter(
795-
|(_, b, _)| match assoc_name {
796-
Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name),
797-
None => true,
798-
},
799-
)
800-
})
801-
.flat_map(|(bt, b, bvars)| predicates_from_bound(self, bt, b, bvars))
802-
.collect()
777+
let mut bounds = Bounds::default();
778+
779+
for predicate in ast_generics.predicates {
780+
let hir::WherePredicate::BoundPredicate(predicate) = predicate else {
781+
continue;
782+
};
783+
784+
let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
785+
ty
786+
} else if !only_self_bounds.0 {
787+
self.to_ty(predicate.bounded_ty)
788+
} else {
789+
continue;
790+
};
791+
792+
let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
793+
self.astconv().add_bounds(
794+
bound_ty,
795+
predicate.bounds.iter().filter(|bound| {
796+
assoc_name
797+
.map_or(true, |assoc_name| self.bound_defines_assoc_item(bound, assoc_name))
798+
}),
799+
&mut bounds,
800+
bound_vars,
801+
);
802+
}
803+
804+
bounds.predicates().collect()
803805
}
804806

805807
#[instrument(level = "trace", skip(self))]
@@ -817,19 +819,3 @@ impl<'tcx> ItemCtxt<'tcx> {
817819
}
818820
}
819821
}
820-
821-
/// Converts a specific `GenericBound` from the AST into a set of
822-
/// predicates that apply to the self type. A vector is returned
823-
/// because this can be anywhere from zero predicates (`T: ?Sized` adds no
824-
/// predicates) to one (`T: Foo`) to many (`T: Bar<X = i32>` adds `T: Bar`
825-
/// and `<T as Bar>::X == i32`).
826-
fn predicates_from_bound<'tcx>(
827-
astconv: &dyn AstConv<'tcx>,
828-
param_ty: Ty<'tcx>,
829-
bound: &'tcx hir::GenericBound<'tcx>,
830-
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
831-
) -> Vec<(ty::Predicate<'tcx>, Span)> {
832-
let mut bounds = Bounds::default();
833-
astconv.add_bounds(param_ty, [bound].into_iter(), &mut bounds, bound_vars);
834-
bounds.predicates().collect()
835-
}

0 commit comments

Comments
 (0)