@@ -1245,22 +1245,92 @@ impl Bytes { ... } // error, same as above
1245
1245
"## ,
1246
1246
1247
1247
E0117 : r##"
1248
- You tried to implement a trait on a type which isn't defined in your crate.
1249
- Erroneous code example:
1248
+ You got this error because because you tried to implement a foreign
1249
+ trait for a foreign type (with maybe a foreign type parameter). Erroneous
1250
+ code example:
1250
1251
1251
1252
```
1252
1253
impl Drop for u32 {}
1253
1254
```
1254
1255
1255
- The type on which you want to implement the trait has to be defined in
1256
- your crate. Example:
1256
+ The type, trait or the type parameter (or all of them) has to be defined
1257
+ in your crate. Example:
1257
1258
1258
1259
```
1259
1260
pub struct Foo; // you define your type in your crate
1260
1261
1261
1262
impl Drop for Foo { // and you can implement the trait on it!
1262
1263
// code of trait implementation here
1263
1264
}
1265
+
1266
+ trait Bar { // or define your trait in your crate
1267
+ fn get(&self) -> usize;
1268
+ }
1269
+
1270
+ impl Bar for u32 { // and then you implement it on a foreign type
1271
+ fn get(&self) -> usize { 0 }
1272
+ }
1273
+
1274
+ impl From<Foo> for i32 { // or you use a type from your crate as
1275
+ // a type parameter
1276
+ fn from(i: Foo) -> i32 {
1277
+ 0
1278
+ }
1279
+ }
1280
+ ```
1281
+ "## ,
1282
+
1283
+ E0119 : r##"
1284
+ There are conflicting trait implementations for the same type.
1285
+ Erroneous code example:
1286
+
1287
+ ```
1288
+ trait MyTrait {
1289
+ fn get(&self) -> usize;
1290
+ }
1291
+
1292
+ impl<T> MyTrait for T {
1293
+ fn get(&self) -> usize { 0 }
1294
+ }
1295
+
1296
+ struct Foo {
1297
+ value: usize
1298
+ }
1299
+
1300
+ impl MyTrait for Foo { // error: conflicting implementations for trait
1301
+ // `MyTrait`
1302
+ fn get(&self) -> usize { self.value }
1303
+ }
1304
+ ```
1305
+
1306
+ When you write:
1307
+
1308
+ ```
1309
+ impl<T> MyTrait for T {
1310
+ fn get(&self) -> usize { 0 }
1311
+ }
1312
+ ```
1313
+
1314
+ This makes the trait implemented on all types in the scope. So if you
1315
+ try to implement it on another one after that, the implementations will
1316
+ conflict. Example:
1317
+
1318
+ ```
1319
+ trait MyTrait {
1320
+ fn get(&self) -> usize;
1321
+ }
1322
+
1323
+ impl<T> MyTrait for T {
1324
+ fn get(&self) -> usize { 0 }
1325
+ }
1326
+
1327
+ struct Foo;
1328
+
1329
+ fn main() {
1330
+ let f = Foo;
1331
+
1332
+ f.get(); // the trait is implemented so we can use it
1333
+ }
1264
1334
```
1265
1335
"## ,
1266
1336
@@ -1403,7 +1473,7 @@ impl Trait for Foo {
1403
1473
}
1404
1474
```
1405
1475
1406
- The `'b` lifetime constraint for bar() implementation does not match the
1476
+ The lifetime constraint `'b` for bar() implementation does not match the
1407
1477
trait declaration. Ensure lifetime declarations match exactly in both trait
1408
1478
declaration and implementation. Example:
1409
1479
@@ -1601,8 +1671,8 @@ impl Copy for &'static Bar { } // error
1601
1671
"## ,
1602
1672
1603
1673
E0207 : r##"
1604
- You passed an unused type parameter when implementing a trait
1605
- on an object. Erroneous code example:
1674
+ You declared an unused type parameter when implementing a trait on an object.
1675
+ Erroneous code example:
1606
1676
1607
1677
```
1608
1678
trait MyTrait {
@@ -1883,7 +1953,6 @@ register_diagnostics! {
1883
1953
E0103 ,
1884
1954
E0104 ,
1885
1955
E0118 ,
1886
- E0119 ,
1887
1956
E0120 ,
1888
1957
E0122 ,
1889
1958
E0123 ,
0 commit comments