Skip to content

Commit f980f81

Browse files
committed
Improve ManuallyDrop suggestion
1 parent f820d9d commit f980f81

File tree

6 files changed

+42
-43
lines changed

6 files changed

+42
-43
lines changed

compiler/rustc_typeck/src/check/check.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,25 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
371371
let param_env = tcx.param_env(item_def_id);
372372
for field in fields {
373373
let field_ty = field.ty(tcx, substs);
374-
// We are currently checking the type this field came from, so it must be local.
375-
let field_span = tcx.hir().span_if_local(field.did).unwrap();
374+
let (field_span, ty_span) =
375+
// We are currently checking the type this field came from, so it must be local.
376+
if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() {
377+
(field.span, field.ty.span)
378+
} else {
379+
unreachable!("mir field has to correspond to hir field");
380+
};
376381
if field_ty.needs_drop(tcx, param_env) {
377382
struct_span_err!(
378383
tcx.sess,
379384
field_span,
380385
E0740,
381386
"unions may not contain fields that need dropping"
382387
)
383-
.span_note(field_span, "`std::mem::ManuallyDrop` can be used to wrap the type")
388+
.multipart_suggestion_verbose(
389+
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
390+
vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))],
391+
Applicability::MaybeIncorrect,
392+
)
384393
.emit();
385394
return false;
386395
}

src/test/ui/feature-gates/feature-gate-untagged_unions.stderr

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@ error[E0740]: unions may not contain fields that need dropping
1313
LL | a: String,
1414
| ^^^^^^^^^
1515
|
16-
note: `std::mem::ManuallyDrop` can be used to wrap the type
17-
--> $DIR/feature-gate-untagged_unions.rs:16:5
16+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
1817
|
19-
LL | a: String,
20-
| ^^^^^^^^^
18+
LL | a: std::mem::ManuallyDrop<String>,
19+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2120

2221
error[E0740]: unions may not contain fields that need dropping
2322
--> $DIR/feature-gate-untagged_unions.rs:24:5
2423
|
2524
LL | a: T,
2625
| ^^^^
2726
|
28-
note: `std::mem::ManuallyDrop` can be used to wrap the type
29-
--> $DIR/feature-gate-untagged_unions.rs:24:5
27+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
3028
|
31-
LL | a: T,
32-
| ^^^^
29+
LL | a: std::mem::ManuallyDrop<T>,
30+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
3331

3432
error: aborting due to 3 previous errors
3533

src/test/ui/union/issue-41073.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping
44
LL | a: A,
55
| ^^^^
66
|
7-
note: `std::mem::ManuallyDrop` can be used to wrap the type
8-
--> $DIR/issue-41073.rs:4:5
7+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
98
|
10-
LL | a: A,
11-
| ^^^^
9+
LL | a: std::mem::ManuallyDrop<A>,
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
1211

1312
error: aborting due to previous error
1413

src/test/ui/union/union-custom-drop.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping
44
LL | bar: Bar,
55
| ^^^^^^^^
66
|
7-
note: `std::mem::ManuallyDrop` can be used to wrap the type
8-
--> $DIR/union-custom-drop.rs:7:5
7+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
98
|
10-
LL | bar: Bar,
11-
| ^^^^^^^^
9+
LL | bar: std::mem::ManuallyDrop<Bar>,
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1211

1312
error: aborting due to previous error
1413

src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@ error[E0740]: unions may not contain fields that need dropping
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
note: `std::mem::ManuallyDrop` can be used to wrap the type
8-
--> $DIR/union-with-drop-fields.rs:11:5
7+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
98
|
10-
LL | a: String,
11-
| ^^^^^^^^^
9+
LL | a: std::mem::ManuallyDrop<String>,
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1211

1312
error[E0740]: unions may not contain fields that need dropping
1413
--> $DIR/union-with-drop-fields.rs:19:5
1514
|
1615
LL | a: S,
1716
| ^^^^
1817
|
19-
note: `std::mem::ManuallyDrop` can be used to wrap the type
20-
--> $DIR/union-with-drop-fields.rs:19:5
18+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
2119
|
22-
LL | a: S,
23-
| ^^^^
20+
LL | a: std::mem::ManuallyDrop<S>,
21+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
2422

2523
error[E0740]: unions may not contain fields that need dropping
2624
--> $DIR/union-with-drop-fields.rs:24:5
2725
|
2826
LL | a: T,
2927
| ^^^^
3028
|
31-
note: `std::mem::ManuallyDrop` can be used to wrap the type
32-
--> $DIR/union-with-drop-fields.rs:24:5
29+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
3330
|
34-
LL | a: T,
35-
| ^^^^
31+
LL | a: std::mem::ManuallyDrop<T>,
32+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
3633

3734
error: aborting due to 3 previous errors
3835

src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,32 @@ error[E0740]: unions may not contain fields that need dropping
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
note: `std::mem::ManuallyDrop` can be used to wrap the type
8-
--> $DIR/union-with-drop-fields.rs:11:5
7+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
98
|
10-
LL | a: String,
11-
| ^^^^^^^^^
9+
LL | a: std::mem::ManuallyDrop<String>,
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1211

1312
error[E0740]: unions may not contain fields that need dropping
1413
--> $DIR/union-with-drop-fields.rs:19:5
1514
|
1615
LL | a: S,
1716
| ^^^^
1817
|
19-
note: `std::mem::ManuallyDrop` can be used to wrap the type
20-
--> $DIR/union-with-drop-fields.rs:19:5
18+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
2119
|
22-
LL | a: S,
23-
| ^^^^
20+
LL | a: std::mem::ManuallyDrop<S>,
21+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
2422

2523
error[E0740]: unions may not contain fields that need dropping
2624
--> $DIR/union-with-drop-fields.rs:24:5
2725
|
2826
LL | a: T,
2927
| ^^^^
3028
|
31-
note: `std::mem::ManuallyDrop` can be used to wrap the type
32-
--> $DIR/union-with-drop-fields.rs:24:5
29+
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
3330
|
34-
LL | a: T,
35-
| ^^^^
31+
LL | a: std::mem::ManuallyDrop<T>,
32+
| ~~~~~~~~~~~~~~~~~~~~~~~~~
3633

3734
error: aborting due to 3 previous errors
3835

0 commit comments

Comments
 (0)