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, Lift, TyCtxt, Region};
19 use ty::wf::ImpliedBound;
20 use rustc_data_structures::transitive_relation::TransitiveRelation;
22 #[derive(Clone, RustcEncodable, RustcDecodable)]
23 pub struct FreeRegionMap<'tcx> {
24 // Stores the relation `a < b`, where `a` and `b` are regions.
26 // Invariant: only free regions like `'x` or `'static` are stored
27 // in this relation, not scopes.
28 relation: TransitiveRelation<Region<'tcx>>
31 impl<'tcx> FreeRegionMap<'tcx> {
32 pub fn new() -> Self {
33 FreeRegionMap { relation: TransitiveRelation::new() }
36 pub fn is_empty(&self) -> bool {
37 self.relation.is_empty()
40 pub fn relate_free_regions_from_implied_bounds(&mut self,
41 implied_bounds: &[ImpliedBound<'tcx>])
43 debug!("relate_free_regions_from_implied_bounds()");
44 for implied_bound in implied_bounds {
45 debug!("implied bound: {:?}", implied_bound);
46 match *implied_bound {
47 ImpliedBound::RegionSubRegion(a @ &ty::ReFree(_), b @ &ty::ReFree(_)) |
48 ImpliedBound::RegionSubRegion(a @ &ty::ReStatic, b @ &ty::ReFree(_)) => {
49 self.relate_regions(a, b);
51 ImpliedBound::RegionSubRegion(..) |
52 ImpliedBound::RegionSubParam(..) |
53 ImpliedBound::RegionSubProjection(..) => {
59 pub fn relate_free_regions_from_predicates(&mut self,
60 predicates: &[ty::Predicate<'tcx>]) {
61 debug!("relate_free_regions_from_predicates(predicates={:?})", predicates);
62 for predicate in predicates {
64 ty::Predicate::Projection(..) |
65 ty::Predicate::Trait(..) |
66 ty::Predicate::Equate(..) |
67 ty::Predicate::Subtype(..) |
68 ty::Predicate::WellFormed(..) |
69 ty::Predicate::ObjectSafe(..) |
70 ty::Predicate::ClosureKind(..) |
71 ty::Predicate::TypeOutlives(..) => {
72 // No region bounds here
74 ty::Predicate::RegionOutlives(ty::Binder(ty::OutlivesPredicate(r_a, r_b))) => {
76 // `'static: 'x` is not notable
77 (&ty::ReStatic, &ty::ReFree(_)) => {},
79 (&ty::ReFree(_), &ty::ReStatic) |
80 (&ty::ReFree(_), &ty::ReFree(_)) => {
81 // Record that `'a:'b`. Or, put another way, `'b <= 'a`.
82 self.relate_regions(r_b, r_a);
86 // All named regions are instantiated with free regions.
87 bug!("record_region_bounds: non free region: {:?} / {:?}",
97 fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
98 assert!(match *sub { ty::ReFree(_) | ty::ReStatic => true, _ => false });
99 assert!(match *sup { ty::ReFree(_) | ty::ReStatic => true, _ => false });
100 self.relation.add(sub, sup)
103 pub fn lub_free_regions<'a, 'gcx>(&self,
104 tcx: TyCtxt<'a, 'gcx, 'tcx>,
108 assert!(match *r_a { ty::ReFree(_) => true, _ => false });
109 assert!(match *r_b { ty::ReFree(_) => true, _ => false });
110 let result = if r_a == r_b { r_a } else {
111 match self.relation.postdom_upper_bound(&r_a, &r_b) {
112 None => tcx.mk_region(ty::ReStatic),
116 debug!("lub_free_regions(r_a={:?}, r_b={:?}) = {:?}", r_a, r_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<'a, 'gcx>(&self,
123 tcx: TyCtxt<'a, 'gcx, 'tcx>,
124 sub_region: ty::Region<'tcx>,
125 super_region: ty::Region<'tcx>)
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 // 1. It is safe to unwrap `fr.scope` because we
138 // should only ever wind up comparing against
139 // `ReScope` in the context of a method or
140 // body, where `fr.scope` should be `Some`.
141 tcx.region_maps().is_subscope_of(sub_scope, fr.scope.unwrap() /*1*/) ||
142 self.is_static(tcx, super_region)
145 (&ty::ReFree(_), &ty::ReFree(_)) =>
146 self.relation.contains(&sub_region, &super_region) ||
147 self.is_static(tcx, super_region),
149 (&ty::ReStatic, &ty::ReFree(_)) =>
150 self.is_static(tcx, super_region),
156 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
157 sub_region, super_region, result);
161 /// Determines whether this free-region is required to be 'static
162 fn is_static<'a, 'gcx>(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, super_region: ty::Region<'tcx>)
164 debug!("is_static(super_region={:?})", super_region);
165 match *super_region {
166 ty::ReStatic => true,
168 let re_static = tcx.mk_region(ty::ReStatic);
169 self.relation.contains(&re_static, &super_region)
171 _ => bug!("only free regions should be given to `is_static`")
176 impl_stable_hash_for!(struct FreeRegionMap<'tcx> {
180 impl<'a, 'tcx> Lift<'tcx> for FreeRegionMap<'a> {
181 type Lifted = FreeRegionMap<'tcx>;
182 fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<FreeRegionMap<'tcx>> {
183 self.relation.maybe_map(|&fr| fr.lift_to_tcx(tcx))
184 .map(|relation| FreeRegionMap { relation })