Skip to content

Commit e668175

Browse files
committed
Add diagnostics for E0120
1 parent 2e5b165 commit e668175

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,45 @@ fn main() {
13161316
```
13171317
"##,
13181318

1319+
E0120: r##"
1320+
An attempt was made to implement Drop on a trait, which is not allowed: only
1321+
structs and enums can implement Drop. An example causing this error:
1322+
1323+
```
1324+
trait MyTrait {}
1325+
1326+
impl Drop for MyTrait {
1327+
fn drop(&mut self) {}
1328+
}
1329+
```
1330+
1331+
A workaround for this problem is to wrap the trait up in a struct, and implement
1332+
Drop on that. An example is shown below:
1333+
1334+
```
1335+
trait MyTrait {}
1336+
struct MyWrapper<T: MyTrait> { foo: T }
1337+
1338+
impl <T: MyTrait> Drop for MyWrapper<T> {
1339+
fn drop(&mut self) {}
1340+
}
1341+
1342+
```
1343+
1344+
Alternatively, wrapping trait objects requires something like the following:
1345+
1346+
```
1347+
trait MyTrait {}
1348+
1349+
//or Box<MyTrait>, if you wanted an owned trait object
1350+
struct MyWrapper<'a> { foo: &'a MyTrait }
1351+
1352+
impl <'a> Drop for MyWrapper<'a> {
1353+
fn drop(&mut self) {}
1354+
}
1355+
```
1356+
"##,
1357+
13191358
E0121: r##"
13201359
In order to be consistent with Rust's lack of global type inference, type
13211360
placeholders are disallowed by design in item signatures.
@@ -2195,7 +2234,6 @@ register_diagnostics! {
21952234
E0103,
21962235
E0104,
21972236
E0118,
2198-
E0120,
21992237
E0122,
22002238
E0123,
22012239
E0127,

0 commit comments

Comments
 (0)