Skip to content

Comments

Payload captures no longer alias the original value#4454

Merged
andrewrk merged 2 commits intoziglang:masterfrom
LemonBoy:capture-reform
Feb 14, 2020
Merged

Payload captures no longer alias the original value#4454
andrewrk merged 2 commits intoziglang:masterfrom
LemonBoy:capture-reform

Conversation

@LemonBoy
Copy link
Contributor

Ad discussed with Andrew in #4443 I've changed the behavior of for/if/while/switch so that a "simple" capture (|v|) always creates a copy of the payload in a scope-local variable. If you need to modify the underlying memory just use a "ref" capture (|*v|).

I've added a few tests to make sure this new behavior is respected and I've run the stage1/std test suite and I've not spotted any regression. That's possibly a sign of how nobody expected the capture to behave like it did :P or maybe everybody knew that and worked around it.

Closes #4361
Closes #2915

The payload doesn't alias anymore the same memory it comes from and is
instead a fresh copy of the original object.
@JesseRMeyer
Copy link

Curious -- Do you know if LLVM only emits a copy in Release build modes when the capture was modified?

@andrewrk andrewrk merged commit 9e1afdc into ziglang:master Feb 14, 2020
@andrewrk
Copy link
Member

Nice work.

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.

slice of allowzero pointer is rejected in for Unwrapped optional aliasing footgun

3 participants