Bug 113021 - [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
Summary: [constexpr] gcc rejects initializing struct containing vector during constant...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 13.2.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2023-12-14 14:25 UTC by Miro Palmu
Modified: 2023-12-15 19:40 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 12.1.0, 14.0
Last reconfirmed: 2023-12-14 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Miro Palmu 2023-12-14 14:25:51 UTC
Following code fails to compile on gcc (GCC) 13.2.1 20230801 but on clang (16.0.6 or 17.0.1). 

```
#include <vector>

struct A {
  std::vector<int> vec;
  int a;
};

consteval auto foo() { const A tmp1{{1, 2}, 42}; }

int main() {
  foo();
  return 0;
}

```

With following error message:

```
> g++ -Wall -Wextra -std=c++20 main.cpp
In file included from /usr/include/c++/13.2.1/vector:66,
                 from main2.cpp:1:
/usr/include/c++/13.2.1/bits/stl_vector.h: In function ‘int main()’:
main2.cpp:11:6:   in ‘constexpr’ expansion of ‘foo()’
main2.cpp:8:49:   in ‘constexpr’ expansion of ‘std::vector<int>(std::initializer_list<int>{((const int*)(& const int [2]{1, 2})), 2}, ((const std::vector<int>::allocator_type&)(& std::allocator<int>())))’
/usr/include/c++/13.2.1/bits/stl_vector.h:679:21:   in ‘constexpr’ expansion of ‘((std::vector<int>*)this)->std::vector<int>::_M_range_initialize<const int*>(__l.std::initializer_list<int>::begin(), __l.std::initializer_list<int>::end(), (std::random_access_iterator_tag{std::bidirectional_iterator_tag()}, std::forward_iterator_tag()))’
/usr/include/c++/13.2.1/bits/stl_vector.h:1689:13: error: modifying a const object ‘((std::vector<int>*)this)->std::vector<int>::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int> >::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_Vector_impl_data::_M_start’ is not allowed in a constant expression
 1688 |           this->_M_impl._M_start
      |           ~~~~~~~~~~~~~~~~~~~~~~
 1689 |             = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator()));
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main2.cpp:8:32: note: originally declared ‘const’ here
    8 | consteval auto foo() { const A tmp1{ {1, 2}, 42 }; }
      |                                ^~~~
```

If member variable `a` is removed it compiles:

```
#include <vector>

struct A {
  std::vector<int> vec;
};

consteval auto foo() { const A tmp1{ {1, 2} }; }

int main() {
  foo();
  return 0;
}
```
Comment 1 Drea Pinski 2023-12-14 17:35:06 UTC
Confirmed. We should reduce this to remove the include file too ...
Comment 2 Miro Palmu 2023-12-15 10:44:45 UTC
(In reply to Andrew Pinski from comment #1)
> Confirmed. We should reduce this to remove the include file too ...

Here is somewhat reduced:

```
#include <memory>

auto alloc = std::allocator<int>{};
constexpr auto n = 3;

struct vec {
    int* data;

    constexpr vec() {
        this->data = alloc.allocate(n);
    }

    constexpr ~vec() {
        alloc.deallocate(this->data, n);
    }
};

struct A {
  vec v;
  int a;
};

consteval auto foo() {
    A temp1{{}, 42}; // This compiles fine
    const A tmp2{{}, 42}; // This does not
}

int main() {
  foo();
}
```
Comment 3 Miro Palmu 2023-12-15 10:55:23 UTC
Here is even more reduced. Notice that if the unused data member `pointer` is removed from `vec` the problem goes away. Same with data member `a` in struct `A`.

```
struct vec {
    int data;
    int* pointer; // This is required for compilation to fail

    constexpr vec() {
        data = 42;
    }

};

struct A {
  vec v;
  int a; // This is required for compilation to fail
};

consteval auto foo() {
    A temp1{{}, 42};      // This compiles fine
    const A tmp2{{}, 42}; // This does not
}

int main() {
  foo();
}
```
Comment 4 Drea Pinski 2023-12-15 19:40:02 UTC
Looks like it is the uninitialized field pointer which is causing the issues. Looks like GCC does not currently fully support that for const variables inside a constexpr/consteval . The linked PRs are about string but I think the same issue shows up.