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

            Bug ID: 112794
           Summary: [contracts] modifying return value fails
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jason at gcc dot gnu.org
  Target Milestone: ---

Both asserts should pass.  f1 seems to fail because we pass a copy of r to
mutate instead of r itself; the .gimple dump for f2 looks fine but the
optimizers get confused for some reason.

#include <cassert>

bool mutate(const int &i) {
  const_cast<int&>(i) = 42;
  return true;
}

int f1()
  [[ post r: mutate(r) ]]
{
  return 0;
}

struct S {
  S(int i) : i(i) {}
  S(S&&) = delete; // non-moveable                                              
  int i;
};

bool mutate(const S& s) {
  const_cast<S&>(s).i = 42;
  return true;
}

S f2()
  [[ post r: mutate(r) ]]
{
  return S{0};
}

int main() {
  assert(f1() == 42);   // Fails.                                               
  assert(f2().i == 42); // Passes at -O0, ICEs at -O1, fails at -O2.            
}

Reply via email to