Skip to content

Commit cc9db52

Browse files
Add long error explanation for E0493
1 parent c9edc02 commit cc9db52

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/librustc_mir/error_codes.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,51 @@ Remember this solution is unsafe! You will have to ensure that accesses to the
11281128
cell are synchronized.
11291129
"##,
11301130

1131+
E0493: r##"
1132+
A type with a `Drop` implementation was destructured when trying to initialize
1133+
a static item.
1134+
1135+
Erroneous code example:
1136+
1137+
```compile_fail,E0493
1138+
enum DropType {
1139+
A,
1140+
}
1141+
1142+
impl Drop for DropType {
1143+
fn drop(&mut self) {}
1144+
}
1145+
1146+
struct Foo {
1147+
field1: DropType,
1148+
}
1149+
1150+
static FOO: Foo = Foo { ..Foo { field1: DropType::A } }; // error!
1151+
```
1152+
1153+
The problem here is that if the given type or one of its fields implements the
1154+
`Drop` trait, this `Drop` implementation cannot be called during the static
1155+
type initialization which might cause a memory leak. To prevent this issue,
1156+
you need to instantiate all the static type's fields by hand.
1157+
1158+
```
1159+
enum DropType {
1160+
A,
1161+
}
1162+
1163+
impl Drop for DropType {
1164+
fn drop(&mut self) {}
1165+
}
1166+
1167+
struct Foo {
1168+
field1: DropType,
1169+
}
1170+
1171+
static FOO: Foo = Foo { field1: DropType::A }; // We initialize all fields
1172+
// by hand.
1173+
```
1174+
"##,
1175+
11311176
E0499: r##"
11321177
A variable was borrowed as mutable more than once. Erroneous code example:
11331178
@@ -2391,7 +2436,6 @@ There are some known bugs that trigger this message.
23912436
// E0299, // mismatched types between arms
23922437
// E0471, // constant evaluation error (in pattern)
23932438
// E0385, // {} in an aliasable location
2394-
E0493, // destructors cannot be evaluated at compile-time
23952439
E0521, // borrowed data escapes outside of closure
23962440
E0524, // two closures require unique access to `..` at the same time
23972441
E0526, // shuffle indices are not constant

0 commit comments

Comments
 (0)