From patchwork Thu Aug 1 21:08:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1968039 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fS8Gqz3s; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZhSH4btpz1yZv for ; Fri, 2 Aug 2024 07:10:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 226973858CD9 for ; Thu, 1 Aug 2024 21:10:04 +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 ESMTP id 67862385C6C7 for ; Thu, 1 Aug 2024 21:09:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67862385C6C7 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 67862385C6C7 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=1722546582; cv=none; b=Mz+QQvfpQAp0by4c9r6OGon4ccAxnKyRAhOzK4CU4oW/j3MHoqzHk63GhkFh+rI4gkNoThd0zoshDpbyWNNy+aFTzQ3//FrNROPbVvsf8cSjp8C4FqX7VZCu2KM02f1xy0HiQY9bOEcoj9+5iK0oh3UdWwFcolnPlTsQ+USyNUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722546582; c=relaxed/simple; bh=NrTwjIQQkGS53mSCtHOT/T52aHVHsJuKaYvtGZDC5do=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CEPvHnmZSPm/XXQvgCytmy8q/AT+hg6tgEYqlQRllyICyDZWvrXGBO6pQZPPm3n7igmOVFN/KJFKoKZwVpOtmRz0oxQCeyJ767BOvT3F5KjiUrmaMOG55z4V3XVIGaVdznbGuIiuMK2GRDqb5Dm05EwVoAO+C2bwJJEW9IwgmJo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722546579; 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=w4mgUKl1ACOywz1GPy8DrR6LIlH9Tzy1K8Y+WWhi1iQ=; b=fS8Gqz3sNaJ4vcSg5wY2e71dwPaHPFQPJx09Lvas3Bb+8Cav79soZH7Z+/8VKdoBKoKWGW wq6VfToFa4c3YWfUP4+d1bIQ+EfJKYvIu7iqaJzoMWxZBrbL2NjqxkSujvgl8PETIoGZBq 6gra8KzT8fcdgk4kdG1Lz9v8eYK1iTs= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-15-vKI9H4XHOpaOE4RtGidztw-1; Thu, 01 Aug 2024 17:09:37 -0400 X-MC-Unique: vKI9H4XHOpaOE4RtGidztw-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D942A1956088; Thu, 1 Aug 2024 21:09:35 +0000 (UTC) Received: from localhost (unknown [10.42.28.21]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0D57319560AE; Thu, 1 Aug 2024 21:09:34 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Constrain std::basic_string default constructor [PR113841] Date: Thu, 1 Aug 2024 22:08:57 +0100 Message-ID: <20240801210933.386029-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX 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~incoming=patchwork.ozlabs.org@gcc.gnu.org I added similar constraints to std::vector recently. The same problem exists for std::string, so this fixes it. Tested x86_64-linux. Pushed to trunk. -- >8 -- This is needed to avoid errors outside the immediate context when evaluating is_default_constructible_v> when A is not default constructible. This change is not sufficient to solve the problem because there are a large number of member functions which have a default argument that constructs an allocator. libstdc++-v3/ChangeLog: PR libstdc++/113841 * include/bits/basic_string.h (basic_string::basic_string()): Constrain so that it's only present if the allocator is default constructible. * include/bits/cow_string.h (basic_string::basic_string()): Likewise. * testsuite/21_strings/basic_string/cons/113841.cc: New test. --- libstdc++-v3/include/bits/basic_string.h | 3 ++ libstdc++-v3/include/bits/cow_string.h | 5 ++++ .../21_strings/basic_string/cons/113841.cc | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 libstdc++-v3/testsuite/21_strings/basic_string/cons/113841.cc diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 8a695a494ef..944bd230704 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -526,6 +526,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 _GLIBCXX20_CONSTEXPR basic_string() _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value) +#if __cpp_concepts && __glibcxx_type_trait_variable_templates + requires is_default_constructible_v<_Alloc> +#endif : _M_dataplus(_M_local_data()) { _M_init_local_buf(); diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 75a2d887ad6..2298844f38d 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -515,6 +515,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION basic_string() #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 _GLIBCXX_NOEXCEPT +#endif +#if __cpp_concepts && __glibcxx_type_trait_variable_templates + requires is_default_constructible_v<_Alloc> +#endif +#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) #else : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()) diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/113841.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/113841.cc new file mode 100644 index 00000000000..5a08610ea14 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/113841.cc @@ -0,0 +1,28 @@ +// { dg-do compile { target c++20 } } + +#include + +template +struct Alloc +{ + using value_type = T; + + Alloc(int) { } // not default constructible + + template Alloc(const Alloc&) { } + + T* allocate(std::size_t n) { return std::allocator().allocate(n); } + void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p, n); } +}; + +template struct wrap { T t; }; + +template void do_adl(T&) { } + +void test_pr113841() +{ + using Tr = std::char_traits; + using test_type = std::basic_string>; + std::pair>* h = nullptr; + do_adl(h); +}