-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Incorrect span label when non-() else-less if
is tail expression in -> ()
function
#124819
Comments
why we don't suggest add fn main() {
if true {
""; // add `;` at here.
}
} |
@chenyukang I believe it is because we check whether the tail expression is not a value. |
Hi, I'm new to the rust development and want to start with this issue. |
@cardigan1008 you can make a comment saying (so people filtering for unassigned issues to work on don't have it show up) |
@rustbot claim |
I tried to investigate on this issue and here is a case that has the correct output: fn main() {
if true {
""
}
1;
} The difference between the two cases lies in: rust/compiler/rustc_middle/src/hir/map/mod.rs Line 547 in 37dc766
The correct case is parsed into nodes that include a rust/compiler/rustc_middle/src/hir/map/mod.rs Line 566 in 37dc766
The correct case has a early return with None here: rust/compiler/rustc_middle/src/hir/map/mod.rs Line 549 in 37dc766
Finally, in the function suggest_mismatched_types_on_tail, the wrong case enters into the if condition unlike the other one:
I'm uncertain about where to start with a fix. Should I modify the structure of this node or introduce some limitations? I've drafted a potential fix here: #124917, but it's tailored specifically to this case. I'd appreciate any thoughts or feedback. Thanks! |
@cardigan1008 I believe you can just modify The other case:
I think is ok. It could do with a more targeted error that will be more involved (to emit a single error instead of multiple), but I wouldn't consider it in scope for this ticket. |
@estebank Thanks for you kind and clear instructions! I've ran fn main() {
if true {
""
} else if false {
} else {
""
}
}
In this case, would aggregating errors into a single message make the correction less clear? I think the multiple errors might be a good reminder for users to fix errors in several specific branches. Maybe I misunderstand your meaning about the single error. Sorry for that. |
Yeah, you're right. The only case where emitting a single E0308 error for this is when the expected type of the whole expression is one and all of the arms are of the same type. |
Check whether the next_node is else-less if in get_return_block Fix rust-lang#124819
Rollup merge of rust-lang#124917 - cardigan1008:issue-124819, r=pnkfelix Check whether the next_node is else-less if in get_return_block Fix rust-lang#124819
Code
Current output
Desired output
Rationale and extra context
When the
if
is not the tail expression, we provide the right output. We might just have to change the order of operation when looking for additional context to print.Other cases
if true { "" } else { "" }
as tail expression has the same problem.Rust Version
All checked, current version 1.78.
The text was updated successfully, but these errors were encountered: