Skip to content

Specific Error Message or Note for Final Expression being Loop and Return not Unit #39968

Closed
@Havvy

Description

@Havvy
fn loop_ending() -> i32 {
    loop {
        if false { break; }
        return 42;
    }
}

produces

error[E0308]: mismatched types
 --> <anon>:2:5
  |
2 |       loop {
  |  _____^ starting here...
3 | |         if false { break; }
4 | |         return 42;
5 | |     }
  | |_____^ ...ending here: expected i32, found ()
  |
  = note: expected type `i32`
  = note:    found type `()`

error: aborting due to previous error

But the compiler can check that the final expression is a loop and must always evaluate into (), so we can recommend more advanced information.

This has caused real confusion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions