<div dir="ltr">The modified version doesn't seem to change any of the results (on -O or -Onone). Note that the problem is that it's <b>not</b> uniquely referenced inside bar where it actually should be – that would mean that ownership is currently not directly transferred right?</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 1, 2016 at 10:27 AM, Joe Groff <span dir="ltr"><<a href="mailto:jgroff@apple.com" target="_blank">jgroff@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Mar 31, 2016, at 4:21 PM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>> wrote:<br>
><br>
>><br>
>> On Mar 31, 2016, at 3:58 PM, Patrick Pijnappel via swift-dev <<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>> wrote:<br>
>><br>
>> In trying to implement a COW type, but I'm running into problems with isUniqueReferenced breaking in even fairly simple cases. For example (with -O) the code below prints "bar: false", commenting out the print in test() makes it print "bar: true", and removing the var parameter var foo: Foo and using var foo = foo instead breaks it again. Am I doing something wrong here?<br>
>><br>
>> class FooStorage { var x: Int = 0 }<br>
>><br>
>> struct Foo { var storage = FooStorage() }<br>
>><br>
>> func bar(var foo: Foo) {<br>
>> print("bar: \(isUniquelyReferencedNonObjC(&foo.storage))")<br>
>> }<br>
>><br>
>> func test() {<br>
>> var foo = Foo()<br>
>> print("test: \(isUniquelyReferencedNonObjC(&foo.storage))")<br>
>> bar(foo)<br>
>> }<br>
>><br>
>> test()<br>
><br>
> You're not doing anything wrong, this is just the ARC optimizer at work. `foo` inside `test` is dead after the call to `bar`, so ownership is transferred directly to `bar`'s parameter.<br>
<br>
</span>If you want to ensure that `foo` remains alive despite this, you can use `withExtendedLifetime`:<br>
<span class=""><br>
class FooStorage { var x: Int = 0 }<br>
<br>
struct Foo { var storage = FooStorage() }<br>
<br>
func bar(var foo: Foo) {<br>
print("bar: \(isUniquelyReferencedNonObjC(&foo.storage))")<br>
}<br>
<br>
func test() {<br>
var foo = Foo()<br>
print("test: \(isUniquelyReferencedNonObjC(&foo.storage))")<br>
</span> withExtendedLifetime(foo) {<br>
bar(foo)<br>
}<br>
}<br>
<span class="HOEnZb"><font color="#888888"><br>
-Joe</font></span></blockquote></div><br></div>