- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inference
Description
Rust has support for break within loop returning a value:
let x = loop {
 // ...
 break 5;
}This is a relatively less-known feature.
If your loop is in a position where a type other than unit is expected, breaks in the loop may get a rather weird error (playpen)
fn foo() -> u8 {
    loop {
        // ...
        if true {
            break;
        }
        // ...
    }
}error[E0308]: mismatched types
 --> src/main.rs:7:13
  |
7 |             break;
  |             ^^^^^ expected (), found u8
  |
  = note: expected type `()`
             found type `u8`
This error makes sense if you know that break can take a value, but most people don't, and this makes it kinda confusing. We should have better diagnostics here, perhaps move the primary error to the loop itself and add a note to the break, or vice versa.
(found by @jubitaneja)
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inference