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(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.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 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 #[track_caller]
93 pub fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> {
94 tcx.intern_region(ty::ReError(guar))
95 }
96
97 #[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 #[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 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
156impl<'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 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 pub fn is_param(self) -> bool {
276 matches!(self.kind(), ty::ReEarlyParam(_) | ty::ReLateParam(_))
277 }
278
279 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 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)]
341pub struct LateParamRegion {
350 pub scope: DefId,
351 pub kind: LateParamRegionKind,
352}
353
354#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
359#[derive(HashStable)]
360pub enum LateParamRegionKind {
361 Anon(u32),
369
370 Named(DefId, Symbol),
375
376 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 Anon,
421
422 Named(DefId, Symbol),
427
428 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}