Skip to content

Commit ec0fc17

Browse files
authored
Unrolled build for rust-lang#127949
Rollup merge of rust-lang#127949 - princess-entrapta:master, r=tgross35 fix: explain E0120 better cover cases when its raised Fixes rust-lang#98996 Wording change on the explain of E0120 as requested
2 parents 0cd01aa + af7ecb6 commit ec0fc17

File tree

1 file changed

+16
-8
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+16
-8
lines changed

compiler/rustc_error_codes/src/error_codes/E0120.md

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
77
trait 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
```
1826
trait 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
```
3038
trait 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
3543
impl <'a> Drop for MyWrapper<'a> {
3644
fn drop(&mut self) {}

0 commit comments

Comments
 (0)