@@ -1810,12 +1810,12 @@ bitflags! {
1810
1810
/// Definition of a variant -- a struct's fields or a enum variant.
1811
1811
#[ derive( Debug ) ]
1812
1812
pub 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 > ,
1819
1819
/// Variant or struct name.
1820
1820
pub ident : Ident ,
1821
1821
/// Discriminant of this variant.
@@ -1824,11 +1824,6 @@ pub struct VariantDef {
1824
1824
pub fields : Vec < FieldDef > ,
1825
1825
/// Type of constructor of variant.
1826
1826
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 ,
1832
1827
/// Flags of the variant (e.g. is field list non-exhaustive)?
1833
1828
flags : VariantFlags ,
1834
1829
/// Recovered?
@@ -1856,7 +1851,7 @@ impl<'a, 'gcx, 'tcx> VariantDef {
1856
1851
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
1857
1852
ident : Ident ,
1858
1853
variant_did : Option < DefId > ,
1859
- ctor_did : Option < DefId > ,
1854
+ ctor_def_id : Option < DefId > ,
1860
1855
discr : VariantDiscr ,
1861
1856
fields : Vec < FieldDef > ,
1862
1857
ctor_kind : CtorKind ,
@@ -1865,9 +1860,9 @@ impl<'a, 'gcx, 'tcx> VariantDef {
1865
1860
recovered : bool ,
1866
1861
) -> Self {
1867
1862
debug ! (
1868
- "VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_did = {:?}, discr = {:?},
1863
+ "VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
1869
1864
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,
1871
1866
) ;
1872
1867
1873
1868
let mut flags = VariantFlags :: NO_VARIANT_FLAGS ;
@@ -1877,14 +1872,13 @@ impl<'a, 'gcx, 'tcx> VariantDef {
1877
1872
}
1878
1873
1879
1874
VariantDef {
1880
- variant_did,
1881
- ctor_did ,
1875
+ def_id : variant_did. unwrap_or ( parent_did ) ,
1876
+ ctor_def_id ,
1882
1877
ident,
1883
1878
discr,
1884
1879
fields,
1885
1880
ctor_kind,
1886
1881
flags,
1887
- parent_did,
1888
1882
recovered,
1889
1883
}
1890
1884
}
@@ -1894,62 +1888,16 @@ impl<'a, 'gcx, 'tcx> VariantDef {
1894
1888
pub fn is_field_list_non_exhaustive ( & self ) -> bool {
1895
1889
self . flags . intersects ( VariantFlags :: IS_FIELD_LIST_NON_EXHAUSTIVE )
1896
1890
}
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
- }
1942
1891
}
1943
1892
1944
1893
impl_stable_hash_for ! ( struct VariantDef {
1945
- variant_did ,
1946
- ctor_did ,
1894
+ def_id ,
1895
+ ctor_def_id ,
1947
1896
ident -> ( ident. name) ,
1948
1897
discr,
1949
1898
fields,
1950
1899
ctor_kind,
1951
1900
flags,
1952
- parent_did,
1953
1901
recovered
1954
1902
} ) ;
1955
1903
@@ -2204,7 +2152,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
2204
2152
AdtKind :: Struct => AdtFlags :: IS_STRUCT ,
2205
2153
} ;
2206
2154
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 ( ) {
2208
2156
flags |= AdtFlags :: HAS_CTOR ;
2209
2157
}
2210
2158
@@ -2351,51 +2299,29 @@ impl<'a, 'gcx, 'tcx> AdtDef {
2351
2299
self . variants . iter ( ) . all ( |v| v. fields . is_empty ( ) )
2352
2300
}
2353
2301
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" )
2359
2305
}
2360
2306
2361
2307
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) )
2365
2309
. expect ( "variant_with_ctor_id: unknown variant" )
2366
2310
}
2367
2311
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
2374
2315
}
2375
2316
2376
2317
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
2394
2320
}
2395
2321
2396
2322
pub fn variant_of_def ( & self , def : Def ) -> & VariantDef {
2397
2323
match def {
2398
- Def :: Variant ( vid) => self . variant_with_variant_id ( vid) ,
2324
+ Def :: Variant ( vid) => self . variant_with_id ( vid) ,
2399
2325
Def :: Ctor ( hir:: CtorOf :: Variant , cid, ..) => self . variant_with_ctor_id ( cid) ,
2400
2326
Def :: Struct ( ..) | Def :: Ctor ( ..) | Def :: Union ( ..) |
2401
2327
Def :: TyAlias ( ..) | Def :: AssociatedTy ( ..) | Def :: SelfTy ( ..) |
@@ -2933,8 +2859,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
2933
2859
2934
2860
pub fn find_field_index ( self , ident : Ident , variant : & VariantDef ) -> Option < usize > {
2935
2861
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 ( )
2938
2863
} )
2939
2864
}
2940
2865
@@ -3011,7 +2936,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
3011
2936
match def {
3012
2937
Def :: Variant ( did) => {
3013
2938
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)
3015
2940
}
3016
2941
Def :: Struct ( did) | Def :: Union ( did) => {
3017
2942
self . adt_def ( did) . non_enum_variant ( )
@@ -3029,11 +2954,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
3029
2954
}
3030
2955
}
3031
2956
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
-
3037
2957
pub fn item_name ( self , id : DefId ) -> InternedString {
3038
2958
if id. index == CRATE_DEF_INDEX {
3039
2959
self . original_crate_name ( id. krate ) . as_interned_str ( )
0 commit comments