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

            Bug ID: 119592
           Summary: Error "array subscript [...] is partly outside array
                    bounds of [...]"
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sandberg.sven at gmail dot com
  Target Milestone: ---

gcc 13.1 and up reports a spurious error "array subscript [...] is partly
outside array bounds of [...]" for this code: https://godbolt.org/z/f3M143a1e

The error is spurious because it occurs only when it inlines line 20, but line
20 cannot be reached since m_always_true is initialized to true and never
altered.

Command line: `gcc -std=c++20 -Werror=array-bounds=1 -O2`

Code:
```
#include <iostream>
#include <set>

using Set1 = std::set<int>;
using Set2 = std::set<Set1>;
using Iter1 = Set1::const_iterator;
using Iter2 = Set2::const_iterator;

struct Iter_pair {
  Iter2 m_iter2{};
  Iter1 m_iter1{};
  bool m_always_true{true};

  Iter_pair(const Iter2 &iter2) : m_iter2(iter2), m_always_true(true) {}

  bool set_iter1_if_true_is_false(Iter_pair &other) {
    if (m_always_true && other.m_always_true) return true;
    // not reached
    m_iter1 = m_iter2->begin();
    other.m_iter1 = other.m_iter2->begin(); // <-- line 20
    return m_iter1 == other.m_iter1;
  }
};

// can't be minimized out
void f1();
void f2(const auto& lhs_value);

int main() {
  Set2 s;
  auto it1 = Iter_pair(s.end());
  auto it2 = Iter_pair(s.end());
  if (it1.set_iter1_if_true_is_false(it2)) f1(); else f2(it1);
  auto it3 = Iter_pair(s.end());
  if (it1.set_iter1_if_true_is_false(it3)) f1();
  return 0;
}
```

Full error:
```
In member function 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,
_Alloc>::begin() const [with _Key = int; _Val = int; _KeyOfValue =
std::_Identity<int>; _Compare = std::less<int>; _Alloc = std::allocator<int>]',
    inlined from 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key,
_Compare, _Alloc>::begin() const [with _Key = int; _Compare = std::less<int>;
_Alloc = std::allocator<int>]' at
/opt/compiler-explorer/gcc-trunk-20250402/include/c++/15.0.1/bits/stl_set.h:369:26,
    inlined from 'bool Iter_pair::set_iter1_if_true_is_false(Iter_pair&)' at
<source>:20:41,
    inlined from 'int main()' at <source>:35:37:
/opt/compiler-explorer/gcc-trunk-20250402/include/c++/15.0.1/bits/stl_tree.h:1625:55:
error: array subscript 'const std::_Rb_tree<int, int, std::_Identity<int>,
std::less<int>, std::allocator<int> >[0]' is partly outside array bounds of
'Set2 [1]' {aka 'std::set<std::set<int> > [1]'} [-Werror=array-bounds=]
```

Reply via email to