-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Type error for loops in a non-() position moves to the break
statement
#51669
Comments
Thanks, @Manishearth for teaching me this cool trick! Breaking with a value is really useful. |
That type error is backwards. We are expecting a u8, nothing in your example supplies a u8 |
Also I think the error is exactly where it should be, but we should definitely add some explanation. Value-break is probably not the only place where a type mismatch error could benefit from some additional notes |
Tweak mismatched types error - Change expected/found for type mismatches in `break` - Be more accurate when talking about diverging match arms - Tweak wording of function without a return value - Suggest calling bare functions when their return value can be coerced to the expected type - Give more parsing errors when encountering `foo(_, _, _)` Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
Handled in #63337.
If that is so, then this ticket should be closed when that PR is merged. Otherwise we can keep open exclusively to point at the enclosing |
Tweak mismatched types error - Change expected/found for type mismatches in `break` - Be more accurate when talking about diverging match arms - Tweak wording of function without a return value - Suggest calling bare functions when their return value can be coerced to the expected type - Give more parsing errors when encountering `foo(_, _, _)` Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
Tweak mismatched types error - Change expected/found for type mismatches in `break` - Be more accurate when talking about diverging match arms - Tweak wording of function without a return value - Suggest calling bare functions when their return value can be coerced to the expected type - Give more parsing errors when encountering `foo(_, _, _)` Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
Current output:
|
Rust has support for
break
withinloop
returning a value: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)
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 thebreak
, or vice versa.(found by @jubitaneja)
The text was updated successfully, but these errors were encountered: