public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members
@ 2024-03-16 21:50 teodor_spaeren at riseup dot net
  2024-03-16 22:13 ` [Bug libstdc++/114367] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: teodor_spaeren at riseup dot net @ 2024-03-16 21:50 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114367
           Summary: std::vector<bool> constexpr initialization doesn't
                    start lifetime of array members
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: teodor_spaeren at riseup dot net
  Target Milestone: ---

#include<vector>
consteval int foo(){
    std::vector<bool>seen(26);
    return 0;
}
int main(){
    return foo();
}

This is the code. If you compile this with g++, it compiles fine, but with
clang it gives a warning about object lifetimes.

lel.cpp:7:12: error: call to consteval function 'foo' is not a constant
expression
    return foo();
           ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/stl_bvector.h:649:15:
note: assignment to object outside its lifetime is not allowed in a constant
expression
            __p[__i] = 0ul;
                     ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/stl_bvector.h:1317:31:
note: in call to '&seen->_M_allocate(1)'
            _Bit_pointer __q = this->_M_allocate(__n);
                                     ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/stl_bvector.h:768:2:
note: in call to '&seen->_M_initialize(26)'
        _M_initialize(__n);
        ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/stl_bvector.h:755:9:
note: in call to 'vector(26, false, allocator_type())'
      : vector(__n, false, __a)
        ^
lel.cpp:3:22: note: in call to 'vector(26, allocator_type())'
    std::vector<bool>seen(26);
                     ^
lel.cpp:7:12: note: in call to 'foo()'
    return foo();
           ^
1 error generated.

When compiling with 

https://github.com/gcc-mirror/gcc/blob/releases/gcc-13/libstdc%2B%2B-v3/include/bits/stl_bvector.h#L676-L685

In the code it seems it never constructs the array, but starts accessing the
members directly. I don't know if this is affected by
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html or not.
I've submitted a bug report to clang also.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
@ 2024-03-16 22:13 ` pinskia at gcc dot gnu.org
  2024-03-16 22:14 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-16 22:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm:

        _Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl, _S_nword(__n));
#if __cpp_lib_is_constant_evaluated
        if (std::is_constant_evaluated())
        {
          __n = _S_nword(__n);
          for (size_t __i = 0; __i < __n; ++__i)
            __p[__i] = 0ul;
        }
#endif

Maybe I misunderstand how this should work.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
  2024-03-16 22:13 ` [Bug libstdc++/114367] " pinskia at gcc dot gnu.org
@ 2024-03-16 22:14 ` pinskia at gcc dot gnu.org
  2024-03-17  9:15 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-16 22:14 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-03-16
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Hmm:
> 
>         _Bit_pointer __p = _Bit_alloc_traits::allocate(_M_impl,
> _S_nword(__n));
> #if __cpp_lib_is_constant_evaluated
>         if (std::is_constant_evaluated())
>         {
>           __n = _S_nword(__n);
>           for (size_t __i = 0; __i < __n; ++__i)
>             __p[__i] = 0ul;
>         }
> #endif
> 
> Maybe I misunderstand how this should work.

Oh yes see https://github.com/llvm/llvm-project/issues/46391

So yes this is a GCC bug with the above.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
  2024-03-16 22:13 ` [Bug libstdc++/114367] " pinskia at gcc dot gnu.org
  2024-03-16 22:14 ` pinskia at gcc dot gnu.org
@ 2024-03-17  9:15 ` redi at gcc dot gnu.org
  2024-03-18 13:19 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-17  9:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Yeah this needs to use construct_at

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
                   ` (2 preceding siblings ...)
  2024-03-17  9:15 ` redi at gcc dot gnu.org
@ 2024-03-18 13:19 ` redi at gcc dot gnu.org
  2024-03-19 15:14 ` cvs-commit at gcc dot gnu.org
  2024-04-03 10:46 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-18 13:19 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.4
           Keywords|                            |rejects-valid
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
                   ` (3 preceding siblings ...)
  2024-03-18 13:19 ` redi at gcc dot gnu.org
@ 2024-03-19 15:14 ` cvs-commit at gcc dot gnu.org
  2024-04-03 10:46 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-03-19 15:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:16afbd9c9c4282d56062cef95e6eccfdcf3efe03

commit r14-9545-g16afbd9c9c4282d56062cef95e6eccfdcf3efe03
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Mar 18 13:00:17 2024 +0000

    libstdc++: Begin lifetime of storage in std::vector<bool> [PR114367]

    This doesn't cause a problem with GCC, but Clang correctly diagnoses a
    bug in the code. The objects in the allocated storage need to begin
    their lifetime before we start using them.

    This change uses the allocator's construct function instead of using
    std::construct_at directly, in order to support fancy pointers.

    libstdc++-v3/ChangeLog:

            PR libstdc++/114367
            * include/bits/stl_bvector.h (_M_allocate): Use allocator's
            construct function to begin lifetime of words.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug libstdc++/114367] std::vector<bool> constexpr initialization doesn't start lifetime of array members
  2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
                   ` (4 preceding siblings ...)
  2024-03-19 15:14 ` cvs-commit at gcc dot gnu.org
@ 2024-04-03 10:46 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-04-03 10:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:d8d71b19f0b1e28fd6d413a6874ec55c568865b0

commit r13-8568-gd8d71b19f0b1e28fd6d413a6874ec55c568865b0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Mar 18 13:00:17 2024 +0000

    libstdc++: Begin lifetime of storage in std::vector<bool> [PR114367]

    This doesn't cause a problem with GCC, but Clang correctly diagnoses a
    bug in the code. The objects in the allocated storage need to begin
    their lifetime before we start using them.

    This change uses the allocator's construct function instead of using
    std::construct_at directly, in order to support fancy pointers.

    libstdc++-v3/ChangeLog:

            PR libstdc++/114367
            * include/bits/stl_bvector.h (_M_allocate): Use allocator's
            construct function to begin lifetime of words.

    (cherry picked from commit 16afbd9c9c4282d56062cef95e6eccfdcf3efe03)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-04-03 10:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-16 21:50 [Bug libstdc++/114367] New: std::vector<bool> constexpr initialization doesn't start lifetime of array members teodor_spaeren at riseup dot net
2024-03-16 22:13 ` [Bug libstdc++/114367] " pinskia at gcc dot gnu.org
2024-03-16 22:14 ` pinskia at gcc dot gnu.org
2024-03-17  9:15 ` redi at gcc dot gnu.org
2024-03-18 13:19 ` redi at gcc dot gnu.org
2024-03-19 15:14 ` cvs-commit at gcc dot gnu.org
2024-04-03 10:46 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).