<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - -Winvalid-constexpr fails with specialisation when interacting with automatic reference counting"
   href="https://bugs.llvm.org/show_bug.cgi?id=45020">45020</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>-Winvalid-constexpr fails with specialisation when interacting with automatic reference counting
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </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>sdefresne@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Specialisation of a template to support "id" (the Objective-C type) uniformly
the same way as pointer to Objective-C object cause failure of "constexpr"
compilation.

Small reproduction case:

=== s.mm
#import <Foundation/Foundation.h>

template<typename T>
struct a {
  explicit constexpr a(__unsafe_unretained T o = nil) : o_(o) {}

  __unsafe_unretained T o_;
};

template<typename T>
struct b : a<T> {
  explicit constexpr b(T __attribute__((ns_consumed)) o = nil) : a<T>(o) {}
};

template<typename T>
struct c : b<T*> {
  explicit constexpr c(T* __attribute__((ns_consumed)) o = nil) : b<T*>(o) {}
};

template<>
struct c<id> : b<id> {
  explicit constexpr c(id __attribute__((ns_consumed)) o = nil) : b<id>(o) {}
};

void foo() {
  b<id> _0([[NSObject alloc] init]);
  c<id> _1([[NSObject alloc] init]);
  c<NSObject> _2(([[NSObject alloc] init]));
}
===

Compiling this with the following options "--std=c++14 -fobjc-arc" results in
the following error:

s.mm:23:22: error: constexpr constructor never produces a constant expression
[-Winvalid-constexpr]
  explicit constexpr c(id __attribute__((ns_consumed)) o = nil) : b<id>(o) {}
                     ^
s.mm:23:73: note: subexpression not valid in a constant expression
  explicit constexpr c(id __attribute__((ns_consumed)) o = nil) : b<id>(o) {}
                                                                        ^
I would expect the "constexpr" to fail compilation for all of b<id>, c<id> and
c<NSObject> or for none of them. However, it only fails for c<id> which makes
me think this is a bad interaction between specialisation, ARC (automatic
reference counting) and the annotations.

The following alternative way to specialise via trait does not produce any
error:

===t.mm
#import <Foundation/Foundation.h>

template<typename T>
struct a {
  explicit constexpr a(__unsafe_unretained T o = nil) : o_(o) {}

  __unsafe_unretained T o_;
};

template<typename T>
struct b : a<T> {
  explicit constexpr b(T __attribute__((ns_consumed)) o = nil) : a<T>(o) {}
};

template<typename T>
struct t {
  typedef T* tp;
};

template<>
struct t<id> {
  typedef id tp;
};

template<typename T>
struct c : b<typename t<T>::tp> {
  typedef typename t<T>::tp tp;
  explicit constexpr c(tp __attribute__((ns_consumed)) o = nil) : b<tp>(o) {}
};

void foo() {
  b<id> _0([[NSObject alloc] init]);
  c<id> _1([[NSObject alloc] init]);
  c<NSObject> _2(([[NSObject alloc] init]));
}
===</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>