]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/free_region.rs
introduce per-fn RegionMaps
[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::RegionMaps;
20 use ty::{self, Lift, TyCtxt, Region};
21 use ty::wf::ImpliedBound;
22 use rustc_data_structures::transitive_relation::TransitiveRelation;
23
24 /// Combines a `RegionMaps` (which governs relationships between
25 /// scopes) and a `FreeRegionMap` (which governs relationships between
26 /// free regions) to yield a complete relation between concrete
27 /// regions.
28 ///
29 /// This stuff is a bit convoluted and should be refactored, but as we
30 /// move to NLL it'll all go away anyhow.
31 pub struct RegionRelations<'a, 'gcx: 'tcx, 'tcx: 'a> {
32     pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
33
34     /// context used to fetch the region maps
35     pub context: DefId,
36
37     /// region maps for the given context
38     pub region_maps: &'a RegionMaps<'tcx>,
39
40     /// free-region relationships
41     pub free_regions: &'a FreeRegionMap<'tcx>,
42 }
43
44 impl<'a, 'gcx, 'tcx> RegionRelations<'a, 'gcx, 'tcx> {
45     pub fn new(
46         tcx: TyCtxt<'a, 'gcx, 'tcx>,
47         context: DefId,
48         region_maps: &'a RegionMaps<'tcx>,
49         free_regions: &'a FreeRegionMap<'tcx>,
50     ) -> Self {
51         Self {
52             tcx,
53             context,
54             region_maps,
55             free_regions,
56         }
57     }
58
59     /// Determines whether one region is a subregion of another.  This is intended to run *after
60     /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
61     pub fn is_subregion_of(&self,
62                            sub_region: ty::Region<'tcx>,
63                            super_region: ty::Region<'tcx>)
64                            -> bool {
65         let result = sub_region == super_region || {
66             match (sub_region, super_region) {
67                 (&ty::ReEmpty, _) |
68                 (_, &ty::ReStatic) =>
69                     true,
70
71                 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
72                     self.region_maps.is_subscope_of(sub_scope, super_scope),
73
74                 (&ty::ReScope(sub_scope), &ty::ReFree(fr)) => {
75                     // 1. It is safe to unwrap `fr.scope` because we
76                     // should only ever wind up comparing against
77                     // `ReScope` in the context of a method or
78                     // body, where `fr.scope` should be `Some`.
79                     self.region_maps.is_subscope_of(sub_scope, fr.scope.unwrap() /*1*/) ||
80                         self.is_static(super_region)
81                 }
82
83                 (&ty::ReFree(_), &ty::ReFree(_)) =>
84                     self.free_regions.relation.contains(&sub_region, &super_region) ||
85                         self.is_static(super_region),
86
87                 (&ty::ReStatic, &ty::ReFree(_)) =>
88                     self.is_static(super_region),
89
90                 _ =>
91                     false,
92             }
93         };
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::ReFree(_) => {
105                 let re_static = self.tcx.mk_region(ty::ReStatic);
106                 self.free_regions.relation.contains(&re_static, &super_region)
107             }
108             _ => bug!("only free regions should be given to `is_static`")
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)]
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_implied_bounds(&mut self,
139                                                    implied_bounds: &[ImpliedBound<'tcx>])
140     {
141         debug!("relate_free_regions_from_implied_bounds()");
142         for implied_bound in implied_bounds {
143             debug!("implied bound: {:?}", implied_bound);
144             match *implied_bound {
145                 ImpliedBound::RegionSubRegion(a @ &ty::ReFree(_), b @ &ty::ReFree(_)) |
146                 ImpliedBound::RegionSubRegion(a @ &ty::ReStatic, b @ &ty::ReFree(_)) => {
147                     self.relate_regions(a, b);
148                 }
149                 ImpliedBound::RegionSubRegion(..) |
150                 ImpliedBound::RegionSubParam(..) |
151                 ImpliedBound::RegionSubProjection(..) => {
152                 }
153             }
154         }
155     }
156
157     pub fn relate_free_regions_from_predicates(&mut self,
158                                                predicates: &[ty::Predicate<'tcx>]) {
159         debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
160         for predicate in predicates {
161             match *predicate {
162                 ty::Predicate::Projection(..) |
163                 ty::Predicate::Trait(..) |
164                 ty::Predicate::Equate(..) |
165                 ty::Predicate::Subtype(..) |
166                 ty::Predicate::WellFormed(..) |
167                 ty::Predicate::ObjectSafe(..) |
168                 ty::Predicate::ClosureKind(..) |
169                 ty::Predicate::TypeOutlives(..) => {
170                     // No region bounds here
171                 }
172                 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
173                     match (r_a, r_b) {
174                         // `'static: 'x` is not notable
175                         (&ty::ReStatic, &ty::ReFree(_)) => {},
176
177                         (&ty::ReFree(_), &ty::ReStatic) |
178                         (&ty::ReFree(_), &ty::ReFree(_)) => {
179                             // Record that `'a:'b`. Or, put another way, `'b <= 'a`.
180                             self.relate_regions(r_b, r_a);
181                         }
182
183                         _ => {
184                             // All named regions are instantiated with free regions.
185                             bug!("record_region_bounds: non free region: {:?} / {:?}",
186                                  r_a,
187                                  r_b);
188                         }
189                     }
190                 }
191             }
192         }
193     }
194
195     fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
196         assert!(match *sub { ty::ReFree(_) | ty::ReStatic => true, _ => false });
197         assert!(match *sup { ty::ReFree(_) | ty::ReStatic => true, _ => false });
198         self.relation.add(sub, sup)
199     }
200
201     pub fn lub_free_regions<'a, 'gcx>(&self,
202                                       tcx: TyCtxt<'a, 'gcx, 'tcx>,
203                                       r_a: Region<'tcx>,
204                                       r_b: Region<'tcx>)
205                                       -> Region<'tcx> {
206         assert!(match *r_a { ty::ReFree(_) => true, _ => false });
207         assert!(match *r_b { ty::ReFree(_) => true, _ => false });
208         let result = if r_a == r_b { r_a } else {
209             match self.relation.postdom_upper_bound(&r_a, &r_b) {
210                 None => tcx.mk_region(ty::ReStatic),
211                 Some(r) => *r,
212             }
213         };
214         debug!("lub_free_regions(r_a={:?}, r_b={:?}) = {:?}", r_a, r_b, result);
215         result
216     }
217 }
218
219 impl_stable_hash_for!(struct FreeRegionMap<'tcx> {
220     relation
221 });
222
223 impl<'a, 'tcx> Lift<'tcx> for FreeRegionMap<'a> {
224     type Lifted = FreeRegionMap<'tcx>;
225     fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<FreeRegionMap<'tcx>> {
226         self.relation.maybe_map(|&fr| fr.lift_to_tcx(tcx))
227                      .map(|relation| FreeRegionMap { relation })
228     }
229 }