@@ -1810,12 +1810,12 @@ bitflags! {
18101810/// Definition of a variant -- a struct's fields or a enum variant.
18111811#[ derive( Debug ) ]
18121812pub struct VariantDef {
1813- /// `DefId` that identifies this enum variant. If this `VariantDef` is part of a struct or
1814- /// union then this is `None `.
1815- variant_did : Option < DefId > ,
1816- /// `DefId` that identifies this enum variant or struct 's constructor. If this is a
1817- /// `Struct`- variant then this is `None`.
1818- ctor_did : Option < DefId > ,
1813+ /// `DefId` that identifies the variant itself.
1814+ /// If this variant belongs to a struct or union, then this is a copy of its `DefId `.
1815+ pub def_id : DefId ,
1816+ /// `DefId` that identifies the variant's constructor.
1817+ /// If this variant is a struct variant, then this is `None`.
1818+ pub ctor_def_id : Option < DefId > ,
18191819 /// Variant or struct name.
18201820 pub ident : Ident ,
18211821 /// Discriminant of this variant.
@@ -1824,11 +1824,6 @@ pub struct VariantDef {
18241824 pub fields : Vec < FieldDef > ,
18251825 /// Type of constructor of variant.
18261826 pub ctor_kind : CtorKind ,
1827- /// `DefId` of the parent `AdtDef` representing the struct or enum. This is required as there
1828- /// is a valid scenario where this type represents a `Struct`-struct and both `ctor_did` and
1829- /// `variant_did` would be `None` and we would still want a way to get back to the original
1830- /// `AdtDef`.
1831- parent_did : DefId ,
18321827 /// Flags of the variant (e.g. is field list non-exhaustive)?
18331828 flags : VariantFlags ,
18341829 /// Recovered?
@@ -1856,7 +1851,7 @@ impl<'a, 'gcx, 'tcx> VariantDef {
18561851 tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
18571852 ident : Ident ,
18581853 variant_did : Option < DefId > ,
1859- ctor_did : Option < DefId > ,
1854+ ctor_def_id : Option < DefId > ,
18601855 discr : VariantDiscr ,
18611856 fields : Vec < FieldDef > ,
18621857 ctor_kind : CtorKind ,
@@ -1865,9 +1860,9 @@ impl<'a, 'gcx, 'tcx> VariantDef {
18651860 recovered : bool ,
18661861 ) -> Self {
18671862 debug ! (
1868- "VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_did = {:?}, discr = {:?},
1863+ "VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
18691864 fields = {:?}, ctor_kind = {:?}, adt_kind = {:?}, parent_did = {:?})" ,
1870- ident, variant_did, ctor_did , discr, fields, ctor_kind, adt_kind, parent_did,
1865+ ident, variant_did, ctor_def_id , discr, fields, ctor_kind, adt_kind, parent_did,
18711866 ) ;
18721867
18731868 let mut flags = VariantFlags :: NO_VARIANT_FLAGS ;
@@ -1877,14 +1872,13 @@ impl<'a, 'gcx, 'tcx> VariantDef {
18771872 }
18781873
18791874 VariantDef {
1880- variant_did,
1881- ctor_did ,
1875+ def_id : variant_did. unwrap_or ( parent_did ) ,
1876+ ctor_def_id ,
18821877 ident,
18831878 discr,
18841879 fields,
18851880 ctor_kind,
18861881 flags,
1887- parent_did,
18881882 recovered,
18891883 }
18901884 }
@@ -1894,62 +1888,16 @@ impl<'a, 'gcx, 'tcx> VariantDef {
18941888 pub fn is_field_list_non_exhaustive ( & self ) -> bool {
18951889 self . flags . intersects ( VariantFlags :: IS_FIELD_LIST_NON_EXHAUSTIVE )
18961890 }
1897-
1898- /// Returns `true` if this `VariantDef` represents a enum's variant.
1899- #[ inline]
1900- pub fn is_enum_variant ( & self ) -> bool {
1901- self . variant_did . is_some ( )
1902- }
1903-
1904- /// Returns `true` if this `VariantDef` represents a struct.
1905- #[ inline]
1906- pub fn is_struct ( & self ) -> bool {
1907- !self . is_enum_variant ( )
1908- }
1909-
1910- /// Returns the `DefId` of this variant if this `VariantDef` represents an enum's variant, or
1911- /// returns the `DefId` of the parent struct.
1912- #[ inline]
1913- pub fn variant_did_or_parent_struct_did ( & self ) -> DefId {
1914- self . variant_did . unwrap_or ( self . parent_did )
1915- }
1916-
1917- /// Returns `true` if the variant is defined in the local crate.
1918- #[ inline]
1919- pub fn is_local ( & self ) -> bool {
1920- self . variant_did_or_parent_struct_did ( ) . krate == LOCAL_CRATE
1921- }
1922-
1923- /// Returns the `DefId` of this variant if this `VariantDef` represents an enum's variant or
1924- /// panics.
1925- #[ inline]
1926- pub fn variant_did ( & self ) -> DefId {
1927- self . variant_did . expect ( "enum variant without a variant id" )
1928- }
1929-
1930- /// Returns the `DefId` of this variant's constructor if this is a unit or
1931- /// tuple-variant/struct.
1932- #[ inline]
1933- pub fn ctor_did ( & self ) -> Option < DefId > {
1934- self . ctor_did
1935- }
1936-
1937- /// Returns the `AdtDef` representing the struct or enum associated with this `VariantDef`.
1938- #[ inline]
1939- pub fn adt_def ( & self , tcx : TyCtxt < ' a , ' tcx , ' gcx > ) -> & ' tcx AdtDef {
1940- tcx. adt_def ( self . parent_did )
1941- }
19421891}
19431892
19441893impl_stable_hash_for ! ( struct VariantDef {
1945- variant_did ,
1946- ctor_did ,
1894+ def_id ,
1895+ ctor_def_id ,
19471896 ident -> ( ident. name) ,
19481897 discr,
19491898 fields,
19501899 ctor_kind,
19511900 flags,
1952- parent_did,
19531901 recovered
19541902} ) ;
19551903
@@ -2204,7 +2152,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
22042152 AdtKind :: Struct => AdtFlags :: IS_STRUCT ,
22052153 } ;
22062154
2207- if kind == AdtKind :: Struct && variants[ VariantIdx :: new ( 0 ) ] . ctor_did . is_some ( ) {
2155+ if kind == AdtKind :: Struct && variants[ VariantIdx :: new ( 0 ) ] . ctor_def_id . is_some ( ) {
22082156 flags |= AdtFlags :: HAS_CTOR ;
22092157 }
22102158
@@ -2351,51 +2299,29 @@ impl<'a, 'gcx, 'tcx> AdtDef {
23512299 self . variants . iter ( ) . all ( |v| v. fields . is_empty ( ) )
23522300 }
23532301
2354- pub fn variant_with_variant_id ( & self , vid : DefId ) -> & VariantDef {
2355- self . variants
2356- . iter ( )
2357- . find ( |v| v. variant_did . map ( |did| did == vid) . unwrap_or ( false ) )
2358- . expect ( "variant_with_variant_id: unknown variant" )
2302+ pub fn variant_with_id ( & self , vid : DefId ) -> & VariantDef {
2303+ self . variants . iter ( ) . find ( |v| v. def_id == vid)
2304+ . expect ( "variant_with_id: unknown variant" )
23592305 }
23602306
23612307 pub fn variant_with_ctor_id ( & self , cid : DefId ) -> & VariantDef {
2362- self . variants
2363- . iter ( )
2364- . find ( |v| v. ctor_did . map ( |did| did == cid) . unwrap_or ( false ) )
2308+ self . variants . iter ( ) . find ( |v| v. ctor_def_id == Some ( cid) )
23652309 . expect ( "variant_with_ctor_id: unknown variant" )
23662310 }
23672311
2368- pub fn variant_index_with_variant_id ( & self , vid : DefId ) -> VariantIdx {
2369- self . variants
2370- . iter_enumerated ( )
2371- . find ( |( _, v) | v. variant_did . map ( |did| did == vid) . unwrap_or ( false ) )
2372- . expect ( "variant_index_with_variant_id: unknown variant" )
2373- . 0
2312+ pub fn variant_index_with_id ( & self , vid : DefId ) -> VariantIdx {
2313+ self . variants . iter_enumerated ( ) . find ( |( _, v) | v. def_id == vid)
2314+ . expect ( "variant_index_with_id: unknown variant" ) . 0
23742315 }
23752316
23762317 pub fn variant_index_with_ctor_id ( & self , cid : DefId ) -> VariantIdx {
2377- self . variants
2378- . iter_enumerated ( )
2379- . find ( |( _, v) | v. ctor_did . map ( |did| did == cid) . unwrap_or ( false ) )
2380- . expect ( "variant_index_with_ctor_id: unknown variant" )
2381- . 0
2382- }
2383-
2384- pub fn variant_index_with_ctor_or_variant_id ( & self , id : DefId ) -> VariantIdx {
2385- self . variants
2386- . iter_enumerated ( )
2387- . find ( |( _, v) | {
2388- let ctor = v. ctor_did . map ( |did| did == id) ;
2389- let variant = v. variant_did . map ( |did| did == id) ;
2390- ctor. or ( variant) . unwrap_or ( false )
2391- } )
2392- . expect ( "variant_index_with_ctor_or_variant_id: unknown variant" )
2393- . 0
2318+ self . variants . iter_enumerated ( ) . find ( |( _, v) | v. ctor_def_id == Some ( cid) )
2319+ . expect ( "variant_index_with_ctor_id: unknown variant" ) . 0
23942320 }
23952321
23962322 pub fn variant_of_def ( & self , def : Def ) -> & VariantDef {
23972323 match def {
2398- Def :: Variant ( vid) => self . variant_with_variant_id ( vid) ,
2324+ Def :: Variant ( vid) => self . variant_with_id ( vid) ,
23992325 Def :: Ctor ( hir:: CtorOf :: Variant , cid, ..) => self . variant_with_ctor_id ( cid) ,
24002326 Def :: Struct ( ..) | Def :: Ctor ( ..) | Def :: Union ( ..) |
24012327 Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) | Def :: SelfTy ( ..) |
@@ -2933,8 +2859,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
29332859
29342860 pub fn find_field_index ( self , ident : Ident , variant : & VariantDef ) -> Option < usize > {
29352861 variant. fields . iter ( ) . position ( |field| {
2936- let did = variant. variant_did . unwrap_or ( variant. parent_did ) ;
2937- self . adjust_ident ( ident, did, hir:: DUMMY_HIR_ID ) . 0 == field. ident . modern ( )
2862+ self . adjust_ident ( ident, variant. def_id , hir:: DUMMY_HIR_ID ) . 0 == field. ident . modern ( )
29382863 } )
29392864 }
29402865
@@ -3011,7 +2936,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30112936 match def {
30122937 Def :: Variant ( did) => {
30132938 let enum_did = self . parent ( did) . unwrap ( ) ;
3014- self . adt_def ( enum_did) . variant_with_variant_id ( did)
2939+ self . adt_def ( enum_did) . variant_with_id ( did)
30152940 }
30162941 Def :: Struct ( did) | Def :: Union ( did) => {
30172942 self . adt_def ( did) . non_enum_variant ( )
@@ -3029,11 +2954,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
30292954 }
30302955 }
30312956
3032- /// Given a `VariantDef`, returns the def-id of the `AdtDef` of which it is a part.
3033- pub fn adt_def_id_of_variant ( self , variant_def : & ' tcx VariantDef ) -> DefId {
3034- variant_def. parent_did
3035- }
3036-
30372957 pub fn item_name ( self , id : DefId ) -> InternedString {
30382958 if id. index == CRATE_DEF_INDEX {
30392959 self . original_crate_name ( id. krate ) . as_interned_str ( )
0 commit comments