Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ManuallyDrop suggestion #90901

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,25 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
let param_env = tcx.param_env(item_def_id);
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 (field_span, ty_span) =
// We are currently checking the type this field came from, so it must be local.
if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() {
(field.span, field.ty.span)
} else {
unreachable!("mir field has to correspond to hir field");
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let (field_span, ty_span) =
// We are currently checking the type this field came from, so it must be local.
if let Node::Field(field) = tcx.hir().get_if_local(field.did).unwrap() {
(field.span, field.ty.span)
} else {
unreachable!("mir field has to correspond to hir field");
};
let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) {
// We are currently checking the type this field came from, so it must be local.
Some(Node::Field(field)) => (field.span, field.ty.span),
_ => unreachable!("mir field has to correspond to hir field"),
};

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")
.multipart_suggestion_verbose(
"wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped",
vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vec![(ty_span, format!("std::mem::ManuallyDrop<{}>", field_ty))],
vec![
(ty_span.shrink_to_lo(), "std::mem::ManuallyDrop<".to_string()),
(ty_span.shrink_to_hi(), ">".to_string()),
],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thats clever!

Applicability::MaybeIncorrect,
)
.emit();
return false;
}
Expand Down
14 changes: 6 additions & 8 deletions src/test/ui/feature-gates/feature-gate-untagged_unions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@ error[E0740]: unions may not contain fields that need dropping
LL | a: String,
| ^^^^^^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/feature-gate-untagged_unions.rs:16:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: String,
| ^^^^^^^^^
LL | a: std::mem::ManuallyDrop<String>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0740]: unions may not contain fields that need dropping
--> $DIR/feature-gate-untagged_unions.rs:24:5
|
LL | a: T,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/feature-gate-untagged_unions.rs:24:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: T,
| ^^^^
LL | a: std::mem::ManuallyDrop<T>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 3 previous errors

Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/union/issue-41073.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping
LL | a: A,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/issue-41073.rs:4:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: A,
| ^^^^
LL | a: std::mem::ManuallyDrop<A>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/union/union-custom-drop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ error[E0740]: unions may not contain fields that need dropping
LL | bar: Bar,
| ^^^^^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-custom-drop.rs:7:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | bar: Bar,
| ^^^^^^^^
LL | bar: std::mem::ManuallyDrop<Bar>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to previous error

Expand Down
21 changes: 9 additions & 12 deletions src/test/ui/union/union-with-drop-fields.mirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@ error[E0740]: unions may not contain fields that need dropping
LL | a: String,
| ^^^^^^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:11:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: String,
| ^^^^^^^^^
LL | a: std::mem::ManuallyDrop<String>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0740]: unions may not contain fields that need dropping
--> $DIR/union-with-drop-fields.rs:19:5
|
LL | a: S,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:19:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: S,
| ^^^^
LL | a: std::mem::ManuallyDrop<S>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0740]: unions may not contain fields that need dropping
--> $DIR/union-with-drop-fields.rs:24:5
|
LL | a: T,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:24:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: T,
| ^^^^
LL | a: std::mem::ManuallyDrop<T>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 3 previous errors

Expand Down
21 changes: 9 additions & 12 deletions src/test/ui/union/union-with-drop-fields.thirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,32 @@ error[E0740]: unions may not contain fields that need dropping
LL | a: String,
| ^^^^^^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:11:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: String,
| ^^^^^^^^^
LL | a: std::mem::ManuallyDrop<String>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0740]: unions may not contain fields that need dropping
--> $DIR/union-with-drop-fields.rs:19:5
|
LL | a: S,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:19:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: S,
| ^^^^
LL | a: std::mem::ManuallyDrop<S>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0740]: unions may not contain fields that need dropping
--> $DIR/union-with-drop-fields.rs:24:5
|
LL | a: T,
| ^^^^
|
note: `std::mem::ManuallyDrop` can be used to wrap the type
--> $DIR/union-with-drop-fields.rs:24:5
help: wrap the type with `std::mem::ManuallyDrop` and ensure it is manually dropped
|
LL | a: T,
| ^^^^
LL | a: std::mem::ManuallyDrop<T>,
| ~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 3 previous errors

Expand Down