This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +16
-8
lines changed
compiler/rustc_error_codes/src/error_codes Expand file tree Collapse file tree 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.
33
4- Erroneous code example :
4+ Erroneous code examples :
55
66``` compile_fail,E0120
77trait MyTrait {}
@@ -11,8 +11,16 @@ impl Drop for MyTrait {
1111}
1212```
1313
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:
1624
1725```
1826trait MyTrait {}
@@ -24,13 +32,13 @@ impl <T: MyTrait> Drop for MyWrapper<T> {
2432
2533```
2634
27- Alternatively, wrapping trait objects requires something :
35+ Alternatively, the ` Drop ` wrapper can contain the trait object :
2836
2937```
3038trait MyTrait {}
3139
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 }
3442
3543impl <'a> Drop for MyWrapper<'a> {
3644 fn drop(&mut self) {}
You can’t perform that action at this time.
0 commit comments