From patchwork Fri Jan 19 19:13:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 189671 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1269181dyb; Fri, 19 Jan 2024 12:34:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IHkZ2WR0Kk5gFo8F3ZKKOg2FYgBxr4DrLHymDd9LKKYYgdTujWmG5j8LC2xzMxeNglWhFe5 X-Received: by 2002:a9d:768b:0:b0:6dc:7e7e:cd7c with SMTP id j11-20020a9d768b000000b006dc7e7ecd7cmr421922otl.5.1705696471735; Fri, 19 Jan 2024 12:34:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705696471; cv=pass; d=google.com; s=arc-20160816; b=Ytoi/R+JREkUjjIikkdbcJl5pYfNtgLBWo73+iCDNHnxxC5D4Lya/pN/rHW7jE3YTO XsK4PhKJchFSS6wK3Ij9Nj51AhFeqMTufJN0ZW0JU5iitTxz6gx6kk9CJxQKG4lknpGv NbqSVLZc/zVs1vx4IH6lxRnnHh0PxGAHKS6HAmZoIkjNexzu0IfZxrxY6OCelCGUJCbP jActZV+TbxZbndSxhipUdEFJ7tCZiDD2jqthy/CX7PkDTZsxASVPxHlaEOSquaH9sMaH WdbI6do2r1IWIxBuSl/TPwzi+6ofktiYUpUkUQWZqqq1NYFpziLKWU6NFWDVL6ImgW5d 2CuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=hK6h1MJrFp1s+UTOSMarnt1uCuMw9vEP5XZop5m9Br4=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=0YzK8HWykJEbQefyOVrviKI2/B2gEzXZ1HJExC7UPccS5P9crQm42qjx/jAomSSazo 0cUrwGAicXhKO4NoI1JqPjFYCDs4ENv+EN9zKrGgdN7IJxNufzjYJAcfVKp3v+bOZ6Ih FBWekjxPrN2N+l52WXPxa5RF0ZM0B/z7ULLFe40ofnDEQTkwimK+DwypCWPgG+f73qrU BCET/X5Wvt1xzylOfdBLUPjFi0KH0vCyX3VQUFUeyMyREB9i9W0gBi+S913IYeJ+3TBb cadS8JC9/qTvJRn7/+h29Ihhvz0GqFoGi6y2+rkab2JgAzhISGIbPntsFhUjUwr3ZxoW Dcdw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JF3FdldR; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v3-20020a05622a144300b00429f350015esi9105qtx.236.2024.01.19.12.34.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 12:34:31 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=JF3FdldR; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C16BA3857C76 for ; Fri, 19 Jan 2024 19:14:43 +0000 (GMT) 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 134A5385840D for ; Fri, 19 Jan 2024 19:13:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 134A5385840D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 134A5385840D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705691634; cv=none; b=VysgYcN/PNE6YL4bWhrPr+k4kXS3wzIQJkaFdFZh09Y8AnXLMTlaqwruO/YV/Gia/t8GGLyrXyn+5Z5lLPLabo+cDNExJdrOJZ6cyxolAD9nmD2lv9G5NKhIze1irnU6vjgasLAALWUl9BaeDO7x6S8OH7mUZ060f2zM3qTEHas= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705691634; c=relaxed/simple; bh=yXPGuQwdpO4q8VBVxVlX2ckUxEM1wcTn8efqb2Oi6LY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=k8tfdgb5UyBUID4vMdCRjH6MadYJQmmouAnmm+o2aAZQPHrvkHXttVP1niKCDl8gmINYBvm9bbYejFQSp85kIINGXzYO6YAkHRVg+/jtBvzHFBUFwz79SjiLU51LoqYbdcIdOrxtYDBfNuwEGlKS6XGEAw9AW2pmY6rl+SKwluU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705691631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hK6h1MJrFp1s+UTOSMarnt1uCuMw9vEP5XZop5m9Br4=; b=JF3FdldRzW7C1EO5b8j6N4S/1EumL+UX2qIVojB/p1bHTHV5lpmydZT7YiMa8cIJMpu70P 3h/GPggS1L5YjhkWXarPtl8w5bUV9FHwZ+79dadpi7whqJ6el/DKKCLBhBWhjxgsjI0qZB jVyLqanvlORb5QlW34bbqTXuCuzcNKs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-517-71huFQvjN4iGw-ru9l7A_w-1; Fri, 19 Jan 2024 14:13:48 -0500 X-MC-Unique: 71huFQvjN4iGw-ru9l7A_w-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4E87F85A588; Fri, 19 Jan 2024 19:13:48 +0000 (UTC) Received: from localhost (unknown [10.42.28.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 171CA40D1B60; Fri, 19 Jan 2024 19:13:47 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix P2255R2 dangling checks for std::tuple in C++17 [PR108822] Date: Fri, 19 Jan 2024 19:13:32 +0000 Message-ID: <20240119191347.269495-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788552383867145510 X-GMAIL-MSGID: 1788552383867145510 Tested powerp64le-linux. Pushed to trunk. -- >8 -- I accidentally used && in a fold-expression instead of || which meant that in C++17 the tuple(UElements&&...) constructor only failed its debug assertion if all tuple elements were dangling references. Some missing tests (noted as "TODO") meant this wasn't tested. This fixes the fold expression and adds the missing tests. libstdc++-v3/ChangeLog: PR libstdc++/108822 * include/std/tuple (__glibcxx_no_dangling_refs) [C++17]: Fix wrong fold-operator. * testsuite/20_util/tuple/dangling_ref.cc: Check tuples with one element and three elements. Check allocator-extended constructors. --- libstdc++-v3/include/std/tuple | 2 +- .../testsuite/20_util/tuple/dangling_ref.cc | 156 ++++++++++++------ 2 files changed, 110 insertions(+), 48 deletions(-) diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple index 7a045b3e6a1..be92f1eb973 100644 --- a/libstdc++-v3/include/std/tuple +++ b/libstdc++-v3/include/std/tuple @@ -1299,7 +1299,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Error if construction from U... would create a dangling ref. # if __cpp_fold_expressions # define __glibcxx_dangling_refs(U) \ - (__reference_constructs_from_temporary(_Elements, U) && ...) + (__reference_constructs_from_temporary(_Elements, U) || ...) # else # define __glibcxx_dangling_refs(U) \ __or_<__bool_constant<__reference_constructs_from_temporary(_Elements, U) \ diff --git a/libstdc++-v3/testsuite/20_util/tuple/dangling_ref.cc b/libstdc++-v3/testsuite/20_util/tuple/dangling_ref.cc index 74fdc242349..b2dcf359438 100644 --- a/libstdc++-v3/testsuite/20_util/tuple/dangling_ref.cc +++ b/libstdc++-v3/testsuite/20_util/tuple/dangling_ref.cc @@ -7,6 +7,7 @@ #if __cplusplus >= 202002L // For C++20 and later, constructors are constrained to disallow dangling. +static_assert(!std::is_constructible_v, long>); static_assert(!std::is_constructible_v, long, int>); static_assert(!std::is_constructible_v, int, long>); static_assert(!std::is_constructible_v, @@ -30,76 +31,137 @@ static_assert(!std::is_constructible_v, void test_ary_ctors() { - std::tuple t1(1L, 2); - // { dg-error "here" "" { target { c++17_down && hosted } } 33 } - // { dg-error "use of deleted function" "" { target c++20 } 33 } + std::tuple t1(1L); + // { dg-error "here" "" { target { c++17_down && hosted } } 34 } + // { dg-error "use of deleted function" "" { target c++20 } 34 } - std::tuple t2(1, 2L); - // { dg-error "here" "" { target { c++17_down && hosted } } 37 } - // { dg-error "use of deleted function" "" { target c++20 } 37 } + std::tuple t2(1L, 2); + // { dg-error "here" "" { target { c++17_down && hosted } } 38 } + // { dg-error "use of deleted function" "" { target c++20 } 38 } - std::tuple t3(1L, 2L); - // { dg-error "here" "" { target { c++17_down && hosted } } 41 } - // { dg-error "use of deleted function" "" { target c++20 } 41 } + std::tuple t3(1, 2L); + // { dg-error "here" "" { target { c++17_down && hosted } } 42 } + // { dg-error "use of deleted function" "" { target c++20 } 42 } - std::tuple t4(std::pair{}); - // { dg-error "here" "" { target { c++17_down && hosted } } 45 } - // { dg-error "use of deleted function" "" { target c++20 } 45 } + std::tuple t4(1L, 2L); + // { dg-error "here" "" { target { c++17_down && hosted } } 46 } + // { dg-error "use of deleted function" "" { target c++20 } 46 } - std::pair p; - std::tuple t5(p); + std::tuple t5(std::pair{}); // { dg-error "here" "" { target { c++17_down && hosted } } 50 } // { dg-error "use of deleted function" "" { target c++20 } 50 } + + std::pair p; + std::tuple t6(p); + // { dg-error "here" "" { target { c++17_down && hosted } } 55 } + // { dg-error "use of deleted function" "" { target c++20 } 55 } + + std::tuple t7(1L, 2, 3); + // { dg-error "here" "" { target { c++17_down && hosted } } 59 } + // { dg-error "use of deleted function" "" { target c++20 } 59 } } void test_converting_ctors() { - std::tuple t0; + std::tuple t10; - std::tuple t1(t0); - // { dg-error "here" "" { target { c++17_down && hosted } } 60 } - // { dg-error "use of deleted function" "" { target c++20 } 60 } + std::tuple t11(t10); + // { dg-error "here" "" { target { c++17_down && hosted } } 69 } + // { dg-error "use of deleted function" "" { target c++20 } 69 } - std::tuple t2(t0); - // { dg-error "here" "" { target { c++17_down && hosted } } 64 } - // { dg-error "use of deleted function" "" { target c++20 } 64 } + std::tuple t12(std::move(t10)); + // { dg-error "here" "" { target { c++17_down && hosted } } 73 } + // { dg-error "use of deleted function" "" { target c++20 } 73 } - std::tuple t3(t0); - // { dg-error "here" "" { target { c++17_down && hosted } } 68 } - // { dg-error "use of deleted function" "" { target c++20 } 68 } + std::tuple t20; - std::tuple t4(std::move(t0)); - // { dg-error "here" "" { target { c++17_down && hosted } } 72 } - // { dg-error "use of deleted function" "" { target c++20 } 72 } + std::tuple t21(t20); + // { dg-error "here" "" { target { c++17_down && hosted } } 79 } + // { dg-error "use of deleted function" "" { target c++20 } 79 } - std::tuple t5(std::move(t0)); - // { dg-error "here" "" { target { c++17_down && hosted } } 76 } - // { dg-error "use of deleted function" "" { target c++20 } 76 } + std::tuple t22(t20); + // { dg-error "here" "" { target { c++17_down && hosted } } 83 } + // { dg-error "use of deleted function" "" { target c++20 } 83 } - std::tuple t6(std::move(t0)); - // { dg-error "here" "" { target { c++17_down && hosted } } 80 } - // { dg-error "use of deleted function" "" { target c++20 } 80 } + std::tuple t23(t20); + // { dg-error "here" "" { target { c++17_down && hosted } } 87 } + // { dg-error "use of deleted function" "" { target c++20 } 87 } + + std::tuple t24(std::move(t20)); + // { dg-error "here" "" { target { c++17_down && hosted } } 91 } + // { dg-error "use of deleted function" "" { target c++20 } 91 } + + std::tuple t25(std::move(t20)); + // { dg-error "here" "" { target { c++17_down && hosted } } 95 } + // { dg-error "use of deleted function" "" { target c++20 } 95 } + + std::tuple t26(std::move(t20)); + // { dg-error "here" "" { target { c++17_down && hosted } } 99 } + // { dg-error "use of deleted function" "" { target c++20 } 99 } std::pair p0; - std::tuple t7(p0); - // { dg-error "here" "" { target { c++17_down && hosted } } 85 } - // { dg-error "use of deleted function" "" { target c++20 } 85 } + std::tuple t27(p0); + // { dg-error "here" "" { target { c++17_down && hosted } } 104 } + // { dg-error "use of deleted function" "" { target c++20 } 104 } - std::tuple t8(p0); - // { dg-error "here" "" { target { c++17_down && hosted } } 89 } - // { dg-error "use of deleted function" "" { target c++20 } 89 } + std::tuple t28(p0); + // { dg-error "here" "" { target { c++17_down && hosted } } 108 } + // { dg-error "use of deleted function" "" { target c++20 } 108 } - std::tuple t9(std::move(p0)); - // { dg-error "here" "" { target { c++17_down && hosted } } 93 } - // { dg-error "use of deleted function" "" { target c++20 } 93 } + std::tuple t29(std::move(p0)); + // { dg-error "here" "" { target { c++17_down && hosted } } 112 } + // { dg-error "use of deleted function" "" { target c++20 } 112 } - std::tuple t10(std::move(p0)); - // { dg-error "here" "" { target { c++17_down && hosted } } 97 } - // { dg-error "use of deleted function" "" { target c++20 } 97 } + std::tuple t210(std::move(p0)); + // { dg-error "here" "" { target { c++17_down && hosted } } 116 } + // { dg-error "use of deleted function" "" { target c++20 } 116 } } -// TODO: test allocator-extended ctors -// TODO: test 1-tuple or 3-tuple, not just 2-tuple +#include + +void +test_allocator_extended_ctors() +{ + std::allocator a; + + std::tuple t1(std::allocator_arg, a, 1L); + // { dg-error "here" "" { target { c++17_down && hosted } } 128 } + // { dg-error "use of deleted function" "" { target c++20 } 128 } + + std::tuple t2(std::allocator_arg, a, 1L, 2); + // { dg-error "here" "" { target { c++17_down && hosted } } 132 } + // { dg-error "use of deleted function" "" { target c++20 } 132 } + + std::tuple tl; + + std::tuple t3(std::allocator_arg, a, tl); + // { dg-error "here" "" { target { c++17_down && hosted } } 138 } + // { dg-error "use of deleted function" "" { target c++20 } 138 } + + std::tuple t4(std::allocator_arg, a, std::move(tl)); + // { dg-error "here" "" { target { c++17_down && hosted } } 142 } + // { dg-error "use of deleted function" "" { target c++20 } 142 } + + std::tuple tll; + + std::tuple t5(std::allocator_arg, a, tll); + // { dg-error "here" "" { target { c++17_down && hosted } } 148 } + // { dg-error "use of deleted function" "" { target c++20 } 148 } + + std::tuple t6(std::allocator_arg, a, std::move(tll)); + // { dg-error "here" "" { target { c++17_down && hosted } } 152 } + // { dg-error "use of deleted function" "" { target c++20 } 152 } + + std::pair pll; + + std::tuple t7(std::allocator_arg, a, pll); + // { dg-error "here" "" { target { c++17_down && hosted } } 158 } + // { dg-error "use of deleted function" "" { target c++20 } 158 } + + std::tuple t8(std::allocator_arg, a, std::move(pll)); + // { dg-error "here" "" { target { c++17_down && hosted } } 162 } + // { dg-error "use of deleted function" "" { target c++20 } 162 } +} // { dg-error "static assert.* dangling reference" "" { target { c++17_down && hosted } } 0 }