rustc_middle/ty/
region.rs

1use rustc_data_structures::intern::Interned;
2use rustc_errors::MultiSpan;
3use rustc_hir::def_id::DefId;
4use rustc_macros::{HashStable, TyDecodable, TyEncodable};
5use rustc_span::{DUMMY_SP, ErrorGuaranteed, Symbol, kw, sym};
6use rustc_type_ir::RegionKind as IrRegionKind;
7pub use rustc_type_ir::RegionVid;
8use tracing::debug;
9
10use crate::ty::{self, BoundVar, TyCtxt, TypeFlags};
11
12pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
13
14/// Use this rather than `RegionKind`, whenever possible.
15#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
16#[rustc_pass_by_value]
17pub struct Region<'tcx>(pub Interned<'tcx, RegionKind<'tcx>>);
18
19impl<'tcx> rustc_type_ir::inherent::IntoKind for Region<'tcx> {
20    type Kind = RegionKind<'tcx>;
21
22    fn kind(self) -> RegionKind<'tcx> {
23        *self.0.0
24    }
25}
26
27impl<'tcx> rustc_type_ir::Flags for Region<'tcx> {
28    fn flags(&self) -> TypeFlags {
29        self.type_flags()
30    }
31
32    fn outer_exclusive_binder(&self) -> ty::DebruijnIndex {
33        match self.kind() {
34            ty::ReBound(debruijn, _) => debruijn.shifted_in(1),
35            _ => ty::INNERMOST,
36        }
37    }
38}
39
40impl<'tcx> Region<'tcx> {
41    #[inline]
42    pub fn new_early_param(
43        tcx: TyCtxt<'tcx>,
44        early_bound_region: ty::EarlyParamRegion,
45    ) -> Region<'tcx> {
46        tcx.intern_region(ty::ReEarlyParam(early_bound_region))
47    }
48
49    #[inline]
50    pub fn new_bound(
51        tcx: TyCtxt<'tcx>,
52        debruijn: ty::DebruijnIndex,
53        bound_region: ty::BoundRegion,
54    ) -> Region<'tcx> {
55        // Use a pre-interned one when possible.
56        if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
57            && let Some(inner) = tcx.lifetimes.re_late_bounds.get(debruijn.as_usize())
58            && let Some(re) = inner.get(var.as_usize()).copied()
59        {
60            re
61        } else {
62            tcx.intern_region(ty::ReBound(debruijn, bound_region))
63        }
64    }
65
66    #[inline]
67    pub fn new_late_param(
68        tcx: TyCtxt<'tcx>,
69        scope: DefId,
70        kind: LateParamRegionKind,
71    ) -> Region<'tcx> {
72        let data = LateParamRegion { scope, kind };
73        tcx.intern_region(ty::ReLateParam(data))
74    }
75
76    #[inline]
77    pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> {
78        // Use a pre-interned one when possible.
79        tcx.lifetimes
80            .re_vars
81            .get(v.as_usize())
82            .copied()
83            .unwrap_or_else(|| tcx.intern_region(ty::ReVar(v)))
84    }
85
86    #[inline]
87    pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion) -> Region<'tcx> {
88        tcx.intern_region(ty::RePlaceholder(placeholder))
89    }
90
91    /// Constructs a `RegionKind::ReError` region.
92    #[track_caller]
93    pub fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> {
94        tcx.intern_region(ty::ReError(guar))
95    }
96
97    /// Constructs a `RegionKind::ReError` region and registers a delayed bug to ensure it gets
98    /// used.
99    #[track_caller]
100    pub fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> {
101        Region::new_error_with_message(
102            tcx,
103            DUMMY_SP,
104            "RegionKind::ReError constructed but no error reported",
105        )
106    }
107
108    /// Constructs a `RegionKind::ReError` region and registers a delayed bug with the given `msg`
109    /// to ensure it gets used.
110    #[track_caller]
111    pub fn new_error_with_message<S: Into<MultiSpan>>(
112        tcx: TyCtxt<'tcx>,
113        span: S,
114        msg: &'static str,
115    ) -> Region<'tcx> {
116        let reported = tcx.dcx().span_delayed_bug(span, msg);
117        Region::new_error(tcx, reported)
118    }
119
120    /// Avoid this in favour of more specific `new_*` methods, where possible,
121    /// to avoid the cost of the `match`.
122    pub fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> {
123        match kind {
124            ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
125            ty::ReBound(debruijn, region) => Region::new_bound(tcx, debruijn, region),
126            ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
127                Region::new_late_param(tcx, scope, kind)
128            }
129            ty::ReStatic => tcx.lifetimes.re_static,
130            ty::ReVar(vid) => Region::new_var(tcx, vid),
131            ty::RePlaceholder(region) => Region::new_placeholder(tcx, region),
132            ty::ReErased => tcx.lifetimes.re_erased,
133            ty::ReError(reported) => Region::new_error(tcx, reported),
134        }
135    }
136}
137
138impl<'tcx> rustc_type_ir::inherent::Region<TyCtxt<'tcx>> for Region<'tcx> {
139    fn new_bound(
140        interner: TyCtxt<'tcx>,
141        debruijn: ty::DebruijnIndex,
142        var: ty::BoundRegion,
143    ) -> Self {
144        Region::new_bound(interner, debruijn, var)
145    }
146
147    fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
148        Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon })
149    }
150
151    fn new_static(tcx: TyCtxt<'tcx>) -> Self {
152        tcx.lifetimes.re_static
153    }
154}
155
156/// Region utilities
157impl<'tcx> Region<'tcx> {
158    pub fn kind(self) -> RegionKind<'tcx> {
159        *self.0.0
160    }
161
162    pub fn get_name(self) -> Option<Symbol> {
163        if self.has_name() {
164            match self.kind() {
165                ty::ReEarlyParam(ebr) => Some(ebr.name),
166                ty::ReBound(_, br) => br.kind.get_name(),
167                ty::ReLateParam(fr) => fr.kind.get_name(),
168                ty::ReStatic => Some(kw::StaticLifetime),
169                ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(),
170                _ => None,
171            }
172        } else {
173            None
174        }
175    }
176
177    pub fn get_name_or_anon(self) -> Symbol {
178        match self.get_name() {
179            Some(name) => name,
180            None => sym::anon,
181        }
182    }
183
184    /// Is this region named by the user?
185    pub fn has_name(self) -> bool {
186        match self.kind() {
187            ty::ReEarlyParam(ebr) => ebr.has_name(),
188            ty::ReBound(_, br) => br.kind.is_named(),
189            ty::ReLateParam(fr) => fr.kind.is_named(),
190            ty::ReStatic => true,
191            ty::ReVar(..) => false,
192            ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(),
193            ty::ReErased => false,
194            ty::ReError(_) => false,
195        }
196    }
197
198    #[inline]
199    pub fn is_error(self) -> bool {
200        matches!(self.kind(), ty::ReError(_))
201    }
202
203    #[inline]
204    pub fn is_static(self) -> bool {
205        matches!(self.kind(), ty::ReStatic)
206    }
207
208    #[inline]
209    pub fn is_erased(self) -> bool {
210        matches!(self.kind(), ty::ReErased)
211    }
212
213    #[inline]
214    pub fn is_bound(self) -> bool {
215        matches!(self.kind(), ty::ReBound(..))
216    }
217
218    #[inline]
219    pub fn is_placeholder(self) -> bool {
220        matches!(self.kind(), ty::RePlaceholder(..))
221    }
222
223    #[inline]
224    pub fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
225        match self.kind() {
226            ty::ReBound(debruijn, _) => debruijn >= index,
227            _ => false,
228        }
229    }
230
231    pub fn type_flags(self) -> TypeFlags {
232        let mut flags = TypeFlags::empty();
233
234        match self.kind() {
235            ty::ReVar(..) => {
236                flags = flags | TypeFlags::HAS_FREE_REGIONS;
237                flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
238                flags = flags | TypeFlags::HAS_RE_INFER;
239            }
240            ty::RePlaceholder(..) => {
241                flags = flags | TypeFlags::HAS_FREE_REGIONS;
242                flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
243                flags = flags | TypeFlags::HAS_RE_PLACEHOLDER;
244            }
245            ty::ReEarlyParam(..) => {
246                flags = flags | TypeFlags::HAS_FREE_REGIONS;
247                flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
248                flags = flags | TypeFlags::HAS_RE_PARAM;
249            }
250            ty::ReLateParam { .. } => {
251                flags = flags | TypeFlags::HAS_FREE_REGIONS;
252                flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
253            }
254            ty::ReStatic => {
255                flags = flags | TypeFlags::HAS_FREE_REGIONS;
256            }
257            ty::ReBound(..) => {
258                flags = flags | TypeFlags::HAS_RE_BOUND;
259            }
260            ty::ReErased => {
261                flags = flags | TypeFlags::HAS_RE_ERASED;
262            }
263            ty::ReError(_) => {
264                flags = flags | TypeFlags::HAS_FREE_REGIONS;
265                flags = flags | TypeFlags::HAS_ERROR;
266            }
267        }
268
269        debug!("type_flags({:?}) = {:?}", self, flags);
270
271        flags
272    }
273
274    /// True for free regions other than `'static`.
275    pub fn is_param(self) -> bool {
276        matches!(self.kind(), ty::ReEarlyParam(_) | ty::ReLateParam(_))
277    }
278
279    /// True for free region in the current context.
280    ///
281    /// This is the case for `'static` and param regions.
282    pub fn is_free(self) -> bool {
283        match self.kind() {
284            ty::ReStatic | ty::ReEarlyParam(..) | ty::ReLateParam(..) => true,
285            ty::ReVar(..)
286            | ty::RePlaceholder(..)
287            | ty::ReBound(..)
288            | ty::ReErased
289            | ty::ReError(..) => false,
290        }
291    }
292
293    pub fn is_var(self) -> bool {
294        matches!(self.kind(), ty::ReVar(_))
295    }
296
297    pub fn as_var(self) -> RegionVid {
298        match self.kind() {
299            ty::ReVar(vid) => vid,
300            _ => bug!("expected region {:?} to be of kind ReVar", self),
301        }
302    }
303
304    /// Given some item `binding_item`, check if this region is a generic parameter introduced by it
305    /// or one of the parent generics. Returns the `DefId` of the parameter definition if so.
306    pub fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option<DefId> {
307        match self.kind() {
308            ty::ReEarlyParam(ebr) => {
309                Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
310            }
311            ty::ReLateParam(ty::LateParamRegion {
312                kind: ty::LateParamRegionKind::Named(def_id, _),
313                ..
314            }) => Some(def_id),
315            _ => None,
316        }
317    }
318}
319
320#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
321#[derive(HashStable)]
322pub struct EarlyParamRegion {
323    pub index: u32,
324    pub name: Symbol,
325}
326
327impl rustc_type_ir::inherent::ParamLike for EarlyParamRegion {
328    fn index(self) -> u32 {
329        self.index
330    }
331}
332
333impl std::fmt::Debug for EarlyParamRegion {
334    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
335        write!(f, "{}/#{}", self.name, self.index)
336    }
337}
338
339#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
340#[derive(HashStable)]
341/// The parameter representation of late-bound function parameters, "some region
342/// at least as big as the scope `fr.scope`".
343///
344/// Similar to a placeholder region as we create `LateParam` regions when entering a binder
345/// except they are always in the root universe and instead of using a boundvar to distinguish
346/// between others we use the `DefId` of the parameter. For this reason the `bound_region` field
347/// should basically always be `BoundRegionKind::Named` as otherwise there is no way of telling
348/// different parameters apart.
349pub struct LateParamRegion {
350    pub scope: DefId,
351    pub kind: LateParamRegionKind,
352}
353
354/// When liberating bound regions, we map their [`BoundRegionKind`]
355/// to this as we need to track the index of anonymous regions. We
356/// otherwise end up liberating multiple bound regions to the same
357/// late-bound region.
358#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
359#[derive(HashStable)]
360pub enum LateParamRegionKind {
361    /// An anonymous region parameter for a given fn (&T)
362    ///
363    /// Unlike [`BoundRegionKind::Anon`], this tracks the index of the
364    /// liberated bound region.
365    ///
366    /// We should ideally never liberate anonymous regions, but do so for the
367    /// sake of diagnostics in `FnCtxt::sig_of_closure_with_expectation`.
368    Anon(u32),
369
370    /// Named region parameters for functions (a in &'a T)
371    ///
372    /// The `DefId` is needed to distinguish free regions in
373    /// the event of shadowing.
374    Named(DefId, Symbol),
375
376    /// Anonymous region for the implicit env pointer parameter
377    /// to a closure
378    ClosureEnv,
379}
380
381impl LateParamRegionKind {
382    pub fn from_bound(var: BoundVar, br: BoundRegionKind) -> LateParamRegionKind {
383        match br {
384            BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
385            BoundRegionKind::Named(def_id, name) => LateParamRegionKind::Named(def_id, name),
386            BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
387        }
388    }
389
390    pub fn is_named(&self) -> bool {
391        match *self {
392            LateParamRegionKind::Named(_, name) => name != kw::UnderscoreLifetime,
393            _ => false,
394        }
395    }
396
397    pub fn get_name(&self) -> Option<Symbol> {
398        if self.is_named() {
399            match *self {
400                LateParamRegionKind::Named(_, name) => return Some(name),
401                _ => unreachable!(),
402            }
403        }
404
405        None
406    }
407
408    pub fn get_id(&self) -> Option<DefId> {
409        match *self {
410            LateParamRegionKind::Named(id, _) => Some(id),
411            _ => None,
412        }
413    }
414}
415
416#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
417#[derive(HashStable)]
418pub enum BoundRegionKind {
419    /// An anonymous region parameter for a given fn (&T)
420    Anon,
421
422    /// Named region parameters for functions (a in &'a T)
423    ///
424    /// The `DefId` is needed to distinguish free regions in
425    /// the event of shadowing.
426    Named(DefId, Symbol),
427
428    /// Anonymous region for the implicit env pointer parameter
429    /// to a closure
430    ClosureEnv,
431}
432
433#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
434#[derive(HashStable)]
435pub struct BoundRegion {
436    pub var: BoundVar,
437    pub kind: BoundRegionKind,
438}
439
440impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundRegion {
441    fn var(self) -> BoundVar {
442        self.var
443    }
444
445    fn assert_eq(self, var: ty::BoundVariableKind) {
446        assert_eq!(self.kind, var.expect_region())
447    }
448}
449
450impl core::fmt::Debug for BoundRegion {
451    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
452        match self.kind {
453            BoundRegionKind::Anon => write!(f, "{:?}", self.var),
454            BoundRegionKind::ClosureEnv => write!(f, "{:?}.Env", self.var),
455            BoundRegionKind::Named(def, symbol) => {
456                write!(f, "{:?}.Named({:?}, {:?})", self.var, def, symbol)
457            }
458        }
459    }
460}
461
462impl BoundRegionKind {
463    pub fn is_named(&self) -> bool {
464        match *self {
465            BoundRegionKind::Named(_, name) => name != kw::UnderscoreLifetime,
466            _ => false,
467        }
468    }
469
470    pub fn get_name(&self) -> Option<Symbol> {
471        if self.is_named() {
472            match *self {
473                BoundRegionKind::Named(_, name) => return Some(name),
474                _ => unreachable!(),
475            }
476        }
477
478        None
479    }
480
481    pub fn get_id(&self) -> Option<DefId> {
482        match *self {
483            BoundRegionKind::Named(id, _) => Some(id),
484            _ => None,
485        }
486    }
487}