title: Swift: How ARC deals with anonymous objects?
body: <p>ARC came many years ago to replace painful MRC, but since ARC is only a compiler technique its magic happens during the compile time. So, basically what it does - automatically inserts <code>retain</code> and <code>release</code> calls where appropriate. So, now comes my question - how ARC deal with the following examples</p>
  <pre><code>func foo() {
   X().doSomething()
  }
  
  func bar() {
   anotherFunc(X())
  }
  </code></pre>
  <p>Objects are created with <code>ref count == 1</code>(as far as I know it, at least in old MRC objc <code>alloc</code>, <code>new</code>, <code>copy</code> where calling <code>retain</code>. Please correct me if I'm wrong), so the compiler have to decrease ref count on these objects in order to dealloc them ? But how is this done since there is actually no ref to these objects, they are anonymous ! I assume that compiler may do the following optimisation</p>
  <pre><code>func foo() {
   let var0001 = X()
   var0001.doSomething()
   var0001.release()
  }
  </code></pre>
  <p>I mean that compiler will make the reference to the anonymous object so it will be able to call <code>release</code> on it. But, I'm not sure that this is exactly how it works! So, can somebody explain please what actually happens in this scenario ?<br>Kind Regards,<br>Andre</p>