Fix check_expr_if to point to a more accurate location of the compilation error in some cases #147484
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes #146190
The problem linked on the issue above lays on how Expr::If is processed.
Currently, Expr::If is processed by depth-first like approach, which is natural considering that Expr::If is a nested structure.
e.g.
Simplified Expr::If structure for this code is like below.
Current check_expr_if processes this Expr::If by depth-first-search like approach, that is why "
if
andelse
have incompatible types" occurs between else if let Some(a) = oa2 and else, not between if let Some(a) = oa and if let Some(a) = oa2.New approach on this PR is first flatten the Expr::If nested nodes, then process forward.
I created _if.rs and moved the check_expr_if becase this new approach requires a bit more lines of codes.
I added a test case that guarantees the problem on the linked issue has been resolved.
tests/ui/expr/if/if-multi-else-if-incompatible-types-issue-146190.rs
Discussion on Zulip: https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Struggling.20to.20fix.20issue.20.23146190.20check_expr_if/with/540960074