public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111607] New: False positive -Wdangling-reference
@ 2023-09-27 7:32 fiesh at zefix dot tv
2024-01-18 22:02 ` [Bug c++/111607] " mpolacek at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: fiesh at zefix dot tv @ 2023-09-27 7:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111607
Bug ID: 111607
Summary: False positive -Wdangling-reference
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: fiesh at zefix dot tv
Target Milestone: ---
The following code triggers a `-Wdangling-reference` warning:
t.cpp: In function ‘consteval auto f(const V&)’:
t.cpp:19:22: warning: possibly dangling reference to a temporary
[-Wdangling-reference]
19 | auto const & s = std::visit([](auto const & v) -> S const & {
return v.s; }, v);
| ^
t.cpp:19:36: note: the temporary was destroyed at the end of the full
expression ‘std::visit<f(const V&)::<lambda(const auto:31&)>, const
variant<A>&>(<lambda closure object>f(const V&)::<lambda(const auto:31&)>(), (*
& v))’
19 | auto const & s = std::visit([](auto const & v) -> S const & {
return v.s; }, v);
|
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <variant>
struct S {
constexpr S(int i_) : i(i_) {}
S(S const &) = delete;
S & operator=(S const &) = delete;
S(S &&) = delete;
S & operator=(S &&) = delete;
int i;
};
struct A {
S s{0};
};
using V = std::variant<A>;
consteval auto f(V const & v) {
auto const & s = std::visit([](auto const & v) -> S const & { return
v.s; }, v);
return s.i;
}
int main() {
constexpr V a{std::in_place_type<A>};
constexpr auto i = f(a);
return i;
}
It makes sure the warning is wrong though by
* having S be non-copyable
* evaluating everything at compile time where UB is not allowed to happen
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111607] False positive -Wdangling-reference
2023-09-27 7:32 [Bug c++/111607] New: False positive -Wdangling-reference fiesh at zefix dot tv
@ 2024-01-18 22:02 ` mpolacek at gcc dot gnu.org
2024-01-23 21:36 ` cvs-commit at gcc dot gnu.org
2024-01-23 22:18 ` mpolacek at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-01-18 22:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111607
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Assignee|unassigned at gcc dot gnu.org |mpolacek at gcc dot gnu.org
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=113256
Last reconfirmed| |2024-01-18
Ever confirmed|0 |1
Status|UNCONFIRMED |ASSIGNED
--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Thanks for the report. I suppose we should not warn when the argument is a
lambda type. This might also fix bug 113256.
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -14123,7 +14123,8 @@ do_warn_dangling_reference (tree expr, bool arg_p)
tree e = expr;
while (handled_component_p (e))
e = TREE_OPERAND (e, 0);
- if (!reference_like_class_p (TREE_TYPE (e)))
+ if (!reference_like_class_p (TREE_TYPE (e))
+ && !LAMBDA_TYPE_P (TREE_TYPE (e)))
return expr;
}
@@ -14180,10 +14181,10 @@ do_warn_dangling_reference (tree expr, bool arg_p)
initializing this reference parameter. */
if (do_warn_dangling_reference (arg, /*arg_p=*/true))
return expr;
- /* Don't warn about member function like:
+ /* Don't warn about member functions like:
std::any a(...);
S& s = a.emplace<S>({0}, 0);
- which constructs a new object and returns a reference to it, but
+ which construct a new object and return a reference to it, but
we still want to detect:
struct S { const S& self () { return *this; } };
const S& s = S().self();
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111607] False positive -Wdangling-reference
2023-09-27 7:32 [Bug c++/111607] New: False positive -Wdangling-reference fiesh at zefix dot tv
2024-01-18 22:02 ` [Bug c++/111607] " mpolacek at gcc dot gnu.org
@ 2024-01-23 21:36 ` cvs-commit at gcc dot gnu.org
2024-01-23 22:18 ` mpolacek at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-23 21:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111607
--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:
https://gcc.gnu.org/g:9010fdba68063beccfbab0aa9ec8739f232ca2f4
commit r14-8375-g9010fdba68063beccfbab0aa9ec8739f232ca2f4
Author: Marek Polacek <polacek@redhat.com>
Date: Fri Jan 19 13:59:41 2024 -0500
c++: -Wdangling-reference and lambda false warning [PR109640]
-Wdangling-reference checks if a function receives a temporary as its
argument, and only warns if any of the arguments was a temporary. But
we should not warn when the temporary represents a lambda or we generate
false positives as in the attached testcases.
PR c++/113256
PR c++/111607
PR c++/109640
gcc/cp/ChangeLog:
* call.cc (do_warn_dangling_reference): Don't warn if the temporary
is of lambda type.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wdangling-reference14.C: New test.
* g++.dg/warn/Wdangling-reference15.C: New test.
* g++.dg/warn/Wdangling-reference16.C: New test.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c++/111607] False positive -Wdangling-reference
2023-09-27 7:32 [Bug c++/111607] New: False positive -Wdangling-reference fiesh at zefix dot tv
2024-01-18 22:02 ` [Bug c++/111607] " mpolacek at gcc dot gnu.org
2024-01-23 21:36 ` cvs-commit at gcc dot gnu.org
@ 2024-01-23 22:18 ` mpolacek at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-01-23 22:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111607
Marek Polacek <mpolacek at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|ASSIGNED |RESOLVED
--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Should be fixed.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-01-23 22:18 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-27 7:32 [Bug c++/111607] New: False positive -Wdangling-reference fiesh at zefix dot tv
2024-01-18 22:02 ` [Bug c++/111607] " mpolacek at gcc dot gnu.org
2024-01-23 21:36 ` cvs-commit at gcc dot gnu.org
2024-01-23 22:18 ` mpolacek 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).