From 0653694fdc46a2bca119b9790d1dfd62e1b4901e Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 21 Oct 2019 09:08:05 +0200 Subject: [PATCH] Don't silently do nothing on mis_use of `check_union_fields` --- src/librustc_typeck/check/mod.rs | 33 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 3e0527393cd12..d5182d69c3e70 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1393,7 +1393,7 @@ fn check_union(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) { /// When the `#![feature(untagged_unions)]` gate is active, /// check that the fields of the `union` does not contain fields that need dropping. -fn check_union_fields(tcx: TyCtxt<'_>, _: Span, item_def_id: DefId) -> bool { +fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: DefId) -> bool { // Without the feature we check that all fields are `Copy` in our stability checking // infrastructure. if !tcx.features().untagged_unions { @@ -1401,23 +1401,24 @@ fn check_union_fields(tcx: TyCtxt<'_>, _: Span, item_def_id: DefId) -> bool { } let item_type = tcx.type_of(item_def_id); if let ty::Adt(def, substs) = item_type.kind { - if def.is_union() { - let fields = &def.non_enum_variant().fields; - for field in fields { - let field_ty = field.ty(tcx, substs); - // We are currently checking the type this field came from, so it must be local. - let field_span = tcx.hir().span_if_local(field.did).unwrap(); - let param_env = tcx.param_env(field.did); - if field_ty.needs_drop(tcx, param_env) { - struct_span_err!(tcx.sess, field_span, E0740, - "unions may not contain fields that need dropping") - .span_note(field_span, - "`std::mem::ManuallyDrop` can be used to wrap the type") - .emit(); - return false; - } + assert!(def.is_union()); + let fields = &def.non_enum_variant().fields; + for field in fields { + let field_ty = field.ty(tcx, substs); + // We are currently checking the type this field came from, so it must be local. + let field_span = tcx.hir().span_if_local(field.did).unwrap(); + let param_env = tcx.param_env(field.did); + if field_ty.needs_drop(tcx, param_env) { + struct_span_err!(tcx.sess, field_span, E0740, + "unions may not contain fields that need dropping") + .span_note(field_span, + "`std::mem::ManuallyDrop` can be used to wrap the type") + .emit(); + return false; } } + } else { + span_bug!(span, "unions must be ty::Adt, but got {:?}", item_type.kind); } return true; }