<div dir="ltr">Ok I checked, in the version from my original email we have:<div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)"><span style="">callq<span class="" style="white-space:pre">        </span>_swift_retain</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)">movq<span class="" style="white-space:pre">        </span>%rbx, %rdi</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)"><span style="">callq<span class="" style="white-space:pre">        </span>__TF4main3barFVS_3FooT_</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)"><span style="">movq<span class="" style="white-space:pre">        </span>%rbx, %rdi</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)"><span style="">callq<span class="" style="white-space:pre">        </span>_swift_release</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(66,66,66)"><span style=""><br></span></p><p style="margin:0px;line-height:normal">Which makes foo obviously non-uniquely referenced inside bar, preventing COW optimization. So it seems the optimizer somehow doesn't notice it's dead here and doesn't transfer ownership (?).</p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 1, 2016 at 5:49 PM, Patrick Pijnappel <span dir="ltr"><<a href="mailto:patrickpijnappel@gmail.com" target="_blank">patrickpijnappel@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="HOEnZb"><div class="h5"><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><br>
> On Mar 31, 2016, at 4:21 PM, Joe Groff via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank">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" target="_blank">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><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><font color="#888888"><br>
-Joe</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>