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;
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(&mut self,
52 predicates: &[ty::Predicate]) {
53 debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
54 for predicate in predicates {
56 ty::Predicate::Projection(..) |
57 ty::Predicate::Trait(..) |
58 ty::Predicate::Equate(..) |
59 ty::Predicate::WellFormed(..) |
60 ty::Predicate::ObjectSafe(..) |
61 ty::Predicate::ClosureKind(..) |
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.
75 bug!("record_region_bounds: non free region: {:?} / {:?}",
85 fn relate_to_static(&mut self, sup: FreeRegion) {
86 self.relation.add(ty::ReStatic, ty::ReFree(sup));
89 fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
90 self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
93 /// Determines whether two free regions have a subregion relationship
94 /// by walking the graph encoded in `map`. Note that
95 /// it is possible that `sub != sup` and `sub <= sup` and `sup <= sub`
96 /// (that is, the user can give two different names to the same lifetime).
97 pub fn sub_free_region(&self, sub: FreeRegion, sup: FreeRegion) -> bool {
98 let result = sub == sup || {
99 let sub = ty::ReFree(sub);
100 let sup = ty::ReFree(sup);
101 self.relation.contains(&sub, &sup) || self.relation.contains(&ty::ReStatic, &sup)
103 debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
107 pub fn lub_free_regions(&self, fr_a: FreeRegion, fr_b: FreeRegion) -> Region {
108 let r_a = ty::ReFree(fr_a);
109 let r_b = ty::ReFree(fr_b);
110 let result = if fr_a == fr_b { r_a } else {
111 match self.relation.postdom_upper_bound(&r_a, &r_b) {
112 None => ty::ReStatic,
116 debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
120 /// Determines whether one region is a subregion of another. This is intended to run *after
121 /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
122 pub fn is_subregion_of(&self,
124 sub_region: &ty::Region,
125 super_region: &ty::Region)
127 let result = sub_region == super_region || {
128 match (sub_region, super_region) {
130 (_, &ty::ReStatic) =>
133 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
134 tcx.region_maps.is_subscope_of(sub_scope, super_scope),
136 (&ty::ReScope(sub_scope), &ty::ReFree(fr)) =>
137 tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
140 (&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>
141 self.sub_free_region(sub_fr, super_fr),
143 (&ty::ReStatic, &ty::ReFree(sup_fr)) =>
144 self.is_static(sup_fr),
150 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
151 sub_region, super_region, result);
155 /// Determines whether this free-region is required to be 'static
156 pub fn is_static(&self, super_region: ty::FreeRegion) -> bool {
157 debug!("is_static(super_region={:?})", super_region);
158 self.relation.contains(&ty::ReStatic, &ty::ReFree(super_region))
163 fn free_region(index: u32) -> FreeRegion {
164 use middle::region::DUMMY_CODE_EXTENT;
165 FreeRegion { scope: DUMMY_CODE_EXTENT,
166 bound_region: ty::BoundRegion::BrAnon(index) }
171 // a very VERY basic test, but see the tests in
172 // TransitiveRelation, which are much more thorough.
173 let frs: Vec<_> = (0..3).map(|i| free_region(i)).collect();
174 let mut map = FreeRegionMap::new();
175 map.relate_free_regions(frs[0], frs[2]);
176 map.relate_free_regions(frs[1], frs[2]);
177 assert_eq!(map.lub_free_regions(frs[0], frs[1]), ty::ReFree(frs[2]));