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#[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(ty::BoundVarIndexKind::Bound(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 if let ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon } = bound_region
57 && let Some(inner) = tcx.lifetimes.anon_re_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(ty::BoundVarIndexKind::Bound(debruijn), bound_region))
63 }
64 }
65
66 #[inline]
67 pub fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: ty::BoundVar) -> Region<'tcx> {
68 if let Some(re) = tcx.lifetimes.anon_re_canonical_bounds.get(var.as_usize()).copied() {
70 re
71 } else {
72 tcx.intern_region(ty::ReBound(
73 ty::BoundVarIndexKind::Canonical,
74 ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
75 ))
76 }
77 }
78
79 #[inline]
80 pub fn new_late_param(
81 tcx: TyCtxt<'tcx>,
82 scope: DefId,
83 kind: LateParamRegionKind,
84 ) -> Region<'tcx> {
85 let data = LateParamRegion { scope, kind };
86 tcx.intern_region(ty::ReLateParam(data))
87 }
88
89 #[inline]
90 pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> {
91 tcx.lifetimes
93 .re_vars
94 .get(v.as_usize())
95 .copied()
96 .unwrap_or_else(|| tcx.intern_region(ty::ReVar(v)))
97 }
98
99 #[inline]
100 pub fn new_placeholder(
101 tcx: TyCtxt<'tcx>,
102 placeholder: ty::PlaceholderRegion<'tcx>,
103 ) -> Region<'tcx> {
104 tcx.intern_region(ty::RePlaceholder(placeholder))
105 }
106
107 #[track_caller]
109 pub fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> {
110 tcx.intern_region(ty::ReError(guar))
111 }
112
113 #[track_caller]
116 pub fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> {
117 Region::new_error_with_message(
118 tcx,
119 DUMMY_SP,
120 "RegionKind::ReError constructed but no error reported",
121 )
122 }
123
124 #[track_caller]
127 pub fn new_error_with_message<S: Into<MultiSpan>>(
128 tcx: TyCtxt<'tcx>,
129 span: S,
130 msg: &'static str,
131 ) -> Region<'tcx> {
132 let reported = tcx.dcx().span_delayed_bug(span, msg);
133 Region::new_error(tcx, reported)
134 }
135
136 pub fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> {
139 match kind {
140 ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
141 ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), region) => {
142 Region::new_bound(tcx, debruijn, region)
143 }
144 ty::ReBound(ty::BoundVarIndexKind::Canonical, region) => {
145 Region::new_canonical_bound(tcx, region.var)
146 }
147 ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
148 Region::new_late_param(tcx, scope, kind)
149 }
150 ty::ReStatic => tcx.lifetimes.re_static,
151 ty::ReVar(vid) => Region::new_var(tcx, vid),
152 ty::RePlaceholder(region) => Region::new_placeholder(tcx, region),
153 ty::ReErased => tcx.lifetimes.re_erased,
154 ty::ReError(reported) => Region::new_error(tcx, reported),
155 }
156 }
157}
158
159impl<'tcx> rustc_type_ir::inherent::Region<TyCtxt<'tcx>> for Region<'tcx> {
160 fn new_bound(
161 interner: TyCtxt<'tcx>,
162 debruijn: ty::DebruijnIndex,
163 var: ty::BoundRegion,
164 ) -> Self {
165 Region::new_bound(interner, debruijn, var)
166 }
167
168 fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
169 Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon })
170 }
171
172 fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: rustc_type_ir::BoundVar) -> Self {
173 Region::new_canonical_bound(tcx, var)
174 }
175
176 fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion<'tcx>) -> Self {
177 Region::new_placeholder(tcx, placeholder)
178 }
179
180 fn new_static(tcx: TyCtxt<'tcx>) -> Self {
181 tcx.lifetimes.re_static
182 }
183}
184
185impl<'tcx> Region<'tcx> {
187 pub fn kind(self) -> RegionKind<'tcx> {
188 *self.0.0
189 }
190
191 pub fn get_name(self, tcx: TyCtxt<'tcx>) -> Option<Symbol> {
192 match self.kind() {
193 ty::ReEarlyParam(ebr) => ebr.is_named().then_some(ebr.name),
194 ty::ReBound(_, br) => br.kind.get_name(tcx),
195 ty::ReLateParam(fr) => fr.kind.get_name(tcx),
196 ty::ReStatic => Some(kw::StaticLifetime),
197 ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(tcx),
198 _ => None,
199 }
200 }
201
202 pub fn get_name_or_anon(self, tcx: TyCtxt<'tcx>) -> Symbol {
203 match self.get_name(tcx) {
204 Some(name) => name,
205 None => sym::anon,
206 }
207 }
208
209 pub fn is_named(self, tcx: TyCtxt<'tcx>) -> bool {
211 match self.kind() {
212 ty::ReEarlyParam(ebr) => ebr.is_named(),
213 ty::ReBound(_, br) => br.kind.is_named(tcx),
214 ty::ReLateParam(fr) => fr.kind.is_named(tcx),
215 ty::ReStatic => true,
216 ty::ReVar(..) => false,
217 ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(tcx),
218 ty::ReErased => false,
219 ty::ReError(_) => false,
220 }
221 }
222
223 #[inline]
224 pub fn is_error(self) -> bool {
225 matches!(self.kind(), ty::ReError(_))
226 }
227
228 #[inline]
229 pub fn is_static(self) -> bool {
230 matches!(self.kind(), ty::ReStatic)
231 }
232
233 #[inline]
234 pub fn is_erased(self) -> bool {
235 matches!(self.kind(), ty::ReErased)
236 }
237
238 #[inline]
239 pub fn is_bound(self) -> bool {
240 matches!(self.kind(), ty::ReBound(..))
241 }
242
243 #[inline]
244 pub fn is_placeholder(self) -> bool {
245 matches!(self.kind(), ty::RePlaceholder(..))
246 }
247
248 #[inline]
249 pub fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
250 match self.kind() {
251 ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) => debruijn >= index,
252 _ => false,
253 }
254 }
255
256 pub fn type_flags(self) -> TypeFlags {
257 let mut flags = TypeFlags::empty();
258
259 match self.kind() {
260 ty::ReVar(..) => {
261 flags = flags | TypeFlags::HAS_FREE_REGIONS;
262 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
263 flags = flags | TypeFlags::HAS_RE_INFER;
264 }
265 ty::RePlaceholder(..) => {
266 flags = flags | TypeFlags::HAS_FREE_REGIONS;
267 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
268 flags = flags | TypeFlags::HAS_RE_PLACEHOLDER;
269 }
270 ty::ReEarlyParam(..) => {
271 flags = flags | TypeFlags::HAS_FREE_REGIONS;
272 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
273 flags = flags | TypeFlags::HAS_RE_PARAM;
274 }
275 ty::ReLateParam { .. } => {
276 flags = flags | TypeFlags::HAS_FREE_REGIONS;
277 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
278 }
279 ty::ReStatic => {
280 flags = flags | TypeFlags::HAS_FREE_REGIONS;
281 }
282 ty::ReBound(ty::BoundVarIndexKind::Canonical, _) => {
283 flags = flags | TypeFlags::HAS_RE_BOUND;
284 flags = flags | TypeFlags::HAS_CANONICAL_BOUND;
285 }
286 ty::ReBound(ty::BoundVarIndexKind::Bound(..), _) => {
287 flags = flags | TypeFlags::HAS_RE_BOUND;
288 }
289 ty::ReErased => {
290 flags = flags | TypeFlags::HAS_RE_ERASED;
291 }
292 ty::ReError(_) => {
293 flags = flags | TypeFlags::HAS_FREE_REGIONS;
294 flags = flags | TypeFlags::HAS_ERROR;
295 }
296 }
297
298 debug!("type_flags({:?}) = {:?}", self, flags);
299
300 flags
301 }
302
303 pub fn is_param(self) -> bool {
305 matches!(self.kind(), ty::ReEarlyParam(_) | ty::ReLateParam(_))
306 }
307
308 pub fn is_free(self) -> bool {
312 match self.kind() {
313 ty::ReStatic | ty::ReEarlyParam(..) | ty::ReLateParam(..) => true,
314 ty::ReVar(..)
315 | ty::RePlaceholder(..)
316 | ty::ReBound(..)
317 | ty::ReErased
318 | ty::ReError(..) => false,
319 }
320 }
321
322 pub fn is_var(self) -> bool {
323 matches!(self.kind(), ty::ReVar(_))
324 }
325
326 pub fn as_var(self) -> RegionVid {
327 match self.kind() {
328 ty::ReVar(vid) => vid,
329 _ => bug!("expected region {:?} to be of kind ReVar", self),
330 }
331 }
332
333 pub fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option<DefId> {
336 match self.kind() {
337 ty::ReEarlyParam(ebr) => {
338 Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
339 }
340 ty::ReLateParam(ty::LateParamRegion {
341 kind: ty::LateParamRegionKind::Named(def_id),
342 ..
343 }) => Some(def_id),
344 _ => None,
345 }
346 }
347}
348
349#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
350#[derive(HashStable)]
351pub struct EarlyParamRegion {
352 pub index: u32,
353 pub name: Symbol,
354}
355
356impl EarlyParamRegion {
357 pub fn is_named(&self) -> bool {
360 self.name != kw::UnderscoreLifetime
361 }
362}
363
364impl rustc_type_ir::inherent::ParamLike for EarlyParamRegion {
365 fn index(self) -> u32 {
366 self.index
367 }
368}
369
370impl std::fmt::Debug for EarlyParamRegion {
371 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
372 write!(f, "{}/#{}", self.name, self.index)
373 }
374}
375
376#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
377#[derive(HashStable)]
378pub struct LateParamRegion {
387 pub scope: DefId,
388 pub kind: LateParamRegionKind,
389}
390
391#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
396#[derive(HashStable)]
397pub enum LateParamRegionKind {
398 Anon(u32),
406
407 NamedAnon(u32, Symbol),
411
412 Named(DefId),
414
415 ClosureEnv,
418}
419
420impl LateParamRegionKind {
421 pub fn from_bound(var: BoundVar, br: BoundRegionKind) -> LateParamRegionKind {
422 match br {
423 BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
424 BoundRegionKind::Named(def_id) => LateParamRegionKind::Named(def_id),
425 BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
426 BoundRegionKind::NamedAnon(name) => LateParamRegionKind::NamedAnon(var.as_u32(), name),
427 }
428 }
429
430 pub fn is_named(&self, tcx: TyCtxt<'_>) -> bool {
431 self.get_name(tcx).is_some()
432 }
433
434 pub fn get_name(&self, tcx: TyCtxt<'_>) -> Option<Symbol> {
435 match *self {
436 LateParamRegionKind::Named(def_id) => {
437 let name = tcx.item_name(def_id);
438 if name != kw::UnderscoreLifetime { Some(name) } else { None }
439 }
440 LateParamRegionKind::NamedAnon(_, name) => Some(name),
441 _ => None,
442 }
443 }
444
445 pub fn get_id(&self) -> Option<DefId> {
446 match *self {
447 LateParamRegionKind::Named(id) => Some(id),
448 _ => None,
449 }
450 }
451}
452
453#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
454#[derive(HashStable)]
455pub enum BoundRegionKind {
456 Anon,
458
459 NamedAnon(Symbol),
463
464 Named(DefId),
466
467 ClosureEnv,
470}
471
472#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
473#[derive(HashStable)]
474pub struct BoundRegion {
475 pub var: BoundVar,
476 pub kind: BoundRegionKind,
477}
478
479impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundRegion {
480 fn var(self) -> BoundVar {
481 self.var
482 }
483
484 fn assert_eq(self, var: ty::BoundVariableKind) {
485 assert_eq!(self.kind, var.expect_region())
486 }
487}
488
489impl core::fmt::Debug for BoundRegion {
490 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
491 match self.kind {
492 BoundRegionKind::Anon => write!(f, "{:?}", self.var),
493 BoundRegionKind::ClosureEnv => write!(f, "{:?}.Env", self.var),
494 BoundRegionKind::Named(def) => {
495 write!(f, "{:?}.Named({:?})", self.var, def)
496 }
497 BoundRegionKind::NamedAnon(symbol) => {
498 write!(f, "{:?}.NamedAnon({:?})", self.var, symbol)
499 }
500 }
501 }
502}
503
504impl BoundRegionKind {
505 pub fn is_named(&self, tcx: TyCtxt<'_>) -> bool {
506 self.get_name(tcx).is_some()
507 }
508
509 pub fn get_name(&self, tcx: TyCtxt<'_>) -> Option<Symbol> {
510 match *self {
511 BoundRegionKind::Named(def_id) => {
512 let name = tcx.item_name(def_id);
513 if name != kw::UnderscoreLifetime { Some(name) } else { None }
514 }
515 BoundRegionKind::NamedAnon(name) => Some(name),
516 _ => None,
517 }
518 }
519
520 pub fn get_id(&self) -> Option<DefId> {
521 match *self {
522 BoundRegionKind::Named(id) => Some(id),
523 _ => None,
524 }
525 }
526}
527
528#[cfg(target_pointer_width = "64")]
530mod size_asserts {
531 use rustc_data_structures::static_assert_size;
532
533 use super::*;
534 static_assert_size!(RegionKind<'_>, 20);
536 static_assert_size!(ty::WithCachedTypeInfo<RegionKind<'_>>, 48);
537 }