From patchwork Tue Aug 23 15:39:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp1013186wro; Tue, 23 Aug 2022 08:40:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR5g2CZddthkF2tuRxTXbH7vqrJYbhRIePvKh8TfZWtHU5ZbBI3/LMSDonn0dfpDHXeJ6Tf4 X-Received: by 2002:a17:907:1c8a:b0:6e9:2a0d:d7b7 with SMTP id nb10-20020a1709071c8a00b006e92a0dd7b7mr116882ejc.572.1661269212848; Tue, 23 Aug 2022 08:40:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661269212; cv=none; d=google.com; s=arc-20160816; b=KtoXqm3ilcTyCd1L8dPGJr3GqgEOW+cCMwXzUTW5zgfpKqwBz4/TgJku26NMAeMar8 wfGRf5gr8piflZSPKOObQryaQwp5/sR96pVJYUSnIfs02o1sg33HAox4GlNDeKFNqcxV GYxv0F8Qc0yB9ZXM+aQyerrTJ9naPGHstTMeMCATo82V7VkkTv+/0VJDvgcNKlIs0yZQ FjLhJMX7I7ddTiEMHl+4OcocjEcJYhRFuRhJ1cWpH5I5rKXPtPznOdM8ZJ4WikgNuOne eFo+MmPfIAgiNj7hb89o/WNGK2KqvSDVHKaBN7+gVdMQ2JllRAZByzUN4lO181VZ9Mjw ar/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ky7DyRzHFe3CGhvqQ2+ZIZ8IYquqv4/ZwywcG90fprU=; b=n61JLN7DRpPDRRfq/ZuvooxUrOaMeFph3vfEBGVevTdMobPWaGNaBmAqtC730n9Xxm lLn2ZO+Aj70Y9IUEIXxGVCRYxPkC19F1WApK1UFFAaMQYzKGJTX3nVo9+QIeXQMs+5f/ 0snj3SVtX1CooEY3UM6CIGMxzD85ya+SIaNOr7oYFwo9D5EKiZcUlLg3F3RE+BKvI4w5 v0J0E4e0zpXJKeKwXmkzkr2cTdAKHuwL0Z/kyq0OVl+/JzLyzaVahBPd1/nGE8mQjT94 nkIN7FFs4/D92VJ2O0mMdSeyj7lQcDId6PHffKNfYtfkfuBzEo69UkijQ/6DnpOZ9SET weAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=xcLdvcPc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sa32-20020a1709076d2000b0073d9a2c69d6si2103771ejc.847.2022.08.23.08.40.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Aug 2022 08:40:12 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=xcLdvcPc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 94BC43857C68 for ; Tue, 23 Aug 2022 15:40:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94BC43857C68 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661269200; bh=ky7DyRzHFe3CGhvqQ2+ZIZ8IYquqv4/ZwywcG90fprU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=xcLdvcPcn463EWjoZvR+cz4kE4dK6OQYMOhkIweZo03E9hCvCEtWLqOTAOVyL7m5D s2HKcLhMXEw2MBnmnxK/Po+Ynxy0gzFVasbCdGlDewwM4HWQ3av8p1P23THiibgj8D vmT9OjjsrfwrCjMNvN14BOoSm4m/IhALpsuv9EDU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 822183857C49 for ; Tue, 23 Aug 2022 15:39:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 822183857C49 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301-39s311x6NDOfN6ie_a6PMw-1; Tue, 23 Aug 2022 11:39:13 -0400 X-MC-Unique: 39s311x6NDOfN6ie_a6PMw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6117A18A01C0; Tue, 23 Aug 2022 15:39:13 +0000 (UTC) Received: from localhost (unknown [10.33.36.78]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28D972026D4C; Tue, 23 Aug 2022 15:39:13 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix visit(v) for non-void visitors [PR106589] Date: Tue, 23 Aug 2022 16:39:12 +0100 Message-Id: <20220823153912.229679-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1741967026430868675?= X-GMAIL-MSGID: =?utf-8?q?1741967026430868675?= Tested powerpc64le-linux, pushed to trunk. Backport to gcc-12 needed. -- >8 -- The optimization for the common case of std::visit forgot to handle the edge case of passing zero variants to a non-void visitor and converting the result to void. libstdc++-v3/ChangeLog: PR libstdc++/106589 * include/std/variant (__do_visit): Handle is_void for zero argument case. * testsuite/20_util/variant/visit_r.cc: Check std::visit(v). --- libstdc++-v3/include/std/variant | 7 ++++++- libstdc++-v3/testsuite/20_util/variant/visit_r.cc | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index f8f15665433..c234b54421e 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1728,7 +1728,12 @@ namespace __variant { // Get the silly case of visiting no variants out of the way first. if constexpr (sizeof...(_Variants) == 0) - return std::forward<_Visitor>(__visitor)(); + { + if constexpr (is_void_v<_Result_type>) + return (void) std::forward<_Visitor>(__visitor)(); + else + return std::forward<_Visitor>(__visitor)(); + } else { constexpr size_t __max = 11; // "These go to eleven." diff --git a/libstdc++-v3/testsuite/20_util/variant/visit_r.cc b/libstdc++-v3/testsuite/20_util/variant/visit_r.cc index 712459f25e3..c77b259c386 100644 --- a/libstdc++-v3/testsuite/20_util/variant/visit_r.cc +++ b/libstdc++-v3/testsuite/20_util/variant/visit_r.cc @@ -54,10 +54,18 @@ void test02() std::visit(Visitor(), v); } +void test03() +{ + // PR libstdc++/106589 - visit rejects lambdas that do not return void + auto visitor = []{ return 0; }; + std::visit(visitor); + std::visit(static_cast(visitor)); +} int main() { test01(); test02(); + test03(); }