]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/free_region.rs
nits from pnkfelix
[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 defines
12
13 use middle::ty::{self, FreeRegion, Region};
14 use middle::wf::ImpliedBound;
15 use rustc_data_structures::transitive_relation::TransitiveRelation;
16
17 #[derive(Clone)]
18 pub struct FreeRegionMap {
19     // Stores the relation `a < b`, where `a` and `b` are regions.
20     relation: TransitiveRelation<Region>
21 }
22
23 impl FreeRegionMap {
24     pub fn new() -> FreeRegionMap {
25         FreeRegionMap { relation: TransitiveRelation::new() }
26     }
27
28     pub fn relate_free_regions_from_implied_bounds<'tcx>(&mut self,
29                                                         implied_bounds: &[ImpliedBound<'tcx>])
30     {
31         debug!("relate_free_regions_from_implied_bounds()");
32         for implied_bound in implied_bounds {
33             debug!("implied bound: {:?}", implied_bound);
34             match *implied_bound {
35                 ImpliedBound::RegionSubRegion(ty::ReFree(free_a), ty::ReFree(free_b)) => {
36                     self.relate_free_regions(free_a, free_b);
37                 }
38                 ImpliedBound::RegionSubRegion(..) |
39                 ImpliedBound::RegionSubParam(..) |
40                 ImpliedBound::RegionSubProjection(..) => {
41                 }
42             }
43         }
44     }
45
46     pub fn relate_free_regions_from_predicates<'tcx>(&mut self,
47                                                      tcx: &ty::ctxt<'tcx>,
48                                                      predicates: &[ty::Predicate<'tcx>]) {
49         debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
50         for predicate in predicates {
51             match *predicate {
52                 ty::Predicate::Projection(..) |
53                 ty::Predicate::Trait(..) |
54                 ty::Predicate::Equate(..) |
55                 ty::Predicate::WellFormed(..) |
56                 ty::Predicate::ObjectSafe(..) |
57                 ty::Predicate::TypeOutlives(..) => {
58                     // No region bounds here
59                 }
60                 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
61                     match (r_a, r_b) {
62                         (ty::ReStatic, ty::ReFree(_)) => {},
63                         (ty::ReFree(fr_a), ty::ReStatic) => self.relate_to_static(fr_a),
64                         (ty::ReFree(fr_a), ty::ReFree(fr_b)) => {
65                             // Record that `'a:'b`. Or, put another way, `'b <= 'a`.
66                             self.relate_free_regions(fr_b, fr_a);
67                         }
68                         _ => {
69                             // All named regions are instantiated with free regions.
70                             tcx.sess.bug(
71                                 &format!("record_region_bounds: non free region: {:?} / {:?}",
72                                          r_a,
73                                          r_b));
74                         }
75                     }
76                 }
77             }
78         }
79     }
80
81     fn relate_to_static(&mut self, sup: FreeRegion) {
82         self.relation.add(ty::ReStatic, ty::ReFree(sup));
83     }
84
85     fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
86         self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
87     }
88
89     /// Determines whether two free regions have a subregion relationship
90     /// by walking the graph encoded in `map`.  Note that
91     /// it is possible that `sub != sup` and `sub <= sup` and `sup <= sub`
92     /// (that is, the user can give two different names to the same lifetime).
93     pub fn sub_free_region(&self, sub: FreeRegion, sup: FreeRegion) -> bool {
94         let result = sub == sup || {
95             let sub = ty::ReFree(sub);
96             let sup = ty::ReFree(sup);
97             self.relation.contains(&sub, &sup) || self.relation.contains(&ty::ReStatic, &sup)
98         };
99         debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
100         result
101     }
102
103     pub fn lub_free_regions(&self, fr_a: FreeRegion, fr_b: FreeRegion) -> Region {
104         let r_a = ty::ReFree(fr_a);
105         let r_b = ty::ReFree(fr_b);
106         let result = if fr_a == fr_b { r_a } else {
107             match self.relation.best_upper_bound(&r_a, &r_b) {
108                 None => ty::ReStatic,
109                 Some(r) => *r,
110             }
111         };
112         debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
113         result
114     }
115
116     /// Determines whether one region is a subregion of another.  This is intended to run *after
117     /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
118     pub fn is_subregion_of(&self,
119                            tcx: &ty::ctxt,
120                            sub_region: ty::Region,
121                            super_region: ty::Region)
122                            -> bool {
123         let result = sub_region == super_region || {
124             match (sub_region, super_region) {
125                 (ty::ReEmpty, _) |
126                 (_, ty::ReStatic) =>
127                     true,
128
129                 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) =>
130                     tcx.region_maps.is_subscope_of(sub_scope, super_scope),
131
132                 (ty::ReScope(sub_scope), ty::ReFree(fr)) =>
133                     tcx.region_maps.is_subscope_of(sub_scope, fr.scope.to_code_extent()) ||
134                     self.is_static(fr),
135
136                 (ty::ReFree(sub_fr), ty::ReFree(super_fr)) =>
137                     self.sub_free_region(sub_fr, super_fr),
138
139                 (ty::ReStatic, ty::ReFree(sup_fr)) =>
140                     self.is_static(sup_fr),
141
142                 _ =>
143                     false,
144             }
145         };
146         debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
147                sub_region, super_region, result);
148         result
149     }
150
151     /// Determines whether this free-region is required to be 'static
152     pub fn is_static(&self, super_region: ty::FreeRegion) -> bool {
153         debug!("is_static(super_region={:?})", super_region);
154         self.relation.contains(&ty::ReStatic, &ty::ReFree(super_region))
155     }
156 }
157
158 #[cfg(test)]
159 fn free_region(index: u32) -> FreeRegion {
160     use middle::region::DestructionScopeData;
161     FreeRegion { scope: DestructionScopeData::new(0),
162                  bound_region: ty::BoundRegion::BrAnon(index) }
163 }
164
165 #[test]
166 fn lub() {
167     // a very VERY basic test, but see the tests in
168     // TransitiveRelation, which are much more thorough.
169     let frs: Vec<_> = (0..3).map(|i| free_region(i)).collect();
170     let mut map = FreeRegionMap::new();
171     map.relate_free_regions(frs[0], frs[2]);
172     map.relate_free_regions(frs[1], frs[2]);
173     assert_eq!(map.lub_free_regions(frs[0], frs[1]), ty::ReFree(frs[2]));
174 }
175