Skip to content

Commit

Permalink
Don't silently do nothing on mis_use of check_union_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 21, 2019
1 parent bb5a652 commit 0653694
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -1393,31 +1393,32 @@ 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 {
return true;
}
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;
}
Expand Down

0 comments on commit 0653694

Please sign in to comment.