public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: suppress -Wdangling-reference for std::span [PR107532]
@ 2023-03-10 19:07 Marek Polacek
  2023-03-13 15:18 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2023-03-10 19:07 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

std::span is a view and therefore should be treated as a reference
wrapper class for the purposes of -Wdangling-reference.  I'm not sure
there's a pattern that we could check for.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c++/107532

gcc/cp/ChangeLog:

	* call.cc (reference_like_class_p): Check for std::span.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wdangling-reference10.C: New test.
---
 gcc/cp/call.cc                                    |  1 +
 gcc/testsuite/g++.dg/warn/Wdangling-reference10.C | 12 ++++++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference10.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 3dfa12a0733..c01e7b82457 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -13800,6 +13800,7 @@ reference_like_class_p (tree ctype)
       tree name = DECL_NAME (tdecl);
       return (name
 	      && (id_equal (name, "reference_wrapper")
+		  || id_equal (name, "span")
 		  || id_equal (name, "ref_view")));
     }
   for (tree fields = TYPE_FIELDS (ctype);
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C
new file mode 100644
index 00000000000..733fb8cce63
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C
@@ -0,0 +1,12 @@
+// PR c++/107532
+// { dg-do compile { target c++20 } }
+// { dg-options "-Wdangling-reference" }
+
+#include <span>
+#include <vector>
+
+void f(const std::vector<int>& v)
+{
+  const int& r = std::span<const int>(v)[0]; // { dg-bogus "dangling reference" }
+  (void) r;
+}

base-commit: 20d790aa3ea5b0d240032cab997b8e0938cac62c
-- 
2.39.2


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

* Re: [PATCH] c++: suppress -Wdangling-reference for std::span [PR107532]
  2023-03-10 19:07 [PATCH] c++: suppress -Wdangling-reference for std::span [PR107532] Marek Polacek
@ 2023-03-13 15:18 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-03-13 15:18 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 3/10/23 14:07, Marek Polacek wrote:
> std::span is a view and therefore should be treated as a reference
> wrapper class for the purposes of -Wdangling-reference.  I'm not sure
> there's a pattern that we could check for.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> 	PR c++/107532
> 
> gcc/cp/ChangeLog:
> 
> 	* call.cc (reference_like_class_p): Check for std::span.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wdangling-reference10.C: New test.
> ---
>   gcc/cp/call.cc                                    |  1 +
>   gcc/testsuite/g++.dg/warn/Wdangling-reference10.C | 12 ++++++++++++
>   2 files changed, 13 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference10.C
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 3dfa12a0733..c01e7b82457 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -13800,6 +13800,7 @@ reference_like_class_p (tree ctype)
>         tree name = DECL_NAME (tdecl);
>         return (name
>   	      && (id_equal (name, "reference_wrapper")
> +		  || id_equal (name, "span")
>   		  || id_equal (name, "ref_view")));
>       }
>     for (tree fields = TYPE_FIELDS (ctype);
> diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C
> new file mode 100644
> index 00000000000..733fb8cce63
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference10.C
> @@ -0,0 +1,12 @@
> +// PR c++/107532
> +// { dg-do compile { target c++20 } }
> +// { dg-options "-Wdangling-reference" }
> +
> +#include <span>
> +#include <vector>
> +
> +void f(const std::vector<int>& v)
> +{
> +  const int& r = std::span<const int>(v)[0]; // { dg-bogus "dangling reference" }
> +  (void) r;
> +}
> 
> base-commit: 20d790aa3ea5b0d240032cab997b8e0938cac62c


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

end of thread, other threads:[~2023-03-13 15:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-10 19:07 [PATCH] c++: suppress -Wdangling-reference for std::span [PR107532] Marek Polacek
2023-03-13 15:18 ` Jason Merrill

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