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.
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.
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.
18 use ty::{self, TyCtxt, FreeRegion, Region};
19 use ty::wf::ImpliedBound;
20 use rustc_data_structures::transitive_relation::TransitiveRelation;
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>
29 pub fn new() -> FreeRegionMap {
30 FreeRegionMap { relation: TransitiveRelation::new() }
33 pub fn is_empty(&self) -> bool {
34 self.relation.is_empty()
37 pub fn relate_free_regions_from_implied_bounds<'tcx>(&mut self,
38 implied_bounds: &[ImpliedBound<'tcx>])
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);
47 ImpliedBound::RegionSubRegion(..) |
48 ImpliedBound::RegionSubParam(..) |
49 ImpliedBound::RegionSubProjection(..) => {
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 {
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
70 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(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);
79 // All named regions are instantiated with free regions.
80 bug!("record_region_bounds: non free region: {:?} / {:?}",
90 fn relate_to_static(&mut self, sup: FreeRegion) {
91 self.relation.add(ty::ReStatic, ty::ReFree(sup));
94 fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
95 self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
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)
108 debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
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,
121 debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
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,
129 sub_region: &ty::Region,
130 super_region: &ty::Region)
132 let result = sub_region == super_region || {
133 match (sub_region, super_region) {
135 (_, &ty::ReStatic) =>
138 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
139 tcx.region_maps().is_subscope_of(sub_scope, super_scope),
141 (&ty::ReScope(sub_scope), &ty::ReFree(fr)) => {
142 // 1. It is safe to unwrap `fr.scope` because we
143 // should only ever wind up comparing against
144 // `ReScope` in the context of a method or fn
145 // body, where `fr.scope` should be `Some`.
146 tcx.region_maps().is_subscope_of(sub_scope, fr.scope.unwrap() /*1*/) ||
150 (&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>
151 self.sub_free_region(sub_fr, super_fr),
153 (&ty::ReStatic, &ty::ReFree(sup_fr)) =>
154 self.is_static(sup_fr),
160 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
161 sub_region, super_region, result);
165 /// Determines whether this free-region is required to be 'static
166 pub fn is_static(&self, super_region: ty::FreeRegion) -> bool {
167 debug!("is_static(super_region={:?})", super_region);
168 self.relation.contains(&ty::ReStatic, &ty::ReFree(super_region))
173 fn free_region(index: u32) -> FreeRegion {
174 FreeRegion { scope: None, bound_region: ty::BoundRegion::BrAnon(index) }
179 // a very VERY basic test, but see the tests in
180 // TransitiveRelation, which are much more thorough.
181 let frs: Vec<_> = (0..3).map(|i| free_region(i)).collect();
182 let mut map = FreeRegionMap::new();
183 map.relate_free_regions(frs[0], frs[2]);
184 map.relate_free_regions(frs[1], frs[2]);
185 assert_eq!(map.lub_free_regions(frs[0], frs[1]), ty::ReFree(frs[2]));
188 impl_stable_hash_for!(struct FreeRegionMap {