1
Fork 0

Rollup merge of #108882 - compiler-errors:E0740, r=eholk

Tweak E0740

Also drive-by suppress E0740 if it's an unresolved type.
This commit is contained in:
Matthias Krüger 2023-03-08 21:26:52 +01:00 committed by GitHub
commit e6e82db307
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 101 additions and 67 deletions

View file

@ -163,3 +163,10 @@ hir_analysis_pass_to_variadic_function = can't pass `{$ty}` to variadic function
.help = cast the value to `{$cast_ty}` .help = cast the value to `{$cast_ty}`
hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}` hir_analysis_cast_thin_pointer_to_fat_pointer = cannot cast thin pointer `{$expr_ty}` to fat pointer `{$cast_ty}`
hir_analysis_invalid_union_field =
field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
.note = union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
hir_analysis_invalid_union_field_sugg =
wrap the field type in `ManuallyDrop<...>`

View file

@ -1,5 +1,5 @@
use crate::check::intrinsicck::InlineAsmCtxt; use crate::check::intrinsicck::InlineAsmCtxt;
use crate::errors::LinkageType; use crate::errors::{self, LinkageType};
use super::compare_impl_item::check_type_bounds; use super::compare_impl_item::check_type_bounds;
use super::compare_impl_item::{compare_impl_method, compare_impl_ty}; use super::compare_impl_item::{compare_impl_method, compare_impl_ty};
@ -114,9 +114,11 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
allowed_union_field(*elem, tcx, param_env) allowed_union_field(*elem, tcx, param_env)
} }
_ => { _ => {
// Fallback case: allow `ManuallyDrop` and things that are `Copy`. // Fallback case: allow `ManuallyDrop` and things that are `Copy`,
// also no need to report an error if the type is unresolved.
ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop()) ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop())
|| ty.is_copy_modulo_regions(tcx, param_env) || ty.is_copy_modulo_regions(tcx, param_env)
|| ty.references_error()
} }
} }
} }
@ -131,26 +133,14 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
Some(Node::Field(field)) => (field.span, field.ty.span), Some(Node::Field(field)) => (field.span, field.ty.span),
_ => unreachable!("mir field has to correspond to hir field"), _ => unreachable!("mir field has to correspond to hir field"),
}; };
struct_span_err!( tcx.sess.emit_err(errors::InvalidUnionField {
tcx.sess,
field_span, field_span,
E0740, sugg: errors::InvalidUnionFieldSuggestion {
"unions cannot contain fields that may need dropping" lo: ty_span.shrink_to_lo(),
) hi: ty_span.shrink_to_hi(),
.note( },
"a type is guaranteed not to need dropping \ note: (),
when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type", });
)
.multipart_suggestion_verbose(
"when the type does not implement `Copy`, \
wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
vec![
(ty_span.shrink_to_lo(), "std::mem::ManuallyDrop<".into()),
(ty_span.shrink_to_hi(), ">".into()),
],
Applicability::MaybeIncorrect,
)
.emit();
return false; return false;
} else if field_ty.needs_drop(tcx, param_env) { } else if field_ty.needs_drop(tcx, param_env) {
// This should never happen. But we can get here e.g. in case of name resolution errors. // This should never happen. But we can get here e.g. in case of name resolution errors.

View file

@ -5,7 +5,7 @@ use rustc_errors::{
error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic, error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
MultiSpan, MultiSpan,
}; };
use rustc_macros::Diagnostic; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_span::{symbol::Ident, Span, Symbol}; use rustc_span::{symbol::Ident, Span, Symbol};
@ -430,3 +430,23 @@ pub(crate) struct CastThinPointerToFatPointer<'tcx> {
pub expr_ty: Ty<'tcx>, pub expr_ty: Ty<'tcx>,
pub cast_ty: String, pub cast_ty: String,
} }
#[derive(Diagnostic)]
#[diag(hir_analysis_invalid_union_field, code = "E0740")]
pub(crate) struct InvalidUnionField {
#[primary_span]
pub field_span: Span,
#[subdiagnostic]
pub sugg: InvalidUnionFieldSuggestion,
#[note]
pub note: (),
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(hir_analysis_invalid_union_field_sugg, applicability = "machine-applicable")]
pub(crate) struct InvalidUnionFieldSuggestion {
#[suggestion_part(code = "std::mem::ManuallyDrop<")]
pub lo: Span,
#[suggestion_part(code = ">")]
pub hi: Span,
}

View file

@ -21,15 +21,15 @@ union U24<T> { // OK
} }
union U3 { union U3 {
a: String, //~ ERROR unions cannot contain fields that may need dropping a: String, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union U32 { // field that does not drop but is not `Copy`, either union U32 { // field that does not drop but is not `Copy`, either
a: std::cell::RefCell<i32>, //~ ERROR unions cannot contain fields that may need dropping a: std::cell::RefCell<i32>, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union U4<T> { union U4<T> {
a: T, //~ ERROR unions cannot contain fields that may need dropping a: T, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union U5 { // Having a drop impl is OK union U5 { // Having a drop impl is OK
@ -41,11 +41,11 @@ impl Drop for U5 {
} }
union U5Nested { // a nested union that drops is NOT OK union U5Nested { // a nested union that drops is NOT OK
nest: U5, //~ ERROR unions cannot contain fields that may need dropping nest: U5, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union U5Nested2 { // for now we don't special-case empty arrays union U5Nested2 { // for now we don't special-case empty arrays
nest: [U5; 0], //~ ERROR unions cannot contain fields that may need dropping nest: [U5; 0], //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union U6 { // OK union U6 { // OK

View file

@ -1,59 +1,59 @@
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:24:5 --> $DIR/field_checks.rs:24:5
| |
LL | a: String, LL | a: String,
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<String>, LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:28:5 --> $DIR/field_checks.rs:28:5
| |
LL | a: std::cell::RefCell<i32>, LL | a: std::cell::RefCell<i32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<std::cell::RefCell<i32>>, LL | a: std::mem::ManuallyDrop<std::cell::RefCell<i32>>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:32:5 --> $DIR/field_checks.rs:32:5
| |
LL | a: T, LL | a: T,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<T>, LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:44:5 --> $DIR/field_checks.rs:44:5
| |
LL | nest: U5, LL | nest: U5,
| ^^^^^^^^ | ^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | nest: std::mem::ManuallyDrop<U5>, LL | nest: std::mem::ManuallyDrop<U5>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/field_checks.rs:48:5 --> $DIR/field_checks.rs:48:5
| |
LL | nest: [U5; 0], LL | nest: [U5; 0],
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | nest: std::mem::ManuallyDrop<[U5; 0]>, LL | nest: std::mem::ManuallyDrop<[U5; 0]>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +

View file

@ -1,5 +1,5 @@
union Test { union Test {
a: A, //~ ERROR unions cannot contain fields that may need dropping a: A, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
b: B b: B
} }

View file

@ -1,11 +1,11 @@
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/issue-41073.rs:2:5 --> $DIR/issue-41073.rs:2:5
| |
LL | a: A, LL | a: A,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<A>, LL | a: std::mem::ManuallyDrop<A>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +

View file

@ -1,35 +1,35 @@
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:11:5 --> $DIR/union-with-drop-fields.rs:11:5
| |
LL | a: String, LL | a: String,
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<String>, LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:19:5 --> $DIR/union-with-drop-fields.rs:19:5
| |
LL | a: S, LL | a: S,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<S>, LL | a: std::mem::ManuallyDrop<S>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:24:5 --> $DIR/union-with-drop-fields.rs:24:5
| |
LL | a: T, LL | a: T,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<T>, LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +

View file

@ -8,7 +8,7 @@ union U {
} }
union W { union W {
a: String, //~ ERROR unions cannot contain fields that may need dropping a: String, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
b: String, // OK, only one field is reported b: String, // OK, only one field is reported
} }
@ -16,12 +16,12 @@ struct S(String);
// `S` doesn't implement `Drop` trait, but still has non-trivial destructor // `S` doesn't implement `Drop` trait, but still has non-trivial destructor
union Y { union Y {
a: S, //~ ERROR unions cannot contain fields that may need dropping a: S, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
// We don't know if `T` is trivially-destructable or not until trans // We don't know if `T` is trivially-destructable or not until trans
union J<T> { union J<T> {
a: T, //~ ERROR unions cannot contain fields that may need dropping a: T, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
} }
union H<T: Copy> { union H<T: Copy> {

View file

@ -1,35 +1,35 @@
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:11:5 --> $DIR/union-with-drop-fields.rs:11:5
| |
LL | a: String, LL | a: String,
| ^^^^^^^^^ | ^^^^^^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<String>, LL | a: std::mem::ManuallyDrop<String>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:19:5 --> $DIR/union-with-drop-fields.rs:19:5
| |
LL | a: S, LL | a: S,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<S>, LL | a: std::mem::ManuallyDrop<S>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +
error[E0740]: unions cannot contain fields that may need dropping error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> $DIR/union-with-drop-fields.rs:24:5 --> $DIR/union-with-drop-fields.rs:24:5
| |
LL | a: T, LL | a: T,
| ^^^^ | ^^^^
| |
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type = note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped help: wrap the field type in `ManuallyDrop<...>`
| |
LL | a: std::mem::ManuallyDrop<T>, LL | a: std::mem::ManuallyDrop<T>,
| +++++++++++++++++++++++ + | +++++++++++++++++++++++ +

View file

@ -0,0 +1,8 @@
// Unresolved fields are not copy, but also shouldn't report an extra E0740.
pub union Foo {
x: *const Missing,
//~^ ERROR cannot find type `Missing` in this scope
}
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0412]: cannot find type `Missing` in this scope
--> $DIR/unresolved-field-isnt-copy.rs:4:15
|
LL | x: *const Missing,
| ^^^^^^^ not found in this scope
error: aborting due to previous error
For more information about this error, try `rustc --explain E0412`.