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_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderRegion) -> Self {
152 Region::new_placeholder(tcx, placeholder)
153 }
154
155 fn new_static(tcx: TyCtxt<'tcx>) -> Self {
156 tcx.lifetimes.re_static
157 }
158}
159
160impl<'tcx> Region<'tcx> {
162 pub fn kind(self) -> RegionKind<'tcx> {
163 *self.0.0
164 }
165
166 pub fn get_name(self) -> Option<Symbol> {
167 if self.has_name() {
168 match self.kind() {
169 ty::ReEarlyParam(ebr) => Some(ebr.name),
170 ty::ReBound(_, br) => br.kind.get_name(),
171 ty::ReLateParam(fr) => fr.kind.get_name(),
172 ty::ReStatic => Some(kw::StaticLifetime),
173 ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(),
174 _ => None,
175 }
176 } else {
177 None
178 }
179 }
180
181 pub fn get_name_or_anon(self) -> Symbol {
182 match self.get_name() {
183 Some(name) => name,
184 None => sym::anon,
185 }
186 }
187
188 pub fn has_name(self) -> bool {
190 match self.kind() {
191 ty::ReEarlyParam(ebr) => ebr.has_name(),
192 ty::ReBound(_, br) => br.kind.is_named(),
193 ty::ReLateParam(fr) => fr.kind.is_named(),
194 ty::ReStatic => true,
195 ty::ReVar(..) => false,
196 ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(),
197 ty::ReErased => false,
198 ty::ReError(_) => false,
199 }
200 }
201
202 #[inline]
203 pub fn is_error(self) -> bool {
204 matches!(self.kind(), ty::ReError(_))
205 }
206
207 #[inline]
208 pub fn is_static(self) -> bool {
209 matches!(self.kind(), ty::ReStatic)
210 }
211
212 #[inline]
213 pub fn is_erased(self) -> bool {
214 matches!(self.kind(), ty::ReErased)
215 }
216
217 #[inline]
218 pub fn is_bound(self) -> bool {
219 matches!(self.kind(), ty::ReBound(..))
220 }
221
222 #[inline]
223 pub fn is_placeholder(self) -> bool {
224 matches!(self.kind(), ty::RePlaceholder(..))
225 }
226
227 #[inline]
228 pub fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
229 match self.kind() {
230 ty::ReBound(debruijn, _) => debruijn >= index,
231 _ => false,
232 }
233 }
234
235 pub fn type_flags(self) -> TypeFlags {
236 let mut flags = TypeFlags::empty();
237
238 match self.kind() {
239 ty::ReVar(..) => {
240 flags = flags | TypeFlags::HAS_FREE_REGIONS;
241 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
242 flags = flags | TypeFlags::HAS_RE_INFER;
243 }
244 ty::RePlaceholder(..) => {
245 flags = flags | TypeFlags::HAS_FREE_REGIONS;
246 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
247 flags = flags | TypeFlags::HAS_RE_PLACEHOLDER;
248 }
249 ty::ReEarlyParam(..) => {
250 flags = flags | TypeFlags::HAS_FREE_REGIONS;
251 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
252 flags = flags | TypeFlags::HAS_RE_PARAM;
253 }
254 ty::ReLateParam { .. } => {
255 flags = flags | TypeFlags::HAS_FREE_REGIONS;
256 flags = flags | TypeFlags::HAS_FREE_LOCAL_REGIONS;
257 }
258 ty::ReStatic => {
259 flags = flags | TypeFlags::HAS_FREE_REGIONS;
260 }
261 ty::ReBound(..) => {
262 flags = flags | TypeFlags::HAS_RE_BOUND;
263 }
264 ty::ReErased => {
265 flags = flags | TypeFlags::HAS_RE_ERASED;
266 }
267 ty::ReError(_) => {
268 flags = flags | TypeFlags::HAS_FREE_REGIONS;
269 flags = flags | TypeFlags::HAS_ERROR;
270 }
271 }
272
273 debug!("type_flags({:?}) = {:?}", self, flags);
274
275 flags
276 }
277
278 pub fn is_param(self) -> bool {
280 matches!(self.kind(), ty::ReEarlyParam(_) | ty::ReLateParam(_))
281 }
282
283 pub fn is_free(self) -> bool {
287 match self.kind() {
288 ty::ReStatic | ty::ReEarlyParam(..) | ty::ReLateParam(..) => true,
289 ty::ReVar(..)
290 | ty::RePlaceholder(..)
291 | ty::ReBound(..)
292 | ty::ReErased
293 | ty::ReError(..) => false,
294 }
295 }
296
297 pub fn is_var(self) -> bool {
298 matches!(self.kind(), ty::ReVar(_))
299 }
300
301 pub fn as_var(self) -> RegionVid {
302 match self.kind() {
303 ty::ReVar(vid) => vid,
304 _ => bug!("expected region {:?} to be of kind ReVar", self),
305 }
306 }
307
308 pub fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option<DefId> {
311 match self.kind() {
312 ty::ReEarlyParam(ebr) => {
313 Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
314 }
315 ty::ReLateParam(ty::LateParamRegion {
316 kind: ty::LateParamRegionKind::Named(def_id, _),
317 ..
318 }) => Some(def_id),
319 _ => None,
320 }
321 }
322}
323
324#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
325#[derive(HashStable)]
326pub struct EarlyParamRegion {
327 pub index: u32,
328 pub name: Symbol,
329}
330
331impl rustc_type_ir::inherent::ParamLike for EarlyParamRegion {
332 fn index(self) -> u32 {
333 self.index
334 }
335}
336
337impl std::fmt::Debug for EarlyParamRegion {
338 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
339 write!(f, "{}/#{}", self.name, self.index)
340 }
341}
342
343#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
344#[derive(HashStable)]
345pub struct LateParamRegion {
354 pub scope: DefId,
355 pub kind: LateParamRegionKind,
356}
357
358#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
363#[derive(HashStable)]
364pub enum LateParamRegionKind {
365 Anon(u32),
373
374 Named(DefId, Symbol),
379
380 ClosureEnv,
383}
384
385impl LateParamRegionKind {
386 pub fn from_bound(var: BoundVar, br: BoundRegionKind) -> LateParamRegionKind {
387 match br {
388 BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
389 BoundRegionKind::Named(def_id, name) => LateParamRegionKind::Named(def_id, name),
390 BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
391 }
392 }
393
394 pub fn is_named(&self) -> bool {
395 match *self {
396 LateParamRegionKind::Named(_, name) => name != kw::UnderscoreLifetime,
397 _ => false,
398 }
399 }
400
401 pub fn get_name(&self) -> Option<Symbol> {
402 if self.is_named() {
403 match *self {
404 LateParamRegionKind::Named(_, name) => return Some(name),
405 _ => unreachable!(),
406 }
407 }
408
409 None
410 }
411
412 pub fn get_id(&self) -> Option<DefId> {
413 match *self {
414 LateParamRegionKind::Named(id, _) => Some(id),
415 _ => None,
416 }
417 }
418}
419
420#[derive(Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Copy)]
421#[derive(HashStable)]
422pub enum BoundRegionKind {
423 Anon,
425
426 Named(DefId, Symbol),
431
432 ClosureEnv,
435}
436
437#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
438#[derive(HashStable)]
439pub struct BoundRegion {
440 pub var: BoundVar,
441 pub kind: BoundRegionKind,
442}
443
444impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundRegion {
445 fn var(self) -> BoundVar {
446 self.var
447 }
448
449 fn assert_eq(self, var: ty::BoundVariableKind) {
450 assert_eq!(self.kind, var.expect_region())
451 }
452}
453
454impl core::fmt::Debug for BoundRegion {
455 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
456 match self.kind {
457 BoundRegionKind::Anon => write!(f, "{:?}", self.var),
458 BoundRegionKind::ClosureEnv => write!(f, "{:?}.Env", self.var),
459 BoundRegionKind::Named(def, symbol) => {
460 write!(f, "{:?}.Named({:?}, {:?})", self.var, def, symbol)
461 }
462 }
463 }
464}
465
466impl BoundRegionKind {
467 pub fn is_named(&self) -> bool {
468 match *self {
469 BoundRegionKind::Named(_, name) => name != kw::UnderscoreLifetime,
470 _ => false,
471 }
472 }
473
474 pub fn get_name(&self) -> Option<Symbol> {
475 if self.is_named() {
476 match *self {
477 BoundRegionKind::Named(_, name) => return Some(name),
478 _ => unreachable!(),
479 }
480 }
481
482 None
483 }
484
485 pub fn get_id(&self) -> Option<DefId> {
486 match *self {
487 BoundRegionKind::Named(id, _) => Some(id),
488 _ => None,
489 }
490 }
491}