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

            Bug ID: 127089
           Summary: [[assume(!v.empty())]] does not suppress
                    -Warray-bounds on vector::pop_back, unlike if
                    (empty()) std::unreachable()
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rogerio.souza at gmail dot com
  Target Milestone: ---

The following testcase produces a -Warray-bounds warning for
std::vector::pop_back() even when the vector's non-emptiness is explicitly
provided using the standard C++23 [[assume]] attribute:

testcase-assume.cpp:
===============================
#include <vector>

struct H;
const H* const* beg();
const H* const* end();

void assumed(const H* x)
{
    std::vector<const H*> v(beg(), end());

    [[assume(!v.empty())]];
    v.pop_back();

    v.push_back(x);
}

void unreachable(const H* x)
{
    std::vector<const H*> v(beg(), end());

    if (v.empty())
        std::unreachable();

    v.pop_back();
    v.push_back(x);
}
===============================

Compile with:
===============================
$ g++ -c -std=c++23 -O2 -Wall testcase-assume.cpp
===============================

GCC emits this warning for assumed:
===============================
.../bits/stl_construct.h:88:9: warning: array subscript -1 is outside array
bounds of 'const H* [1152921504606846975]' [-Warray-bounds=]
   88 |         __location->~_Tp();
      |         ~~~~~~~~~~~~~~~~^~
... inlined from 'constexpr void std::vector<_Tp, _Alloc>::pop_back()'
... inlined from 'void assumed(const H*)' at testcase-assume.cpp:12:15
===============================

Version results
The unguarded pop_back() testcase was compiled with -std=c++23 -Wall:

g++     -O1     -O2     -O3
12.4.0  clean   warns   warns
14.2.0  clean   warns   warns
14.3.0  clean   warns   warns
15.2.0  clean   clean   clean
15.3.0  clean   clean   clean
16.1.0  clean   warns   warns

The behavior is independent of the selected language mode in the tested range
(-std=c++17 through -std=c++26).
  • [Bug tree-optimization/127089]... rogerio.souza at gmail dot com via Gcc-bugs

Reply via email to