1 //! This module handles the relationships between "free regions", i.e., lifetime parameters.
2 //! Ordinarily, free regions are unrelated to one another, but they can be related via implied
3 //! or explicit bounds. In that case, we track the bounds using the `TransitiveRelation` type,
4 //! and use that to decide when one free region outlives another, and so forth.
6 use crate::middle::region;
7 use crate::ty::free_region_map::{FreeRegionMap, FreeRegionRelations};
8 use crate::ty::{self, Region, TyCtxt};
9 use rustc_hir::def_id::DefId;
11 /// Combines a `region::ScopeTree` (which governs relationships between
12 /// scopes) and a `FreeRegionMap` (which governs relationships between
13 /// free regions) to yield a complete relation between concrete
16 /// This stuff is a bit convoluted and should be refactored, but as we
17 /// transition to NLL, it'll all go away anyhow.
18 pub struct RegionRelations<'a, 'tcx> {
19 pub tcx: TyCtxt<'tcx>,
21 /// The context used to fetch the region maps.
24 /// The region maps for the given context.
25 pub region_scope_tree: &'a region::ScopeTree,
27 /// Free-region relationships.
28 pub free_regions: &'a FreeRegionMap<'tcx>,
31 impl<'a, 'tcx> RegionRelations<'a, 'tcx> {
35 region_scope_tree: &'a region::ScopeTree,
36 free_regions: &'a FreeRegionMap<'tcx>,
38 Self { tcx, context, region_scope_tree, free_regions }
41 /// Determines whether one region is a subregion of another. This is intended to run *after
42 /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
43 pub fn is_subregion_of(
45 sub_region: ty::Region<'tcx>,
46 super_region: ty::Region<'tcx>,
48 let result = sub_region == super_region || {
49 match (sub_region, super_region) {
50 (ty::ReEmpty, _) | (_, ty::ReStatic) => true,
52 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) => {
53 self.region_scope_tree.is_subscope_of(*sub_scope, *super_scope)
56 (ty::ReScope(sub_scope), ty::ReEarlyBound(ref br)) => {
57 let fr_scope = self.region_scope_tree.early_free_scope(self.tcx, br);
58 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
61 (ty::ReScope(sub_scope), ty::ReFree(fr)) => {
62 let fr_scope = self.region_scope_tree.free_scope(self.tcx, fr);
63 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
66 (ty::ReEarlyBound(_), ty::ReEarlyBound(_))
67 | (ty::ReFree(_), ty::ReEarlyBound(_))
68 | (ty::ReEarlyBound(_), ty::ReFree(_))
69 | (ty::ReFree(_), ty::ReFree(_)) => {
70 self.free_regions.sub_free_regions(sub_region, super_region)
76 let result = result || self.is_static(super_region);
78 "is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
79 sub_region, super_region, result
84 /// Determines whether this free region is required to be `'static`.
85 fn is_static(&self, super_region: ty::Region<'tcx>) -> bool {
86 debug!("is_static(super_region={:?})", super_region);
89 ty::ReEarlyBound(_) | ty::ReFree(_) => {
90 let re_static = self.tcx.mk_region(ty::ReStatic);
91 self.free_regions.sub_free_regions(&re_static, &super_region)
97 pub fn lub_free_regions(&self, r_a: Region<'tcx>, r_b: Region<'tcx>) -> Region<'tcx> {
98 self.free_regions.lub_free_regions(self.tcx, r_a, r_b)