Skip to content

Commit 45affd5

Browse files
authored
Rollup merge of #108882 - compiler-errors:E0740, r=eholk
Tweak E0740 Also drive-by suppress E0740 if it's an unresolved type.
2 parents 693d05b + 64eea3c commit 45affd5

12 files changed

+101
-67
lines changed

compiler/rustc_hir_analysis/locales/en-US.ftl

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

compiler/rustc_hir_analysis/src/check/check.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::check::intrinsicck::InlineAsmCtxt;
2-
use crate::errors::LinkageType;
2+
use crate::errors::{self, LinkageType};
33

44
use super::compare_impl_item::check_type_bounds;
55
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
114114
allowed_union_field(*elem, tcx, param_env)
115115
}
116116
_ => {
117-
// Fallback case: allow `ManuallyDrop` and things that are `Copy`.
117+
// Fallback case: allow `ManuallyDrop` and things that are `Copy`,
118+
// also no need to report an error if the type is unresolved.
118119
ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop())
119120
|| ty.is_copy_modulo_regions(tcx, param_env)
121+
|| ty.references_error()
120122
}
121123
}
122124
}
@@ -131,26 +133,14 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b
131133
Some(Node::Field(field)) => (field.span, field.ty.span),
132134
_ => unreachable!("mir field has to correspond to hir field"),
133135
};
134-
struct_span_err!(
135-
tcx.sess,
136+
tcx.sess.emit_err(errors::InvalidUnionField {
136137
field_span,
137-
E0740,
138-
"unions cannot contain fields that may need dropping"
139-
)
140-
.note(
141-
"a type is guaranteed not to need dropping \
142-
when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type",
143-
)
144-
.multipart_suggestion_verbose(
145-
"when the type does not implement `Copy`, \
146-
wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
147-
vec![
148-
(ty_span.shrink_to_lo(), "std::mem::ManuallyDrop<".into()),
149-
(ty_span.shrink_to_hi(), ">".into()),
150-
],
151-
Applicability::MaybeIncorrect,
152-
)
153-
.emit();
138+
sugg: errors::InvalidUnionFieldSuggestion {
139+
lo: ty_span.shrink_to_lo(),
140+
hi: ty_span.shrink_to_hi(),
141+
},
142+
note: (),
143+
});
154144
return false;
155145
} else if field_ty.needs_drop(tcx, param_env) {
156146
// This should never happen. But we can get here e.g. in case of name resolution errors.

compiler/rustc_hir_analysis/src/errors.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{
55
error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
66
MultiSpan,
77
};
8-
use rustc_macros::Diagnostic;
8+
use rustc_macros::{Diagnostic, Subdiagnostic};
99
use rustc_middle::ty::Ty;
1010
use rustc_span::{symbol::Ident, Span, Symbol};
1111

@@ -430,3 +430,23 @@ pub(crate) struct CastThinPointerToFatPointer<'tcx> {
430430
pub expr_ty: Ty<'tcx>,
431431
pub cast_ty: String,
432432
}
433+
434+
#[derive(Diagnostic)]
435+
#[diag(hir_analysis_invalid_union_field, code = "E0740")]
436+
pub(crate) struct InvalidUnionField {
437+
#[primary_span]
438+
pub field_span: Span,
439+
#[subdiagnostic]
440+
pub sugg: InvalidUnionFieldSuggestion,
441+
#[note]
442+
pub note: (),
443+
}
444+
445+
#[derive(Subdiagnostic)]
446+
#[multipart_suggestion(hir_analysis_invalid_union_field_sugg, applicability = "machine-applicable")]
447+
pub(crate) struct InvalidUnionFieldSuggestion {
448+
#[suggestion_part(code = "std::mem::ManuallyDrop<")]
449+
pub lo: Span,
450+
#[suggestion_part(code = ">")]
451+
pub hi: Span,
452+
}

tests/ui/union/field_checks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ union U24<T> { // OK
2121
}
2222

2323
union U3 {
24-
a: String, //~ ERROR unions cannot contain fields that may need dropping
24+
a: String, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2525
}
2626

2727
union U32 { // field that does not drop but is not `Copy`, either
28-
a: std::cell::RefCell<i32>, //~ ERROR unions cannot contain fields that may need dropping
28+
a: std::cell::RefCell<i32>, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2929
}
3030

3131
union U4<T> {
32-
a: T, //~ ERROR unions cannot contain fields that may need dropping
32+
a: T, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
3333
}
3434

3535
union U5 { // Having a drop impl is OK
@@ -41,11 +41,11 @@ impl Drop for U5 {
4141
}
4242

4343
union U5Nested { // a nested union that drops is NOT OK
44-
nest: U5, //~ ERROR unions cannot contain fields that may need dropping
44+
nest: U5, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
4545
}
4646

4747
union U5Nested2 { // for now we don't special-case empty arrays
48-
nest: [U5; 0], //~ ERROR unions cannot contain fields that may need dropping
48+
nest: [U5; 0], //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
4949
}
5050

5151
union U6 { // OK

tests/ui/union/field_checks.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
error[E0740]: unions cannot contain fields that may need dropping
1+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
22
--> $DIR/field_checks.rs:24:5
33
|
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
7+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
8+
help: wrap the field type in `ManuallyDrop<...>`
99
|
1010
LL | a: std::mem::ManuallyDrop<String>,
1111
| +++++++++++++++++++++++ +
1212

13-
error[E0740]: unions cannot contain fields that may need dropping
13+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
1414
--> $DIR/field_checks.rs:28:5
1515
|
1616
LL | a: std::cell::RefCell<i32>,
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
|
19-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
20-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
19+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
20+
help: wrap the field type in `ManuallyDrop<...>`
2121
|
2222
LL | a: std::mem::ManuallyDrop<std::cell::RefCell<i32>>,
2323
| +++++++++++++++++++++++ +
2424

25-
error[E0740]: unions cannot contain fields that may need dropping
25+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2626
--> $DIR/field_checks.rs:32:5
2727
|
2828
LL | a: T,
2929
| ^^^^
3030
|
31-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
32-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
31+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
32+
help: wrap the field type in `ManuallyDrop<...>`
3333
|
3434
LL | a: std::mem::ManuallyDrop<T>,
3535
| +++++++++++++++++++++++ +
3636

37-
error[E0740]: unions cannot contain fields that may need dropping
37+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
3838
--> $DIR/field_checks.rs:44:5
3939
|
4040
LL | nest: U5,
4141
| ^^^^^^^^
4242
|
43-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
44-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
43+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
44+
help: wrap the field type in `ManuallyDrop<...>`
4545
|
4646
LL | nest: std::mem::ManuallyDrop<U5>,
4747
| +++++++++++++++++++++++ +
4848

49-
error[E0740]: unions cannot contain fields that may need dropping
49+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
5050
--> $DIR/field_checks.rs:48:5
5151
|
5252
LL | nest: [U5; 0],
5353
| ^^^^^^^^^^^^^
5454
|
55-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
56-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
55+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
56+
help: wrap the field type in `ManuallyDrop<...>`
5757
|
5858
LL | nest: std::mem::ManuallyDrop<[U5; 0]>,
5959
| +++++++++++++++++++++++ +

tests/ui/union/issue-41073.rs

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

tests/ui/union/issue-41073.stderr

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

tests/ui/union/union-with-drop-fields.mirunsafeck.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
error[E0740]: unions cannot contain fields that may need dropping
1+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
22
--> $DIR/union-with-drop-fields.rs:11:5
33
|
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
7+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
8+
help: wrap the field type in `ManuallyDrop<...>`
99
|
1010
LL | a: std::mem::ManuallyDrop<String>,
1111
| +++++++++++++++++++++++ +
1212

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

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

tests/ui/union/union-with-drop-fields.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ union U {
88
}
99

1010
union W {
11-
a: String, //~ ERROR unions cannot contain fields that may need dropping
11+
a: String, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
1212
b: String, // OK, only one field is reported
1313
}
1414

1515
struct S(String);
1616

1717
// `S` doesn't implement `Drop` trait, but still has non-trivial destructor
1818
union Y {
19-
a: S, //~ ERROR unions cannot contain fields that may need dropping
19+
a: S, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2020
}
2121

2222
// We don't know if `T` is trivially-destructable or not until trans
2323
union J<T> {
24-
a: T, //~ ERROR unions cannot contain fields that may need dropping
24+
a: T, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2525
}
2626

2727
union H<T: Copy> {

tests/ui/union/union-with-drop-fields.thirunsafeck.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
error[E0740]: unions cannot contain fields that may need dropping
1+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
22
--> $DIR/union-with-drop-fields.rs:11:5
33
|
44
LL | a: String,
55
| ^^^^^^^^^
66
|
7-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
8-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
7+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
8+
help: wrap the field type in `ManuallyDrop<...>`
99
|
1010
LL | a: std::mem::ManuallyDrop<String>,
1111
| +++++++++++++++++++++++ +
1212

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

25-
error[E0740]: unions cannot contain fields that may need dropping
25+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
2626
--> $DIR/union-with-drop-fields.rs:24:5
2727
|
2828
LL | a: T,
2929
| ^^^^
3030
|
31-
= note: a type is guaranteed not to need dropping when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type
32-
help: when the type does not implement `Copy`, wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped
31+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
32+
help: wrap the field type in `ManuallyDrop<...>`
3333
|
3434
LL | a: std::mem::ManuallyDrop<T>,
3535
| +++++++++++++++++++++++ +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Unresolved fields are not copy, but also shouldn't report an extra E0740.
2+
3+
pub union Foo {
4+
x: *const Missing,
5+
//~^ ERROR cannot find type `Missing` in this scope
6+
}
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0412]: cannot find type `Missing` in this scope
2+
--> $DIR/unresolved-field-isnt-copy.rs:4:15
3+
|
4+
LL | x: *const Missing,
5+
| ^^^^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)