1 //! Bounds are restrictions applied to some types after they've been converted into the
2 //! `ty` form from the HIR.
4 use rustc_hir::Constness;
5 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
8 /// Collects together a list of type bounds. These lists of bounds occur in many places
12 /// trait Foo: Bar + Baz { }
13 /// ^^^^^^^^^ supertrait list bounding the `Self` type parameter
15 /// fn foo<T: Bar + Baz>() { }
16 /// ^^^^^^^^^ bounding the type parameter `T`
18 /// impl dyn Bar + Baz
19 /// ^^^^^^^^^ bounding the forgotten dynamic type
22 /// Our representation is a bit mixed here -- in some cases, we
23 /// include the self type (e.g., `trait_bounds`) but in others we do not
24 #[derive(Default, PartialEq, Eq, Clone, Debug)]
25 pub struct Bounds<'tcx> {
26 /// A list of region bounds on the (implicit) self type. So if you
27 /// had `T: 'a + 'b` this might would be a list `['a, 'b]` (but
28 /// the `T` is not explicitly included).
29 pub region_bounds: Vec<(ty::Region<'tcx>, Span)>,
31 /// A list of trait bounds. So if you had `T: Debug` this would be
32 /// `T: Debug`. Note that the self-type is explicit here.
33 pub trait_bounds: Vec<(ty::PolyTraitRef<'tcx>, Span, Constness)>,
35 /// A list of projection equality bounds. So if you had `T:
36 /// Iterator<Item = u32>` this would include `<T as
37 /// Iterator>::Item => u32`. Note that the self-type is explicit
39 pub projection_bounds: Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>,
41 /// `Some` if there is *no* `?Sized` predicate. The `span`
42 /// is the location in the source of the `T` declaration which can
43 /// be cited as the source of the `T: Sized` requirement.
44 pub implicitly_sized: Option<Span>,
47 impl<'tcx> Bounds<'tcx> {
48 /// Converts a bounds list into a flat set of predicates (like
49 /// where-clauses). Because some of our bounds listings (e.g.,
50 /// regions) don't include the self-type, you must supply the
51 /// self-type here (the `param_ty` parameter).
56 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
57 // If it could be sized, and is, add the `Sized` predicate.
58 let sized_predicate = self.implicitly_sized.and_then(|span| {
59 tcx.lang_items().sized_trait().map(|sized| {
60 let trait_ref = ty::Binder::bind(ty::TraitRef {
62 substs: tcx.mk_substs_trait(param_ty, &[]),
64 (trait_ref.without_const().to_predicate(tcx), span)
73 .map(|&(region_bound, span)| {
74 // Account for the binder being introduced below; no need to shift `param_ty`
75 // because, at present at least, it either only refers to early-bound regions,
76 // or it's a generic associated type that deliberately has escaping bound vars.
77 let region_bound = ty::fold::shift_region(tcx, region_bound, 1);
78 let outlives = ty::OutlivesPredicate(param_ty, region_bound);
79 (ty::Binder::bind(outlives).to_predicate(tcx), span)
81 .chain(self.trait_bounds.iter().map(|&(bound_trait_ref, span, constness)| {
82 let predicate = bound_trait_ref.with_constness(constness).to_predicate(tcx);
86 self.projection_bounds
88 .map(|&(projection, span)| (projection.to_predicate(tcx), span)),