Skip to content

Commit be38926

Browse files
Add E0119 error explanation
Add more explanations
1 parent 2881e83 commit be38926

File tree

1 file changed

+77
-8
lines changed

1 file changed

+77
-8
lines changed

src/librustc_typeck/diagnostics.rs

+77-8
Original file line numberDiff line numberDiff line change
@@ -1245,22 +1245,92 @@ impl Bytes { ... } // error, same as above
12451245
"##,
12461246

12471247
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:
12501251
12511252
```
12521253
impl Drop for u32 {}
12531254
```
12541255
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:
12571258
12581259
```
12591260
pub struct Foo; // you define your type in your crate
12601261
12611262
impl Drop for Foo { // and you can implement the trait on it!
12621263
// code of trait implementation here
12631264
}
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+
}
12641334
```
12651335
"##,
12661336

@@ -1403,7 +1473,7 @@ impl Trait for Foo {
14031473
}
14041474
```
14051475
1406-
The `'b` lifetime constraint for bar() implementation does not match the
1476+
The lifetime constraint `'b` for bar() implementation does not match the
14071477
trait declaration. Ensure lifetime declarations match exactly in both trait
14081478
declaration and implementation. Example:
14091479
@@ -1601,8 +1671,8 @@ impl Copy for &'static Bar { } // error
16011671
"##,
16021672

16031673
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:
16061676
16071677
```
16081678
trait MyTrait {
@@ -1883,7 +1953,6 @@ register_diagnostics! {
18831953
E0103,
18841954
E0104,
18851955
E0118,
1886-
E0119,
18871956
E0120,
18881957
E0122,
18891958
E0123,

0 commit comments

Comments
 (0)