Skip to content

Commit

Permalink
Stop using string equality to check if ADT is a union
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 11, 2022
1 parent d9ddaf0 commit 5052911
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// re-link the regions that EIfEO can erase.
self.demand_eqtype(span, adt_ty_hint, adt_ty);

let (substs, adt_kind, kind_name) = match adt_ty.kind() {
ty::Adt(adt, substs) => (substs, adt.adt_kind(), adt.variant_descr()),
_ => span_bug!(span, "non-ADT passed to check_expr_struct_fields"),
let ty::Adt(adt, substs) = adt_ty.kind() else {
span_bug!(span, "non-ADT passed to check_expr_struct_fields");
};
let adt_kind = adt.adt_kind();

let mut remaining_fields = variant
.fields
Expand Down Expand Up @@ -1520,7 +1520,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
});
} else {
self.report_unknown_field(
adt_ty, variant, field, ast_fields, kind_name, expr_span,
adt_ty,
variant,
field,
ast_fields,
adt.variant_descr(),
expr_span,
);
}

Expand All @@ -1533,7 +1538,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

// Make sure the programmer specified correct number of fields.
if kind_name == "union" {
if adt_kind == AdtKind::Union {
if ast_fields.len() != 1 {
struct_span_err!(
tcx.sess,
Expand Down Expand Up @@ -1666,7 +1671,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
};
self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr_id, fru_tys);
} else if kind_name != "union" && !remaining_fields.is_empty() {
} else if adt_kind != AdtKind::Union && !remaining_fields.is_empty() {
let inaccessible_remaining_fields = remaining_fields.iter().any(|(_, (_, field))| {
!field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
});
Expand Down

0 comments on commit 5052911

Please sign in to comment.