-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add error description for E0406 #34230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -444,6 +444,52 @@ impl SomeTrait for Foo { // ok! | |
``` | ||
"##, | ||
|
||
E0406: r##" | ||
A function is referring to an associated type which hasn't been declared in | ||
the trait. Example of erroneous code: | ||
|
||
```compile_fail | ||
trait Foo { | ||
type Bar; | ||
|
||
// error: function signature contains a reference to `Self::Baz`, but | ||
// `Baz` hasn't been declared as an associated type of the trait | ||
fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I try to compile this code, I get E0220, not E0406:
I've found that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried searching the codebase, hoping to find answers, but like you, my knowledge of compilers isn't exactly top-notch. I suspect the compiler is showing E0220 until this lands. I asked on the IRC regrading this as well, and it seems that E0406 should be thrown here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah damn, I tested your code on release. Didn't think it'd change on nightly. :-/ |
||
``` | ||
|
||
The use of 'associated types' allows us declare variables which might be used | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that's not what I meant. Don't explain what the user tried to do but in which he/she did wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well.... I can't understand what you are trying to say. 😕 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'll try to explain more clearly. So in here, what's expected is something which helps the user understands the error. Easy example: let mut x: u32 = 0;
x = "salut !"; It'll fail because the expected type doesn't correspond to the one we put in "The type of a variable cannot change once declared. If you want the type of Maybe add a link to the variable chapter in the book in case the user really doesn't know how it works. And then you put examples which show it. Did it help? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, that was useful. I will push a new commit tomorrow, amending the explanation. |
||
in any of the function signatures in a particular trait. This helps in | ||
eliminating redundancies and improves readability of code. It works by using | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "helps eliminate redundancies and improve readability" would have more parallelism (eliminate and improve being bare infinitives complementing helps). |
||
the `type` keyword to declare a variable (Eg: `type A;`), and then using it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "e.g.," is more standard. |
||
in the function signature as `&Self::A`. `Self` refers to the object which | ||
has invoked the method, and this object can be of any type for which trait | ||
`Foo` has been implemented. | ||
|
||
One solution might be to declare the associated type in the trait: | ||
|
||
``` | ||
trait Foo { | ||
type Bar; | ||
type Baz; // declare `Baz` | ||
|
||
fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; | ||
} | ||
``` | ||
|
||
Alternatively, you could remove the input variable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing an empty line before this block. |
||
corresponding to the associated type from the function signature: | ||
|
||
``` | ||
trait Foo { | ||
type Bar; | ||
|
||
// `&Self::Baz` has been removed | ||
fn return_bool(&self, &Self::Bar) -> bool; | ||
} | ||
``` | ||
"##, | ||
|
||
E0407: r##" | ||
A definition of a method not in the implemented trait was given in a trait | ||
implementation. Example of erroneous code: | ||
|
@@ -1105,7 +1151,6 @@ register_diagnostics! { | |
// E0257, | ||
// E0258, | ||
E0402, // cannot use an outer type parameter in this context | ||
E0406, // undeclared associated type | ||
// E0410, merged into 408 | ||
// E0413, merged into 530 | ||
// E0414, merged into 530 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot "Example of erroneous code:". Take a look at the RFC for more information.