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 infer::outlives::free_region_map::{FreeRegionMap, FreeRegionRelations};
19 use hir::def_id::DefId;
21 use ty::{self, TyCtxt, Region};
23 /// Combines a `region::ScopeTree` (which governs relationships between
24 /// scopes) and a `FreeRegionMap` (which governs relationships between
25 /// free regions) to yield a complete relation between concrete
28 /// This stuff is a bit convoluted and should be refactored, but as we
29 /// move to NLL it'll all go away anyhow.
30 pub struct RegionRelations<'a, 'gcx: 'tcx, 'tcx: 'a> {
31 pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
33 /// context used to fetch the region maps
36 /// region maps for the given context
37 pub region_scope_tree: &'a region::ScopeTree,
39 /// free-region relationships
40 pub free_regions: &'a FreeRegionMap<'tcx>,
43 impl<'a, 'gcx, 'tcx> RegionRelations<'a, 'gcx, 'tcx> {
45 tcx: TyCtxt<'a, 'gcx, 'tcx>,
47 region_scope_tree: &'a region::ScopeTree,
48 free_regions: &'a FreeRegionMap<'tcx>,
58 /// Determines whether one region is a subregion of another. This is intended to run *after
59 /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
60 pub fn is_subregion_of(&self,
61 sub_region: ty::Region<'tcx>,
62 super_region: ty::Region<'tcx>)
64 let result = sub_region == super_region || {
65 match (sub_region, super_region) {
70 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) =>
71 self.region_scope_tree.is_subscope_of(*sub_scope, *super_scope),
73 (ty::ReScope(sub_scope), ty::ReEarlyBound(ref br)) => {
74 let fr_scope = self.region_scope_tree.early_free_scope(self.tcx, br);
75 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
78 (ty::ReScope(sub_scope), ty::ReFree(fr)) => {
79 let fr_scope = self.region_scope_tree.free_scope(self.tcx, fr);
80 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
83 (ty::ReEarlyBound(_), ty::ReEarlyBound(_)) |
84 (ty::ReFree(_), ty::ReEarlyBound(_)) |
85 (ty::ReEarlyBound(_), ty::ReFree(_)) |
86 (ty::ReFree(_), ty::ReFree(_)) =>
87 self.free_regions.sub_free_regions(sub_region, super_region),
93 let result = result || self.is_static(super_region);
94 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
95 sub_region, super_region, result);
99 /// Determines whether this free-region is required to be 'static
100 fn is_static(&self, super_region: ty::Region<'tcx>) -> bool {
101 debug!("is_static(super_region={:?})", super_region);
102 match *super_region {
103 ty::ReStatic => true,
104 ty::ReEarlyBound(_) | ty::ReFree(_) => {
105 let re_static = self.tcx.mk_region(ty::ReStatic);
106 self.free_regions.sub_free_regions(&re_static, &super_region)
112 pub fn lub_free_regions(&self,
116 self.free_regions.lub_free_regions(self.tcx, r_a, r_b)