Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Member

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.

```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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to compile this code, I get E0220, not E0406:

$ cat scratch.rs 
trait Foo {
    type Bar;

    fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
}

fn main() {}
$ rustc scratch.rs 
scratch.rs:4:40: 4:49 error: associated type `Baz` not found for `Self` [E0220]
scratch.rs:4     fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool;
                                                    ^~~~~~~~~
scratch.rs:4:40: 4:49 help: run `rustc --explain E0220` to see a detailed explanation
error: aborting due to previous error
$ rustc --version
rustc 1.11.0-nightly (5c2a5d449 2016-06-11)

I've found that greping around in the codebase to see where an error code is being generated can provide a clue as to what kind of examples would trigger that error (although usually not a very good clue, given the vastness of my ignorance of the compiler internals); in this case, it looks like E406 will get emitted during rustc_resolve::Resolver.resolve_generics (the mapping of error to error-code happens elsewhere in the file) ... but I don't know what that means or when it happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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. :-/

```
You tried to use an associated type which hasn't been declared in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing an empty line before this paragraph.

trait body. All associated types must be declared with the
type keyword, e.g. `type a;` before being used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like very much "e.g.". But since I don't have something in mind to replace, let's keep it (except if you find something better).


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
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
```

For more on associated types, refer to the associated types section
of the Rust book:

https://doc.rust-lang.org/book/associated-types.html
"##,

E0407: r##"
A definition of a method not in the implemented trait was given in a trait
implementation. Example of erroneous code:
Expand Down Expand Up @@ -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
Expand Down