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

Error explanation for E0308 is incorrect and confusing #76462

Closed
camelid opened this issue Sep 8, 2020 · 7 comments · Fixed by #78332
Closed

Error explanation for E0308 is incorrect and confusing #76462

camelid opened this issue Sep 8, 2020 · 7 comments · Fixed by #78332
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-error-codes Area: Explanation of an error code (--explain) C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@camelid
Copy link
Member

camelid commented Sep 8, 2020

I believe the error message for E0308 is incorrect. Here it is, with my emphasis on the part that seems incorrect:

Expected type did not match the received type.

Erroneous code example:

let x: i32 = "I am not a number!";
//     ~~~   ~~~~~~~~~~~~~~~~~~~~
//      |             |
//      |    initializing expression;
//      |    compiler infers type `&str`
//      |
//    type `i32` assigned to variable `x`

This error occurs when the compiler is unable to infer the concrete type of a
variable.
It can occur in several cases, the most common being a mismatch
between two types: the type the author explicitly assigned, and the type the
compiler inferred.

Isn't this error because the compiler was able to infer the type, but it was not what was expected? For example, in the example provided in the error explanation, the value assigned to x is most definitely &str, and i32 is expected, so the types don't match.


@rustbot modify labels: A-diagnostics D-incorrect D-confusing C-bug

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Sep 8, 2020
@camelid
Copy link
Member Author

camelid commented Sep 8, 2020

Also the explanation says:

It can occur in several cases, the most common being a mismatch
between two types: the type the author explicitly assigned, and the type the
compiler inferred.

Isn't the most common case passing the wrong type to a function? Usually people do not provide a type annotation to a let unless it is necessary to help the compiler figure out what concrete type a generic is. Perhaps the explanation could be clarified and expanded upon for that.

@camelid camelid changed the title Error explanation for E0308 is incorrect Error explanation for E0308 is incorrect and confusing Sep 8, 2020
@jyn514 jyn514 added the A-error-codes Area: Explanation of an error code (--explain) label Sep 8, 2020
@camelid
Copy link
Member Author

camelid commented Sep 8, 2020

@jyn514 You beat me to it :)

@camelid
Copy link
Member Author

camelid commented Oct 21, 2020

let x: i32 = "I am not a number!";
//     ~~~   ~~~~~~~~~~~~~~~~~~~~
//      |             |
//      |    initializing expression;
//      |    compiler infers type `&str`
//      |
//    type `i32` assigned to variable `x`

Also, the diagnostics shown in the code example seem out of date.

@jyn514
Copy link
Member

jyn514 commented Oct 21, 2020

Mentoring instructions:

Change compiler/rustc_error_codes/src/error_codes/E0308.md to have an up-to-date diagnostic and appropriate long description.

@jyn514 jyn514 added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Oct 21, 2020
@PoignardAzur
Copy link
Contributor

On it.

@PoignardAzur
Copy link
Contributor

PoignardAzur commented Oct 24, 2020

Proposed text:

Expected type did not match the received type.

Erroneous code example:

fn plus_one(x: i32) -> i32 {
    x + 1
}

plus_one("Not a number");
//       ^^^^^^^^^^^^^^ expected `i32`, found `&str`

if "Not a bool" {
// ^^^^^^^^^^^^ expected `bool`, found `&str`
}

This error occurs when an expression was used in a place where the compiler
expected an expression of a different type. It can occur in several cases, the
most common being when calling a function, and passing an argument which has a
different type than the matching argument in the function declaration.

@camelid
Copy link
Member Author

camelid commented Oct 24, 2020

Overall text looks good. Can you open a PR and @-mention me on it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-error-codes Area: Explanation of an error code (--explain) C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants