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

            Bug ID: 111750
           Summary: Spurious -Warray-bounds warning when using member
                    function pointers
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abbeyj+gcc at gmail dot com
  Target Milestone: ---

Created attachment 56086
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56086&action=edit
Reproducer

The source code below generates a -Warray-bounds warning which I believe is
incorrect.  Compile with `g++ -c -Wall -O2`.

```
struct MyClass {
    void my_method();
};

MyClass g;

void pre();

inline void FetchValue(MyClass& c, void(MyClass::*func)()) {
    pre();
    (c.*func)();
}

int get_int();

inline int Check() {
    static const int ret = get_int();
    return ret;
}

inline void ReadValue(MyClass& c, void(MyClass::*func)()) {
    Check();
    FetchValue(c, func);
}

void Main() {
    ReadValue(g, &MyClass::my_method);
}
```

This produces:
```
In function 'void FetchValue(MyClass&, void (MyClass::*)())',
    inlined from 'void ReadValue(MyClass&, void (MyClass::*)())' at
<source>:23:15,
    inlined from 'void Main()' at <source>:27:14:
<source>:11:14: warning: array subscript 'int (**)(...)[0]' is partly outside
array bounds of 'MyClass [1]' [-Warray-bounds=]
   11 |     (c.*func)();
      |     ~~~~~~~~~^~
<source>: In function 'void Main()':
<source>:5:9: note: object 'g' of size 1
    5 | MyClass g;
      |         ^
```

Godbolt link: https://godbolt.org/z/6YsWd9xhr

That this source produces a -Warray-bounds warning is somewhat surprising since
it contains no arrays, no array indexing, and no pointer arithmetic.  Small
changes such as removing the static variable or manually inlining a function
into its caller make the warning go away.  

The earliest version that I've been able to reproduce this with is GCC 11.1 and
it still reproduces on the trunk version that's currently available on
godbolt.org.

Reply via email to