* [PATCH] libstdc++: suppress -Wdangling-reference with operator| [PR111410]
@ 2024-01-20 2:17 Marek Polacek
2024-01-20 7:46 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2024-01-20 2:17 UTC (permalink / raw)
To: GCC Patches, libstdc++, Jonathan Wakely, Jason Merrill
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
It seems to me that we should exclude std::ranges::views::__adaptor::operator|
from the -Wdangling-reference warning. It's commonly used when handling
ranges.
PR c++/111410
libstdc++-v3/ChangeLog:
* include/std/ranges: Add #pragma to disable -Wdangling-reference with
std::ranges::views::__adaptor::operator|.
gcc/testsuite/ChangeLog:
* g++.dg/warn/Wdangling-reference17.C: New test.
---
gcc/testsuite/g++.dg/warn/Wdangling-reference17.C | 15 +++++++++++++++
libstdc++-v3/include/std/ranges | 3 +++
2 files changed, 18 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
new file mode 100644
index 00000000000..223698422c2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
@@ -0,0 +1,15 @@
+// PR c++/111410
+// { dg-do compile { target c++20 } }
+// { dg-options "-Wdangling-reference" }
+
+#include <vector>
+#include <ranges>
+
+int main()
+{
+ std::vector v{1, 2, 3, 4, 5};
+ for (auto i : std::span{v} | std::views::take(1))
+ {
+ (void) i;
+ }
+}
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 7ef835f486a..f2413badd9c 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -942,6 +942,8 @@ namespace views::__adaptor
concept __is_range_adaptor_closure
= requires (_Tp __t) { __adaptor::__is_range_adaptor_closure_fn(__t, __t); };
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdangling-reference"
// range | adaptor is equivalent to adaptor(range).
template<typename _Self, typename _Range>
requires __is_range_adaptor_closure<_Self>
@@ -961,6 +963,7 @@ namespace views::__adaptor
return _Pipe<decay_t<_Lhs>, decay_t<_Rhs>>{std::forward<_Lhs>(__lhs),
std::forward<_Rhs>(__rhs)};
}
+#pragma GCC diagnostic pop
// The base class of every range adaptor non-closure.
//
base-commit: 615e25c82de97acc17ab438f88d6788cf7ffe1d6
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] libstdc++: suppress -Wdangling-reference with operator| [PR111410]
2024-01-20 2:17 [PATCH] libstdc++: suppress -Wdangling-reference with operator| [PR111410] Marek Polacek
@ 2024-01-20 7:46 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-01-20 7:46 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, libstdc++, Jonathan Wakely, Jason Merrill
[-- Attachment #1: Type: text/plain, Size: 2772 bytes --]
On Sat, 20 Jan 2024, 03:47 Marek Polacek, <polacek@redhat.com> wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
OK, thanks.
The standard ranges have their own protection against dangling via the
opt-in borrowed_range concept, and algorithms that don't allow returning
iterators into rvalue ranges, and the automatic use of ref_view or
owning_view as needed. So I think it's reasonable to assume they are less
prone to the bugs this warning detects, at least when used idiomatically.
> -- >8 --
> It seems to me that we should exclude
> std::ranges::views::__adaptor::operator|
> from the -Wdangling-reference warning. It's commonly used when handling
> ranges.
>
> PR c++/111410
>
> libstdc++-v3/ChangeLog:
>
> * include/std/ranges: Add #pragma to disable -Wdangling-reference
> with
> std::ranges::views::__adaptor::operator|.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/warn/Wdangling-reference17.C: New test.
> ---
> gcc/testsuite/g++.dg/warn/Wdangling-reference17.C | 15 +++++++++++++++
> libstdc++-v3/include/std/ranges | 3 +++
> 2 files changed, 18 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
>
> diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
> b/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
> new file mode 100644
> index 00000000000..223698422c2
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference17.C
> @@ -0,0 +1,15 @@
> +// PR c++/111410
> +// { dg-do compile { target c++20 } }
> +// { dg-options "-Wdangling-reference" }
> +
> +#include <vector>
> +#include <ranges>
> +
> +int main()
> +{
> + std::vector v{1, 2, 3, 4, 5};
> + for (auto i : std::span{v} | std::views::take(1))
> + {
> + (void) i;
> + }
> +}
> diff --git a/libstdc++-v3/include/std/ranges
> b/libstdc++-v3/include/std/ranges
> index 7ef835f486a..f2413badd9c 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -942,6 +942,8 @@ namespace views::__adaptor
> concept __is_range_adaptor_closure
> = requires (_Tp __t) {
> __adaptor::__is_range_adaptor_closure_fn(__t, __t); };
>
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Wdangling-reference"
> // range | adaptor is equivalent to adaptor(range).
> template<typename _Self, typename _Range>
> requires __is_range_adaptor_closure<_Self>
> @@ -961,6 +963,7 @@ namespace views::__adaptor
> return _Pipe<decay_t<_Lhs>,
> decay_t<_Rhs>>{std::forward<_Lhs>(__lhs),
>
> std::forward<_Rhs>(__rhs)};
> }
> +#pragma GCC diagnostic pop
>
> // The base class of every range adaptor non-closure.
> //
>
> base-commit: 615e25c82de97acc17ab438f88d6788cf7ffe1d6
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-01-20 7:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-20 2:17 [PATCH] libstdc++: suppress -Wdangling-reference with operator| [PR111410] Marek Polacek
2024-01-20 7:46 ` Jonathan Wakely
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).