Skip to content

Closure error messages should explain how the return type got inferred #78193

Closed
@Aaron1011

Description

@Aaron1011

The following code:

fn main() {
    || {
        if false {
            return 1;
        }
    };
}

produces the following error message:

error[E0308]: mismatched types
 --> src/main.rs:3:9
  |
3 | /         if false {
4 | |             return 1;
5 | |         }
  | |_________^ expected integer, found `()`

The message doesn't explain why an integer was expected here. If the user doesn't explicitly annotate the return type of the closure, we should point to the expression that caused the return type to get inferred to begin with.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions