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

            Bug ID: 119866
           Summary: constant evaluation failure with trivial function
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Not sure how to exactly title this bug.

This is a reproduction while trying to understand a compile failure with a
recent upgrade to {fmt}:

using size_t = decltype(sizeof(int));

constexpr char const* id(char const* s) { return s; }

template <size_t N>
struct fixed_string {
    char data[N] = {}; 

    constexpr fixed_string(char const (&m)[N]) {
        for (size_t i = 0; i != N; ++i) {
            data[i] = m[i];
        }
    }   
};

int main() {
    static constexpr char msg[] = "hello\n";
    static_assert(__builtin_strlen(msg) == 6);              // ok
    static_assert(__builtin_strlen(id(msg)) == 6);          // ok

    static constexpr auto str = fixed_string("hello\n");
    static_assert(__builtin_strlen(str.data) == 6);         // ok
    static_assert(__builtin_strlen(auto(str.data)) == 6);   // ok
    static_assert(__builtin_strlen(id(str.data)) == 6);     // error
}

The last assertion fails (on trunk c++26, but also lots of versions going
back):

<source>:24:50: error: non-constant condition for static assertion
   24 |     static_assert(__builtin_strlen(id(str.data)) == 6);     // error
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
<source>:24:35: error: '__builtin_strlen(((const char*)(&
str.fixed_string<7>::data)))' is not a constant expression
   24 |     static_assert(__builtin_strlen(id(str.data)) == 6);     // error
      |                   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~

id(x) is just x, it's not doing anything to the parameter. It's like the
constant evaluator loses track of the pointer.

Reply via email to