1use rustc_errors::MultiSpan;
2use rustc_hir::def_id::DefId;
3use rustc_macros::{StableHash, TyDecodable, TyEncodable, extension};
4use rustc_span::{DUMMY_SP, ErrorGuaranteed, Symbol, kw, sym};
5pub use rustc_type_ir::RegionVid;
6use rustc_type_ir::{
7 LateParamRegion as IrLateParamRegion, Region as IrRegion, RegionKind as IrRegionKind,
8};
9
10use crate::ty::{self, BoundVar, TyCtxt};
11
12pub type Region<'tcx> = IrRegion<TyCtxt<'tcx>>;
13pub type RegionKind<'tcx> = IrRegionKind<TyCtxt<'tcx>>;
14pub type LateParamRegion<'tcx> = IrLateParamRegion<TyCtxt<'tcx>>;
15
16impl<'tcx> RegionExt<'tcx> for Region<'tcx> {
#[inline]
fn new_early_param(tcx: TyCtxt<'tcx>,
early_bound_region: ty::EarlyParamRegion) -> Region<'tcx> {
tcx.intern_region(ty::ReEarlyParam(early_bound_region))
}
#[inline]
fn new_late_param(tcx: TyCtxt<'tcx>, scope: DefId,
kind: LateParamRegionKind) -> Region<'tcx> {
let data = LateParamRegion { scope, kind };
tcx.intern_region(ty::ReLateParam(data))
}
#[inline]
fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> {
tcx.lifetimes.re_vars.get(v.as_usize()).copied().unwrap_or_else(||
tcx.intern_region(ty::ReVar(v)))
}
#[doc = " Constructs a `RegionKind::ReError` region."]
#[track_caller]
fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> {
tcx.intern_region(ty::ReError(guar))
}
#[doc =
" Constructs a `RegionKind::ReError` region and registers a delayed bug to ensure it gets"]
#[doc = " used."]
#[track_caller]
fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> {
Region::new_error_with_message(tcx, DUMMY_SP,
"RegionKind::ReError constructed but no error reported")
}
#[doc =
" Constructs a `RegionKind::ReError` region and registers a delayed bug with the given `msg`"]
#[doc = " to ensure it gets used."]
#[track_caller]
fn new_error_with_message<S: Into<MultiSpan>>(tcx: TyCtxt<'tcx>, span: S,
msg: &'static str) -> Region<'tcx> {
let reported = tcx.dcx().span_delayed_bug(span, msg);
Region::new_error(tcx, reported)
}
#[doc =
" Avoid this in favour of more specific `new_*` methods, where possible,"]
#[doc = " to avoid the cost of the `match`."]
fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>)
-> Region<'tcx> {
match kind {
ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), region) => {
Region::new_bound(tcx, debruijn, region)
}
ty::ReBound(ty::BoundVarIndexKind::Canonical, region) => {
Region::new_canonical_bound(tcx, region.var)
}
ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
Region::new_late_param(tcx, scope, kind)
}
ty::ReStatic => tcx.lifetimes.re_static,
ty::ReVar(vid) => Region::new_var(tcx, vid),
ty::RePlaceholder(region) => Region::new_placeholder(tcx, region),
ty::ReErased => tcx.lifetimes.re_erased,
ty::ReError(reported) => Region::new_error(tcx, reported),
}
}
fn get_name(self, tcx: TyCtxt<'tcx>) -> Option<Symbol> {
match self.kind() {
ty::ReEarlyParam(ebr) => ebr.is_named().then_some(ebr.name),
ty::ReBound(_, br) => br.kind.get_name(tcx),
ty::ReLateParam(fr) => fr.kind.get_name(tcx),
ty::ReStatic => Some(kw::StaticLifetime),
ty::RePlaceholder(placeholder) =>
placeholder.bound.kind.get_name(tcx),
_ => None,
}
}
fn get_name_or_anon(self, tcx: TyCtxt<'tcx>) -> Symbol {
match self.get_name(tcx) { Some(name) => name, None => sym::anon, }
}
#[doc = " Is this region named by the user?"]
fn is_named(self, tcx: TyCtxt<'tcx>) -> bool {
match self.kind() {
ty::ReEarlyParam(ebr) => ebr.is_named(),
ty::ReBound(_, br) => br.kind.is_named(tcx),
ty::ReLateParam(fr) => fr.kind.is_named(tcx),
ty::ReStatic => true,
ty::ReVar(..) => false,
ty::RePlaceholder(placeholder) =>
placeholder.bound.kind.is_named(tcx),
ty::ReErased => false,
ty::ReError(_) => false,
}
}
#[inline]
fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
match self.kind() {
ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) =>
debruijn >= index,
_ => false,
}
}
#[doc =
" Given some item `binding_item`, check if this region is a generic parameter introduced by it"]
#[doc =
" or one of the parent generics. Returns the `DefId` of the parameter definition if so."]
fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId)
-> Option<DefId> {
match self.kind() {
ty::ReEarlyParam(ebr) => {
Some(tcx.generics_of(binding_item).region_param(ebr,
tcx).def_id)
}
ty::ReLateParam(ty::LateParamRegion {
kind: ty::LateParamRegionKind::Named(def_id), .. }) =>
Some(def_id),
_ => None,
}
}
}#[extension(pub trait RegionExt<'tcx>)]
17impl<'tcx> Region<'tcx> {
18 #[inline]
19 fn new_early_param(
20 tcx: TyCtxt<'tcx>,
21 early_bound_region: ty::EarlyParamRegion,
22 ) -> Region<'tcx> {
23 tcx.intern_region(ty::ReEarlyParam(early_bound_region))
24 }
25
26 #[inline]
27 fn new_late_param(tcx: TyCtxt<'tcx>, scope: DefId, kind: LateParamRegionKind) -> Region<'tcx> {
28 let data = LateParamRegion { scope, kind };
29 tcx.intern_region(ty::ReLateParam(data))
30 }
31
32 #[inline]
33 fn new_var(tcx: TyCtxt<'tcx>, v: ty::RegionVid) -> Region<'tcx> {
34 tcx.lifetimes
36 .re_vars
37 .get(v.as_usize())
38 .copied()
39 .unwrap_or_else(|| tcx.intern_region(ty::ReVar(v)))
40 }
41
42 #[track_caller]
44 fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Region<'tcx> {
45 tcx.intern_region(ty::ReError(guar))
46 }
47
48 #[track_caller]
51 fn new_error_misc(tcx: TyCtxt<'tcx>) -> Region<'tcx> {
52 Region::new_error_with_message(
53 tcx,
54 DUMMY_SP,
55 "RegionKind::ReError constructed but no error reported",
56 )
57 }
58
59 #[track_caller]
62 fn new_error_with_message<S: Into<MultiSpan>>(
63 tcx: TyCtxt<'tcx>,
64 span: S,
65 msg: &'static str,
66 ) -> Region<'tcx> {
67 let reported = tcx.dcx().span_delayed_bug(span, msg);
68 Region::new_error(tcx, reported)
69 }
70
71 fn new_from_kind(tcx: TyCtxt<'tcx>, kind: RegionKind<'tcx>) -> Region<'tcx> {
74 match kind {
75 ty::ReEarlyParam(region) => Region::new_early_param(tcx, region),
76 ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), region) => {
77 Region::new_bound(tcx, debruijn, region)
78 }
79 ty::ReBound(ty::BoundVarIndexKind::Canonical, region) => {
80 Region::new_canonical_bound(tcx, region.var)
81 }
82 ty::ReLateParam(ty::LateParamRegion { scope, kind }) => {
83 Region::new_late_param(tcx, scope, kind)
84 }
85 ty::ReStatic => tcx.lifetimes.re_static,
86 ty::ReVar(vid) => Region::new_var(tcx, vid),
87 ty::RePlaceholder(region) => Region::new_placeholder(tcx, region),
88 ty::ReErased => tcx.lifetimes.re_erased,
89 ty::ReError(reported) => Region::new_error(tcx, reported),
90 }
91 }
92
93 fn get_name(self, tcx: TyCtxt<'tcx>) -> Option<Symbol> {
94 match self.kind() {
95 ty::ReEarlyParam(ebr) => ebr.is_named().then_some(ebr.name),
96 ty::ReBound(_, br) => br.kind.get_name(tcx),
97 ty::ReLateParam(fr) => fr.kind.get_name(tcx),
98 ty::ReStatic => Some(kw::StaticLifetime),
99 ty::RePlaceholder(placeholder) => placeholder.bound.kind.get_name(tcx),
100 _ => None,
101 }
102 }
103
104 fn get_name_or_anon(self, tcx: TyCtxt<'tcx>) -> Symbol {
105 match self.get_name(tcx) {
106 Some(name) => name,
107 None => sym::anon,
108 }
109 }
110
111 fn is_named(self, tcx: TyCtxt<'tcx>) -> bool {
113 match self.kind() {
114 ty::ReEarlyParam(ebr) => ebr.is_named(),
115 ty::ReBound(_, br) => br.kind.is_named(tcx),
116 ty::ReLateParam(fr) => fr.kind.is_named(tcx),
117 ty::ReStatic => true,
118 ty::ReVar(..) => false,
119 ty::RePlaceholder(placeholder) => placeholder.bound.kind.is_named(tcx),
120 ty::ReErased => false,
121 ty::ReError(_) => false,
122 }
123 }
124
125 #[inline]
126 fn bound_at_or_above_binder(self, index: ty::DebruijnIndex) -> bool {
127 match self.kind() {
128 ty::ReBound(ty::BoundVarIndexKind::Bound(debruijn), _) => debruijn >= index,
129 _ => false,
130 }
131 }
132
133 fn opt_param_def_id(self, tcx: TyCtxt<'tcx>, binding_item: DefId) -> Option<DefId> {
136 match self.kind() {
137 ty::ReEarlyParam(ebr) => {
138 Some(tcx.generics_of(binding_item).region_param(ebr, tcx).def_id)
139 }
140 ty::ReLateParam(ty::LateParamRegion {
141 kind: ty::LateParamRegionKind::Named(def_id),
142 ..
143 }) => Some(def_id),
144 _ => None,
145 }
146 }
147}
148
149#[derive(#[automatically_derived]
impl ::core::marker::Copy for EarlyParamRegion { }Copy, #[automatically_derived]
impl ::core::clone::Clone for EarlyParamRegion {
#[inline]
fn clone(&self) -> EarlyParamRegion {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<Symbol>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for EarlyParamRegion {
#[inline]
fn eq(&self, other: &EarlyParamRegion) -> bool {
self.index == other.index && self.name == other.name
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for EarlyParamRegion {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<u32>;
let _: ::core::cmp::AssertParamIsEq<Symbol>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for EarlyParamRegion {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.index, state);
::core::hash::Hash::hash(&self.name, state)
}
}Hash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for EarlyParamRegion {
fn encode(&self, __encoder: &mut __E) {
match *self {
EarlyParamRegion {
index: ref __binding_0, name: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for EarlyParamRegion {
fn decode(__decoder: &mut __D) -> Self {
EarlyParamRegion {
index: ::rustc_serialize::Decodable::decode(__decoder),
name: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
150#[derive(const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for
EarlyParamRegion {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
EarlyParamRegion {
index: ref __binding_0, name: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
151pub struct EarlyParamRegion {
152 pub index: u32,
153 pub name: Symbol,
154}
155
156impl EarlyParamRegion {
157 pub fn is_named(&self) -> bool {
160 self.name != kw::UnderscoreLifetime
161 }
162}
163
164impl rustc_type_ir::inherent::ParamLike for EarlyParamRegion {
165 fn index(self) -> u32 {
166 self.index
167 }
168}
169
170impl std::fmt::Debug for EarlyParamRegion {
171 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172 f.write_fmt(format_args!("{0}/#{1}", self.name, self.index))write!(f, "{}/#{}", self.name, self.index)
173 }
174}
175
176#[derive(#[automatically_derived]
impl ::core::clone::Clone for LateParamRegionKind {
#[inline]
fn clone(&self) -> LateParamRegionKind {
let _: ::core::clone::AssertParamIsClone<u32>;
let _: ::core::clone::AssertParamIsClone<Symbol>;
let _: ::core::clone::AssertParamIsClone<DefId>;
*self
}
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for LateParamRegionKind {
#[inline]
fn eq(&self, other: &LateParamRegionKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(LateParamRegionKind::Anon(__self_0),
LateParamRegionKind::Anon(__arg1_0)) =>
__self_0 == __arg1_0,
(LateParamRegionKind::NamedAnon(__self_0, __self_1),
LateParamRegionKind::NamedAnon(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(LateParamRegionKind::Named(__self_0),
LateParamRegionKind::Named(__arg1_0)) =>
__self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for LateParamRegionKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<u32>;
let _: ::core::cmp::AssertParamIsEq<Symbol>;
let _: ::core::cmp::AssertParamIsEq<DefId>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for LateParamRegionKind {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
LateParamRegionKind::Anon(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
LateParamRegionKind::NamedAnon(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
LateParamRegionKind::Named(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for LateParamRegionKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
LateParamRegionKind::Anon(ref __binding_0) => { 0usize }
LateParamRegionKind::NamedAnon(ref __binding_0,
ref __binding_1) => {
1usize
}
LateParamRegionKind::Named(ref __binding_0) => { 2usize }
LateParamRegionKind::ClosureEnv => { 3usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
LateParamRegionKind::Anon(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
LateParamRegionKind::NamedAnon(ref __binding_0,
ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
LateParamRegionKind::Named(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
LateParamRegionKind::ClosureEnv => {}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for LateParamRegionKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
LateParamRegionKind::Anon(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
LateParamRegionKind::NamedAnon(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
2usize => {
LateParamRegionKind::Named(::rustc_serialize::Decodable::decode(__decoder))
}
3usize => { LateParamRegionKind::ClosureEnv }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `LateParamRegionKind`, expected 0..4, actual {0}",
n));
}
}
}
}
};TyDecodable, #[automatically_derived]
impl ::core::marker::Copy for LateParamRegionKind { }Copy)]
181#[derive(const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for
LateParamRegionKind {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
LateParamRegionKind::Anon(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
LateParamRegionKind::NamedAnon(ref __binding_0,
ref __binding_1) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
LateParamRegionKind::Named(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
LateParamRegionKind::ClosureEnv => {}
}
}
}
};StableHash)]
182pub enum LateParamRegionKind {
183 Anon(u32),
191
192 NamedAnon(u32, Symbol),
196
197 Named(DefId),
199
200 ClosureEnv,
203}
204
205impl LateParamRegionKind {
206 pub fn from_bound(var: BoundVar, br: ty::BoundRegionKind<'_>) -> LateParamRegionKind {
207 match br {
208 ty::BoundRegionKind::Anon => LateParamRegionKind::Anon(var.as_u32()),
209 ty::BoundRegionKind::Named(def_id) => LateParamRegionKind::Named(def_id),
210 ty::BoundRegionKind::ClosureEnv => LateParamRegionKind::ClosureEnv,
211 ty::BoundRegionKind::NamedForPrinting(name) => {
212 LateParamRegionKind::NamedAnon(var.as_u32(), name)
213 }
214 }
215 }
216
217 pub fn is_named(&self, tcx: TyCtxt<'_>) -> bool {
218 self.get_name(tcx).is_some()
219 }
220
221 pub fn get_name(&self, tcx: TyCtxt<'_>) -> Option<Symbol> {
222 match *self {
223 LateParamRegionKind::Named(def_id) => {
224 let name = tcx.item_name(def_id);
225 if name != kw::UnderscoreLifetime { Some(name) } else { None }
226 }
227 LateParamRegionKind::NamedAnon(_, name) => Some(name),
228 _ => None,
229 }
230 }
231
232 pub fn get_id(&self) -> Option<DefId> {
233 match *self {
234 LateParamRegionKind::Named(id) => Some(id),
235 _ => None,
236 }
237 }
238}
239
240#[cfg(target_pointer_width = "64")]
242mod size_asserts {
243 use rustc_data_structures::static_assert_size;
244
245 use super::*;
246 const _: [(); 20] = [(); ::std::mem::size_of::<RegionKind<'_>>()];static_assert_size!(RegionKind<'_>, 20);
248 const _: [(); 28] =
[(); ::std::mem::size_of::<ty::WithCachedTypeInfo<RegionKind<'_>>>()];static_assert_size!(ty::WithCachedTypeInfo<RegionKind<'_>>, 28);
249 }