public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: Implement -Wdangling-reference [PR106393]
Date: Tue, 25 Oct 2022 12:50:36 +0100	[thread overview]
Message-ID: <CACb0b4ngTKH301xEv6HbVr9JJBpx=68bKv68TLKqcohk5S=93w@mail.gmail.com> (raw)
In-Reply-To: <20221021232824.1093138-1-polacek@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 809 bytes --]

On Sat, 22 Oct 2022 at 00:28, Marek Polacek wrote:
> I've run the testsuite/bootstrap with the warning enabled by default.
> There were just a few FAILs:
> * g++.dg/warn/Wdangling-pointer-2.C
> * 20_util/any/misc/any_cast.cc
> * 20_util/forward/c_neg.cc
> * 20_util/forward/f_neg.cc

These two are XFAIL tests, they're checking for a required static
assert due to misusing std::forward, so I don't think we want to
change them to remove a "bug" (they're meant to be buggy).
I think they should use -Wno-dangling-reference

> * experimental/any/misc/any_cast.cc
> all of these look like genuine bugs.  A bootstrap with the warning
> enabled by default passed.


I've attached a patch for the two any_cast.cc tests that avoids
creating dangling references, without changing the intended behaviour
of the tests.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1755 bytes --]

diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc
index 8d63dfbba9b..43f90c336c9 100644
--- a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc
+++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc
@@ -93,7 +93,8 @@ void test03()
   MoveEnabled m;
   MoveEnabled m2 = any_cast<MoveEnabled>(any(m));
   VERIFY(move_count == 1);
-  MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m));
+  auto rvalue = [](MoveEnabled&&) { };
+  rvalue(any_cast<MoveEnabled&&>(any(m))); // No move construction, just a cast.
   VERIFY(move_count == 1);
 }
 
diff --git a/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc b/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
index f3ce83ad702..98347f6d37d 100644
--- a/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
+++ b/libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
@@ -92,7 +92,8 @@ void test03()
   MoveEnabled m;
   MoveEnabled m2 = any_cast<MoveEnabled>(any(m));
   VERIFY(move_count == 1);
-  MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m));
+  auto rvalue = [](MoveEnabled&&) { }; // Can only be called with an rvalue.
+  rvalue(any_cast<MoveEnabled&&>(any(m))); // Doesn't move, just casts to rval.
   VERIFY(move_count == 1);
   struct MoveDeleted
   {
@@ -101,8 +102,9 @@ void test03()
     MoveDeleted(const MoveDeleted&) = default;
   };
   MoveDeleted md;
-  MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md)));
-  MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
+  auto rvalue2 = [](MoveDeleted&&) { }; // Can only be called with an rvalue.
+  rvalue2(any_cast<MoveDeleted>(any(std::move(md))));
+  rvalue2(any_cast<MoveDeleted&&>(any(std::move(md))));
 }
 
 void test05()

  parent reply	other threads:[~2022-10-25 11:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-21 23:28 Marek Polacek
2022-10-24 17:30 ` Jason Merrill
2022-10-25 11:34   ` Jonathan Wakely
2022-10-25 13:14     ` Marek Polacek
2022-10-25 15:39       ` Jason Merrill
2022-10-25 16:35         ` Marek Polacek
2022-10-25 15:21   ` [PATCH v2] " Marek Polacek
2022-10-25 15:53     ` Jason Merrill
2022-10-26 16:10       ` [PATCH v3] " Marek Polacek
2022-10-26 16:42         ` Jason Merrill
2022-10-26 18:26           ` [PATCH v4] " Marek Polacek
2022-10-26 18:42             ` Jason Merrill
2022-10-25 11:50 ` Jonathan Wakely [this message]
2022-10-25 15:24   ` [PATCH] " Marek Polacek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACb0b4ngTKH301xEv6HbVr9JJBpx=68bKv68TLKqcohk5S=93w@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).