[swift-users] inout params seem to have undefined behavior

Jens Alfke jens at mooseyard.com
Mon Jun 13 16:12:01 CDT 2016


> On Jun 13, 2016, at 1:43 PM, Joe Groff <jgroff at apple.com> wrote:
> 
> I see, missed that part. That's a bug—`acopy` should remain a distinct copy of `a`.

I’m guessing that when `a` is copied into `acopy`, the same array contents are shared between them, then when `a` is mutated it gets a new copy of the contents. Then the assignment to `b` writes into the original array contents now held by `acopy`.

Still seems like it’s caused by having aliased `inout` variables, but the effect is nastier. I can imagine horrible bugs where `acopy` gets passed around somewhere else and then changes, breaking the something-else that’s holding it. It could be hard to track that back to its original cause, especially if there are two different codebases involved.

—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160613/06439622/attachment.html>


More information about the swift-users mailing list