]> git.proxmox.com Git - rustc.git/blob - src/librustc/middle/free_region.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / librustc / middle / free_region.rs
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.
5
6 use crate::infer::outlives::free_region_map::{FreeRegionMap, FreeRegionRelations};
7 use crate::hir::def_id::DefId;
8 use crate::middle::region;
9 use crate::ty::{self, TyCtxt, Region};
10
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
14 /// regions.
15 ///
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>,
20
21 /// The context used to fetch the region maps.
22 pub context: DefId,
23
24 /// The region maps for the given context.
25 pub region_scope_tree: &'a region::ScopeTree,
26
27 /// Free-region relationships.
28 pub free_regions: &'a FreeRegionMap<'tcx>,
29 }
30
31 impl<'a, 'tcx> RegionRelations<'a, 'tcx> {
32 pub fn new(
33 tcx: TyCtxt<'tcx>,
34 context: DefId,
35 region_scope_tree: &'a region::ScopeTree,
36 free_regions: &'a FreeRegionMap<'tcx>,
37 ) -> Self {
38 Self {
39 tcx,
40 context,
41 region_scope_tree,
42 free_regions,
43 }
44 }
45
46 /// Determines whether one region is a subregion of another. This is intended to run *after
47 /// inference* and sadly the logic is somewhat duplicated with the code in infer.rs.
48 pub fn is_subregion_of(&self,
49 sub_region: ty::Region<'tcx>,
50 super_region: ty::Region<'tcx>)
51 -> bool {
52 let result = sub_region == super_region || {
53 match (sub_region, super_region) {
54 (ty::ReEmpty, _) |
55 (_, ty::ReStatic) =>
56 true,
57
58 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) =>
59 self.region_scope_tree.is_subscope_of(*sub_scope, *super_scope),
60
61 (ty::ReScope(sub_scope), ty::ReEarlyBound(ref br)) => {
62 let fr_scope = self.region_scope_tree.early_free_scope(self.tcx, br);
63 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
64 }
65
66 (ty::ReScope(sub_scope), ty::ReFree(fr)) => {
67 let fr_scope = self.region_scope_tree.free_scope(self.tcx, fr);
68 self.region_scope_tree.is_subscope_of(*sub_scope, fr_scope)
69 }
70
71 (ty::ReEarlyBound(_), ty::ReEarlyBound(_)) |
72 (ty::ReFree(_), ty::ReEarlyBound(_)) |
73 (ty::ReEarlyBound(_), ty::ReFree(_)) |
74 (ty::ReFree(_), ty::ReFree(_)) =>
75 self.free_regions.sub_free_regions(sub_region, super_region),
76
77 _ =>
78 false,
79 }
80 };
81 let result = result || self.is_static(super_region);
82 debug!("is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
83 sub_region, super_region, result);
84 result
85 }
86
87 /// Determines whether this free region is required to be `'static`.
88 fn is_static(&self, super_region: ty::Region<'tcx>) -> bool {
89 debug!("is_static(super_region={:?})", super_region);
90 match *super_region {
91 ty::ReStatic => true,
92 ty::ReEarlyBound(_) | ty::ReFree(_) => {
93 let re_static = self.tcx.mk_region(ty::ReStatic);
94 self.free_regions.sub_free_regions(&re_static, &super_region)
95 }
96 _ => false
97 }
98 }
99
100 pub fn lub_free_regions(&self,
101 r_a: Region<'tcx>,
102 r_b: Region<'tcx>)
103 -> Region<'tcx> {
104 self.free_regions.lub_free_regions(self.tcx, r_a, r_b)
105 }
106 }