Skip to content

Comments

fix: use ExprIsRead::Yes for rhs of binary operators#21654

Open
Albab-Hasan wants to merge 4 commits intorust-lang:masterfrom
Albab-Hasan:fix/binop-rhs-never-coercion
Open

fix: use ExprIsRead::Yes for rhs of binary operators#21654
Albab-Hasan wants to merge 4 commits intorust-lang:masterfrom
Albab-Hasan:fix/binop-rhs-never-coercion

Conversation

@Albab-Hasan
Copy link
Contributor

the rhs of binary operators (including compound assignments like +=) was inferred with ExprIsRead::No, which prevented divergence detection for place expressions of type !. this caused false type mismatches when a function's return type depended on the block diverging after something like x += *never_ptr. the rhs of any binary operator is always consumed (read), so this uses ExprIsRead::Yes to be consistent with the ordinary assignment and let binding paths.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 16, 2026
@Albab-Hasan Albab-Hasan force-pushed the fix/binop-rhs-never-coercion branch from d91ef9c to 0bed4fb Compare February 16, 2026 07:47
both operands of binary operators (including compound assignments like
`+=`) were inferred with `ExprIsRead::No`, which prevented divergence
detection for place expressions of type `!`. this caused false type
mismatches when a function's return type depended on the block diverging
after something like `x += *never_ptr` or `let _ = *never_ptr + 1`.
both operands of any binary operator are always consumed (read), so this
uses `ExprIsRead::Yes` to be consistent with the ordinary assignment and
let binding paths.
@Albab-Hasan Albab-Hasan force-pushed the fix/binop-rhs-never-coercion branch from 0bed4fb to b3feb5f Compare February 16, 2026 08:07
}

#[test]
fn binop_lhs_never_place_diverges() {
Copy link
Contributor

Choose a reason for hiding this comment

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

This... does not check what it declares?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad. one sec, ill fix it

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you fix it?

yes

Copy link
Contributor

Choose a reason for hiding this comment

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

How? It still does not test coercion in the LHS.

The other comment was my mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry i just switched to check_infer_with_mismatches thinking that showingthe inferred types was enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its fixed now

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems there is a misunderstanding. The LHS in the assignment a = b is a. Asserting that a never coercion happens in the LHS means something like *p += 1 where *p is *const !.

Copy link
Contributor

Choose a reason for hiding this comment

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

This test is still not correct as there is no assignment now.

@Albab-Hasan Albab-Hasan force-pushed the fix/binop-rhs-never-coercion branch from 8006f04 to 2f12839 Compare February 16, 2026 09:59
@Albab-Hasan
Copy link
Contributor Author

Albab-Hasan commented Feb 16, 2026

@ChayimFriedman2 hey, so im really confused rn.

This... does not check what it declares?

I get this. i pushed the fix. but im not sure what ur trying to say in the first comment.

the changes on lines 181 and 183 are in the _ => branch thats regular binops like + not assignments. both operands of + are consumed by Add::add so they're read. i tried reverting those lines back to ExprIsRead::No and the binop_lhs_never_place_diverges test breaks *p + 1 where p: *const ! stops triggering NeverToAny coercion and get expected i32 got (). the ordinary assignment LHS (x in x = expr) is already ExprIsRead::No in expr.rs and wasnt touched here.

@Albab-Hasan
Copy link
Contributor Author

@ChayimFriedman2 can i get some instructions or should i close this pr and work on one liners again?

@Albab-Hasan
Copy link
Contributor Author

@ChayimFriedman2 sorry for wasting your time

@ChayimFriedman2
Copy link
Contributor

The change is good, only the test is problematic. A good test will be something like (not verified):

let a: *const ! = 0 as _;
*a += 1;

@Albab-Hasan
Copy link
Contributor Author

Albab-Hasan commented Feb 17, 2026

@ChayimFriedman2 ok ill try to fix the test

@Albab-Hasan
Copy link
Contributor Author

@ChayimFriedman2 can you review the changes? the test is fixed

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants