Skip to content

Commit 7f5a8dc

Browse files
committed
Remove VariantDef::parent_did
1 parent c667c2f commit 7f5a8dc

File tree

15 files changed

+72
-202
lines changed

15 files changed

+72
-202
lines changed

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<'tcx> cmt_<'tcx> {
215215
};
216216
let variant_def = match self.cat {
217217
Categorization::Downcast(_, variant_did) => {
218-
adt_def.variant_with_variant_id(variant_did)
218+
adt_def.variant_with_id(variant_did)
219219
}
220220
_ => {
221221
assert_eq!(adt_def.variants.len(), 1);

src/librustc/mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2405,13 +2405,12 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24052405

24062406
AggregateKind::Adt(adt_def, variant, substs, _user_ty, _) => {
24072407
let variant_def = &adt_def.variants[variant];
2408-
let did = variant_def.variant_did_or_parent_struct_did();
24092408

24102409
let f = &mut *fmt;
24112410
ty::tls::with(|tcx| {
24122411
let substs = tcx.lift(&substs).expect("could not lift for printing");
24132412
FmtPrinter::new(tcx, f, Namespace::ValueNS)
2414-
.print_def_path(did, substs)?;
2413+
.print_def_path(variant_def.def_id, substs)?;
24152414
Ok(())
24162415
})?;
24172416

src/librustc/ty/inhabitedness/mod.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
104104
fn ty_inhabitedness_forest(self, ty: Ty<'tcx>) -> DefIdForest {
105105
ty.uninhabited_from(self)
106106
}
107-
108-
pub fn is_enum_variant_uninhabited_from(self,
109-
module: DefId,
110-
variant: &'tcx VariantDef,
111-
substs: SubstsRef<'tcx>)
112-
-> bool
113-
{
114-
self.variant_inhabitedness_forest(variant, substs).contains(self, module)
115-
}
116-
117-
pub fn is_variant_uninhabited_from_all_modules(self,
118-
variant: &'tcx VariantDef,
119-
substs: SubstsRef<'tcx>)
120-
-> bool
121-
{
122-
!self.variant_inhabitedness_forest(variant, substs).is_empty()
123-
}
124-
125-
fn variant_inhabitedness_forest(self, variant: &'tcx VariantDef, substs: SubstsRef<'tcx>)
126-
-> DefIdForest {
127-
// Determine the ADT kind:
128-
let adt_def_id = self.adt_def_id_of_variant(variant);
129-
let adt_kind = self.adt_def(adt_def_id).adt_kind();
130-
131-
// Compute inhabitedness forest:
132-
variant.uninhabited_from(self, substs, adt_kind)
133-
}
134107
}
135108

136109
impl<'a, 'gcx, 'tcx> AdtDef {
@@ -148,7 +121,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
148121

149122
impl<'a, 'gcx, 'tcx> VariantDef {
150123
/// Calculate the forest of DefIds from which this variant is visibly uninhabited.
151-
fn uninhabited_from(
124+
pub fn uninhabited_from(
152125
&self,
153126
tcx: TyCtxt<'a, 'gcx, 'tcx>,
154127
substs: SubstsRef<'tcx>,

src/librustc/ty/mod.rs

+26-106
Original file line numberDiff line numberDiff line change
@@ -1810,12 +1810,12 @@ bitflags! {
18101810
/// Definition of a variant -- a struct's fields or a enum variant.
18111811
#[derive(Debug)]
18121812
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>,
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

19441893
impl_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()

src/librustc_metadata/encoder.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
579579
let tcx = self.tcx;
580580
let def = tcx.adt_def(enum_did);
581581
let variant = &def.variants[index];
582-
let def_id = variant.variant_did();
582+
let def_id = variant.def_id;
583583
debug!("IsolatedEncoder::encode_enum_variant_info({:?})", def_id);
584584

585585
let data = VariantData {
586586
ctor_kind: variant.ctor_kind,
587587
discr: variant.discr,
588-
ctor: variant.ctor_did().map(|did| did.index),
588+
ctor: variant.ctor_def_id.map(|did| did.index),
589589
ctor_sig: None,
590590
};
591591

@@ -628,7 +628,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
628628
let tcx = self.tcx;
629629
let def = tcx.adt_def(enum_did);
630630
let variant = &def.variants[index];
631-
let def_id = variant.ctor_did().unwrap();
631+
let def_id = variant.ctor_def_id.unwrap();
632632
debug!("IsolatedEncoder::encode_enum_variant_ctor({:?})", def_id);
633633

634634
let data = VariantData {
@@ -726,9 +726,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
726726
let def_id = field.did;
727727
debug!("IsolatedEncoder::encode_field({:?})", def_id);
728728

729-
let variant_id = tcx.hir()
730-
.as_local_hir_id(variant.variant_did_or_parent_struct_did())
731-
.unwrap();
729+
let variant_id = tcx.hir().as_local_hir_id(variant.def_id).unwrap();
732730
let variant_data = tcx.hir().expect_variant_data(variant_id);
733731

734732
Entry {
@@ -1218,9 +1216,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12181216
hir::ItemKind::Enum(..) => {
12191217
let def = self.tcx.adt_def(def_id);
12201218
self.lazy_seq(def.variants.iter().map(|v| {
1221-
let did = v.variant_did();
1222-
assert!(did.is_local());
1223-
did.index
1219+
assert!(v.def_id.is_local());
1220+
v.def_id.index
12241221
}))
12251222
}
12261223
hir::ItemKind::Struct(..) |
@@ -1813,12 +1810,12 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
18131810

18141811
let def = self.tcx.adt_def(def_id);
18151812
for (i, variant) in def.variants.iter_enumerated() {
1816-
self.record(variant.variant_did(),
1813+
self.record(variant.def_id,
18171814
IsolatedEncoder::encode_enum_variant_info,
18181815
(def_id, Untracked(i)));
18191816

1820-
if let Some(ctor_hir_did) = variant.ctor_did() {
1821-
self.record(ctor_hir_did,
1817+
if let Some(ctor_def_id) = variant.ctor_def_id {
1818+
self.record(ctor_def_id,
18221819
IsolatedEncoder::encode_enum_variant_ctor,
18231820
(def_id, Untracked(i)));
18241821
}

src/librustc_mir/build/matches/simplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
163163
i == variant_index || {
164164
self.hir.tcx().features().never_type &&
165165
self.hir.tcx().features().exhaustive_patterns &&
166-
self.hir.tcx().is_variant_uninhabited_from_all_modules(v, substs)
166+
!v.uninhabited_from(self.hir.tcx(), substs, adt_def.adt_kind()).is_empty()
167167
}
168168
});
169169
if irrefutable {

src/librustc_mir/hair/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
486486
Def::Variant(variant_id) => {
487487
assert!(base.is_none());
488488

489-
let index = adt.variant_index_with_variant_id(variant_id);
489+
let index = adt.variant_index_with_id(variant_id);
490490
let user_provided_types = cx.tables().user_provided_types();
491491
let user_ty = user_provided_types.get(expr.hir_id)
492492
.map(|u_ty| *u_ty);

0 commit comments

Comments
 (0)