From patchwork Sat Sep 23 07:30:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 143837 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp35693vqu; Sat, 23 Sep 2023 00:31:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGjWGkWySPF/cOEbxCPNIS/mSeJtLi91lJZN4LAaylyeW5KZBj2q2B+98NVKqPOY5qdqaUW X-Received: by 2002:a17:906:191:b0:9a1:c352:b6a2 with SMTP id 17-20020a170906019100b009a1c352b6a2mr1276594ejb.52.1695454277489; Sat, 23 Sep 2023 00:31:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695454277; cv=none; d=google.com; s=arc-20160816; b=XgVJpbk1xkboVW5VBV2faBhnYCmypyi50aInYwzjs7cZyP6f0hz2OmvEw1Kms6qnOp zwMirUSm+Xzp5ENG1EmqI1AWXdBsNyCy48UXx1VINWa40fUosbMHIrk1Eap2r3D7FZwE pIBRh+wiKkW30b9/2YBnv94HlmhJA6vLlrEu/yfWNXtt8S904p2jnkPMc9yvyV82pIvR ZJ3MfDnNX9QxReNY0I6P7q4Trce6gjhWpXM+vM2lZ4tsVdJoZTsSLcVbRiOx3TAoS0b1 l+oI9L+ccYWZvr6grh5mrDEsBac2R+7ukjxR0E614haV1hYErxobaLWLT6jEp2q+zwlH DyEw== ARC-Message-Signature: i=1; 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:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dmarc-filter:delivered-to; bh=+Ik1paxo3shgNgHjHVhZRo3WKFR3XFhSs+yvBduQZzw=; fh=XITfwKDGmCI2fbZqG63/cMPybc+hQTR7L/RjIEhD4ok=; b=dsSLeRY311sYuwS07Pll+Y7KFXdjPjzHbAFA2CYqzd1N4tYIbFxeWwYQ7Tki9R4oCP IViVn3rZYX2Rn0Uw/YdmWCoiXiv0CgIfzhz52nVEXliY+SVX6SQ5WYEKrqQ/CWo6WTIA qaIG48/+QUBbR70sgRwSzk3kzkiy7s1hQlH0MxoxDtlRZ8KUXFzfaUaUeLpUmF+5ZiZi f9gXG3tz9eDp4ktIM4zqy1FKw0pWt096LWwgRwdUV2rFVylVlz8E9THgLRKYm5F6l66T hzZ+mQn5R77ST/I7zCsNn1fjhEX4gkEFIYVsfK0BacQcJLhl22GD0Tst3XtA4pCURMFC Zywg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="R4OJGmw/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l25-20020a1709066b9900b0099233cac125si4889122ejr.915.2023.09.23.00.31.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 00:31:17 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="R4OJGmw/"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 20D70385C6FD for ; Sat, 23 Sep 2023 07:30:53 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 328B3385841D; Sat, 23 Sep 2023 07:30:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 328B3385841D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1c5dd017b30so23773725ad.0; Sat, 23 Sep 2023 00:30:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695454215; x=1696059015; darn=gcc.gnu.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+Ik1paxo3shgNgHjHVhZRo3WKFR3XFhSs+yvBduQZzw=; b=R4OJGmw/7mF/0hCb2j/8jBqxObENXGpyKsCMbRcEBAHdcsrAM5StXKjI2SjPHcauUF bDIlgtHhpyGiTEVSX2ScjY0sNcXNNoMiazSDHTQVbwQ69f+7RrKEdGes771BE0Rdv+5B 0+9ICWlg/kCLAi9BgEC4ha1IGeoYieXrTSh16SxPyKVUCkuOtmzYQycQiWHd+iZNRVf8 g8EiNOvLNbmPSI6x6WA2gjSMpijdPuzL1njP94QBNEKVZM8x72bbOMUZgbrwxfMxYMeF e3cBy0VCq9LGbx0KLlX9fS7mNR9JlZuyUmAVFSrEAYvH9LoXHV62xeLHxEdjBi7lwNqJ 9zSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695454215; x=1696059015; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+Ik1paxo3shgNgHjHVhZRo3WKFR3XFhSs+yvBduQZzw=; b=FtQKwZ1CwtBSg9SNVyNNtSoWkWuUudc1tVpbKbK4UYK5fMXi7YtvJHhOz5tDDXMJTm DmXI609Xn1NwDPJjy5Iq4eHlbsx/PvkW6Rl1zPCnE6KCl+e+9qADdYN+TBJxtUpeN/jo +GQDDQtZEe4QMG7KH65JZ5G0Ev3ChhV/GSm16OniOk7pAZYZKaIN5HlEa25jz9gr5hzp Nv+jXd3NHJuNVkWgr0rP6HstQU5BB3Rw8Rdl9WyppU07vpDOGMLrweBLpjFUuKA776Md PqWRdOGkUQOnmproWAj9g7+4qLCC8xng+UWfxtl9j1jK6wi2kK8pIJoyxQN5iTHw2taz ks2g== X-Gm-Message-State: AOJu0Yyrdxr2lig55CArQcwnOiExZcqm2mpqA22K6kAlczcQSO5KCieK y+QpKlQx5vMl+eIIz4ayh/A= X-Received: by 2002:a17:902:d2c6:b0:1c5:6f43:9502 with SMTP id n6-20020a170902d2c600b001c56f439502mr2859859plc.14.1695454214857; Sat, 23 Sep 2023 00:30:14 -0700 (PDT) Received: from Thaum.localdomain ([124.168.125.94]) by smtp.gmail.com with ESMTPSA id q15-20020a170902a3cf00b001bb9aadfb04sm4795174plb.220.2023.09.23.00.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 00:30:14 -0700 (PDT) Date: Sat, 23 Sep 2023 17:30:08 +1000 From: Nathaniel Shead To: Jonathan Wakely Cc: Jason Merrill , libstdc++ , gcc-patches Subject: [PATCH] libstdc++: Ensure active union member is correctly set Message-ID: References: <053faf76-f918-7527-4a41-755a18d0018a@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.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: 1777812664230202369 X-GMAIL-MSGID: 1777812664230202369 On Sat, Sep 23, 2023 at 07:40:48AM +0100, Jonathan Wakely wrote: > On Sat, 23 Sept 2023, 01:39 Nathaniel Shead via Libstdc++, < > libstdc++@gcc.gnu.org> wrote: > > > Now that bootstrap has finished, I have gotten regressions in the > > following libstdc++ tests: > > > > Running libstdc++:libstdc++-dg/conformance.exp ... > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++23 (test for excess > > errors) > > FAIL: 20_util/bitset/access/constexpr.cc -std=gnu++26 (test for excess > > errors) > > FAIL: 20_util/variant/constexpr.cc -std=gnu++20 (test for excess errors) > > FAIL: 20_util/variant/constexpr.cc -std=gnu++26 (test for excess errors) > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++20 (test > > for excess errors) > > FAIL: 21_strings/basic_string/cons/char/constexpr.cc -std=gnu++26 (test > > for excess errors) > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++20 (test > > for excess errors) > > FAIL: 21_strings/basic_string/cons/wchar_t/constexpr.cc -std=gnu++26 (test > > for excess errors) > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc > > -std=gnu++20 (test for excess errors) > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr-wchar_t.cc > > -std=gnu++26 (test for excess errors) > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++20 > > (test for excess errors) > > FAIL: 21_strings/basic_string/modifiers/swap/constexpr.cc -std=gnu++26 > > (test for excess errors) > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++23 (test for excess > > errors) > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++23 compilation > > failed to produce executable > > FAIL: std/ranges/adaptors/join_with/1.cc -std=gnu++26 (test for excess > > errors) > > UNRESOLVED: std/ranges/adaptors/join_with/1.cc -std=gnu++26 compilation > > failed to produce executable > > > > On investigation though it looks like the issue might be with libstdc++ > > rather than the patch itself; running the failing tests using clang with > > libstdc++ also produces similar errors, and my reading of the code > > suggests that this is correct. > > > > What's the way forward here? Should I look at creating a patch to fix > > the libstdc++ issues before resubmitting this patch for the C++ > > frontend? Or should I submit a version of this patch without the > > `std::construct_at` changes and wait till libstdc++ gets fixed for that? > > > > I think we should fix libstdc++. There are probably only a few places that > need a fix, which cause all those failures. > > I can help with those fixes. I'll look into it after the weekend. > Thanks. I did end up getting a chance to look at it earlier today, and with the following patch I had no regressions when applying the frontend changes. Bootstrapped and regtested on x86_64-pc-linux-gnu. -- >8 -- This patch ensures that the union members for std::string and std::variant are always properly set when a change occurs. libstdc++-v3/ChangeLog: * include/bits/basic_string.h: (basic_string(basic_string&&)): Activate _M_local_buf when needed. (basic_string(basic_string&&, const _Alloc&)): Likewise. * include/bits/basic_string.tcc: (basic_string::swap): Likewise. * include/std/variant: (__detail::__variant::__construct_n): New. (__detail::_variant::__emplace): Use __construct_n. Signed-off-by: Nathaniel Shead Signed-off-by: Nathaniel Shead --- libstdc++-v3/include/bits/basic_string.h | 7 +++-- libstdc++-v3/include/bits/basic_string.tcc | 8 +++--- libstdc++-v3/include/std/variant | 32 ++++++++++++++++++++-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 09fd62afa66..7c342879827 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -678,7 +678,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { - traits_type::copy(_M_local_buf, __str._M_local_buf, + traits_type::copy(_M_use_local_data(), __str._M_local_buf, __str.length() + 1); } else @@ -691,7 +691,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 // basic_stringbuf relies on writing into unallocated capacity so // we mess up the contents if we put a '\0' in the string. _M_length(__str.length()); - __str._M_data(__str._M_local_data()); + __str._M_data(__str._M_use_local_data()); __str._M_set_length(0); } @@ -717,6 +717,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { if (__str._M_is_local()) { + _M_use_local_data(); traits_type::copy(_M_local_buf, __str._M_local_buf, __str.length() + 1); _M_length(__str.length()); @@ -728,7 +729,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _M_data(__str._M_data()); _M_length(__str.length()); _M_capacity(__str._M_allocated_capacity); - __str._M_data(__str._M_local_buf); + __str._M_data(__str._M_use_local_data()); __str._M_set_length(0); } else diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 104a517f794..ee6e57da555 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -79,7 +79,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else if (__s.length()) { - traits_type::copy(_M_local_buf, __s._M_local_buf, + traits_type::copy(_M_use_local_data(), __s._M_local_buf, __s.length() + 1); _M_length(__s.length()); __s._M_set_length(0); @@ -87,7 +87,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else if (length()) { - traits_type::copy(__s._M_local_buf, _M_local_buf, + traits_type::copy(__s._M_use_local_data(), _M_local_buf, length() + 1); __s._M_length(length()); _M_set_length(0); @@ -97,7 +97,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else { const size_type __tmp_capacity = __s._M_allocated_capacity; - traits_type::copy(__s._M_local_buf, _M_local_buf, + traits_type::copy(__s._M_use_local_data(), _M_local_buf, length() + 1); _M_data(__s._M_data()); __s._M_data(__s._M_local_buf); @@ -108,7 +108,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const size_type __tmp_capacity = _M_allocated_capacity; if (__s._M_is_local()) { - traits_type::copy(_M_local_buf, __s._M_local_buf, + traits_type::copy(_M_use_local_data(), __s._M_local_buf, __s.length() + 1); __s._M_data(_M_data()); _M_data(_M_local_buf); diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index c0e41740dcf..7f24e760bb1 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -320,6 +320,33 @@ namespace __variant __get(_Variant&& __v) noexcept { return __variant::__get_n<_Np>(std::forward<_Variant>(__v)._M_u); } + // Gets the _Uninitialized to construct into for __u. + template + constexpr decltype(auto) + __construct_n(_Union& __u) noexcept + { + if constexpr (_Np == 0) + return &__u._M_first; + else if constexpr (_Np == 1) + { + std::_Construct(&__u._M_rest); + return &__u._M_rest._M_first; + } + else if constexpr (_Np == 2) + { + std::_Construct(&__u._M_rest); + std::_Construct(&__u._M_rest._M_rest); + return &__u._M_rest._M_rest._M_first; + } + else + { + std::_Construct(&__u._M_rest); + std::_Construct(&__u._M_rest._M_rest); + std::_Construct(&__u._M_rest._M_rest._M_rest); + return __variant::__construct_n<_Np - 3>(__u._M_rest._M_rest._M_rest); + } + } + template struct _Traits { @@ -536,8 +563,9 @@ namespace __variant __emplace(_Variant_storage<_Triv, _Types...>& __v, _Args&&... __args) { __v._M_reset(); - auto* __addr = std::__addressof(__variant::__get_n<_Np>(__v._M_u)); - std::_Construct(__addr, std::forward<_Args>(__args)...); + auto* __addr = __variant::__construct_n<_Np>(__v._M_u); + std::_Construct(__addr, in_place_index<0>, + std::forward<_Args>(__args)...); // Construction didn't throw, so can set the new index now: __v._M_index = _Np; }