Bug 63181 - GCC should warn about "obvious" bugs in binding a reference to temporary
Summary: GCC should warn about "obvious" bugs in binding a reference to temporary
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
: 63606 (view as bug list)
Depends on:
Blocks: 7651 new-warning, new_warning
  Show dependency treegraph
 
Reported: 2014-09-05 00:02 UTC by Brooks Moses
Modified: 2024-01-01 02:53 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-09-05 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brooks Moses 2014-09-05 00:02:34 UTC
GCC should warn about "obvious" bugs in binding a reference to temporary.

Small test case:

struct Foo {
   Foo(int x): x_(x) { }
   int& x_;
};


int main()
{
   Foo f(0);
   return f.x_;
}

Gcc -Wall is silent.


Clang detects this clearly:
clang++ foo.cc -Wall
foo.cc:2:19: warning: binding reference member 'x_' to stack allocated parameter 'x' [-Wdangling-field]
   Foo(int x): x_(x) { }
                  ^
foo.cc:3:9: note: reference member declared here
   int& x_;
        ^
1 warning generated.
Comment 1 Richard Biener 2014-09-05 08:39:37 UTC
Confirmed.
Comment 2 Jonathan Wakely 2014-09-05 09:05:06 UTC
N.B. that's not a temporary, it's a named lvalue, but we should definitely diagnose it.

Since 4.7 GCC does now diagnose similar cases with temporaries resulting from implicit conversions:

struct Foo {
   Foo(short x): x_(x) { }
   const int& x_;
};

int main()
{
   Foo f(0);
   return f.x_;
}

t.cc: In constructor ‘Foo::Foo(short int)’:
t.cc:2:22: warning: a temporary bound to ‘Foo::x_’ only persists until the constructor exits [-Wextra]
    Foo(short x): x_(x) { }
                      ^
Comment 3 Jonathan Wakely 2014-10-20 22:06:00 UTC
*** Bug 63606 has been marked as a duplicate of this bug. ***
Comment 4 Eric Gallager 2019-11-21 09:31:41 UTC
(In reply to Brooks Moses from comment #0)
> GCC should warn about "obvious" bugs in binding a reference to temporary.
> 
> Small test case:
> 
> struct Foo {
>    Foo(int x): x_(x) { }
>    int& x_;
> };
> 
> 
> int main()
> {
>    Foo f(0);
>    return f.x_;
> }
> 
> Gcc -Wall is silent.
> 
> 
> Clang detects this clearly:
> clang++ foo.cc -Wall
> foo.cc:2:19: warning: binding reference member 'x_' to stack allocated
> parameter 'x' [-Wdangling-field]
>    Foo(int x): x_(x) { }
>                   ^
> foo.cc:3:9: note: reference member declared here
>    int& x_;
>         ^
> 1 warning generated.

If we reuse clang's -Wdangling-field name, this would be a new warning, so making it block the relevant meta-bug.

(In reply to Jonathan Wakely from comment #2)
> N.B. that's not a temporary, it's a named lvalue, but we should definitely
> diagnose it.
> 
> Since 4.7 GCC does now diagnose similar cases with temporaries resulting
> from implicit conversions:
> 
> struct Foo {
>    Foo(short x): x_(x) { }
>    const int& x_;
> };
> 
> int main()
> {
>    Foo f(0);
>    return f.x_;
> }
> 
> t.cc: In constructor ‘Foo::Foo(short int)’:
> t.cc:2:22: warning: a temporary bound to ‘Foo::x_’ only persists until the
> constructor exits [-Wextra]
>     Foo(short x): x_(x) { }
>                       ^

This one should be moved to its own separate option per bug 7651
Comment 5 Jonathan Wakely 2019-11-21 11:46:23 UTC
(In reply to Eric Gallager from comment #4)
> This one should be moved to its own separate option per bug 7651

Indeed it should, and if we add -Wdangling-field then that would be the ideal option to move it to.