You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The compiler fails to recognize that let mut x and let mut y ultimately resolve to constant values 1 and 2 respectively in the second code example. While rustc's constant propagation typically handles such cases effectively, this particular pattern involving mutable variables with arithmetic operations appears to prevent the optimization.
I'd appreciate any insights into why the compiler can't deduce the final constant values in this scenario.
Not a bug. Rust integers default to being i32 unless some type information forces it to be some other type.
In your second code, when you do y *= 2057990635 / 1;. You are computing 3 * 2057990635. The result of this operation is 6173971905, which doesn't fit in an i32, which can hold values at most 2147483647. Therefore, your code has an integer overflow.
In debug mode, rust will panic as soon as you do this multiplication. In release mode, which is what your godbolt link uses, the result "wraps around". Therefore, after the multiplication, y is not equal to 6173971905, but is instead equal to 1879004609. Then, after y /= 2057990635;, y is set to 0. As a result, the assertion fails, so the assembly reflects this behavior.
You can check this by actually running the code, as opposed to inspecting the assembly. You will see that it does, in fact, panic from overflow in debug mode, and panic from failing the assertion in release mode.
jieyouxu
added
C-discussion
Category: Discussion or questions that doesn't represent real issues.
and removed
C-bug
Category: This is a bug.
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
labels
Apr 14, 2025
I tried these codes: (opt-level=3)
https://godbolt.org/z/b3je31qea
and:
I expected to see this happen:
Instead, this happened:
The compiler fails to recognize that
let mut x
andlet mut y
ultimately resolve to constant values1
and2
respectively in the second code example. While rustc's constant propagation typically handles such cases effectively, this particular pattern involving mutable variables with arithmetic operations appears to prevent the optimization.I'd appreciate any insights into why the compiler can't deduce the final constant values in this scenario.
Meta
The text was updated successfully, but these errors were encountered: