<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59684>59684</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang++ incorrectly rejects program containing nested classes and operator= overload
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          NobodyNada
      </td>
    </tr>
</table>

<pre>
    I'm seeing a strange compile error when trying to compile the [bsnes-plus](https://github.com/devinacker/bsnes-plus) emulator. Here's the minimal excerpt of the source code that reproduces the problem:

```cpp
class CPUcore {
public:
  struct flag_t {
 bool n, v, m, x, d, i, z, c;

    inline operator unsigned() const {
      return (n << 7) + (v << 6) + (m << 5) + (x << 4)
           + (d << 3) + (i << 2) + (z << 1) + (c << 0);
    }

    inline unsigned operator=(unsigned data) {
      n = data & 0x80; v = data & 0x40; m = data & 0x20; x = data & 0x10;
      d = data & 0x08; i = data & 0x04; z = data & 0x02; c = data & 0x01;
      return data;
 }

    inline unsigned operator|=(unsigned data) { return operator=(operator unsigned() | data); }
    inline unsigned operator^=(unsigned data) { return operator=(operator unsigned() ^ data); }
    inline unsigned operator&=(unsigned data) { return operator=(operator unsigned() & data); }

    flag_t() : n(0), v(0), m(0), x(0), d(0), i(0), z(0), c(0) {}
  };

  struct regs_t {
 int &a;
    flag_t p;
  };

  regs_t regs;
};

int main() {

}
```

The error is:
```
> clang++ test.cpp
test.cpp:17:56: error: no matching member function for call to 'operator='
    inline unsigned operator|=(unsigned data) { return operator=(operator unsigned() | data); }
 ^~~~~~~~~
test.cpp:3:10: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'unsigned int' to 'CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
test.cpp:3:10: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unsigned int' to 'const CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
test.cpp:18:56: error: no matching member function for call to 'operator='
    inline unsigned operator^=(unsigned data) { return operator=(operator unsigned() ^ data); }
 ^~~~~~~~~
test.cpp:3:10: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'unsigned int' to 'CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
test.cpp:3:10: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unsigned int' to 'const CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
test.cpp:19:56: error: no matching member function for call to 'operator='
    inline unsigned operator&=(unsigned data) { return operator=(operator unsigned() & data); }
 ^~~~~~~~~
test.cpp:3:10: note: candidate function (the implicit move assignment operator) not viable: no known conversion from 'unsigned int' to 'CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
test.cpp:3:10: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unsigned int' to 'const CPUcore::flag_t' for 1st argument
  struct flag_t {
 ^
3 errors generated.
```

It appears the compiler is not recognizing the `operator=` overload.

Every component of the `CPUcore` declaration reproduced here is necessary to trigger the error -- the `flag_t` struct must be declared inside CPUcore, it must be used a member variable of `regs_t` alongside a reference variable, and the `regs_t` struct must be used as a member variable of `CPUcore` Changing any one of those conditions will cause the code to compile successfully.

I'm running macOS 13 on an M1 Pro. The bug is reproducible in LLVM 11.0 and newer when built from source or installed via Homebrew; bisecting finds the bug was introduced in d144601. Curiously, the bug is not reproducible on the version of Clang that ships with the Apple developer tools.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWEtv2z4S_zT0ZRBDomzJPviQOAlaoO0W2Md1QVNjmVuKFEjKiXPYz74Y6mHZaboo0BZ_oDUCivmRnMePw-FDeK8qg7hhyzu2vJ-JNhys23yyO1uePolSzKiyec94UYNHVKYCAT44YSoEaetGaQR0zjp4OqCB4E7UJ9ixMRwQ2PJu5w36m0a3ni3vGV8dQmg8y24Zf2T8sVLh0O7m0taMP5Z4VEbIL-gYf5yM42vAutUiWDeHd-iQ8cJH-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