]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/free_region.rs
Rollup merge of #68438 - Aaron1011:fix/tait-non-defining, r=estebank
[rust.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::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;
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 { tcx, context, region_scope_tree, free_regions }
39     }
40
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(
44         &self,
45         sub_region: ty::Region<'tcx>,
46         super_region: ty::Region<'tcx>,
47     ) -> bool {
48         let result = sub_region == super_region || {
49             match (sub_region, super_region) {
50                 (ty::ReEmpty, _) | (_, ty::ReStatic) => true,
51
52                 (ty::ReScope(sub_scope), ty::ReScope(super_scope)) => {
53                     self.region_scope_tree.is_subscope_of(*sub_scope, *super_scope)
54                 }
55
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)
59                 }
60
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)
64                 }
65
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)
71                 }
72
73                 _ => false,
74             }
75         };
76         let result = result || self.is_static(super_region);
77         debug!(
78             "is_subregion_of(sub_region={:?}, super_region={:?}) = {:?}",
79             sub_region, super_region, result
80         );
81         result
82     }
83
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);
87         match *super_region {
88             ty::ReStatic => true,
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)
92             }
93             _ => false,
94         }
95     }
96
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)
99     }
100 }