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::WellFormed(..) |
64 ty::Predicate::ObjectSafe(..) |
65 ty::Predicate::ClosureKind(..) |
66 ty::Predicate::TypeOutlives(..) => {
67 // No region bounds here
69 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
71 (&ty::ReStatic, &ty::ReFree(_)) => {},
72 (&ty::ReFree(fr_a), &ty::ReStatic) => self.relate_to_static(fr_a),
73 (&ty::ReFree(fr_a), &ty::ReFree(fr_b)) => {
74 // Record that `'a:'b`. Or, put another way, `'b <= 'a`.
75 self.relate_free_regions(fr_b, fr_a);
78 // All named regions are instantiated with free regions.
79 bug!("record_region_bounds: non free region: {:?} / {:?}",
89 fn relate_to_static(&mut self, sup: FreeRegion) {
90 self.relation.add(ty::ReStatic, ty::ReFree(sup));
93 fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
94 self.relation.add(ty::ReFree(sub), ty::ReFree(sup))
97 /// Determines whether two free regions have a subregion relationship
98 /// by walking the graph encoded in `map`. Note that
99 /// it is possible that `sub != sup` and `sub <= sup` and `sup <= sub`
100 /// (that is, the user can give two different names to the same lifetime).
101 pub fn sub_free_region(&self, sub: FreeRegion, sup: FreeRegion) -> bool {
102 let result = sub == sup || {
103 let sub = ty::ReFree(sub);
104 let sup = ty::ReFree(sup);
105 self.relation.contains(&sub, &sup) || self.relation.contains(&ty::ReStatic, &sup)
107 debug!("sub_free_region(sub={:?}, sup={:?}) = {:?}", sub, sup, result);
111 pub fn lub_free_regions(&self, fr_a: FreeRegion, fr_b: FreeRegion) -> Region {
112 let r_a = ty::ReFree(fr_a);
113 let r_b = ty::ReFree(fr_b);
114 let result = if fr_a == fr_b { r_a } else {
115 match self.relation.postdom_upper_bound(&r_a, &r_b) {
116 None => ty::ReStatic,
120 debug!("lub_free_regions(fr_a={:?}, fr_b={:?}) = {:?}", fr_a, fr_b, result);
124 /// Determines whether one region is a subregion of another. This is intended to run *after
125 /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
126 pub fn is_subregion_of(&self,
128 sub_region: &ty::Region,
129 super_region: &ty::Region)
131 let result = sub_region == super_region || {
132 match (sub_region, super_region) {
134 (_, &ty::ReStatic) =>
137 (&ty::ReScope(sub_scope), &ty::ReScope(super_scope)) =>
138 tcx.region_maps.is_subscope_of(sub_scope, super_scope),
140 (&ty::ReScope(sub_scope), &ty::ReFree(fr)) =>
141 tcx.region_maps.is_subscope_of(sub_scope, fr.scope) ||
144 (&ty::ReFree(sub_fr), &ty::ReFree(super_fr)) =>
145 self.sub_free_region(sub_fr, super_fr),
147 (&ty::ReStatic, &ty::ReFree(sup_fr)) =>
148 self.is_static(sup_fr),
154 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
155 sub_region, super_region, result);
159 /// Determines whether this free-region is required to be 'static
160 pub fn is_static(&self, super_region: ty::FreeRegion) -> bool {
161 debug!("is_static(super_region={:?})", super_region);
162 self.relation.contains(&ty::ReStatic, &ty::ReFree(super_region))
167 fn free_region(index: u32) -> FreeRegion {
168 use middle::region::DUMMY_CODE_EXTENT;
169 FreeRegion { scope: DUMMY_CODE_EXTENT,
170 bound_region: ty::BoundRegion::BrAnon(index) }
175 // a very VERY basic test, but see the tests in
176 // TransitiveRelation, which are much more thorough.
177 let frs: Vec<_> = (0..3).map(|i| free_region(i)).collect();
178 let mut map = FreeRegionMap::new();
179 map.relate_free_regions(frs[0], frs[2]);
180 map.relate_free_regions(frs[1], frs[2]);
181 assert_eq!(map.lub_free_regions(frs[0], frs[1]), ty::ReFree(frs[2]));