public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning
@ 2023-06-12  3:47 boris at kolpackov dot net
  2023-06-12  3:49 ` [Bug c++/110213] " boris at kolpackov dot net
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: boris at kolpackov dot net @ 2023-06-12  3:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110213
           Summary: Bogus (as opposed to false positive)
                    -Wdangling-reference warning
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: boris at kolpackov dot net
  Target Milestone: ---

I have a call that, AFAICS, doesn't involve any binding of references to
temporaries but which nevertheless still produces this warning. The code boils
down to the following:

const target& search (const target&, name);

void pattern::apply(target& t) const
{
  name n = ...;
  const target& p (search (t, std::move (n))); // <- Warning points here.
}

However, I was not able to reduce it to a small reproducer so it seems to
depend on the overall context. So I am attaching a partially preprocessed TU.
To reproduce, run:

$ g++ -std=c++23 -Wall -Wextra -fdirectives-only -c adhoc-rule-regex-pattern.ii

adhoc-rule-regex-pattern.cxx: In member function ‘virtual void
build2::adhoc_rule_regex_pattern::apply_prerequisites(build2::action,
build2::target&, const scope&, build2::match_extra&) const’:
adhoc-rule-regex-pattern.cxx:485:21: warning: possibly dangling reference to a
temporary [-Wdangling-reference]
  485 |       const target& pt (search (t, move (n), *s, &e.type));
      |                     ^~
adhoc-rule-regex-pattern.cxx:485:32: note: the temporary was destroyed at the
end of the full expression 
‘build2::search((*(const build2::target*)(& t)), build2::name((* &
std::move<build2::name&>(n))), (* s), (&
e.build2::adhoc_rule_regex_pattern::element::type))’
  485 |       const target& pt (search (t, move (n), *s, &e.type));
      |                         ~~~~~~~^~

The last two arguments (*s, &e.type) in the call do not make any difference.
Changing the search() function to take name by const& or && makes the warning
disappear.

$ g++ --version
g++-13 (Debian 13.1.0-3) 13.1.0

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

* [Bug c++/110213] Bogus (as opposed to false positive) -Wdangling-reference warning
  2023-06-12  3:47 [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning boris at kolpackov dot net
@ 2023-06-12  3:49 ` boris at kolpackov dot net
  2023-06-12  4:13 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: boris at kolpackov dot net @ 2023-06-12  3:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Boris Kolpackov <boris at kolpackov dot net> ---
Created attachment 55304
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55304&action=edit
reproducer

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

* [Bug c++/110213] Bogus (as opposed to false positive) -Wdangling-reference warning
  2023-06-12  3:47 [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning boris at kolpackov dot net
  2023-06-12  3:49 ` [Bug c++/110213] " boris at kolpackov dot net
@ 2023-06-12  4:13 ` pinskia at gcc dot gnu.org
  2023-06-12  4:20 ` pinskia at gcc dot gnu.org
  2024-03-01 21:13 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-12  4:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase:
#include <utility>
struct f
{
  f(const f&);
  f();
};

struct g{};

g &search(f);

void h()
{
        f n;
        const g& pt (search (std::move(n)));
}

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

* [Bug c++/110213] Bogus (as opposed to false positive) -Wdangling-reference warning
  2023-06-12  3:47 [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning boris at kolpackov dot net
  2023-06-12  3:49 ` [Bug c++/110213] " boris at kolpackov dot net
  2023-06-12  4:13 ` pinskia at gcc dot gnu.org
@ 2023-06-12  4:20 ` pinskia at gcc dot gnu.org
  2024-03-01 21:13 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-12  4:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't see an issue with this warning really as there is a temporary being
created for the argument of type name and that is what the issue is warning
about. the argument of type name is still passed via reference even and the
temp is created on caller side as needed by the C++ standard even.

There are other bugs dealing Wdangling-reference where they have false positive
warnings dealing with different types of being warned about.

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

* [Bug c++/110213] Bogus (as opposed to false positive) -Wdangling-reference warning
  2023-06-12  3:47 [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning boris at kolpackov dot net
                   ` (2 preceding siblings ...)
  2023-06-12  4:20 ` pinskia at gcc dot gnu.org
@ 2024-03-01 21:13 ` mpolacek at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-03-01 21:13 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

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

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
You can now disable the warning by using the [[gnu::no_dangling]] attribute:

https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=c7607c4cf18986025430ca8626abfe56bfe87106

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

end of thread, other threads:[~2024-03-01 21:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-12  3:47 [Bug c++/110213] New: Bogus (as opposed to false positive) -Wdangling-reference warning boris at kolpackov dot net
2023-06-12  3:49 ` [Bug c++/110213] " boris at kolpackov dot net
2023-06-12  4:13 ` pinskia at gcc dot gnu.org
2023-06-12  4:20 ` pinskia at gcc dot gnu.org
2024-03-01 21:13 ` 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).