<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - ARC optimizer prematurely releases result of ternary operator when assigning to __attribute__(objc_precise_lifetime) variable"
   href="http://llvm.org/bugs/show_bug.cgi?id=17778">17778</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ARC optimizer prematurely releases result of ternary operator when assigning to __attribute__(objc_precise_lifetime) variable
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.3
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bgertzfield@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=11474" name="attach_11474" title="Repro case for bug">attachment 11474</a> <a href="attachment.cgi?id=11474&action=edit" title="Repro case for bug">[details]</a></span>
Repro case for bug

These two statements perform identically with -O0:

__attribute__((objc_precise_lifetime)) Scoped *scoped1 = [[Scoped alloc] init];
__attribute__((objc_precise_lifetime)) Scoped *scoped2 = DoSomeCheck() ?
[[Scoped alloc] init] : nil;

However, with -O1 or higher, the second statement releases the object before
assigning it to 'scoped2'. Attached test case reproduces issue:

2013-11-01 16:53:13.479 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.484 testARC-opt[85825:707] At the end of first block
2013-11-01 16:53:13.485 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff758c081a0>

2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.487 testARC-opt[85825:707] At the end of second block

Interestingly, the following statements behave the same as the first statement,
suggesting it's not just the ternary operator at fault:

Scoped *scopeMe = DoSomeCheck() ? [[Scoped alloc] init] : nil;
__attribute__((objc_precise_lifetime)) Scoped *scoped3 = scopeMe;


Reproduced in Clang 3.3 as shipped with Xcode 5.0 (5A1412).

% clang -v
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix

% cat testARC.m
#import <Foundation/Foundation.h>

static BOOL gSomeValue;

static BOOL DoSomeCheck(void)
{
  return gSomeValue;
}

@interface Scoped : NSObject
@end

@implementation Scoped

- (instancetype)init
{
  self = [super init];
  NSLog(@"%s %@", __func__, self);
  return self;
}

- (void)dealloc
{
  NSLog(@"%s %@", __func__, self);
}

@end

int main(int argc, char **argv)
{
  gSomeValue = YES;

  {
    __attribute__((objc_precise_lifetime)) Scoped *scoped = [[Scoped alloc]
init];
    NSLog(@"At the end of first block");
  }

  {
    __attribute__((objc_precise_lifetime)) Scoped *scoped = DoSomeCheck() ?
[[Scoped alloc] init] : nil;
    NSLog(@"At the end of second block");
  }

  {
    Scoped *scopeMe = DoSomeCheck() ? [[Scoped alloc] init] : nil;
    __attribute__((objc_precise_lifetime)) Scoped *scoped = scopeMe;
    NSLog(@"At the end of third block");
  }
}

% clang -fobjc-arc -O0 -o testARC-dbg ./testARC.m
% clang -fobjc-arc -Os -o testARC-opt ./testARC.m

% ./testARC-dbg                                  
2013-11-01 16:53:27.387 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9102c081c0>
2013-11-01 16:53:27.389 testARC-dbg[85843:707] At the end of first block
2013-11-01 16:53:27.389 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9102c081c0>
2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9103b009d0>
2013-11-01 16:53:27.390 testARC-dbg[85843:707] At the end of second block
2013-11-01 16:53:27.390 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9103b009d0>
2013-11-01 16:53:27.391 testARC-dbg[85843:707] -[Scoped init] <Scoped:
0x7f9102c02b40>
2013-11-01 16:53:27.408 testARC-dbg[85843:707] At the end of third block
2013-11-01 16:53:27.412 testARC-dbg[85843:707] -[Scoped dealloc] <Scoped:
0x7f9102c02b40>

% ./testARC-opt
2013-11-01 16:53:13.479 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.484 testARC-opt[85825:707] At the end of first block
2013-11-01 16:53:13.485 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff758c081a0>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.486 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759b00470>
2013-11-01 16:53:13.487 testARC-opt[85825:707] At the end of second block
2013-11-01 16:53:13.489 testARC-opt[85825:707] -[Scoped init] <Scoped:
0x7ff759900140>
2013-11-01 16:53:13.489 testARC-opt[85825:707] At the end of third block
2013-11-01 16:53:13.490 testARC-opt[85825:707] -[Scoped dealloc] <Scoped:
0x7ff759900140></pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>