1use std::cell::RefCell;
2use std::hash::{Hash, Hasher};
3use std::ops::Range;
4use std::str;
5
6use rustc_abi::{FIRST_VARIANT, FieldIdx, ReprOptions, VariantIdx};
7use rustc_data_structures::fingerprint::Fingerprint;
8use rustc_data_structures::fx::FxHashMap;
9use rustc_data_structures::intern::Interned;
10use rustc_data_structures::stable_hash::{
11 StableHash, StableHashControls, StableHashCtxt, StableHasher,
12};
13use rustc_errors::ErrorGuaranteed;
14use rustc_hir::attrs::lang_items::LangItem;
15use rustc_hir::def::{CtorKind, DefKind, Res};
16use rustc_hir::def_id::DefId;
17use rustc_hir::{self as hir, find_attr};
18use rustc_index::{IndexSlice, IndexVec};
19use rustc_macros::{StableHash, TyDecodable, TyEncodable};
20use rustc_session::DataTypeKind;
21use rustc_span::sym;
22use rustc_type_ir::FieldInfo;
23use rustc_type_ir::solve::AdtDestructorKind;
24use tracing::{debug, info, trace};
25
26use super::{
27 AsyncDestructor, Destructor, FieldDef, GenericClauses, Ty, TyCtxt, VariantDef, VariantDiscr,
28};
29use crate::mir::interpret::ErrorHandled;
30use crate::ty::util::{Discr, IntTypeExt};
31use crate::ty::{self, ConstKind};
32
33#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for AdtFlags { }
#[automatically_derived]
impl ::core::clone::Clone for AdtFlags {
#[inline]
fn clone(&self) -> AdtFlags {
let _: ::core::clone::AssertParamIsClone<u16>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AdtFlags { }Copy, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for AdtFlags { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AdtFlags {
#[inline]
fn eq(&self, other: &AdtFlags) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for AdtFlags {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<u16>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for AdtFlags {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for AdtFlags {
#[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 {
AdtFlags(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for AdtFlags {
fn encode(&self, __encoder: &mut __E) {
let AdtFlags(ref __binding_0) = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for AdtFlags {
fn decode(__decoder: &mut __D) -> Self {
AdtFlags(::rustc_serialize::Decodable::decode(__decoder))
}
}
};TyDecodable)]
34pub struct AdtFlags(u16);
35impl AdtFlags {
#[allow(deprecated, non_upper_case_globals,)]
pub const NO_ADT_FLAGS: Self = Self::from_bits_retain(0);
#[doc = r" Indicates whether the ADT is an enum."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_ENUM: Self = Self::from_bits_retain(1 << 0);
#[doc = r" Indicates whether the ADT is a union."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_UNION: Self = Self::from_bits_retain(1 << 1);
#[doc = r" Indicates whether the ADT is a struct."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_STRUCT: Self = Self::from_bits_retain(1 << 2);
#[doc = r" Indicates whether the ADT is a struct and has a constructor."]
#[allow(deprecated, non_upper_case_globals,)]
pub const HAS_CTOR: Self = Self::from_bits_retain(1 << 3);
#[doc = r" Indicates whether the type is `PhantomData`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_PHANTOM_DATA: Self = Self::from_bits_retain(1 << 4);
#[doc = r" Indicates whether the type has a `#[fundamental]` attribute."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_FUNDAMENTAL: Self = Self::from_bits_retain(1 << 5);
#[doc = r" Indicates whether the type is `Box`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_BOX: Self = Self::from_bits_retain(1 << 6);
#[doc = r" Indicates whether the type is `ManuallyDrop`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_MANUALLY_DROP: Self = Self::from_bits_retain(1 << 7);
#[doc =
r" Indicates whether the variant list of this ADT is `#[non_exhaustive]`."]
#[doc = r" (i.e., this flag is never set unless this ADT is an enum)."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_VARIANT_LIST_NON_EXHAUSTIVE: Self =
Self::from_bits_retain(1 << 8);
#[doc = r" Indicates whether the type is `UnsafeCell`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_UNSAFE_CELL: Self = Self::from_bits_retain(1 << 9);
#[doc = r" Indicates whether the type is `UnsafePinned`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_UNSAFE_PINNED: Self = Self::from_bits_retain(1 << 10);
#[doc = r" Indicates whether the type is `Pin`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_PIN: Self = Self::from_bits_retain(1 << 11);
#[doc = r" Indicates whether the type is `#[pin_project]`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_PIN_PROJECT: Self = Self::from_bits_retain(1 << 12);
#[doc = r" Indicates whether the type is `FieldRepresentingType`."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_FIELD_REPRESENTING_TYPE: Self =
Self::from_bits_retain(1 << 13);
#[doc = r" Indicates whether the type is `MaybeDangling<_>`."]
#[doc =
r#" Note that this is not the only type with "maybe dangling" semantics!"#]
#[doc = r" Use `ty.is_like_maybe_dangling()` to check for that."]
#[allow(deprecated, non_upper_case_globals,)]
pub const IS_MAYBE_DANGLING: Self = Self::from_bits_retain(1 << 14);
}
impl ::bitflags::Flags for AdtFlags {
const FLAGS: &'static [::bitflags::Flag<AdtFlags>] =
&[{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("NO_ADT_FLAGS",
AdtFlags::NO_ADT_FLAGS)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_ENUM", AdtFlags::IS_ENUM)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_UNION", AdtFlags::IS_UNION)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_STRUCT", AdtFlags::IS_STRUCT)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("HAS_CTOR", AdtFlags::HAS_CTOR)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_PHANTOM_DATA",
AdtFlags::IS_PHANTOM_DATA)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_FUNDAMENTAL",
AdtFlags::IS_FUNDAMENTAL)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_BOX", AdtFlags::IS_BOX)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_MANUALLY_DROP",
AdtFlags::IS_MANUALLY_DROP)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_VARIANT_LIST_NON_EXHAUSTIVE",
AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_UNSAFE_CELL",
AdtFlags::IS_UNSAFE_CELL)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_UNSAFE_PINNED",
AdtFlags::IS_UNSAFE_PINNED)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_PIN", AdtFlags::IS_PIN)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_PIN_PROJECT",
AdtFlags::IS_PIN_PROJECT)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_FIELD_REPRESENTING_TYPE",
AdtFlags::IS_FIELD_REPRESENTING_TYPE)
},
{
#[allow(deprecated, non_upper_case_globals,)]
::bitflags::Flag::new("IS_MAYBE_DANGLING",
AdtFlags::IS_MAYBE_DANGLING)
}];
type Bits = u16;
fn bits(&self) -> u16 { AdtFlags::bits(self) }
fn from_bits_retain(bits: u16) -> AdtFlags {
AdtFlags::from_bits_retain(bits)
}
}
#[allow(dead_code, deprecated, unused_doc_comments, unused_attributes,
unused_mut, unused_imports, non_upper_case_globals, clippy ::
assign_op_pattern, clippy :: iter_without_into_iter,)]
const _: () =
{
#[allow(dead_code, deprecated, unused_attributes)]
impl AdtFlags {
#[inline]
pub const fn empty() -> Self {
Self(<u16 as ::bitflags::Bits>::EMPTY)
}
#[inline]
pub const fn all() -> Self {
let mut truncated = <u16 as ::bitflags::Bits>::EMPTY;
let mut i = 0;
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
{
{
let flag =
<AdtFlags as ::bitflags::Flags>::FLAGS[i].value().bits();
truncated = truncated | flag;
i += 1;
}
};
let _ = i;
Self(truncated)
}
#[inline]
pub const fn bits(&self) -> u16 { self.0 }
#[inline]
pub const fn from_bits(bits: u16)
-> ::bitflags::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).0;
if truncated == bits {
::bitflags::__private::core::option::Option::Some(Self(bits))
} else { ::bitflags::__private::core::option::Option::None }
}
#[inline]
pub const fn from_bits_truncate(bits: u16) -> Self {
Self(bits & Self::all().0)
}
#[inline]
pub const fn from_bits_retain(bits: u16) -> Self { Self(bits) }
#[inline]
pub fn from_name(name: &str)
-> ::bitflags::__private::core::option::Option<Self> {
{
if name == "NO_ADT_FLAGS" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::NO_ADT_FLAGS.bits()));
}
};
;
{
if name == "IS_ENUM" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_ENUM.bits()));
}
};
;
{
if name == "IS_UNION" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_UNION.bits()));
}
};
;
{
if name == "IS_STRUCT" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_STRUCT.bits()));
}
};
;
{
if name == "HAS_CTOR" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::HAS_CTOR.bits()));
}
};
;
{
if name == "IS_PHANTOM_DATA" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_PHANTOM_DATA.bits()));
}
};
;
{
if name == "IS_FUNDAMENTAL" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_FUNDAMENTAL.bits()));
}
};
;
{
if name == "IS_BOX" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_BOX.bits()));
}
};
;
{
if name == "IS_MANUALLY_DROP" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_MANUALLY_DROP.bits()));
}
};
;
{
if name == "IS_VARIANT_LIST_NON_EXHAUSTIVE" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE.bits()));
}
};
;
{
if name == "IS_UNSAFE_CELL" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_UNSAFE_CELL.bits()));
}
};
;
{
if name == "IS_UNSAFE_PINNED" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_UNSAFE_PINNED.bits()));
}
};
;
{
if name == "IS_PIN" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_PIN.bits()));
}
};
;
{
if name == "IS_PIN_PROJECT" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_PIN_PROJECT.bits()));
}
};
;
{
if name == "IS_FIELD_REPRESENTING_TYPE" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_FIELD_REPRESENTING_TYPE.bits()));
}
};
;
{
if name == "IS_MAYBE_DANGLING" {
return ::bitflags::__private::core::option::Option::Some(Self(AdtFlags::IS_MAYBE_DANGLING.bits()));
}
};
;
let _ = name;
::bitflags::__private::core::option::Option::None
}
#[inline]
pub const fn is_empty(&self) -> bool {
self.0 == <u16 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn is_all(&self) -> bool {
Self::all().0 | self.0 == self.0
}
#[inline]
pub const fn intersects(&self, other: Self) -> bool {
self.0 & other.0 != <u16 as ::bitflags::Bits>::EMPTY
}
#[inline]
pub const fn contains(&self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[inline]
pub fn insert(&mut self, other: Self) {
*self = Self(self.0).union(other);
}
#[inline]
pub fn remove(&mut self, other: Self) {
*self = Self(self.0).difference(other);
}
#[inline]
pub fn toggle(&mut self, other: Self) {
*self = Self(self.0).symmetric_difference(other);
}
#[inline]
pub fn set(&mut self, other: Self, value: bool) {
if value { self.insert(other); } else { self.remove(other); }
}
#[inline]
#[must_use]
pub const fn intersection(self, other: Self) -> Self {
Self(self.0 & other.0)
}
#[inline]
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[inline]
#[must_use]
pub const fn difference(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
#[inline]
#[must_use]
pub const fn symmetric_difference(self, other: Self) -> Self {
Self(self.0 ^ other.0)
}
#[inline]
#[must_use]
pub const fn complement(self) -> Self {
Self::from_bits_truncate(!self.0)
}
}
impl ::bitflags::__private::core::fmt::Binary for AdtFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Binary::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::Octal for AdtFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::Octal::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::LowerHex for AdtFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::LowerHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::fmt::UpperHex for AdtFlags {
fn fmt(&self, f: &mut ::bitflags::__private::core::fmt::Formatter)
-> ::bitflags::__private::core::fmt::Result {
let inner = self.0;
::bitflags::__private::core::fmt::UpperHex::fmt(&inner, f)
}
}
impl ::bitflags::__private::core::ops::BitOr for AdtFlags {
type Output = Self;
#[inline]
fn bitor(self, other: AdtFlags) -> Self { self.union(other) }
}
impl ::bitflags::__private::core::ops::BitOrAssign for AdtFlags {
#[inline]
fn bitor_assign(&mut self, other: Self) { self.insert(other); }
}
impl ::bitflags::__private::core::ops::BitXor for AdtFlags {
type Output = Self;
#[inline]
fn bitxor(self, other: Self) -> Self {
self.symmetric_difference(other)
}
}
impl ::bitflags::__private::core::ops::BitXorAssign for AdtFlags {
#[inline]
fn bitxor_assign(&mut self, other: Self) { self.toggle(other); }
}
impl ::bitflags::__private::core::ops::BitAnd for AdtFlags {
type Output = Self;
#[inline]
fn bitand(self, other: Self) -> Self { self.intersection(other) }
}
impl ::bitflags::__private::core::ops::BitAndAssign for AdtFlags {
#[inline]
fn bitand_assign(&mut self, other: Self) {
*self =
Self::from_bits_retain(self.bits()).intersection(other);
}
}
impl ::bitflags::__private::core::ops::Sub for AdtFlags {
type Output = Self;
#[inline]
fn sub(self, other: Self) -> Self { self.difference(other) }
}
impl ::bitflags::__private::core::ops::SubAssign for AdtFlags {
#[inline]
fn sub_assign(&mut self, other: Self) { self.remove(other); }
}
impl ::bitflags::__private::core::ops::Not for AdtFlags {
type Output = Self;
#[inline]
fn not(self) -> Self { self.complement() }
}
impl ::bitflags::__private::core::iter::Extend<AdtFlags> for AdtFlags
{
fn extend<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(&mut self, iterator: T) {
for item in iterator { self.insert(item) }
}
}
impl ::bitflags::__private::core::iter::FromIterator<AdtFlags> for
AdtFlags {
fn from_iter<T: ::bitflags::__private::core::iter::IntoIterator<Item
= Self>>(iterator: T) -> Self {
use ::bitflags::__private::core::iter::Extend;
let mut result = Self::empty();
result.extend(iterator);
result
}
}
impl AdtFlags {
#[inline]
pub const fn iter(&self) -> ::bitflags::iter::Iter<AdtFlags> {
::bitflags::iter::Iter::__private_const_new(<AdtFlags as
::bitflags::Flags>::FLAGS,
AdtFlags::from_bits_retain(self.bits()),
AdtFlags::from_bits_retain(self.bits()))
}
#[inline]
pub const fn iter_names(&self)
-> ::bitflags::iter::IterNames<AdtFlags> {
::bitflags::iter::IterNames::__private_const_new(<AdtFlags as
::bitflags::Flags>::FLAGS,
AdtFlags::from_bits_retain(self.bits()),
AdtFlags::from_bits_retain(self.bits()))
}
}
impl ::bitflags::__private::core::iter::IntoIterator for AdtFlags {
type Item = AdtFlags;
type IntoIter = ::bitflags::iter::Iter<AdtFlags>;
fn into_iter(self) -> Self::IntoIter { self.iter() }
}
};bitflags::bitflags! {
36 impl AdtFlags: u16 {
37 const NO_ADT_FLAGS = 0;
38 const IS_ENUM = 1 << 0;
40 const IS_UNION = 1 << 1;
42 const IS_STRUCT = 1 << 2;
44 const HAS_CTOR = 1 << 3;
46 const IS_PHANTOM_DATA = 1 << 4;
48 const IS_FUNDAMENTAL = 1 << 5;
50 const IS_BOX = 1 << 6;
52 const IS_MANUALLY_DROP = 1 << 7;
54 const IS_VARIANT_LIST_NON_EXHAUSTIVE = 1 << 8;
57 const IS_UNSAFE_CELL = 1 << 9;
59 const IS_UNSAFE_PINNED = 1 << 10;
61 const IS_PIN = 1 << 11;
63 const IS_PIN_PROJECT = 1 << 12;
65 const IS_FIELD_REPRESENTING_TYPE = 1 << 13;
67 const IS_MAYBE_DANGLING = 1 << 14;
71 }
72}
73impl ::std::fmt::Debug for AdtFlags {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
::bitflags::parser::to_writer(self, f)
}
}rustc_data_structures::external_bitflags_debug! { AdtFlags }
74
75#[derive(const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for AdtDefData {
fn encode(&self, __encoder: &mut __E) {
let AdtDefData {
did: ref __binding_0,
variants: ref __binding_1,
flags: ref __binding_2,
repr: ref __binding_3 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for AdtDefData {
fn decode(__decoder: &mut __D) -> Self {
AdtDefData {
did: ::rustc_serialize::Decodable::decode(__decoder),
variants: ::rustc_serialize::Decodable::decode(__decoder),
flags: ::rustc_serialize::Decodable::decode(__decoder),
repr: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
109pub struct AdtDefData {
110 pub did: DefId,
112 variants: IndexVec<VariantIdx, VariantDef>,
114 flags: AdtFlags,
116 repr: ReprOptions,
118}
119
120impl PartialEq for AdtDefData {
121 #[inline]
122 fn eq(&self, other: &Self) -> bool {
123 let Self { did: self_def_id, variants: _, flags: _, repr: _ } = self;
131 let Self { did: other_def_id, variants: _, flags: _, repr: _ } = other;
132
133 let res = self_def_id == other_def_id;
134
135 if truecfg!(debug_assertions) && res {
137 let deep = self.flags == other.flags
138 && self.repr == other.repr
139 && self.variants == other.variants;
140 if !deep {
{
::core::panicking::panic_fmt(format_args!("AdtDefData for the same def-id has differing data"));
}
};assert!(deep, "AdtDefData for the same def-id has differing data");
141 }
142
143 res
144 }
145}
146
147impl Eq for AdtDefData {}
148
149impl Hash for AdtDefData {
152 #[inline]
153 fn hash<H: Hasher>(&self, s: &mut H) {
154 self.did.hash(s)
155 }
156}
157
158impl StableHash for AdtDefData {
159 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
160 const CACHE:
::std::thread::LocalKey<RefCell<FxHashMap<(usize, StableHashControls),
Fingerprint>>> =
{
#[inline]
fn __rust_std_internal_init_fn()
-> RefCell<FxHashMap<(usize, StableHashControls), Fingerprint>> {
Default::default()
}
unsafe {
::std::thread::LocalKey::new(const {
if ::std::mem::needs_drop::<RefCell<FxHashMap<(usize,
StableHashControls), Fingerprint>>>() {
|__rust_std_internal_init|
{
#[thread_local]
static __RUST_STD_INTERNAL_VAL:
::std::thread::local_impl::LazyStorage<RefCell<FxHashMap<(usize,
StableHashControls), Fingerprint>>, ()> =
::std::thread::local_impl::LazyStorage::new();
__RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init,
__rust_std_internal_init_fn)
}
} else {
|__rust_std_internal_init|
{
#[thread_local]
static __RUST_STD_INTERNAL_VAL:
::std::thread::local_impl::LazyStorage<RefCell<FxHashMap<(usize,
StableHashControls), Fingerprint>>, !> =
::std::thread::local_impl::LazyStorage::new();
__RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init,
__rust_std_internal_init_fn)
}
}
})
}
};thread_local! {
161 static CACHE: RefCell<FxHashMap<(usize, StableHashControls), Fingerprint>> = Default::default();
162 }
163
164 let hash: Fingerprint = CACHE.with(|cache| {
165 let addr = self as *const AdtDefData as usize;
166 let stable_hash_controls = hcx.stable_hash_controls();
167 *cache.borrow_mut().entry((addr, stable_hash_controls)).or_insert_with(|| {
168 let ty::AdtDefData { did, ref variants, ref flags, ref repr } = *self;
169
170 let mut hasher = StableHasher::new();
171 did.stable_hash(hcx, &mut hasher);
172 variants.stable_hash(hcx, &mut hasher);
173 flags.stable_hash(hcx, &mut hasher);
174 repr.stable_hash(hcx, &mut hasher);
175
176 hasher.finish()
177 })
178 });
179
180 hash.stable_hash(hcx, hasher);
181 }
182}
183
184#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for AdtDef<'tcx> { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for AdtDef<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::clone::Clone for AdtDef<'tcx> {
#[inline]
fn clone(&self) -> AdtDef<'tcx> {
let _: ::core::clone::AssertParamIsClone<Interned<'tcx, AdtDefData>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::StructuralPartialEq for AdtDef<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for AdtDef<'tcx> {
#[inline]
fn eq(&self, other: &AdtDef<'tcx>) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for AdtDef<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Interned<'tcx, AdtDefData>>;
}
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for AdtDef<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
AdtDef<'tcx> {
#[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 {
AdtDef(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
185#[rustc_pass_by_value]
186pub struct AdtDef<'tcx>(pub Interned<'tcx, AdtDefData>);
187
188impl<'tcx> AdtDef<'tcx> {
189 #[inline]
190 pub fn did(self) -> DefId {
191 self.0.0.did
192 }
193
194 #[inline]
195 pub fn variants(self) -> &'tcx IndexSlice<VariantIdx, VariantDef> {
196 &self.0.0.variants
197 }
198
199 #[inline]
200 pub fn variant(self, idx: VariantIdx) -> &'tcx VariantDef {
201 &self.0.0.variants[idx]
202 }
203
204 #[inline]
205 pub fn flags(self) -> AdtFlags {
206 self.0.0.flags
207 }
208
209 #[inline]
210 pub fn repr(self) -> ReprOptions {
211 self.0.0.repr
212 }
213
214 pub fn field_representing_type_info(
215 self,
216 tcx: TyCtxt<'tcx>,
217 args: ty::GenericArgsRef<'tcx>,
218 ) -> Option<FieldInfo<TyCtxt<'tcx>>> {
219 if !self.is_field_representing_type() {
220 return None;
221 }
222 let base = args.type_at(0);
223 let variant_idx = match args.const_at(1).kind() {
224 ConstKind::Value(v) => VariantIdx::from_u32(v.to_leaf().to_u32()),
225 _ => return None,
226 };
227 let field_idx = match args.const_at(2).kind() {
228 ConstKind::Value(v) => FieldIdx::from_u32(v.to_leaf().to_u32()),
229 _ => return None,
230 };
231 let (ty, variant, name) = match base.kind() {
232 ty::Adt(base_def, base_args) => {
233 let variant = base_def.variant(variant_idx);
234 let field = &variant.fields[field_idx];
235 let ty = field.ty(tcx, base_args).skip_norm_wip();
236 (ty, base_def.is_enum().then_some(variant.name), field.name)
237 }
238 ty::Tuple(tys) => {
239 if variant_idx != FIRST_VARIANT {
240 crate::util::bug::bug_fmt(format_args!("expected variant of tuple to be FIRST_VARIANT, but found {0:?}",
variant_idx))bug!("expected variant of tuple to be FIRST_VARIANT, but found {variant_idx:?}")
241 }
242 (
243 if let Some(ty) = tys.get(field_idx.index()) {
244 *ty
245 } else {
246 crate::util::bug::bug_fmt(format_args!("expected valid tuple index, but got {1:?}, tuple length: {0}",
tys.len(), field_idx))bug!(
247 "expected valid tuple index, but got {field_idx:?}, tuple length: {}",
248 tys.len()
249 )
250 },
251 None,
252 sym::integer(field_idx.index()),
253 )
254 }
255 _ => ::core::panicking::panic("explicit panic")panic!(),
256 };
257 Some(FieldInfo { base, ty, variant, variant_idx, name, field_idx })
258 }
259}
260
261impl<'tcx> rustc_type_ir::inherent::AdtDef<TyCtxt<'tcx>> for AdtDef<'tcx> {
262 fn def_id(self) -> DefId {
263 self.did()
264 }
265
266 fn is_struct(self) -> bool {
267 self.is_struct()
268 }
269
270 fn is_packed(self) -> bool {
271 self.repr().packed()
272 }
273
274 fn struct_tail_ty(self, interner: TyCtxt<'tcx>) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
275 Some(interner.type_of(self.non_enum_variant().tail_opt()?.did))
276 }
277
278 fn is_phantom_data(self) -> bool {
279 self.is_phantom_data()
280 }
281
282 fn is_manually_drop(self) -> bool {
283 self.is_manually_drop()
284 }
285
286 fn field_representing_type_info(
287 self,
288 tcx: TyCtxt<'tcx>,
289 args: ty::GenericArgsRef<'tcx>,
290 ) -> Option<FieldInfo<TyCtxt<'tcx>>> {
291 self.field_representing_type_info(tcx, args)
292 }
293
294 fn all_field_tys(
295 self,
296 tcx: TyCtxt<'tcx>,
297 ) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = Ty<'tcx>>> {
298 ty::EarlyBinder::bind_iter(
299 self.all_fields().map(move |field| tcx.type_of(field.did).skip_binder()),
300 )
301 }
302
303 fn sizedness_constraint(
304 self,
305 tcx: TyCtxt<'tcx>,
306 sizedness: ty::SizedTraitKind,
307 ) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
308 self.sizedness_constraint(tcx, sizedness)
309 }
310
311 fn is_fundamental(self) -> bool {
312 self.is_fundamental()
313 }
314
315 fn destructor(self, tcx: TyCtxt<'tcx>) -> Option<AdtDestructorKind> {
316 Some(match tcx.constness(self.destructor(tcx)?.did) {
317 hir::Constness::Const { always: true } => {
::core::panicking::panic_fmt(format_args!("not implemented: {0}",
format_args!("FIXME(comptime)")));
}unimplemented!("FIXME(comptime)"),
318 hir::Constness::Const { always: false } => AdtDestructorKind::Const,
319 hir::Constness::NotConst => AdtDestructorKind::NotConst,
320 })
321 }
322}
323
324#[derive(#[automatically_derived]
impl ::core::marker::Copy for AdtKind { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for AdtKind { }
#[automatically_derived]
impl ::core::clone::Clone for AdtKind {
#[inline]
fn clone(&self) -> AdtKind { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AdtKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
AdtKind::Struct => "Struct",
AdtKind::Union => "Union",
AdtKind::Enum => "Enum",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for AdtKind { }Eq, #[automatically_derived]
impl ::core::marker::StructuralPartialEq for AdtKind { }
#[automatically_derived]
impl ::core::cmp::PartialEq for AdtKind {
#[inline]
fn eq(&self, other: &AdtKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for AdtKind {
#[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 {
AdtKind::Struct => {}
AdtKind::Union => {}
AdtKind::Enum => {}
}
}
}
};StableHash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for AdtKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
AdtKind::Struct => { 0usize }
AdtKind::Union => { 1usize }
AdtKind::Enum => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for AdtKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { AdtKind::Struct }
1usize => { AdtKind::Union }
2usize => { AdtKind::Enum }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AdtKind`, expected 0..3, actual {0}",
n));
}
}
}
}
};TyDecodable)]
325pub enum AdtKind {
326 Struct,
327 Union,
328 Enum,
329}
330
331impl From<AdtKind> for DataTypeKind {
332 fn from(val: AdtKind) -> Self {
333 match val {
334 AdtKind::Struct => DataTypeKind::Struct,
335 AdtKind::Union => DataTypeKind::Union,
336 AdtKind::Enum => DataTypeKind::Enum,
337 }
338 }
339}
340
341impl AdtKind {
342 pub fn article(self) -> &'static str {
343 match self {
344 AdtKind::Struct => "a",
345 AdtKind::Union => "a",
347 AdtKind::Enum => "an",
348 }
349 }
350}
351
352impl AdtDefData {
353 pub(super) fn new(
355 tcx: TyCtxt<'_>,
356 did: DefId,
357 kind: AdtKind,
358 variants: IndexVec<VariantIdx, VariantDef>,
359 repr: ReprOptions,
360 ) -> Self {
361 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs:361",
"rustc_middle::ty::adt", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs"),
::tracing_core::__macro_support::Option::Some(361u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::adt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("AdtDef::new({0:?}, {1:?}, {2:?}, {3:?})",
did, kind, variants, repr) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("AdtDef::new({:?}, {:?}, {:?}, {:?})", did, kind, variants, repr);
362 let mut flags = AdtFlags::NO_ADT_FLAGS;
363
364 if kind == AdtKind::Enum && {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(did, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(NonExhaustive(..)) => {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(tcx, did, NonExhaustive(..)) {
365 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs:365",
"rustc_middle::ty::adt", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs"),
::tracing_core::__macro_support::Option::Some(365u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::adt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("found non-exhaustive variant list for {0:?}",
did) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("found non-exhaustive variant list for {:?}", did);
366 flags = flags | AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE;
367 }
368 if {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(did, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(PinV2(..)) => {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(tcx, did, PinV2(..)) {
369 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs:369",
"rustc_middle::ty::adt", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs"),
::tracing_core::__macro_support::Option::Some(369u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::adt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("found pin-project type {0:?}",
did) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("found pin-project type {:?}", did);
370 flags |= AdtFlags::IS_PIN_PROJECT;
371 }
372
373 flags |= match kind {
374 AdtKind::Enum => AdtFlags::IS_ENUM,
375 AdtKind::Union => AdtFlags::IS_UNION,
376 AdtKind::Struct => AdtFlags::IS_STRUCT,
377 };
378
379 if kind == AdtKind::Struct && variants[FIRST_VARIANT].ctor.is_some() {
380 flags |= AdtFlags::HAS_CTOR;
381 }
382
383 if {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(did, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(Fundamental) => {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(tcx, did, Fundamental) {
384 flags |= AdtFlags::IS_FUNDAMENTAL;
385 }
386 if tcx.is_lang_item(did, LangItem::PhantomData) {
387 flags |= AdtFlags::IS_PHANTOM_DATA;
388 }
389 if tcx.is_lang_item(did, LangItem::OwnedBox) {
390 flags |= AdtFlags::IS_BOX;
391 }
392 if tcx.is_lang_item(did, LangItem::ManuallyDrop) {
393 flags |= AdtFlags::IS_MANUALLY_DROP;
394 }
395 if tcx.is_lang_item(did, LangItem::MaybeDangling) {
396 flags |= AdtFlags::IS_MAYBE_DANGLING;
397 }
398 if tcx.is_lang_item(did, LangItem::UnsafeCell) {
399 flags |= AdtFlags::IS_UNSAFE_CELL;
400 }
401 if tcx.is_lang_item(did, LangItem::UnsafePinned) {
402 flags |= AdtFlags::IS_UNSAFE_PINNED;
403 }
404 if tcx.is_lang_item(did, LangItem::Pin) {
405 flags |= AdtFlags::IS_PIN;
406 }
407 if tcx.is_lang_item(did, LangItem::FieldRepresentingType) {
408 flags |= AdtFlags::IS_FIELD_REPRESENTING_TYPE;
409 }
410
411 AdtDefData { did, variants, flags, repr }
412 }
413}
414
415impl<'tcx> AdtDef<'tcx> {
416 #[inline]
418 pub fn is_struct(self) -> bool {
419 self.flags().contains(AdtFlags::IS_STRUCT)
420 }
421
422 #[inline]
424 pub fn is_union(self) -> bool {
425 self.flags().contains(AdtFlags::IS_UNION)
426 }
427
428 #[inline]
430 pub fn is_enum(self) -> bool {
431 self.flags().contains(AdtFlags::IS_ENUM)
432 }
433
434 #[inline]
440 pub fn is_variant_list_non_exhaustive(self) -> bool {
441 self.flags().contains(AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE)
442 }
443
444 #[inline]
447 pub fn variant_list_has_applicable_non_exhaustive(self) -> bool {
448 self.is_variant_list_non_exhaustive() && !self.did().is_local()
449 }
450
451 #[inline]
453 pub fn adt_kind(self) -> AdtKind {
454 if self.is_enum() {
455 AdtKind::Enum
456 } else if self.is_union() {
457 AdtKind::Union
458 } else {
459 AdtKind::Struct
460 }
461 }
462
463 pub fn descr(self) -> &'static str {
465 match self.adt_kind() {
466 AdtKind::Struct => "struct",
467 AdtKind::Union => "union",
468 AdtKind::Enum => "enum",
469 }
470 }
471
472 pub fn article(self) -> &'static str {
474 match self.adt_kind() {
475 AdtKind::Struct | AdtKind::Union => "a",
476 AdtKind::Enum => "an",
477 }
478 }
479
480 #[inline]
482 pub fn variant_descr(self) -> &'static str {
483 match self.adt_kind() {
484 AdtKind::Struct => "struct",
485 AdtKind::Union => "union",
486 AdtKind::Enum => "variant",
487 }
488 }
489
490 #[inline]
492 pub fn has_ctor(self) -> bool {
493 self.flags().contains(AdtFlags::HAS_CTOR)
494 }
495
496 #[inline]
499 pub fn is_fundamental(self) -> bool {
500 self.flags().contains(AdtFlags::IS_FUNDAMENTAL)
501 }
502
503 #[inline]
505 pub fn is_phantom_data(self) -> bool {
506 self.flags().contains(AdtFlags::IS_PHANTOM_DATA)
507 }
508
509 #[inline]
511 pub fn is_box(self) -> bool {
512 self.flags().contains(AdtFlags::IS_BOX)
513 }
514
515 #[inline]
517 pub fn is_unsafe_cell(self) -> bool {
518 self.flags().contains(AdtFlags::IS_UNSAFE_CELL)
519 }
520
521 #[inline]
523 pub fn is_unsafe_pinned(self) -> bool {
524 self.flags().contains(AdtFlags::IS_UNSAFE_PINNED)
525 }
526
527 #[inline]
529 pub fn is_manually_drop(self) -> bool {
530 self.flags().contains(AdtFlags::IS_MANUALLY_DROP)
531 }
532
533 #[inline]
535 pub fn is_pin(self) -> bool {
536 self.flags().contains(AdtFlags::IS_PIN)
537 }
538
539 #[inline]
542 pub fn is_pin_project(self) -> bool {
543 self.flags().contains(AdtFlags::IS_PIN_PROJECT)
544 }
545
546 pub fn is_field_representing_type(self) -> bool {
547 self.flags().contains(AdtFlags::IS_FIELD_REPRESENTING_TYPE)
548 }
549
550 pub fn has_dtor(self, tcx: TyCtxt<'tcx>) -> bool {
552 self.destructor(tcx).is_some()
553 }
554
555 pub fn non_enum_variant(self) -> &'tcx VariantDef {
557 if !(self.is_struct() || self.is_union()) {
::core::panicking::panic("assertion failed: self.is_struct() || self.is_union()")
};assert!(self.is_struct() || self.is_union());
558 self.variant(FIRST_VARIANT)
559 }
560
561 #[inline]
562 pub fn clauses(self, tcx: TyCtxt<'tcx>) -> GenericClauses<'tcx> {
563 tcx.clauses_of(self.did())
564 }
565
566 #[inline]
569 pub fn all_fields(self) -> impl Iterator<Item = &'tcx FieldDef> + Clone {
570 self.variants().iter().flat_map(|v| v.fields.iter())
571 }
572
573 pub fn is_payloadfree(self) -> bool {
576 if self.variants().iter().any(|v| {
586 #[allow(non_exhaustive_omitted_patterns)] match v.discr {
VariantDiscr::Explicit(_) => true,
_ => false,
}matches!(v.discr, VariantDiscr::Explicit(_)) && v.ctor_kind() != Some(CtorKind::Const)
587 }) {
588 return false;
589 }
590 self.variants().iter().all(|v| v.fields.is_empty())
591 }
592
593 pub fn variant_with_id(self, vid: DefId) -> &'tcx VariantDef {
595 self.variants().iter().find(|v| v.def_id == vid).expect("variant_with_id: unknown variant")
596 }
597
598 pub fn variant_with_ctor_id(self, cid: DefId) -> &'tcx VariantDef {
600 self.variants()
601 .iter()
602 .find(|v| v.ctor_def_id() == Some(cid))
603 .expect("variant_with_ctor_id: unknown variant")
604 }
605
606 #[inline]
608 pub fn variant_index_with_id(self, vid: DefId) -> VariantIdx {
609 self.variants()
610 .iter_enumerated()
611 .find(|(_, v)| v.def_id == vid)
612 .expect("variant_index_with_id: unknown variant")
613 .0
614 }
615
616 pub fn variant_index_with_ctor_id(self, cid: DefId) -> VariantIdx {
618 self.variants()
619 .iter_enumerated()
620 .find(|(_, v)| v.ctor_def_id() == Some(cid))
621 .expect("variant_index_with_ctor_id: unknown variant")
622 .0
623 }
624
625 pub fn variant_of_res(self, res: Res) -> &'tcx VariantDef {
626 match res {
627 Res::Def(DefKind::Variant, vid) => self.variant_with_id(vid),
628 Res::Def(DefKind::Ctor(..), cid) => self.variant_with_ctor_id(cid),
629 Res::Def(DefKind::Struct, _)
630 | Res::Def(DefKind::Union, _)
631 | Res::Def(DefKind::TyAlias, _)
632 | Res::Def(DefKind::AssocTy, _)
633 | Res::SelfTyParam { .. }
634 | Res::SelfTyAlias { .. }
635 | Res::SelfCtor(..) => self.non_enum_variant(),
636 _ => crate::util::bug::bug_fmt(format_args!("unexpected res {0:?} in variant_of_res",
res))bug!("unexpected res {:?} in variant_of_res", res),
637 }
638 }
639
640 #[inline]
641 pub fn eval_explicit_discr(
642 self,
643 tcx: TyCtxt<'tcx>,
644 expr_did: DefId,
645 ) -> Result<Discr<'tcx>, ErrorGuaranteed> {
646 if !self.is_enum() {
::core::panicking::panic("assertion failed: self.is_enum()")
};assert!(self.is_enum());
647
648 let repr_type = self.repr().discr_type();
649 match tcx.const_eval_poly(expr_did) {
650 Ok(val) => {
651 let typing_env = ty::TypingEnv::post_analysis(tcx, expr_did);
652 let ty = repr_type.to_ty(tcx);
653 if let Some(b) = val.try_to_bits_for_ty(tcx, typing_env, ty) {
654 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs:654",
"rustc_middle::ty::adt", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs"),
::tracing_core::__macro_support::Option::Some(654u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::adt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("discriminants: {0} ({1:?})",
b, repr_type) as &dyn ::tracing::field::Value))])
});
} else { ; }
};trace!("discriminants: {} ({:?})", b, repr_type);
655 Ok(Discr { val: b, ty })
656 } else {
657 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs:657",
"rustc_middle::ty::adt", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("/rustc-dev/215a8af4bb4c106cccf6d6535f84eaae91818265/compiler/rustc_middle/src/ty/adt.rs"),
::tracing_core::__macro_support::Option::Some(657u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::adt"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::INFO <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("invalid enum discriminant: {0:#?}",
val) as &dyn ::tracing::field::Value))])
});
} else { ; }
};info!("invalid enum discriminant: {:#?}", val);
658 let guar = tcx.dcx().emit_err(crate::diagnostics::ConstEvalNonIntError {
659 span: tcx.def_span(expr_did),
660 });
661 Err(guar)
662 }
663 }
664 Err(err) => {
665 let guar = match err {
666 ErrorHandled::Reported(info, _) => info.into(),
667 ErrorHandled::TooGeneric(..) => tcx.dcx().span_delayed_bug(
668 tcx.def_span(expr_did),
669 "enum discriminant depends on generics",
670 ),
671 };
672 Err(guar)
673 }
674 }
675 }
676
677 #[inline]
678 pub fn discriminants(
679 self,
680 tcx: TyCtxt<'tcx>,
681 ) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> {
682 if !self.is_enum() {
::core::panicking::panic("assertion failed: self.is_enum()")
};assert!(self.is_enum());
683 let repr_type = self.repr().discr_type();
684 let initial = repr_type.initial_discriminant(tcx);
685 let mut prev_discr = None::<Discr<'tcx>>;
686 self.variants().iter_enumerated().map(move |(i, v)| {
687 let mut discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
688 if let VariantDiscr::Explicit(expr_did) = v.discr
689 && let Ok(new_discr) = self.eval_explicit_discr(tcx, expr_did)
690 {
691 discr = new_discr;
692 }
693 prev_discr = Some(discr);
694
695 (i, discr)
696 })
697 }
698
699 #[inline]
700 pub fn variant_range(self) -> Range<VariantIdx> {
701 FIRST_VARIANT..self.variants().next_index()
702 }
703
704 #[inline]
710 pub fn discriminant_for_variant(
711 self,
712 tcx: TyCtxt<'tcx>,
713 variant_index: VariantIdx,
714 ) -> Discr<'tcx> {
715 if !self.is_enum() {
::core::panicking::panic("assertion failed: self.is_enum()")
};assert!(self.is_enum());
716 let (val, offset) = self.discriminant_def_for_variant(variant_index);
717 let explicit_value = if let Some(expr_did) = val
718 && let Ok(val) = self.eval_explicit_discr(tcx, expr_did)
719 {
720 val
721 } else {
722 self.repr().discr_type().initial_discriminant(tcx)
723 };
724 explicit_value.checked_add(tcx, offset as u128).0
725 }
726
727 pub fn discriminant_def_for_variant(self, variant_index: VariantIdx) -> (Option<DefId>, u32) {
731 if !!self.variants().is_empty() {
::core::panicking::panic("assertion failed: !self.variants().is_empty()")
};assert!(!self.variants().is_empty());
732 let mut explicit_index = variant_index.as_u32();
733 let expr_did;
734 loop {
735 match self.variant(VariantIdx::from_u32(explicit_index)).discr {
736 ty::VariantDiscr::Relative(0) => {
737 expr_did = None;
738 break;
739 }
740 ty::VariantDiscr::Relative(distance) => {
741 explicit_index -= distance;
742 }
743 ty::VariantDiscr::Explicit(did) => {
744 expr_did = Some(did);
745 break;
746 }
747 }
748 }
749 (expr_did, variant_index.as_u32() - explicit_index)
750 }
751
752 pub fn destructor(self, tcx: TyCtxt<'tcx>) -> Option<Destructor> {
753 tcx.adt_destructor(self.did())
754 }
755
756 pub fn async_destructor(self, tcx: TyCtxt<'tcx>) -> Option<AsyncDestructor> {
759 tcx.adt_async_destructor(self.did())
760 }
761
762 pub fn sizedness_constraint(
766 self,
767 tcx: TyCtxt<'tcx>,
768 sizedness: ty::SizedTraitKind,
769 ) -> Option<ty::EarlyBinder<'tcx, Ty<'tcx>>> {
770 if self.is_struct() { tcx.adt_sizedness_constraint((self.did(), sizedness)) } else { None }
771 }
772}