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 vai 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 middle::ty::{self, FreeRegion, Region};
19 use middle::wf::ImpliedBound;
20 use rustc_data_structures::transitive_relation::TransitiveRelation;
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 relate_free_regions_from_implied_bounds<'tcx>(&mut self,
34 implied_bounds: &[ImpliedBound<'tcx>])
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);
43 ImpliedBound::RegionSubRegion(..) |
44 ImpliedBound::RegionSubParam(..) |
45 ImpliedBound::RegionSubProjection(..) => {
51 pub fn relate_free_regions_from_predicates<'tcx>(&mut self,
53 predicates: &[ty::Predicate<'tcx>]) {
54 debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
55 for predicate in predicates {
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
65 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(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);
74 // All named regions are instantiated with free regions.
76 &format!("record_region_bounds: non free region: {:?} / {:?}",
86 fn relate_to_static(&mut self, sup: FreeRegion) {
87 self.relation.add(ty::ReStatic, ty::ReFree(sup));
90 fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
91 self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
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)
104 debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
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,
117 debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
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,
125 sub_region: ty::Region,
126 super_region: ty::Region)
128 let result = sub_region == super_region || {
129 match (sub_region, super_region) {
134 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) =>
135 tcx.region_maps.is_subscope_of(sub_scope, super_scope),
137 (ty::ReScope(sub_scope), ty::ReFree(fr)) =>
138 tcx.region_maps.is_subscope_of(sub_scope, fr.scope.to_code_extent()) ||
141 (ty::ReFree(sub_fr), ty::ReFree(super_fr)) =>
142 self.sub_free_region(sub_fr, super_fr),
144 (ty::ReStatic, ty::ReFree(sup_fr)) =>
145 self.is_static(sup_fr),
151 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
152 sub_region, super_region, result);
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))
164 fn free_region(index: u32) -> FreeRegion {
165 use middle::region::DestructionScopeData;
166 FreeRegion { scope: DestructionScopeData::new(0),
167 bound_region: ty::BoundRegion::BrAnon(index) }
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]));