Skip to content

Commit

Permalink
Rollup merge of rust-lang#67323 - Centril:tes, r=davidtwco
Browse files Browse the repository at this point in the history
make transparent enums more ordinary

By recognizing that structs & unions have one variant, we can make the treatment of transparent enums less ad-hoc.

cc rust-lang#60405

r? @davidtwco
  • Loading branch information
Centril authored Dec 16, 2019
2 parents c2f0498 + 5d73af2 commit 6c0dd82
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2330,7 +2330,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: &'tcx ty::AdtDef, sp: Span, d
);
let mut err = struct_span_err!(tcx.sess, sp, E0731, "transparent enum {}", msg);
err.span_label(sp, &msg);
if let &[ref start @ .., ref end] = &variant_spans[..] {
if let [start @ .., end] = &*variant_spans {
for variant_span in start {
err.span_label(*variant_span, "");
}
Expand Down Expand Up @@ -2372,23 +2372,14 @@ fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
}
let sp = tcx.sess.source_map().def_span(sp);

if adt.is_enum() {
if !tcx.features().transparent_enums {
feature_err(
&tcx.sess.parse_sess,
sym::transparent_enums,
sp,
"transparent enums are unstable",
)
.emit();
}
if adt.variants.len() != 1 {
bad_variant_count(tcx, adt, sp, def_id);
if adt.variants.is_empty() {
// Don't bother checking the fields. No variants (and thus no fields) exist.
return;
}
}
if adt.is_enum() && !tcx.features().transparent_enums {
feature_err(
&tcx.sess.parse_sess,
sym::transparent_enums,
sp,
"transparent enums are unstable",
)
.emit();
}

if adt.is_union() && !tcx.features().transparent_unions {
Expand All @@ -2401,6 +2392,14 @@ fn check_transparent(tcx: TyCtxt<'_>, sp: Span, def_id: DefId) {
.emit();
}

if adt.variants.len() != 1 {
bad_variant_count(tcx, adt, sp, def_id);
if adt.variants.is_empty() {
// Don't bother checking the fields. No variants (and thus no fields) exist.
return;
}
}

// For each field, figure out if it's known to be a ZST and align(1)
let field_infos = adt.all_fields().map(|field| {
let ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, field.did));
Expand Down

0 comments on commit 6c0dd82

Please sign in to comment.