Skip to content
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

Confusing error message for missing associated type #66380

Closed
amunra opened this issue Nov 13, 2019 · 1 comment · Fixed by #67268
Closed

Confusing error message for missing associated type #66380

amunra opened this issue Nov 13, 2019 · 1 comment · Fixed by #67268
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@amunra
Copy link

amunra commented Nov 13, 2019

I have encountered associated types for the first time through a build error.

Associated types are used infrequently and other programmers will probably encounter the feature for the first time in a similar manner.

The error was particularly confusing because I thought that I was asked to provide a generic type to a type that was not generic. I was left rather puzzled.

It would be particularly helpful if in this instance the compiler could use information passed in from the single call point (i.e., from main) to suggest a fix (Feather<Error=String>).

The error message:

error[E0191]: the value of the associated type `Error` (from the trait `Feather`) must be specified
  --> src/main.rs:22:83
   |
3  |     type Error;
   |     ----------- `Error` defined here
...
22 | fn pick_feather<'a>(first: bool, f1: &'a dyn Feather, f2: &'a dyn Feather) -> &'a dyn Feather {
   |                                                                                   ^^^^^^^^^^^ associated type `Error` must be specified

A short program to replicate the error message:

trait Feather {
    type Error;

    fn float(&self, x: i32, y: Self::Error) -> Result<i32, Self::Error> {
        if x > 0 {
            Ok(x)
        }
        else {
            Err(y)
        }
    }
}

struct RedFeather {}
impl Feather for RedFeather {
    type Error = String;
}

struct BlueFeather {}
impl Feather for BlueFeather {
    type Error = String;
}

fn pick_feather<'a>(first: bool, f1: &'a dyn Feather, f2: &'a dyn Feather) -> &'a dyn Feather {
    if first {
        f1
    }
    else {
        f2
    }
}

fn main() {
    let red = RedFeather {};
    let blue = BlueFeather {};
    let feather = pick_feather(false, &red, &red);
    feather.float(3, "oh no".to_owned()).unwrap();
}

Ideally, the final suggestion would read:

fn pick_feather<'a>(
    first: bool,
    f1: &'a dyn Feather<Error=String>,
    f2: &'a dyn Feather<Error=String>) -> &'a dyn Feather<Error=String> {
@jonas-schievink jonas-schievink added A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 13, 2019
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Nov 13, 2019
@estebank
Copy link
Contributor

estebank commented Dec 12, 2019

Would the following output (as given by #67268) have been enough to help you figure things out?

error[E0191]: the value of the associated type `Error` (from trait `Feather`) must be specified
  --> file12.rs:24:87
   |
2  |     type Error;
   |     ----------- `Error` defined here
...
24 | fn pick_feather<'a>(first: bool, f1: &'a dyn Feather, f2: &'a dyn Feather) -> &'a dyn Feather {
   |                                                                                       ^^^^^^^ help: specify the associated type: `Feather<Error = Type>`

error[E0191]: the value of the associated type `Error` (from trait `Feather`) must be specified
  --> file12.rs:24:46
   |
2  |     type Error;
   |     ----------- `Error` defined here
...
24 | fn pick_feather<'a>(first: bool, f1: &'a dyn Feather, f2: &'a dyn Feather) -> &'a dyn Feather {
   |                                              ^^^^^^^ help: specify the associated type: `Feather<Error = Type>`

error[E0191]: the value of the associated type `Error` (from trait `Feather`) must be specified
  --> file12.rs:24:67
   |
2  |     type Error;
   |     ----------- `Error` defined here
...
24 | fn pick_feather<'a>(first: bool, f1: &'a dyn Feather, f2: &'a dyn Feather) -> &'a dyn Feather {
   |                                                                   ^^^^^^^ help: specify the associated type: `Feather<Error = Type>`

error: aborting due to 3 previous errors

Centril added a commit to Centril/rust that referenced this issue Dec 20, 2019
Tweak errors for missing associated types and type parameters

* On `dyn Trait` missing associated types, provide a structured suggestion for them
* On missing type parameters, provide structured suggestion for them
* Point at trait definition when missing required type parameter
* Tweak output of E0658
* Tweak wording of E0719
* Account for `Trait1 + Trait2` case

Fix rust-lang#66380, fix rust-lang#60595. CC rust-lang#63711.
@bors bors closed this as completed in c0b16b4 Dec 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants