Skip to content

Conversation

sgasho
Copy link
Contributor

@sgasho sgasho commented Oct 8, 2025

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.

fn main() { let oa = Some(1); let oa2 = Some(1); let v = if let Some(a) = oa { Some(&a) } else if let Some(a) = oa2 { &Some(a) } else { None }; println!("{v:?}"); }

Simplified Expr::If structure for this code is like below.

Expr::If { cond: "if let Some(a) = oa", then: "Some(&a)", else: { Expr::If { cond: "else if let Some(a) = oa2", then: "&Some(a)", else: { cond: "else", then: "None", else: None, } } } } 

Current check_expr_if processes this Expr::If by depth-first-search like approach, that is why "if and else 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

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 8, 2025
@sgasho

This comment was marked as outdated.

@rust-log-analyzer

This comment has been minimized.

@sgasho

This comment was marked as outdated.

@sgasho sgasho changed the title needs_help: wip fix 146190 Fix check_expr_if to point to a more accurate location of the compilation error in some cases Oct 10, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@sgasho sgasho force-pushed the fix/141690_check_expr_if branch from f309ba8 to 98bc078 Compare October 13, 2025 01:55
@rust-log-analyzer

This comment has been minimized.

@sgasho sgasho force-pushed the fix/141690_check_expr_if branch from 98bc078 to c50157b Compare October 13, 2025 02:24
@rust-log-analyzer

This comment has been minimized.

@sgasho sgasho force-pushed the fix/141690_check_expr_if branch from c50157b to b522404 Compare October 13, 2025 06:52
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Oct 13, 2025
@sgasho sgasho force-pushed the fix/141690_check_expr_if branch 3 times, most recently from 5212a66 to ebfaecc Compare October 14, 2025 15:41
@sgasho sgasho force-pushed the fix/141690_check_expr_if branch from ebfaecc to 5c38b51 Compare October 15, 2025 13:39
Comment on lines 12 to 17
Copy link
Contributor Author

@sgasho sgasho Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was "if and else (have incompatible types) " appropriate for this case?
I think this else if branch has expression of type ! and it is compatible for any type. (source: "Expressions of type ! can be coerced into any other type." at https://doc.rust-lang.org/reference/types/never.html ).

I could be wrong but my understanding is showing "if may be missing an else clause" for this case (all if-else-if have compatible types and the only problem is missing else branch, which would be evaluated to ())

@sgasho sgasho marked this pull request as ready for review October 15, 2025 15:06
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 15, 2025
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 15, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Oct 15, 2025

⚠️ Warning ⚠️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

4 participants