Skip to content

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

Merged
ChayimFriedman2 merged 5 commits intorust-lang:masterfrom
Albab-Hasan:fix/binop-rhs-never-coercion
Feb 28, 2026
Merged

fix: use ExprIsRead::Yes for rhs of binary operators#21654
ChayimFriedman2 merged 5 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

@Albab-Hasan
Copy link
Contributor Author

@ShoyuVanilla hey can you check this one out too, i want to know if im missing anything?

Copy link
Member

Choose a reason for hiding this comment

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

Well, this still isn't an assignment as Chayim said and more importantly, both tests aren't compiled with rustc.
I think the test code should be the valid Rust code which is compiled with rustc, unless the behavior we'd like to test is diagnostics or error resilience that not panicking or completing/inferencing the partially broken code.
I'd suggest trying out other code that compiled with rustc to test this PR.

@Albab-Hasan
Copy link
Contributor Author

@ChayimFriedman2 its done. let me know if i missed anything

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Feb 28, 2026
Merged via the queue into rust-lang:master with commit a96b6a9 Feb 28, 2026
16 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants