-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why does destructuring not use temporaries? #17283
Comments
Pretty much a duplicate of #12064, although the new syntax certainly makes it even easier to come across. |
This is working as designed. Everybody was aware this behavior would be a consequence of destructuring and there is no plan to change it. |
I noticed that it works as expected when destructuring an array a, b = [_]i32{b, a} will eventually output |
@0Ahmed-0 I think this is current a remnant of the fact that explicitly specifying the type blocks the result location mechanism, as decided in #17194. Explanation of what I think is happening: LLVM (used for optimizations) currently also doesn't optimize it away. |
this is great to see but just to clarify where the discrepancy may arise, tuples are structs and so swaps there are assigning to struct fields just as in the issue this is marked as a duplicate of. |
Zig Version
0.12.0-dev.575+fb6fff256
Steps to Reproduce and Observed Behavior
This is more of a question about whether this behavior is correct or not.
It is related to the new feature in this pull request: #17156
I'm using this feature in this case to get a swap-like behavior.
This chunk of code prints out the following output:
It looks to me like the first assignment 'a = b' happens, and then 'b = a' after that, which would give the same output.
But if we assign '.{ b, a }' to a variable before assignment like here:
We would get a different output:
And that is what I would normally expect.
Now my question is - should we expect a change in behavior if we temporarily save '.{ b, a }' to a variable?
Should these two outputs be different?
Expected Behavior
Possibly(?) we should expect output to be the same like this in two cases:
The text was updated successfully, but these errors were encountered: