Issue 107668
Summary Memory allocated via `__builtin_operator_new` never starts its lifetime
Labels clang:frontend, constexpr
Assignees
Reporter tbaederr
    This code should work:
```c++
namespace std {
  using size_t = decltype(sizeof(0));
  template<typename T> struct allocator {
 constexpr T *allocate(size_t N) {
      return (T*)::operator new(sizeof(T) * N);
    }
    constexpr void deallocate(void *p) {
 ::operator delete(p); // both-note {{std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
    }
 };
}

constexpr int arrayAlloc() {
  int *F = std::allocator<int>().allocate(2);
  F[0] = 10;
  F[1] = 13;
  int Res = F[1] + F[0];
  std::allocator<int>().deallocate(F);
  return Res;
}
static_assert(arrayAlloc() == 23);
```
https://godbolt.org/z/a57f4vvMf

But the assignment to `F[0]` is currently diagnosed:
```
<source>:23:15: error: static assertion _expression_ is not an integral constant _expression_
   23 | static_assert(arrayAlloc() == 23);
      | ^~~~~~~~~~~~~~~~~~
<source>:17:8: note: assignment to object outside its lifetime is not allowed in a constant _expression_
   17 |   F[0] = 10;
 |   ~~~~~^~~~
<source>:23:15: note: in call to 'arrayAlloc()'
   23 | static_assert(arrayAlloc() == 23);
      | ^~~~~~~~~~~~
```

(I used `::operator new` in the sample to please GCC, but using `__builtin_operator_new` has the same problem.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to