mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
Rollup merge of #127949 - princess-entrapta:master, r=tgross35
fix: explain E0120 better cover cases when its raised Fixes https://github.com/rust-lang/rust/issues/98996 Wording change on the explain of E0120 as requested
This commit is contained in:
commit
41d3cb6dbe
@ -1,7 +1,7 @@
|
|||||||
Drop was implemented on a trait, which is not allowed: only structs and
|
`Drop` was implemented on a trait object or reference, which is not allowed;
|
||||||
enums can implement Drop.
|
only structs, enums, and unions can implement Drop.
|
||||||
|
|
||||||
Erroneous code example:
|
Erroneous code examples:
|
||||||
|
|
||||||
```compile_fail,E0120
|
```compile_fail,E0120
|
||||||
trait MyTrait {}
|
trait MyTrait {}
|
||||||
@ -11,8 +11,16 @@ impl Drop for MyTrait {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
A workaround for this problem is to wrap the trait up in a struct, and implement
|
```compile_fail,E0120
|
||||||
Drop on that:
|
struct Concrete {}
|
||||||
|
|
||||||
|
impl Drop for &'_ mut Concrete {
|
||||||
|
fn drop(&mut self) {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
A workaround for traits is to create a wrapper struct with a generic type,
|
||||||
|
add a trait bound to the type, and implement `Drop` on the wrapper:
|
||||||
|
|
||||||
```
|
```
|
||||||
trait MyTrait {}
|
trait MyTrait {}
|
||||||
@ -24,13 +32,13 @@ impl <T: MyTrait> Drop for MyWrapper<T> {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively, wrapping trait objects requires something:
|
Alternatively, the `Drop` wrapper can contain the trait object:
|
||||||
|
|
||||||
```
|
```
|
||||||
trait MyTrait {}
|
trait MyTrait {}
|
||||||
|
|
||||||
//or Box<MyTrait>, if you wanted an owned trait object
|
// or Box<dyn MyTrait>, if you wanted an owned trait object
|
||||||
struct MyWrapper<'a> { foo: &'a MyTrait }
|
struct MyWrapper<'a> { foo: &'a dyn MyTrait }
|
||||||
|
|
||||||
impl <'a> Drop for MyWrapper<'a> {
|
impl <'a> Drop for MyWrapper<'a> {
|
||||||
fn drop(&mut self) {}
|
fn drop(&mut self) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user