]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/free_region.rs
89c3f1668474a498906fce1aa87bc4b476c44068
[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 hir::def_id::DefId;
19 use middle::region;
20 use ty::{self, Lift, TyCtxt, Region};
21 use rustc_data_structures::transitive_relation::TransitiveRelation;
22
23 /// Combines a `region::ScopeTree` (which governs relationships between
24 /// scopes) and a `FreeRegionMap` (which governs relationships between
25 /// free regions) to yield a complete relation between concrete
26 /// regions.
27 ///
28 /// This stuff is a bit convoluted and should be refactored, but as we
29 /// move to NLL it'll all go away anyhow.
30 pub struct RegionRelations<'a, 'gcx: 'tcx, 'tcx: 'a> {
31     pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
32
33     /// context used to fetch the region maps
34     pub context: DefId,
35
36     /// region maps for the given context
37     pub region_scope_tree: &'a region::ScopeTree,
38
39     /// free-region relationships
40     pub free_regions: &'a FreeRegionMap<'tcx>,
41 }
42
43 impl<'a, 'gcx, 'tcx> RegionRelations<'a, 'gcx, 'tcx> {
44     pub fn new(
45         tcx: TyCtxt<'a, 'gcx, 'tcx>,
46         context: DefId,
47         region_scope_tree: &'a region::ScopeTree,
48         free_regions: &'a FreeRegionMap<'tcx>,
49     ) -> Self {
50         Self {
51             tcx,
52             context,
53             region_scope_tree,
54             free_regions,
55         }
56     }
57
58     /// Determines whether one region is a subregion of another.  This is intended to run *after
59     /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
60     pub fn is_subregion_of(&self,
61                            sub_region: ty::Region<'tcx>,
62                            super_region: ty::Region<'tcx>)
63                            -> bool {
64         let result = sub_region == super_region || {
65             match (sub_region, super_region) {
66                 (&ty::ReEmpty, _) |
67                 (_, &ty::ReStatic) =>
68                     true,
69
70                 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
71                     self.region_scope_tree.is_subscope_of(sub_scope, super_scope),
72
73                 (&ty::ReScope(sub_scope), &ty::ReEarlyBound(ref br)) => {
74                     let fr_scope = self.region_scope_tree.early_free_scope(self.tcx, br);
75                     self.region_scope_tree.is_subscope_of(sub_scope, fr_scope)
76                 }
77
78                 (&ty::ReScope(sub_scope), &ty::ReFree(ref fr)) => {
79                     let fr_scope = self.region_scope_tree.free_scope(self.tcx, fr);
80                     self.region_scope_tree.is_subscope_of(sub_scope, fr_scope)
81                 }
82
83                 (&ty::ReEarlyBound(_), &ty::ReEarlyBound(_)) |
84                 (&ty::ReFree(_), &ty::ReEarlyBound(_)) |
85                 (&ty::ReEarlyBound(_), &ty::ReFree(_)) |
86                 (&ty::ReFree(_), &ty::ReFree(_)) =>
87                     self.free_regions.sub_free_regions(&sub_region, &super_region),
88
89                 _ =>
90                     false,
91             }
92         };
93         let result = result || self.is_static(super_region);
94         debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
95                sub_region, super_region, result);
96         result
97     }
98
99     /// Determines whether this free-region is required to be 'static
100     fn is_static(&self, super_region: ty::Region<'tcx>) -> bool {
101         debug!("is_static(super_region={:?})", super_region);
102         match *super_region {
103             ty::ReStatic => true,
104             ty::ReEarlyBound(_) | ty::ReFree(_) => {
105                 let re_static = self.tcx.mk_region(ty::ReStatic);
106                 self.free_regions.relation.contains(&re_static, &super_region)
107             }
108             _ => false
109         }
110     }
111
112     pub fn lub_free_regions(&self,
113                             r_a: Region<'tcx>,
114                             r_b: Region<'tcx>)
115                             -> Region<'tcx> {
116         self.free_regions.lub_free_regions(self.tcx, r_a, r_b)
117     }
118 }
119
120 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
121 pub struct FreeRegionMap<'tcx> {
122     // Stores the relation `a < b`, where `a` and `b` are regions.
123     //
124     // Invariant: only free regions like `'x` or `'static` are stored
125     // in this relation, not scopes.
126     relation: TransitiveRelation<Region<'tcx>>
127 }
128
129 impl<'tcx> FreeRegionMap<'tcx> {
130     pub fn new() -> Self {
131         FreeRegionMap { relation: TransitiveRelation::new() }
132     }
133
134     pub fn is_empty(&self) -> bool {
135         self.relation.is_empty()
136     }
137
138     pub fn relate_free_regions_from_predicates(&mut self,
139                                                predicates: &[ty::Predicate<'tcx>]) {
140         debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
141         for predicate in predicates {
142             match *predicate {
143                 ty::Predicate::Projection(..) |
144                 ty::Predicate::Trait(..) |
145                 ty::Predicate::Equate(..) |
146                 ty::Predicate::Subtype(..) |
147                 ty::Predicate::WellFormed(..) |
148                 ty::Predicate::ObjectSafe(..) |
149                 ty::Predicate::ClosureKind(..) |
150                 ty::Predicate::TypeOutlives(..) |
151                 ty::Predicate::ConstEvaluatable(..) => {
152                     // No region bounds here
153                 }
154                 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
155                     self.relate_regions(r_b, r_a);
156                 }
157             }
158         }
159     }
160
161     /// Record that `'sup:'sub`. Or, put another way, `'sub <= 'sup`.
162     /// (with the exception that `'static: 'x` is not notable)
163     pub fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
164         debug!("relate_regions(sub={:?}, sup={:?})", sub, sup);
165         if (is_free(sub) || *sub == ty::ReStatic) && is_free(sup) {
166             self.relation.add(sub, sup)
167         }
168     }
169
170     /// True if `r_a <= r_b` is known to hold. Both `r_a` and `r_b`
171     /// must be free regions from the function header.
172     pub fn sub_free_regions<'a, 'gcx>(&self,
173                                       r_a: Region<'tcx>,
174                                       r_b: Region<'tcx>)
175                                       -> bool {
176         debug!("sub_free_regions(r_a={:?}, r_b={:?})", r_a, r_b);
177         assert!(is_free(r_a));
178         assert!(is_free(r_b));
179         let result = r_a == r_b || self.relation.contains(&r_a, &r_b);
180         debug!("sub_free_regions: result={}", result);
181         result
182     }
183
184     /// Compute the least-upper-bound of two free regions. In some
185     /// cases, this is more conservative than necessary, in order to
186     /// avoid making arbitrary choices. See
187     /// `TransitiveRelation::postdom_upper_bound` for more details.
188     pub fn lub_free_regions<'a, 'gcx>(&self,
189                                       tcx: TyCtxt<'a, 'gcx, 'tcx>,
190                                       r_a: Region<'tcx>,
191                                       r_b: Region<'tcx>)
192                                       -> Region<'tcx> {
193         debug!("lub_free_regions(r_a={:?}, r_b={:?})", r_a, r_b);
194         assert!(is_free(r_a));
195         assert!(is_free(r_b));
196         let result = if r_a == r_b { r_a } else {
197             match self.relation.postdom_upper_bound(&r_a, &r_b) {
198                 None => tcx.mk_region(ty::ReStatic),
199                 Some(r) => *r,
200             }
201         };
202         debug!("lub_free_regions(r_a={:?}, r_b={:?}) = {:?}", r_a, r_b, result);
203         result
204     }
205
206     /// Returns all regions that are known to outlive `r_a`. For
207     /// example, in a function:
208     ///
209     /// ```
210     /// fn foo<'a, 'b: 'a, 'c: 'b>() { .. }
211     /// ```
212     ///
213     /// if `r_a` represents `'a`, this function would return `{'b, 'c}`.
214     pub fn regions_that_outlive<'a, 'gcx>(&self, r_a: Region<'tcx>) -> Vec<&Region<'tcx>> {
215         assert!(is_free(r_a) || *r_a == ty::ReStatic);
216         self.relation.greater_than(&r_a)
217     }
218 }
219
220 fn is_free(r: Region) -> bool {
221     match *r {
222         ty::ReEarlyBound(_) | ty::ReFree(_) => true,
223         _ => false
224     }
225 }
226
227 impl_stable_hash_for!(struct FreeRegionMap<'tcx> {
228     relation
229 });
230
231 impl<'a, 'tcx> Lift<'tcx> for FreeRegionMap<'a> {
232     type Lifted = FreeRegionMap<'tcx>;
233     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<FreeRegionMap<'tcx>> {
234         self.relation.maybe_map(|&fr| fr.lift_to_tcx(tcx))
235                      .map(|relation| FreeRegionMap { relation })
236     }
237 }