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

Unhelpful message when not using value from a loop break #115905

Closed
not-holar opened this issue Sep 17, 2023 · 1 comment · Fixed by #116071
Closed

Unhelpful message when not using value from a loop break #115905

not-holar opened this issue Sep 17, 2023 · 1 comment · Fixed by #116071
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@not-holar
Copy link

not-holar commented Sep 17, 2023

Code

loop {
    break Some(1);
}

Current output

error[E0308]: mismatched types
  --> src\main.rs:24:15
   |
24 |         break Some(1);
   |               ^^^^^^^ expected `()`, found `Option<{integer}>`
   |
   = note: expected unit type `()`
                   found enum `Option<{integer}>`

error: aborting due to previous error

Desired output

loop value is never used so
break expected unit type `()`
              found enum `Option<{integer}>`

Rationale and extra context

It might not be too hard to figure what the issue is in such a simple example as the above, but when you have multiple levels of nesting with complex layered types, this can quickly become a source of frustration as it sends you on a wild chase of misdirection.

Other cases

No response

Anything else?

No response

@not-holar not-holar added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 17, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 17, 2023
@darklyspaced
Copy link
Contributor

i don't really see this as something that needs to be changed. adding the clause about how the loop's value is never used just seems to needlessly complicate things more for me.

@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 17, 2023
estebank added a commit to estebank/rust that referenced this issue Sep 22, 2023
estebank added a commit to estebank/rust that referenced this issue Sep 22, 2023
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 23, 2023
…rors

Point at cause of expectation of `break` value when possible

When encountering a type error within the value of a `break` statement, climb the HIR tree to identify if the expectation comes from an assignment or a return type (if the loop is the tail expression of a `fn`).

Fix rust-lang#115905.
@bors bors closed this as completed in d3dea30 Sep 23, 2023
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 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.

5 participants