Skip to content

Commit 776d5d4

Browse files
committed
Use union field's span and emit a note for helping about ManuallyDrop
1 parent d17e54f commit 776d5d4

File tree

1 file changed

+8
-5
lines changed
  • src/librustc_typeck/check

1 file changed

+8
-5
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,12 +1302,12 @@ fn check_union<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
13021302
def.destructor(tcx); // force the destructor to be evaluated
13031303
check_representable(tcx, span, def_id);
13041304

1305-
check_dropless(tcx, span, def_id);
1305+
check_union_fields(tcx, span, def_id);
13061306
check_packed(tcx, span, def_id);
13071307
}
13081308

1309-
fn check_dropless<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1310-
sp: Span,
1309+
fn check_union_fields<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1310+
_sp: Span,
13111311
item_def_id: DefId)
13121312
-> bool {
13131313
// Without the feature we check Copy types only later
@@ -1320,11 +1320,14 @@ fn check_dropless<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
13201320
let fields = &def.non_enum_variant().fields;
13211321
for field in fields {
13221322
let field_ty = field.ty(tcx, substs);
1323+
// We are currently checking the type this field came from, so it must be local
1324+
let field_span = tcx.hir().span_if_local(field.did).unwrap();
13231325
let param_env = tcx.param_env(field.did);
13241326
if field_ty.needs_drop(tcx, param_env) {
1325-
struct_span_err!(tcx.sess, sp, E0730,
1327+
struct_span_err!(tcx.sess, field_span, E0730,
13261328
"unions may not contain fields that need dropping")
1327-
.span_label(sp, "ManuallyDrop can be used to wrap such fields")
1329+
.span_note(field_span,
1330+
"`std::mem::ManuallyDrop` can be used to wrap the type")
13281331
.emit();
13291332
return false;
13301333
}

0 commit comments

Comments
 (0)