]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/bounds.rs
Auto merge of #88934 - tmiasko:trace-log, r=davidtwco
[rust.git] / compiler / rustc_typeck / src / bounds.rs
1 //! Bounds are restrictions applied to some types after they've been converted into the
2 //! `ty` form from the HIR.
3
4 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
5 use rustc_span::Span;
6
7 /// Collects together a list of type bounds. These lists of bounds occur in many places
8 /// in Rust's syntax:
9 ///
10 /// ```text
11 /// trait Foo: Bar + Baz { }
12 ///            ^^^^^^^^^ supertrait list bounding the `Self` type parameter
13 ///
14 /// fn foo<T: Bar + Baz>() { }
15 ///           ^^^^^^^^^ bounding the type parameter `T`
16 ///
17 /// impl dyn Bar + Baz
18 ///          ^^^^^^^^^ bounding the forgotten dynamic type
19 /// ```
20 ///
21 /// Our representation is a bit mixed here -- in some cases, we
22 /// include the self type (e.g., `trait_bounds`) but in others we do not
23 #[derive(Default, PartialEq, Eq, Clone, Debug)]
24 pub struct Bounds<'tcx> {
25     /// A list of region bounds on the (implicit) self type. So if you
26     /// had `T: 'a + 'b` this might would be a list `['a, 'b]` (but
27     /// the `T` is not explicitly included).
28     pub region_bounds: Vec<(ty::Binder<'tcx, ty::Region<'tcx>>, Span)>,
29
30     /// A list of trait bounds. So if you had `T: Debug` this would be
31     /// `T: Debug`. Note that the self-type is explicit here.
32     pub trait_bounds: Vec<(ty::PolyTraitRef<'tcx>, Span, ty::BoundConstness)>,
33
34     /// A list of projection equality bounds. So if you had `T:
35     /// Iterator<Item = u32>` this would include `<T as
36     /// Iterator>::Item => u32`. Note that the self-type is explicit
37     /// here.
38     pub projection_bounds: Vec<(ty::PolyProjectionPredicate<'tcx>, Span)>,
39
40     /// `Some` if there is *no* `?Sized` predicate. The `span`
41     /// is the location in the source of the `T` declaration which can
42     /// be cited as the source of the `T: Sized` requirement.
43     pub implicitly_sized: Option<Span>,
44 }
45
46 impl<'tcx> Bounds<'tcx> {
47     /// Converts a bounds list into a flat set of predicates (like
48     /// where-clauses). Because some of our bounds listings (e.g.,
49     /// regions) don't include the self-type, you must supply the
50     /// self-type here (the `param_ty` parameter).
51     pub fn predicates(
52         &self,
53         tcx: TyCtxt<'tcx>,
54         param_ty: Ty<'tcx>,
55     ) -> Vec<(ty::Predicate<'tcx>, Span)> {
56         // If it could be sized, and is, add the `Sized` predicate.
57         let sized_predicate = self.implicitly_sized.and_then(|span| {
58             tcx.lang_items().sized_trait().map(|sized| {
59                 let trait_ref = ty::Binder::dummy(ty::TraitRef {
60                     def_id: sized,
61                     substs: tcx.mk_substs_trait(param_ty, &[]),
62                 });
63                 (trait_ref.without_const().to_predicate(tcx), span)
64             })
65         });
66
67         sized_predicate
68             .into_iter()
69             .chain(self.region_bounds.iter().map(|&(region_bound, span)| {
70                 (
71                     region_bound
72                         .map_bound(|region_bound| ty::OutlivesPredicate(param_ty, region_bound))
73                         .to_predicate(tcx),
74                     span,
75                 )
76             }))
77             .chain(self.trait_bounds.iter().map(|&(bound_trait_ref, span, constness)| {
78                 let predicate = bound_trait_ref.with_constness(constness).to_predicate(tcx);
79                 (predicate, span)
80             }))
81             .chain(
82                 self.projection_bounds
83                     .iter()
84                     .map(|&(projection, span)| (projection.to_predicate(tcx), span)),
85             )
86             .collect()
87     }
88 }