Skip to content

Commit 6648dcd

Browse files
committed
Rollup merge of #47258 - rkruppe:struct-assert, r=eddyb
rustc::ty: Rename struct_variant to non_enum_variant r? @eddyb
2 parents d72a509 + cf3fefe commit 6648dcd

File tree

22 files changed

+55
-54
lines changed

22 files changed

+55
-54
lines changed

src/librustc/middle/dead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
102102
fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
103103
match self.tables.expr_ty_adjusted(lhs).sty {
104104
ty::TyAdt(def, _) => {
105-
self.insert_def_id(def.struct_variant().field_named(name).did);
105+
self.insert_def_id(def.non_enum_variant().field_named(name).did);
106106
}
107107
_ => span_bug!(lhs.span, "named field access on non-ADT"),
108108
}
@@ -111,7 +111,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
111111
fn handle_tup_field_access(&mut self, lhs: &hir::Expr, idx: usize) {
112112
match self.tables.expr_ty_adjusted(lhs).sty {
113113
ty::TyAdt(def, _) => {
114-
self.insert_def_id(def.struct_variant().fields[idx].did);
114+
self.insert_def_id(def.non_enum_variant().fields[idx].did);
115115
}
116116
ty::TyTuple(..) => {}
117117
_ => span_bug!(lhs.span, "numeric field access on non-ADT"),

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
663663
match with_cmt.ty.sty {
664664
ty::TyAdt(adt, substs) if adt.is_struct() => {
665665
// Consume those fields of the with expression that are needed.
666-
for with_field in &adt.struct_variant().fields {
666+
for with_field in &adt.non_enum_variant().fields {
667667
if !contains_field_named(with_field, fields) {
668668
let cmt_field = self.mc.cat_field(
669669
&*with_expr,

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
12481248
Def::StructCtor(_, CtorKind::Fn) => {
12491249
match self.pat_ty(&pat)?.sty {
12501250
ty::TyAdt(adt_def, _) => {
1251-
(cmt, adt_def.struct_variant().fields.len())
1251+
(cmt, adt_def.non_enum_variant().fields.len())
12521252
}
12531253
ref ty => {
12541254
span_bug!(pat.span, "tuple struct pattern unexpected type {:?}", ty);

src/librustc/ty/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,10 +1691,9 @@ impl<'a, 'gcx, 'tcx> AdtDef {
16911691
self.destructor(tcx).is_some()
16921692
}
16931693

1694-
/// Asserts this is a struct and returns the struct's unique
1695-
/// variant.
1696-
pub fn struct_variant(&self) -> &VariantDef {
1697-
assert!(!self.is_enum());
1694+
/// Asserts this is a struct or union and returns its unique variant.
1695+
pub fn non_enum_variant(&self) -> &VariantDef {
1696+
assert!(self.is_struct() || self.is_union());
16981697
&self.variants[0]
16991698
}
17001699

@@ -1733,7 +1732,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
17331732
match def {
17341733
Def::Variant(vid) | Def::VariantCtor(vid, ..) => self.variant_with_id(vid),
17351734
Def::Struct(..) | Def::StructCtor(..) | Def::Union(..) |
1736-
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => self.struct_variant(),
1735+
Def::TyAlias(..) | Def::AssociatedTy(..) | Def::SelfTy(..) => self.non_enum_variant(),
17371736
_ => bug!("unexpected def {:?} in variant_of_def", def)
17381737
}
17391738
}
@@ -2319,11 +2318,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23192318
self.adt_def(enum_did).variant_with_id(did)
23202319
}
23212320
Def::Struct(did) | Def::Union(did) => {
2322-
self.adt_def(did).struct_variant()
2321+
self.adt_def(did).non_enum_variant()
23232322
}
23242323
Def::StructCtor(ctor_did, ..) => {
23252324
let did = self.parent_def_id(ctor_did).expect("struct ctor has no parent");
2326-
self.adt_def(did).struct_variant()
2325+
self.adt_def(did).non_enum_variant()
23272326
}
23282327
_ => bug!("expect_variant_def used with unexpected def {:?}", def)
23292328
}

src/librustc/ty/sty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,15 +1351,15 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
13511351
pub fn simd_type(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
13521352
match self.sty {
13531353
TyAdt(def, substs) => {
1354-
def.struct_variant().fields[0].ty(tcx, substs)
1354+
def.non_enum_variant().fields[0].ty(tcx, substs)
13551355
}
13561356
_ => bug!("simd_type called on invalid type")
13571357
}
13581358
}
13591359

13601360
pub fn simd_size(&self, _cx: TyCtxt) -> usize {
13611361
match self.sty {
1362-
TyAdt(def, _) => def.struct_variant().fields.len(),
1362+
TyAdt(def, _) => def.non_enum_variant().fields.len(),
13631363
_ => bug!("simd_size called on invalid type")
13641364
}
13651365
}

src/librustc/ty/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
258258
adt.variant_with_id(vid).fields.get(i).map(|f| f.ty(self, substs))
259259
}
260260
(&TyAdt(adt, substs), None) => {
261-
// Don't use `struct_variant`, this may be a univariant enum.
261+
// Don't use `non_enum_variant`, this may be a univariant enum.
262262
adt.variants[0].fields.get(i).map(|f| f.ty(self, substs))
263263
}
264264
(&TyTuple(ref v, _), None) => v.get(i).cloned(),
@@ -277,7 +277,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
277277
adt.variant_with_id(vid).find_field_named(n).map(|f| f.ty(self, substs))
278278
}
279279
(&TyAdt(adt, substs), None) => {
280-
adt.struct_variant().find_field_named(n).map(|f| f.ty(self, substs))
280+
adt.non_enum_variant().find_field_named(n).map(|f| f.ty(self, substs))
281281
}
282282
_ => return None
283283
}
@@ -293,7 +293,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
293293
if !def.is_struct() {
294294
break;
295295
}
296-
match def.struct_variant().fields.last() {
296+
match def.non_enum_variant().fields.last() {
297297
Some(f) => ty = f.ty(self, substs),
298298
None => break,
299299
}
@@ -329,7 +329,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
329329
match (&a.sty, &b.sty) {
330330
(&TyAdt(a_def, a_substs), &TyAdt(b_def, b_substs))
331331
if a_def == b_def && a_def.is_struct() => {
332-
if let Some(f) = a_def.struct_variant().fields.last() {
332+
if let Some(f) = a_def.non_enum_variant().fields.last() {
333333
a = f.ty(self, a_substs);
334334
b = f.ty(self, b_substs);
335335
} else {

src/librustc_borrowck/borrowck/gather_loans/restrictions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'tcx> RestrictionsContext<'a, 'tcx> {
107107
ty::TyAdt(adt_def, _) if adt_def.is_union() => match result {
108108
RestrictionResult::Safe => RestrictionResult::Safe,
109109
RestrictionResult::SafeIf(base_lp, mut base_vec) => {
110-
for field in &adt_def.struct_variant().fields {
110+
for field in &adt_def.non_enum_variant().fields {
111111
let field = InteriorKind::InteriorField(mc::NamedField(field.name));
112112
let field_ty = if field == interior {
113113
cmt.ty

src/librustc_borrowck/borrowck/move_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ impl<'a, 'tcx> MoveData<'tcx> {
343343
if let (&ty::TyAdt(adt_def, _), LpInterior(opt_variant_id, interior))
344344
= (&base_lp.ty.sty, lp_elem) {
345345
if adt_def.is_union() {
346-
for field in &adt_def.struct_variant().fields {
346+
for field in &adt_def.non_enum_variant().fields {
347347
let field = InteriorKind::InteriorField(mc::NamedField(field.name));
348348
if field != interior {
349349
let sibling_lp_kind =
@@ -395,7 +395,7 @@ impl<'a, 'tcx> MoveData<'tcx> {
395395
if let LpExtend(ref base_lp, mutbl, LpInterior(opt_variant_id, interior)) = lp.kind {
396396
if let ty::TyAdt(adt_def, _) = base_lp.ty.sty {
397397
if adt_def.is_union() {
398-
for field in &adt_def.struct_variant().fields {
398+
for field in &adt_def.non_enum_variant().fields {
399399
let field = InteriorKind::InteriorField(mc::NamedField(field.name));
400400
let field_ty = if field == interior {
401401
lp.ty

src/librustc_lint/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,15 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
422422
consider adding a #[repr(C)] attribute to the type");
423423
}
424424

425-
if def.struct_variant().fields.is_empty() {
425+
if def.non_enum_variant().fields.is_empty() {
426426
return FfiUnsafe("found zero-size struct in foreign module, consider \
427427
adding a member to this struct");
428428
}
429429

430430
// We can't completely trust repr(C) markings; make sure the
431431
// fields are actually safe.
432432
let mut all_phantom = true;
433-
for field in &def.struct_variant().fields {
433+
for field in &def.non_enum_variant().fields {
434434
let field_ty = cx.fully_normalize_associated_types_in(
435435
&field.ty(cx, substs)
436436
);
@@ -458,13 +458,13 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
458458
consider adding a #[repr(C)] attribute to the type");
459459
}
460460

461-
if def.struct_variant().fields.is_empty() {
461+
if def.non_enum_variant().fields.is_empty() {
462462
return FfiUnsafe("found zero-size union in foreign module, consider \
463463
adding a member to this union");
464464
}
465465

466466
let mut all_phantom = true;
467-
for field in &def.struct_variant().fields {
467+
for field in &def.non_enum_variant().fields {
468468
let field_ty = cx.fully_normalize_associated_types_in(
469469
&field.ty(cx, substs)
470470
);

src/librustc_metadata/encoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
625625
debug!("IsolatedEncoder::encode_struct_ctor({:?})", def_id);
626626
let tcx = self.tcx;
627627
let adt_def = tcx.adt_def(adt_def_id);
628-
let variant = adt_def.struct_variant();
628+
let variant = adt_def.non_enum_variant();
629629

630630
let data = VariantData {
631631
ctor_kind: variant.ctor_kind,
@@ -935,7 +935,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
935935
hir::ItemTy(..) => EntryKind::Type,
936936
hir::ItemEnum(..) => EntryKind::Enum(get_repr_options(&tcx, def_id)),
937937
hir::ItemStruct(ref struct_def, _) => {
938-
let variant = tcx.adt_def(def_id).struct_variant();
938+
let variant = tcx.adt_def(def_id).non_enum_variant();
939939

940940
// Encode def_ids for each field and method
941941
// for methods, write all the stuff get_trait_method
@@ -956,7 +956,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
956956
}), repr_options)
957957
}
958958
hir::ItemUnion(..) => {
959-
let variant = tcx.adt_def(def_id).struct_variant();
959+
let variant = tcx.adt_def(def_id).non_enum_variant();
960960
let repr_options = get_repr_options(&tcx, def_id);
961961

962962
EntryKind::Union(self.lazy(&VariantData {
@@ -1049,7 +1049,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
10491049
hir::ItemStruct(..) |
10501050
hir::ItemUnion(..) => {
10511051
let def = self.tcx.adt_def(def_id);
1052-
self.lazy_seq(def.struct_variant().fields.iter().map(|f| {
1052+
self.lazy_seq(def.non_enum_variant().fields.iter().map(|f| {
10531053
assert!(f.did.is_local());
10541054
f.did.index
10551055
}))

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
719719
ty::TyAdt(def, _) => if def.is_enum() {
720720
format!("{}", field.index())
721721
} else {
722-
format!("{}", def.struct_variant().fields[field.index()].name)
722+
format!("{}", def.non_enum_variant().fields[field.index()].name)
723723
},
724724
ty::TyTuple(_, _) => format!("{}", field.index()),
725725
ty::TyRef(_, tnm) | ty::TyRawPtr(tnm) => {

src/librustc_mir/interpret/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ fn check_ctfe_against_miri<'a, 'tcx>(
488488
miri_place = ecx.place_downcast(miri_place, variant).unwrap();
489489
&def.variants[variant]
490490
} else {
491-
def.struct_variant()
491+
def.non_enum_variant()
492492
};
493493
let vec = match ctfe {
494494
ConstVal::Aggregate(Struct(v)) => v,

src/librustc_mir/monomorphize/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,8 @@ fn find_vtable_types_for_unsizing<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
838838
CustomCoerceUnsized::Struct(i) => i
839839
};
840840

841-
let source_fields = &source_adt_def.struct_variant().fields;
842-
let target_fields = &target_adt_def.struct_variant().fields;
841+
let source_fields = &source_adt_def.non_enum_variant().fields;
842+
let target_fields = &target_adt_def.non_enum_variant().fields;
843843

844844
assert!(coerce_index < source_fields.len() &&
845845
source_fields.len() == target_fields.len());

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,10 +1583,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
15831583
if !self.span.filter_generated(sub_span, ex.span) {
15841584
let span =
15851585
self.span_from_span(sub_span.expect("No span found for var ref"));
1586+
let ref_id =
1587+
::id_from_def_id(def.non_enum_variant().fields[idx.node].did);
15861588
self.dumper.dump_ref(Ref {
15871589
kind: RefKind::Variable,
15881590
span,
1589-
ref_id: ::id_from_def_id(def.struct_variant().fields[idx.node].did),
1591+
ref_id,
15901592
});
15911593
}
15921594
}

src/librustc_save_analysis/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
528528
};
529529
match self.tables.expr_ty_adjusted(&hir_node).sty {
530530
ty::TyAdt(def, _) if !def.is_enum() => {
531-
let f = def.struct_variant().field_named(ident.node.name);
531+
let f = def.non_enum_variant().field_named(ident.node.name);
532532
let sub_span = self.span_utils.span_for_last_ident(expr.span);
533533
filter!(self.span_utils, sub_span, expr.span, None);
534534
let span = self.span_from_span(sub_span.unwrap());

src/librustc_trans/debuginfo/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ fn prepare_struct_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
969969
let struct_name = compute_debuginfo_type_name(cx, struct_type, false);
970970

971971
let (struct_def_id, variant) = match struct_type.sty {
972-
ty::TyAdt(def, _) => (def.did, def.struct_variant()),
972+
ty::TyAdt(def, _) => (def.did, def.non_enum_variant()),
973973
_ => bug!("prepare_struct_metadata on a non-ADT")
974974
};
975975

@@ -1084,7 +1084,7 @@ fn prepare_union_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
10841084
let union_name = compute_debuginfo_type_name(cx, union_type, false);
10851085

10861086
let (union_def_id, variant) = match union_type.sty {
1087-
ty::TyAdt(def, _) => (def.did, def.struct_variant()),
1087+
ty::TyAdt(def, _) => (def.did, def.non_enum_variant()),
10881088
_ => bug!("prepare_union_metadata on a non-ADT")
10891089
};
10901090

src/librustc_typeck/check/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
107107
ty::TyDynamic(ref tty, ..) =>
108108
Some(PointerKind::Vtable(tty.principal().map(|p| p.def_id()))),
109109
ty::TyAdt(def, substs) if def.is_struct() => {
110-
match def.struct_variant().fields.last() {
110+
match def.non_enum_variant().fields.last() {
111111
None => Some(PointerKind::Thin),
112112
Some(f) => {
113113
let field_ty = self.field_ty(span, f, substs);

src/librustc_typeck/check/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
289289
for (ty, _) in self.autoderef(span, rcvr_ty) {
290290
match ty.sty {
291291
ty::TyAdt(def, substs) if !def.is_enum() => {
292-
if let Some(field) = def.struct_variant()
292+
if let Some(field) = def.non_enum_variant()
293293
.find_field_named(item_name) {
294294
let snippet = tcx.sess.codemap().span_to_snippet(expr.span);
295295
let expr_string = match snippet {

0 commit comments

Comments
 (0)