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