rework error messages for incorrect inherent impls
This commit is contained in:
parent
ee62514b16
commit
00cf7af44a
9 changed files with 89 additions and 50 deletions
|
@ -4,7 +4,7 @@ enum, union, or trait object.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0118
|
||||
impl (u8, u8) { // error: no nominal type found for inherent implementation
|
||||
impl fn(u8) { // error: no nominal type found for inherent implementation
|
||||
fn get_state(&self) -> String {
|
||||
// ...
|
||||
}
|
||||
|
@ -20,8 +20,8 @@ trait LiveLongAndProsper {
|
|||
fn get_state(&self) -> String;
|
||||
}
|
||||
|
||||
// and now you can implement it on (u8, u8)
|
||||
impl LiveLongAndProsper for (u8, u8) {
|
||||
// and now you can implement it on fn(u8)
|
||||
impl LiveLongAndProsper for fn(u8) {
|
||||
fn get_state(&self) -> String {
|
||||
"He's dead, Jim!".to_owned()
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ For example, `NewType` is a newtype over `Foo` in `struct NewType(Foo)`.
|
|||
Example:
|
||||
|
||||
```
|
||||
struct TypeWrapper((u8, u8));
|
||||
struct TypeWrapper(fn(u8));
|
||||
|
||||
impl TypeWrapper {
|
||||
fn get_state(&self) -> String {
|
||||
|
@ -41,24 +41,3 @@ impl TypeWrapper {
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
Instead of defining an inherent implementation on a reference, you could also
|
||||
move the reference inside the implementation:
|
||||
|
||||
```compile_fail,E0118
|
||||
struct Foo;
|
||||
|
||||
impl &Foo { // error: no nominal type found for inherent implementation
|
||||
fn bar(self, other: Self) {}
|
||||
}
|
||||
```
|
||||
|
||||
becomes
|
||||
|
||||
```
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar(&self, other: &Self) {}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -8,8 +8,7 @@ struct Foo {
|
|||
}
|
||||
|
||||
impl *mut Foo {}
|
||||
// error: only a single inherent implementation marked with
|
||||
// `#[lang = "mut_ptr"]` is allowed for the `*mut T` primitive
|
||||
// error: cannot define inherent `impl` for primitive types
|
||||
```
|
||||
|
||||
This isn't allowed, but using a trait to implement a method or constant
|
||||
|
@ -29,3 +28,24 @@ impl Bar for *mut Foo {
|
|||
fn bar() {} // ok!
|
||||
}
|
||||
```
|
||||
|
||||
Instead of defining an inherent implementation on a reference, you could also
|
||||
move the reference inside the implementation:
|
||||
|
||||
```compile_fail,E0390
|
||||
struct Foo;
|
||||
|
||||
impl &Foo { // error: no nominal type found for inherent implementation
|
||||
fn bar(self, other: Self) {}
|
||||
}
|
||||
```
|
||||
|
||||
becomes
|
||||
|
||||
```
|
||||
struct Foo;
|
||||
|
||||
impl Foo {
|
||||
fn bar(&self, other: &Self) {}
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue