]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/free_region.rs
Rollup merge of #41249 - GuillaumeGomez:rustdoc-render, r=steveklabnik,frewsxcv
[rust.git] / src / librustc / middle / free_region.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! This file handles the relationships between free regions --
12 //! meaning lifetime parameters. Ordinarily, free regions are
13 //! unrelated to one another, but they can be related via implied or
14 //! explicit bounds.  In that case, we track the bounds using the
15 //! `TransitiveRelation` type and use that to decide when one free
16 //! region outlives another and so forth.
17
18 use ty::{self, TyCtxt, FreeRegion, Region};
19 use ty::wf::ImpliedBound;
20 use rustc_data_structures::transitive_relation::TransitiveRelation;
21
22 #[derive(Clone, RustcEncodable, RustcDecodable)]
23 pub struct FreeRegionMap {
24     // Stores the relation `a < b`, where `a` and `b` are regions.
25     relation: TransitiveRelation<Region>
26 }
27
28 impl FreeRegionMap {
29     pub fn new() -> FreeRegionMap {
30         FreeRegionMap { relation: TransitiveRelation::new() }
31     }
32
33     pub fn is_empty(&self) -> bool {
34         self.relation.is_empty()
35     }
36
37     pub fn relate_free_regions_from_implied_bounds<'tcx>(&mut self,
38                                                         implied_bounds: &[ImpliedBound<'tcx>])
39     {
40         debug!("relate_free_regions_from_implied_bounds()");
41         for implied_bound in implied_bounds {
42             debug!("implied bound: {:?}", implied_bound);
43             match *implied_bound {
44                 ImpliedBound::RegionSubRegion(&ty::ReFree(free_a), &ty::ReFree(free_b)) => {
45                     self.relate_free_regions(free_a, free_b);
46                 }
47                 ImpliedBound::RegionSubRegion(..) |
48                 ImpliedBound::RegionSubParam(..) |
49                 ImpliedBound::RegionSubProjection(..) => {
50                 }
51             }
52         }
53     }
54
55     pub fn relate_free_regions_from_predicates(&mut self,
56                                                predicates: &[ty::Predicate]) {
57         debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
58         for predicate in predicates {
59             match *predicate {
60                 ty::Predicate::Projection(..) |
61                 ty::Predicate::Trait(..) |
62                 ty::Predicate::Equate(..) |
63                 ty::Predicate::Subtype(..) |
64                 ty::Predicate::WellFormed(..) |
65                 ty::Predicate::ObjectSafe(..) |
66                 ty::Predicate::ClosureKind(..) |
67                 ty::Predicate::TypeOutlives(..) => {
68                     // No region bounds here
69                 }
70                 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
71                     match (r_a, r_b) {
72                         (&ty::ReStatic, &ty::ReFree(_)) => {},
73                         (&ty::ReFree(fr_a), &ty::ReStatic) => self.relate_to_static(fr_a),
74                         (&ty::ReFree(fr_a), &ty::ReFree(fr_b)) => {
75                             // Record that `'a:'b`. Or, put another way, `'b <= 'a`.
76                             self.relate_free_regions(fr_b, fr_a);
77                         }
78                         _ => {
79                             // All named regions are instantiated with free regions.
80                             bug!("record_region_bounds: non free region: {:?} / {:?}",
81                                  r_a,
82                                  r_b);
83                         }
84                     }
85                 }
86             }
87         }
88     }
89
90     fn relate_to_static(&mut self, sup: FreeRegion) {
91         self.relation.add(ty::ReStatic, ty::ReFree(sup));
92     }
93
94     fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
95         self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
96     }
97
98     /// Determines whether two free regions have a subregion relationship
99     /// by walking the graph encoded in `map`.  Note that
100     /// it is possible that `sub != sup` and `sub <= sup` and `sup <= sub`
101     /// (that is, the user can give two different names to the same lifetime).
102     pub fn sub_free_region(&self, sub: FreeRegion, sup: FreeRegion) -> bool {
103         let result = sub == sup || {
104             let sub = ty::ReFree(sub);
105             let sup = ty::ReFree(sup);
106             self.relation.contains(&sub, &sup) || self.relation.contains(&ty::ReStatic, &sup)
107         };
108         debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
109         result
110     }
111
112     pub fn lub_free_regions(&self, fr_a: FreeRegion, fr_b: FreeRegion) -> Region {
113         let r_a = ty::ReFree(fr_a);
114         let r_b = ty::ReFree(fr_b);
115         let result = if fr_a == fr_b { r_a } else {
116             match self.relation.postdom_upper_bound(&r_a, &r_b) {
117                 None => ty::ReStatic,
118                 Some(r) => *r,
119             }
120         };
121         debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
122         result
123     }
124
125     /// Determines whether one region is a subregion of another.  This is intended to run *after
126     /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
127     pub fn is_subregion_of(&self,
128                            tcx: TyCtxt,
129                            sub_region: &ty::Region,
130                            super_region: &ty::Region)
131                            -> bool {
132         let result = sub_region == super_region || {
133             match (sub_region, super_region) {
134                 (&ty::ReEmpty, _) |
135                 (_, &ty::ReStatic) =>
136                     true,
137
138                 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
139                     tcx.region_maps.is_subscope_of(sub_scope, super_scope),
140
141                 (&ty::ReScope(sub_scope), &ty::ReFree(fr)) =>
142                     tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
143                     self.is_static(fr),
144
145                 (&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>
146                     self.sub_free_region(sub_fr, super_fr),
147
148                 (&ty::ReStatic, &ty::ReFree(sup_fr)) =>
149                     self.is_static(sup_fr),
150
151                 _ =>
152                     false,
153             }
154         };
155         debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
156                sub_region, super_region, result);
157         result
158     }
159
160     /// Determines whether this free-region is required to be 'static
161     pub fn is_static(&self, super_region: ty::FreeRegion) -> bool {
162         debug!("is_static(super_region={:?})", super_region);
163         self.relation.contains(&ty::ReStatic, &ty::ReFree(super_region))
164     }
165 }
166
167 #[cfg(test)]
168 fn free_region(index: u32) -> FreeRegion {
169     use middle::region::DUMMY_CODE_EXTENT;
170     FreeRegion { scope: DUMMY_CODE_EXTENT,
171                  bound_region: ty::BoundRegion::BrAnon(index) }
172 }
173
174 #[test]
175 fn lub() {
176     // a very VERY basic test, but see the tests in
177     // TransitiveRelation, which are much more thorough.
178     let frs: Vec<_> = (0..3).map(|i| free_region(i)).collect();
179     let mut map = FreeRegionMap::new();
180     map.relate_free_regions(frs[0], frs[2]);
181     map.relate_free_regions(frs[1], frs[2]);
182     assert_eq!(map.lub_free_regions(frs[0], frs[1]), ty::ReFree(frs[2]));
183 }
184
185 impl_stable_hash_for!(struct FreeRegionMap {
186     relation
187 });