-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Confusing error message with missing return #22216
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
Comments
This gives the same error and might be arguably more clear, but not by much because the problem remains. fn forgot_to_return() -> usize {
while true {}
} What would a fix look like though? Should <anon>:4:5: 6:6 error: a `while` loop expression of type `()` is being returned but `usize` is expected
<anon>:4:5: 6:6 error: mismatched types:
expected `usize`,
found `()`
(expected usize,
found ()) [E0308]
<anon>:4 while 4 > 3 {
<anon>:5 /* do something */
<anon>:6 }
error: aborting due to previous error Maybe this: <anon>:4:5: 6:6 error: mismatched types:
expected `usize`,
found `()`
(expected usize,
found a `while` loop expression returning `()`) [E0308]
<anon>:4 while 4 > 3 {
<anon>:5 /* do something */
<anon>:6 }
error: aborting due to previous error I actually like the second better. I wasn't sure how a better error would look before this. Maybe the tip should be via a warning or a |
I think it shouldn't be limited to particular keywords, but general - whenever it's about wrong return type, the error should read "mismatched return type (missing return?)" or something instead of the generic "mismatched types". |
This isn't just restricted to function return types. These are just to highlight that when the expression is big, it's especially unclear what the problem is. It just looks like something is wrong with the This isn't returning anything but has the same issue: fn main() {
let _: usize = while 4 > 3 {
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
};
} Output: <anon>:2:20: 33:6 error: mismatched types:
expected `usize`,
found `()`
(expected usize,
found ()) [E0308]
<anon>:2 let _: usize = while 4 > 3 {
<anon>:3 let _ = 3;
<anon>:4 let _ = 3;
<anon>:5 let _ = 3;
<anon>:6 let _ = 3;
<anon>:7 let _ = 3;
...
error: aborting due to previous error
playpen: application terminated with error code 101 All this error states is that there is a mismatch on line 2 which includes a Similarly, this doesn't work: fn main() {
let x = 3;
let _: usize = match x {
0 => while true {
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
let _ = 3;
},
_ => 3,
};
} <anon>:4:14: 35:10 error: mismatched types:
expected `usize`,
found `()`
(expected usize,
found ()) [E0308]
<anon>:4 0 => while true {
<anon>:5 let _ = 3;
<anon>:6 let _ = 3;
<anon>:7 let _ = 3;
<anon>:8 let _ = 3;
<anon>:9 let _ = 3;
...
error: aborting due to previous error
playpen: application terminated with error code 101 I think nesting can make these even more unclear. |
Triage: this diagnostic remains the same today. |
Closing in favor of collective issue for this general problem: #41897. |
The following code:
Gives the error message:
Playpen: http://is.gd/0sudKI
This is confusing, since it looks like the type of either
4
or3
is()
- it is not clear that the actual error is that the while loop is being treated as the final expression in the function, and its type does match the return type of the function.The text was updated successfully, but these errors were encountered: