Skip to content

Commit 469faa2

Browse files
authored
Rollup merge of rust-lang#90901 - rukai:improve_manuallydrop_help, r=estebank
Improve ManuallyDrop suggestion closes rust-lang#90585 * Fixes the recommended change to use ManuallyDrop as per the issue * Changes the note to a help * improves the span so it only points at the type.
2 parents fb660de + 62acf7f commit 469faa2

6 files changed

+43
-43
lines changed

compiler/rustc_typeck/src/check/check.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,26 @@ 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();
376374
if field_ty.needs_drop(tcx, param_env) {
375+
let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) {
376+
// We are currently checking the type this field came from, so it must be local.
377+
Some(Node::Field(field)) => (field.span, field.ty.span),
378+
_ => unreachable!("mir field has to correspond to hir field"),
379+
};
377380
struct_span_err!(
378381
tcx.sess,
379382
field_span,
380383
E0740,
381384
"unions may not contain fields that need dropping"
382385
)
383-
.span_note(field_span, "`std::mem::ManuallyDrop` can be used to wrap the type")
386+
.multipart_suggestion_verbose(
387+
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
388+
vec![
389+
(ty_span.shrink_to_lo(), format!("std::mem::ManuallyDrop<")),
390+
(ty_span.shrink_to_hi(), ">".into()),
391+
],
392+
Applicability::MaybeIncorrect,
393+
)
384394
.emit();
385395
return false;
386396
}

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)