public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Ville Voutilainen <ville.voutilainen@gmail.com>
Cc: libstdc++ <libstdc++@gcc.gnu.org>,
	gcc-patches List <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] libstdc++: Diagnose visitors with different return types [PR95904]
Date: Sat, 10 Oct 2020 11:52:32 +0100	[thread overview]
Message-ID: <20201010105232.GS7004@redhat.com> (raw)
In-Reply-To: <CAFk2RUZUXAhoQiSahRsxrUiQTMEp4NHDrmn2mi_bpQGaCXOvcg@mail.gmail.com>

On 05/10/20 22:35 +0300, Ville Voutilainen via Libstdc++ wrote:
>On Mon, 5 Oct 2020 at 01:15, Ville Voutilainen
><ville.voutilainen@gmail.com> wrote:
>> The patch is borked, doesn't pass tests, fixing...
>
>Unborked, ok for trunk if full testsuite passes?
>
>2020-10-05  Ville Voutilainen  <ville.voutilainen@gmail.com>
>
>    PR libstdc++/95904
>    * include/std/variant (__deduce_visit_result): Add a nested ::type.
>    (__gen_vtable_impl</*base case*/>::_S_apply):
>    Check the visitor return type.
>    (__same_types): New.
>    (__check_visitor_result): Likewise.
>    (__check_visitor_results): Likewise.
>    (visit(_Visitor&&, _Variants&&...)): Use __check_visitor_results
>    in case we're visiting just one variant.
>    * testsuite/20_util/variant/visit_neg.cc: Adjust.

>diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant
>index dd8847cf829..b32e564fd41 100644
>--- a/libstdc++-v3/include/std/variant
>+++ b/libstdc++-v3/include/std/variant
>@@ -182,7 +182,7 @@ namespace __variant
>   // used for raw visitation with indices passed in
>   struct __variant_idx_cookie { using type = __variant_idx_cookie; };
>   // Used to enable deduction (and same-type checking) for std::visit:
>-  template<typename> struct __deduce_visit_result { };
>+  template<typename _Tp> struct __deduce_visit_result { using type = _Tp; };
> 
>   // Visit variants that might be valueless.
>   template<typename _Visitor, typename... _Variants>
>@@ -1017,7 +1017,26 @@ namespace __variant
> 
>       static constexpr auto
>       _S_apply()
>-      { return _Array_type{&__visit_invoke}; }
>+      {
>+	if constexpr (_Array_type::__result_is_deduced::value)
>+	  {
>+	    constexpr bool __visit_ret_type_mismatch =
>+	      !is_same_v<typename _Result_type::type,
>+			 decltype(__visit_invoke(std::declval<_Visitor>(),
>+				    std::declval<_Variants>()...))>;
>+	    if constexpr (__visit_ret_type_mismatch)
>+	      {
>+		static_assert(!__visit_ret_type_mismatch,
>+		  "std::visit requires the visitor to have the same "
>+		  "return type for all alternatives of a variant");
>+		return __nonesuch{};
>+	      }
>+	    else
>+	      return _Array_type{&__visit_invoke};
>+	  }
>+	else
>+	  return _Array_type{&__visit_invoke};
>+      }
>     };
> 
>   template<typename _Result_type, typename _Visitor, typename... _Variants>
>@@ -1692,6 +1711,26 @@ namespace __variant
> 			   std::forward<_Variants>(__variants)...);
>     }
> 
>+  template<typename _Tp, typename... _Types>
>+     constexpr inline bool __same_types = (is_same_v<_Tp, _Types> && ...);
>+
>+  template <unsigned long int _Idx, typename _Visitor, typename _Variant>
>+    decltype(auto)
>+    __check_visitor_result(_Visitor&& __vis, _Variant&& __variant)
>+    {
>+      return std::__invoke(std::forward<_Visitor>(__vis),
>+			   std::get<_Idx>(std::forward<_Variant>(__variant)));
>+    }
>+
>+  template <typename _Visitor, typename _Variant, unsigned long int... _Idxs>

index_sequence uses size_t not unsigned long. This parameter pack
needs to be size_t... _Idxs, and the NTTP for __check_visitor_result
should be size_t _Idx.


  parent reply	other threads:[~2020-10-10 10:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28 22:12 [PATCH] c++: Diagnose visitors with different return types for std::visit [PR95904] Ville Voutilainen
2020-09-29 11:20 ` Jonathan Wakely
2020-09-29 16:35   ` [PATCH] libstdc++: Diagnose visitors with different return types [PR95904] Ville Voutilainen
2020-10-02 22:14     ` Jonathan Wakely
2020-10-04 22:15       ` Ville Voutilainen
2020-10-05 19:35         ` Ville Voutilainen
2020-10-08 14:27           ` Jonathan Wakely
2020-10-17 17:30             ` Stephan Bergmann
2020-10-17 17:36               ` Ville Voutilainen
2020-10-10 10:52           ` Jonathan Wakely [this message]
2020-10-10 11:15             ` Ville Voutilainen

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=20201010105232.GS7004@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    --cc=ville.voutilainen@gmail.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).