Skip to content

Commit 0653694

Browse files
committed
Don't silently do nothing on mis_use of check_union_fields
1 parent bb5a652 commit 0653694

File tree

1 file changed

+17
-16
lines changed
  • src/librustc_typeck/check

1 file changed

+17
-16
lines changed

src/librustc_typeck/check/mod.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -1393,31 +1393,32 @@ fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) {
13931393

13941394
/// When the `#![feature(untagged_unions)]` gate is active,
13951395
/// check that the fields of the `union` does not contain fields that need dropping.
1396-
fn check_union_fields(tcx: TyCtxt<'_>, _: Span, item_def_id: DefId) -> bool {
1396+
fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: DefId) -> bool {
13971397
// Without the feature we check that all fields are `Copy` in our stability checking
13981398
// infrastructure.
13991399
if !tcx.features().untagged_unions {
14001400
return true;
14011401
}
14021402
let item_type = tcx.type_of(item_def_id);
14031403
if let ty::Adt(def, substs) = item_type.kind {
1404-
if def.is_union() {
1405-
let fields = &def.non_enum_variant().fields;
1406-
for field in fields {
1407-
let field_ty = field.ty(tcx, substs);
1408-
// We are currently checking the type this field came from, so it must be local.
1409-
let field_span = tcx.hir().span_if_local(field.did).unwrap();
1410-
let param_env = tcx.param_env(field.did);
1411-
if field_ty.needs_drop(tcx, param_env) {
1412-
struct_span_err!(tcx.sess, field_span, E0740,
1413-
"unions may not contain fields that need dropping")
1414-
.span_note(field_span,
1415-
"`std::mem::ManuallyDrop` can be used to wrap the type")
1416-
.emit();
1417-
return false;
1418-
}
1404+
assert!(def.is_union());
1405+
let fields = &def.non_enum_variant().fields;
1406+
for field in fields {
1407+
let field_ty = field.ty(tcx, substs);
1408+
// We are currently checking the type this field came from, so it must be local.
1409+
let field_span = tcx.hir().span_if_local(field.did).unwrap();
1410+
let param_env = tcx.param_env(field.did);
1411+
if field_ty.needs_drop(tcx, param_env) {
1412+
struct_span_err!(tcx.sess, field_span, E0740,
1413+
"unions may not contain fields that need dropping")
1414+
.span_note(field_span,
1415+
"`std::mem::ManuallyDrop` can be used to wrap the type")
1416+
.emit();
1417+
return false;
14191418
}
14201419
}
1420+
} else {
1421+
span_bug!(span, "unions must be ty::Adt, but got {:?}", item_type.kind);
14211422
}
14221423
return true;
14231424
}

0 commit comments

Comments
 (0)