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

            Bug ID: 63446
           Summary: dangling reference results in confusing diagnostic
                    from -Wuninitialized
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: M8R-ynb11d at mailinator dot com

struct foo {
    int &ref;
    foo(int &i) : ref(i) {}
};

foo make_foo()
{
    int x = 42;
    return foo(x);
}

int func()
{
    foo f = make_foo();
    return f.ref;
}

This code is obviously broken due to the dangling reference, so I'm glad gcc
gives a warning (clang is silent) but the warning is a bit confusing:

$ g++ -O2 -Wall -c wuninit.cpp
wuninit.cpp: In function ‘int func()’:
wuninit.cpp:15:14: warning: ‘x’ is used uninitialized in this function
[-Wuninitialized]
     return f.ref;
              ^

I get that the diagnostic is generated after inlining has moved x into func(),
but it's still rather confusing as the person that wrote func() might have no
knowledge of the internals of make_foo(), and this would be a real head
scratcher for them.  Additionally, it mentions x being used uninitialized, but
x is initialized.  (I understand that the initialization becomes dead code and
is removed, but that's not immediately obvious.)

In an ideal world gcc would warn about the last line of make_foo() instead of
func(), and it would mention a dangling reference instead of an uninitialized
use.

Reply via email to