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

Add hint to "use break" when attempting to implicit-break a loop #84114

Closed
ash2x3zb9cy opened this issue Apr 11, 2021 · 2 comments · Fixed by #84516
Closed

Add hint to "use break" when attempting to implicit-break a loop #84114

ash2x3zb9cy opened this issue Apr 11, 2021 · 2 comments · Fixed by #84516
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ash2x3zb9cy
Copy link

ash2x3zb9cy commented Apr 11, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6ad29d6923c9ffe687e2a08669a03101

fn main() {
    let x: i8 = loop { 10 };
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let x: i8 = loop { 10 };
  |                        ^^ expected `()`, found integer

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Ideally the output should look like:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/main.rs:2:24
  |
2 |     let x: i8 = loop { 10 };
  |                       ^^^
  |                       | |
  |                       | expected `()`, found integer
  |                       help: consider adding `break` here
error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

It may not be immediately clear to a user why implicit-return/break style works for if expressions, but not for loop. Adding a hint to explicitly break with break value; would be helpful.

@ash2x3zb9cy ash2x3zb9cy 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 Apr 11, 2021
@henryboisdequin
Copy link
Contributor

@rustbot label +A-suggestion-diagnostics

@rustbot rustbot added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. label Apr 16, 2021
@torhovland
Copy link
Contributor

@rustbot claim

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 25, 2021
Add suggestion to "use break" when attempting to implicit-break a loop

Fixes rust-lang#84114
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 25, 2021
Add suggestion to "use break" when attempting to implicit-break a loop

Fixes rust-lang#84114
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 25, 2021
Add suggestion to "use break" when attempting to implicit-break a loop

Fixes rust-lang#84114
@bors bors closed this as completed in ad3389a Apr 26, 2021
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. 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.

4 participants