public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug c++/38958] New: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] @ 2009-01-24 13:32 fs dot open dot work at googlemail dot com 2009-01-30 15:24 ` [Bug c++/38958] " bangerth at gmail dot com 0 siblings, 1 reply; 8+ messages in thread From: fs dot open dot work at googlemail dot com @ 2009-01-24 13:32 UTC (permalink / raw) To: gcc-bugs Given a class C with destructor side-effects (as is common with RAII classes) and a function fn which returns a rvalue of type C C fn() the lifetime of the rvalue returned by fn() can be extended by holding a reference to const C with C const& r = fn() Although r is not explicitly referenced subsequently in the calling scope, upon leaving scope the rvalue's dtor will be invoked. So r is implicitly 'used'. The assignment C c = fn() has the same effect because of return value optimization. Though copy-semantics must be checked, the copy is elided. G++ generates an 'unused variable' warning for 'r' but not for 'c'. The following program demonstrates the warning. The 'volatile int g' and forced-inline noise is to compare the generated assembly to that generated when the const& is removed. The output in both cases is equivalent (identical at -O2; the only difference in lower optimizations is an additional indirection with the const& present). ---------------------------------- volatile int g; // for obvious side-effect visibility in generated assembly struct Lock { __attribute__((always_inline)) // for obvious side-effect visibility in generated assembly ~Lock() { g = 0; } }; Lock AcquireLock() { return Lock(); } int main() { Lock const& lock = AcquireLock(); g = 1; g = 2; g = 3; } ---------------------------------- In both cases, const& present and not present, the sequence of values moved into g are 1,2,3,0 as desired. This might be related to Bug 10416 but does not appear to duplicate it since removing the const& removes the warning in this case. This warning can be avoided by relying on return-value optimization and using the assignment without the const& to generate equivalent code but I don't think it should be emitted at all if it is not for the RVO version. I would expect it emitted for both or not emitted for either. -- Summary: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] Product: gcc Version: 4.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: fs dot open dot work at googlemail dot com GCC build triplet: i686-pc-linux-gnu, mingw GCC host triplet: i686-pc-linux-gnu, mingw GCC target triplet: i686-pc-linux-gnu, mingw, arm-xscale-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] 2009-01-24 13:32 [Bug c++/38958] New: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] fs dot open dot work at googlemail dot com @ 2009-01-30 15:24 ` bangerth at gmail dot com 0 siblings, 0 replies; 8+ messages in thread From: bangerth at gmail dot com @ 2009-01-30 15:24 UTC (permalink / raw) To: gcc-bugs ------- Comment #1 from bangerth at gmail dot com 2009-01-30 15:23 ------- Confirmed. -- bangerth at gmail dot com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 Keywords| |diagnostic Last reconfirmed|0000-00-00 00:00:00 |2009-01-30 15:23:42 date| | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 ^ permalink raw reply [flat|nested] 8+ messages in thread
[parent not found: <bug-38958-4@http.gcc.gnu.org/bugzilla/>]
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> @ 2013-06-12 13:57 ` paolo.carlini at oracle dot com 2013-06-12 21:38 ` paolo.carlini at oracle dot com ` (4 subsequent siblings) 5 siblings, 0 replies; 8+ messages in thread From: paolo.carlini at oracle dot com @ 2013-06-12 13:57 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 Paolo Carlini <paolo.carlini at oracle dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |ASSIGNED CC|gcc-bugs at gcc dot gnu.org | Assignee|unassigned at gcc dot gnu.org |paolo.carlini at oracle dot com Target Milestone|--- |4.9.0 --- Comment #2 from Paolo Carlini <paolo.carlini at oracle dot com> --- Mine. ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> 2013-06-12 13:57 ` paolo.carlini at oracle dot com @ 2013-06-12 21:38 ` paolo.carlini at oracle dot com 2014-07-28 14:38 ` redi at gcc dot gnu.org ` (3 subsequent siblings) 5 siblings, 0 replies; 8+ messages in thread From: paolo.carlini at oracle dot com @ 2013-06-12 21:38 UTC (permalink / raw) To: gcc-bugs http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 Paolo Carlini <paolo.carlini at oracle dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|ASSIGNED |RESOLVED Resolution|--- |FIXED --- Comment #3 from Paolo Carlini <paolo.carlini at oracle dot com> --- Fixed for 4.9.0. ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> 2013-06-12 13:57 ` paolo.carlini at oracle dot com 2013-06-12 21:38 ` paolo.carlini at oracle dot com @ 2014-07-28 14:38 ` redi at gcc dot gnu.org 2014-11-02 8:50 ` paolo.carlini at oracle dot com ` (2 subsequent siblings) 5 siblings, 0 replies; 8+ messages in thread From: redi at gcc dot gnu.org @ 2014-07-28 14:38 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 Jonathan Wakely <redi at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |galdralag at bk dot ru --- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> --- *** Bug 61935 has been marked as a duplicate of this bug. *** ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> ` (2 preceding siblings ...) 2014-07-28 14:38 ` redi at gcc dot gnu.org @ 2014-11-02 8:50 ` paolo.carlini at oracle dot com 2014-11-22 2:23 ` jason at gcc dot gnu.org 2015-01-07 15:18 ` jason at gcc dot gnu.org 5 siblings, 0 replies; 8+ messages in thread From: paolo.carlini at oracle dot com @ 2014-11-02 8:50 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 Paolo Carlini <paolo.carlini at oracle dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |petschy at gmail dot com --- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> --- *** Bug 63657 has been marked as a duplicate of this bug. *** ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> ` (3 preceding siblings ...) 2014-11-02 8:50 ` paolo.carlini at oracle dot com @ 2014-11-22 2:23 ` jason at gcc dot gnu.org 2015-01-07 15:18 ` jason at gcc dot gnu.org 5 siblings, 0 replies; 8+ messages in thread From: jason at gcc dot gnu.org @ 2014-11-22 2:23 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 --- Comment #6 from Jason Merrill <jason at gcc dot gnu.org> --- Author: jason Date: Sat Nov 22 02:21:35 2014 New Revision: 217957 URL: https://gcc.gnu.org/viewcvs?rev=217957&root=gcc&view=rev Log: PR c++/63657 PR c++/38958 * call.c (set_up_extended_ref_temp): Set TREE_USED on the reference if the temporary has a non-trivial destructor. * decl.c (poplevel): Don't look through references. Added: trunk/gcc/testsuite/g++.dg/warn/Wunused-var-22.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/call.c trunk/gcc/cp/decl.c ^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/38958] 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> ` (4 preceding siblings ...) 2014-11-22 2:23 ` jason at gcc dot gnu.org @ 2015-01-07 15:18 ` jason at gcc dot gnu.org 5 siblings, 0 replies; 8+ messages in thread From: jason at gcc dot gnu.org @ 2015-01-07 15:18 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958 --- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> --- Author: jason Date: Wed Jan 7 15:18:05 2015 New Revision: 219306 URL: https://gcc.gnu.org/viewcvs?rev=219306&root=gcc&view=rev Log: PR c++/63657 PR c++/38958 * call.c (set_up_extended_ref_temp): Set TREE_USED on the reference if the temporary has a non-trivial destructor. * decl.c (poplevel): Don't look through references. Added: branches/gcc-4_9-branch/gcc/testsuite/g++.dg/warn/Wunused-var-22.C Modified: branches/gcc-4_9-branch/gcc/cp/ChangeLog branches/gcc-4_9-branch/gcc/cp/call.c branches/gcc-4_9-branch/gcc/cp/decl.c ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-01-07 15:18 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2009-01-24 13:32 [Bug c++/38958] New: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] fs dot open dot work at googlemail dot com 2009-01-30 15:24 ` [Bug c++/38958] " bangerth at gmail dot com [not found] <bug-38958-4@http.gcc.gnu.org/bugzilla/> 2013-06-12 13:57 ` paolo.carlini at oracle dot com 2013-06-12 21:38 ` paolo.carlini at oracle dot com 2014-07-28 14:38 ` redi at gcc dot gnu.org 2014-11-02 8:50 ` paolo.carlini at oracle dot com 2014-11-22 2:23 ` jason at gcc dot gnu.org 2015-01-07 15:18 ` jason at gcc dot gnu.org
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).