https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126362

            Bug ID: 126362
           Summary: Negative array-new size in requires expression
           Product: gcc
           Version: 16.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gongke at ios dot ac.cn
  Target Milestone: ---

Consider this code (godbolt: https://godbolt.org/z/cPxooPE1d)

```cpp
template <typename T>
concept SmallType = requires { new int[-(int)sizeof(T) + 3]; };

int main() {
    static_assert(!SmallType<int>);
    return 0;
}
```

GCC produces "static assertion failed" while Clang accepts it.

[expr.new]/8:
> If the expression in a noptr-new-declarator is present, it is implicitly 
> converted to std​::​size_t. The value of the expression is invalid if
> (8.1) the expression is of non-class type and its value before converting to 
> std​::​size_t is less than zero;
> ...
> If the value of the expression is invalid after converting to std​::​size_t:
> (8.5) if the expression is a potentially evaluated core constant expression, 
> the program is ill-formed;

So a negative constant expression size of an array-new expression should be
ill-formed. It should not be checked only after converting to `std::size_t`.

Therefore, either this new expression should produce an error, or this
`requires` expression should evaluate to `false` with `T=int` (I'm not sure
what kind of ill-formed code can and what cannot be tested by `requires`). But
GCC seems to make this evaluate to `true`.

Reply via email to