From patchwork Wed Jul 24 19:53:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1964482 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=Ai74zYPu; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WTl902fb6z1yZw for ; Thu, 25 Jul 2024 05:54:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8896E385840A for ; Wed, 24 Jul 2024 19:54:42 +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 643AB3858C48 for ; Wed, 24 Jul 2024 19:54:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 643AB3858C48 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 643AB3858C48 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=1721850852; cv=none; b=g9xZxUpsQqECc6IGEzZ927dRpQQRRpHndF8feJxj6WRthjZ5FbreZ/DjhvAVu3tNY2fdMfbRDJASg68di5xdRCePLEIZMcw4qg3vDRzuhHKPiMgMzYZLrLz+wp980miIdPAHGuCo/wh7jkWKlC8aRDUHecwJLHsSa+0+OP5j/yc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850852; c=relaxed/simple; bh=kqHuzz9zfjFCcYYdLzApm7b+VFtUUv+B/k3MSzDRPak=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=gYNkc2voh9gN0J7IhQbVAGfnPDHdBb5wkVYuG/4FLb4EkwqByF6cvG4NZ0dwtuOqS74Iy/PwlZpQXljKwT52fIvs3mbo0eurrA3bn9c74fP0LToVEkWVc3ezvrk8W1Demdr1OMAxdosk5LUGRafz4G9GHjdB6C5CtBJdR2lv7UU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721850849; 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: in-reply-to:in-reply-to:references:references; bh=C5+Q0WZAtwaUEEXei6qd+egPfRzISkikYOiU9Q4ssLc=; b=Ai74zYPuHCQdM8T3baijVobOJF+wTH6GXotGOHvtQLtTsvvBDOl8lVf3OivZFx3TvqCog7 C9YD3A8U+cDAdXJSRKskRmVkTYbkX7SbjDXAiEh2b1iOpiyMTMBtHFEpkneIMuV7k8RB5t aUdfp3pvZefMZnPxuunmJG7Uek1CgDQ= Received: from mx-prod-mc-04.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-691-b-5YgiV4OfCyM1k1pUvh4w-1; Wed, 24 Jul 2024 15:54:05 -0400 X-MC-Unique: b-5YgiV4OfCyM1k1pUvh4w-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BBF831955D47; Wed, 24 Jul 2024 19:54:04 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2030E1955D45; Wed, 24 Jul 2024 19:54:02 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libstdc++: Implement LWG 3836 for std::optional bool conversions Date: Wed, 24 Jul 2024 20:53:29 +0100 Message-ID: <20240724195358.4007988-2-jwakely@redhat.com> In-Reply-To: <20240724195358.4007988-1-jwakely@redhat.com> References: <20240724195358.4007988-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/optional (optional): Constrain constructors to prevent problematic bool conversions, as per LWG 3836. * testsuite/20_util/optional/cons/lwg3836.cc: New test. --- libstdc++-v3/include/std/optional | 58 ++++++++++++++----- .../20_util/optional/cons/lwg3836.cc | 58 +++++++++++++++++++ 2 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 344be5e44d3..700e7047aba 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -749,16 +749,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline constexpr bool __is_optional_v> = true; + template + using __converts_from_any_cvref = __or_< + is_constructible<_Tp, _Wp&>, is_convertible<_Wp&, _Tp>, + is_constructible<_Tp, _Wp>, is_convertible<_Wp, _Tp>, + is_constructible<_Tp, const _Wp&>, is_convertible, + is_constructible<_Tp, const _Wp>, is_convertible + >; + template - using __converts_from_optional = - __or_&>, - is_constructible<_Tp, optional<_Up>&>, - is_constructible<_Tp, const optional<_Up>&&>, - is_constructible<_Tp, optional<_Up>&&>, - is_convertible&, _Tp>, - is_convertible&, _Tp>, - is_convertible&&, _Tp>, - is_convertible&&, _Tp>>; + using __converts_from_optional + = __converts_from_any_cvref<_Tp, optional<_Up>>; template using __assigns_from_optional = @@ -800,6 +801,30 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template using _Requires = enable_if_t<__and_v<_Cond...>, bool>; + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3836. std::expected conversion constructor + // expected(const expected&) should take precedence over + // expected(U&&) with operator bool + template> + struct __not_constructing_bool_from_optional + : true_type + { }; + + template + struct __not_constructing_bool_from_optional<_From, bool> + : bool_constant>> + { }; + + template> + struct __construct_from_contained_value + : __not_<__converts_from_optional<_Tp, _From>> + { }; + + template + struct __construct_from_contained_value<_From, bool> + : true_type + { }; + public: using value_type = _Tp; @@ -811,7 +836,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template, __not_tag<_Up>, is_constructible<_Tp, _Up>, - is_convertible<_Up, _Tp>> = true> + is_convertible<_Up, _Tp>, + __not_constructing_bool_from_optional<_Up>> = true> constexpr optional(_Up&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -820,7 +846,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template, __not_tag<_Up>, is_constructible<_Tp, _Up>, - __not_>> = false> + __not_>, + __not_constructing_bool_from_optional<_Up>> = false> explicit constexpr optional(_Up&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -830,7 +857,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, const _Up&>, is_convertible, - __not_<__converts_from_optional<_Tp, _Up>>> = true> + __construct_from_contained_value<_Up>> = true> constexpr optional(const optional<_Up>& __t) noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) @@ -843,7 +870,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, const _Up&>, __not_>, - __not_<__converts_from_optional<_Tp, _Up>>> = false> + __construct_from_contained_value<_Up>> = false> explicit constexpr optional(const optional<_Up>& __t) noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) @@ -856,7 +883,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, _Up>, is_convertible<_Up, _Tp>, - __not_<__converts_from_optional<_Tp, _Up>>> = true> + __construct_from_contained_value<_Up>> = true> constexpr optional(optional<_Up>&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -869,7 +896,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Requires<__not_>, is_constructible<_Tp, _Up>, __not_>, - __not_<__converts_from_optional<_Tp, _Up>>> = false> + __construct_from_contained_value<_Up>> = false> explicit constexpr optional(optional<_Up>&& __t) noexcept(is_nothrow_constructible_v<_Tp, _Up>) @@ -895,7 +922,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Args...>) : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { } - // Assignment operators. _GLIBCXX20_CONSTEXPR optional& operator=(nullopt_t) noexcept diff --git a/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc new file mode 100644 index 00000000000..816eb1b7def --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/cons/lwg3836.cc @@ -0,0 +1,58 @@ +// { dg-do run { target c++17 } } + +#include +#include + +constexpr void +test_convert_contained_value_to_bool() +{ + struct False { constexpr operator bool() const { return false; } }; + + False f; + std::optional o = f; + + // Should use optional(const optional&) ctor, not optional(U&&): + std::optional o2 = o; + + // Contained value should be static_cast(f) not static_cast(o): + VERIFY( o2.value() == false ); + + std::optional o3; + std::optional o4 = o3; + // Should have no contained value, not static_cast(o3): + VERIFY( ! o4.has_value() ); +} + +constexpr void +test_convert_contained_value_to_bool_explicit() +{ + struct False { constexpr explicit operator bool() const { return false; } }; + + False f; + std::optional o = f; + + // Should use optional(const optional&) ctor, not optional(U&&): + std::optional o2(o); + + // Contained value should be static_cast(f) not static_cast(o): + VERIFY( o2.value() == false ); + + std::optional o3; + std::optional o4(o3); + // Should have no contained value, not static_cast(o3): + VERIFY( ! o4.has_value() ); +} + +int main() +{ + test_convert_contained_value_to_bool(); + test_convert_contained_value_to_bool_explicit(); + +#if __cpp_lib_optional >= 202106 + static_assert([] { + test_convert_contained_value_to_bool(); + test_convert_contained_value_to_bool_explicit(); + return true; + }()); +#endif +}