-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Specific Error Message or Note for Final Expression being Loop and Return not Unit #39968
Comments
What would you think we ought to suggest? |
(In other words, I'm trying to understand what users might be going for when writing code like this... I guess we could say something like "no value returned along this control-flow path" or something?) |
Perhaps saying that the I guess the issue might actually be that we're not showing where the i32 came from. |
Another example (Derived from #18390): fn main() {
let _ = foo();
}
fn foo() -> i32 {
let x = 5;
match x {
1 => {
return 1
},
2 => {
return 2
},
3 => {
return 3
},
4 => {
return 4
},
5 => {
return 5
},
_ => { }
}
} gives:
I guess the key point in both cases is that the user is writing more "C-style" code and not expecting to make use of the "implicit return" here, so the error is sort of surprising. |
It seems to me that we could cover this by a similar rule to the one we use to suggest removing a semicolon -- in particular, if the expected type is not unit, but we are returning unit from a tail expression, and that tail expression is some kind of "control-flow" structure, we might point to the end of it and say something like "no return along this path": error[E0308]: no return along this path
--> src/main.rs:3:20
|
3 | if false { break; }
| ^^^^^ following this path, no value is ever returned
| This will require a bit more state to be plumbed around, I imagine, not entirely sure what (and I'm not entirely sure where the error should be reported -- on the break, as I showed above?). |
The current output is even worse, IMO:
|
…atthewjasper Account for tail expressions when pointing at return type When there's a type mismatch we make an effort to check if it was caused by a function's return type. This logic now makes sure to only point at the return type if the error happens in a tail expression. Turn `walk_parent_nodes` method into an iterator. CC rust-lang#39968, CC rust-lang#40799.
…atthewjasper Account for tail expressions when pointing at return type When there's a type mismatch we make an effort to check if it was caused by a function's return type. This logic now makes sure to only point at the return type if the error happens in a tail expression. Turn `walk_parent_nodes` method into an iterator. CC rust-lang#39968, CC rust-lang#40799.
…atthewjasper Account for tail expressions when pointing at return type When there's a type mismatch we make an effort to check if it was caused by a function's return type. This logic now makes sure to only point at the return type if the error happens in a tail expression. Turn `walk_parent_nodes` method into an iterator. CC rust-lang#39968, CC rust-lang#40799.
Current output:
|
Tweak type mismatch caused by break on tail expr When `break;` has a type mismatch because the `Destination` points at a tail expression with an obligation flowing from a return type, point at the return type. Fix rust-lang#39968.
Tweak type mismatch caused by break on tail expr When `break;` has a type mismatch because the `Destination` points at a tail expression with an obligation flowing from a return type, point at the return type. Fix rust-lang#39968.
produces
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.
The text was updated successfully, but these errors were encountered: