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

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Thiago Macieira from comment #7)
> (In reply to Martin Liška from comment #6)
> > Anyway, upstream removed the pure attribute as we suggested:
> > https://codereview.qt-project.org/c/qt/qtbase/+/392357
> 
> Can we be assured the pure attribute will work for complex return types?
> 
> https://gcc.godbolt.org/z/KE4s74od3
> struct S
> {
>     bool *ptr;
>     S();
>     S(const S &);
> };
> 
> #ifdef __GNUC__
> __attribute__((pure))
> #endif
> S f1();
> bool f2()
> {
>     return *f1().ptr;
> }

I think this came up before.  Yes, the intent is that this is well-defined
if 'f1' has no observable effects on the state of the program other than
to return a value.  There might have been bugs in old versions of GCC
of course - I do remember some points-to analysis oddities around this,
checking GCC 7 it works OK there at least.  A testcase to check would be
for example

struct S
{
  bool *ptr;
  S();
  S(const S &);
};

__attribute__((pure))
S f1(bool *);
void f2()
{
  bool c = false;
  *f1(&c).ptr = true;
  if (!c)
    __builtin_abort ();
}

but all versions godbolt has do not miscompile this.

Reply via email to