public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114225] New: False positive -Werror=dangling-reference
@ 2024-03-04  8:32 asharafutdinov at adalisk dot com
  2024-03-04  9:07 ` [Bug c++/114225] " xry111 at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: asharafutdinov at adalisk dot com @ 2024-03-04  8:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114225
           Summary: False positive -Werror=dangling-reference
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: asharafutdinov at adalisk dot com
  Target Milestone: ---

#include <variant>

struct A
{
    int i;
};

struct Getter
{
    Getter(const A & a) : var(&a) {}
    const int & operator ()(const A * a) const { return a->i; }
    const int & value() const { return std::visit(*this, var); }
    std::variant<const A *> var;
};

int main()
{
    A a { .i = 1 };
    const auto & i = Getter{ a }.value();
    return i;
}

The code above produces an incorrect dangling reference warning with g++-13
-std=c++20 -Wall -Wextra -pedantic-errors:

<source>: In function 'int main()':
<source>:19:18: warning: possibly dangling reference to a temporary
[-Wdangling-reference]
   19 |     const auto & i = Getter{ a }.value();
      |                  ^
<source>:19:39: note: the temporary was destroyed at the end of the full
expression 'Getter(a).Getter::value()'
   19 |     const auto & i = Getter{ a }.value();
      |                      ~~~~~~~~~~~~~~~~~^~

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/114225] False positive -Werror=dangling-reference
  2024-03-04  8:32 [Bug c++/114225] New: False positive -Werror=dangling-reference asharafutdinov at adalisk dot com
@ 2024-03-04  9:07 ` xry111 at gcc dot gnu.org
  2024-03-04  9:30 ` fchelnokov at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-04  9:07 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
-Wdangling-reference is a very vague warning as it's documented.  Either not
use it or suppress it with #pragma (also documented).

Document:
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/C_002b_002b-Dialect-Options.html#index-Wdangling-reference

*** This bug has been marked as a duplicate of bug 114220 ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/114225] False positive -Werror=dangling-reference
  2024-03-04  8:32 [Bug c++/114225] New: False positive -Werror=dangling-reference asharafutdinov at adalisk dot com
  2024-03-04  9:07 ` [Bug c++/114225] " xry111 at gcc dot gnu.org
@ 2024-03-04  9:30 ` fchelnokov at gmail dot com
  2024-03-04  9:33 ` xry111 at gcc dot gnu.org
  2024-03-04  9:53 ` fchelnokov at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: fchelnokov at gmail dot com @ 2024-03-04  9:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Please note that this is a regression in GCC 13. GCC 12 (or other compilers) do
not show a warning here: https://godbolt.org/z/Yhfad47xs

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/114225] False positive -Werror=dangling-reference
  2024-03-04  8:32 [Bug c++/114225] New: False positive -Werror=dangling-reference asharafutdinov at adalisk dot com
  2024-03-04  9:07 ` [Bug c++/114225] " xry111 at gcc dot gnu.org
  2024-03-04  9:30 ` fchelnokov at gmail dot com
@ 2024-03-04  9:33 ` xry111 at gcc dot gnu.org
  2024-03-04  9:53 ` fchelnokov at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-04  9:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Fedor Chelnokov from comment #2)
> Please note that this is a regression in GCC 13. GCC 12 (or other compilers)
> do not show a warning here: https://godbolt.org/z/Yhfad47xs

No, GCC 12 does not have -Wdangling-reference at all.  It was added in GCC 13.1
as a part of -Wall, but then moved to -Wextra in 13.2 because it's vague.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/114225] False positive -Werror=dangling-reference
  2024-03-04  8:32 [Bug c++/114225] New: False positive -Werror=dangling-reference asharafutdinov at adalisk dot com
                   ` (2 preceding siblings ...)
  2024-03-04  9:33 ` xry111 at gcc dot gnu.org
@ 2024-03-04  9:53 ` fchelnokov at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: fchelnokov at gmail dot com @ 2024-03-04  9:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Fedor Chelnokov <fchelnokov at gmail dot com> ---
Ok.

At the same time we see that some false positives of this warning are fixed in
Trunk, e.g.

struct A {
    int i;
};

struct Getter {
    const A * a;
    const int & value() const { return a->i; }
};

int main() {
    A a { .i = 1 };
    const auto & i = Getter{ &a }.value();
    return i;
}

Online demo: https://godbolt.org/z/z9rPx831n

So we assume that this one can be fixed as well, why not?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-03-04  9:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-04  8:32 [Bug c++/114225] New: False positive -Werror=dangling-reference asharafutdinov at adalisk dot com
2024-03-04  9:07 ` [Bug c++/114225] " xry111 at gcc dot gnu.org
2024-03-04  9:30 ` fchelnokov at gmail dot com
2024-03-04  9:33 ` xry111 at gcc dot gnu.org
2024-03-04  9:53 ` fchelnokov at gmail dot com

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).