File tree 1 file changed +16
-8
lines changed
compiler/rustc_error_codes/src/error_codes
1 file changed +16
-8
lines changed Original file line number Diff line number Diff line change 1
- Drop was implemented on a trait, which is not allowed: only structs and
2
- enums can implement Drop.
1
+ ` Drop ` was implemented on a trait object or reference , which is not allowed;
2
+ only structs, enums, and unions can implement Drop.
3
3
4
- Erroneous code example :
4
+ Erroneous code examples :
5
5
6
6
``` compile_fail,E0120
7
7
trait MyTrait {}
@@ -11,8 +11,16 @@ impl Drop for MyTrait {
11
11
}
12
12
```
13
13
14
- A workaround for this problem is to wrap the trait up in a struct, and implement
15
- Drop on that:
14
+ ``` compile_fail,E0120
15
+ struct Concrete {}
16
+
17
+ impl Drop for &'_ mut Concrete {
18
+ fn drop(&mut self) {}
19
+ }
20
+ ```
21
+
22
+ A workaround for traits is to create a wrapper struct with a generic type,
23
+ add a trait bound to the type, and implement ` Drop ` on the wrapper:
16
24
17
25
```
18
26
trait MyTrait {}
@@ -24,13 +32,13 @@ impl <T: MyTrait> Drop for MyWrapper<T> {
24
32
25
33
```
26
34
27
- Alternatively, wrapping trait objects requires something :
35
+ Alternatively, the ` Drop ` wrapper can contain the trait object :
28
36
29
37
```
30
38
trait MyTrait {}
31
39
32
- //or Box<MyTrait>, if you wanted an owned trait object
33
- struct MyWrapper<'a> { foo: &'a MyTrait }
40
+ // or Box<dyn MyTrait>, if you wanted an owned trait object
41
+ struct MyWrapper<'a> { foo: &'a dyn MyTrait }
34
42
35
43
impl <'a> Drop for MyWrapper<'a> {
36
44
fn drop(&mut self) {}
You can’t perform that action at this time.
0 commit comments