From patchwork Wed Jul 24 19:53:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1964483 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=KGWwfGUO; 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 4WTl9L09M1z1yZw for ; Thu, 25 Jul 2024 05:55:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4D4363858428 for ; Wed, 24 Jul 2024 19:55:00 +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.133.124]) by sourceware.org (Postfix) with ESMTP id 1EFCD3858C48 for ; Wed, 24 Jul 2024 19:54:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1EFCD3858C48 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 1EFCD3858C48 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850846; cv=none; b=BKdHixQ48lK34cDkUX9Oi0YEyeyzF7f/ZG3uV7XWApx7bvlecneTLCWYUEtKmEpKGszcr9rxEM79xxihT+KQ2HL3iSTa7mNBjigggBWZm6Gx6uUZJSho+LE8/IktQSxKID5FUEbBRTvC/zcmGwYa0BgvpAGQQQNQdcvUyZ3sdfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721850846; c=relaxed/simple; bh=Q5u9ISyB0znXu4+8FqI5rF4u14wdtoMfpbP0JV20RD8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=rNFcj9k4M0srn6wdlLVVHV3x7pM6DdO7qE55fuVnVhxIZXv+rmQxTV9jgcdIR8Adu8wmk58VoXgksE7nLX4Ap6Lb4oU0VvkTwYhvyP/ibZkWOPTTOV0VHJ675mHyWqQu1sJmWd7FwFaGUK8RH+MdHqfdh439e1RJBzXp19OX8cs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721850843; 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=YGDLH1AtApZsJGYp7EkHWFKGLujU/NkQvMUYRz6Z2HM=; b=KGWwfGUOjyEGbzXxtzPyZfTF8ydKqOMv6TW2ZRv/aVTySLoqNL81KjM2TfmkLjRW2x/um7 14gtTuTmU9rIl4SCufpt1dDh3+PNEe5Gck2paiQ+MuA83cnjCpfwQStJh12fa04D3BQz3n Hb+tTlSAcPlamh8RqgyZak31rexG0kI= Received: from mx-prod-mc-03.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-66-qDVh482PPS6i3_TR-n3geg-1; Wed, 24 Jul 2024 15:54:02 -0400 X-MC-Unique: qDVh482PPS6i3_TR-n3geg-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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 57DE01955D52; Wed, 24 Jul 2024 19:54:01 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A9C341955F6B; Wed, 24 Jul 2024 19:53:59 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] libstdc++: Implement LWG 3836 for std::expected bool conversions Date: Wed, 24 Jul 2024 20:53:28 +0100 Message-ID: <20240724195358.4007988-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.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 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 Tested x86_64-linux. -- >8 -- libstdc++-v3/ChangeLog: * include/std/expected (expected): Constrain constructors to prevent problematic bool conversions, as per LWG 3836. * testsuite/20_util/expected/lwg3836.cc: New test. --- libstdc++-v3/include/std/expected | 59 ++++++++++++++----- .../testsuite/20_util/expected/lwg3836.cc | 34 +++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/expected/lwg3836.cc diff --git a/libstdc++-v3/include/std/expected b/libstdc++-v3/include/std/expected index 86026c3947a..2594cfe131c 100644 --- a/libstdc++-v3/include/std/expected +++ b/libstdc++-v3/include/std/expected @@ -314,6 +314,17 @@ namespace __expected __guard.release(); } } + + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3836. std::expected conversion constructor + // expected(const expected&) should take precedence over + // expected(U&&) with operator bool + + // If T is cv bool, remove_cvref_t is not a specialization of expected. + template + concept __not_constructing_bool_from_expected + = ! is_same_v, bool> + || ! __is_expected>; } /// @endcond @@ -327,26 +338,41 @@ namespace __expected static_assert( ! __expected::__is_unexpected> ); static_assert( __expected::__can_be_unexpected<_Er> ); - template> + // If T is not cv bool, converts-from-any-cvref> and + // is_constructible, cv expected ref-qual> are false. + template, + typename = remove_cv_t<_Tp>> static constexpr bool __cons_from_expected - = __or_v&>, - is_constructible<_Tp, expected<_Up, _Err>>, - is_constructible<_Tp, const expected<_Up, _Err>&>, - is_constructible<_Tp, const expected<_Up, _Err>>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_convertible&, _Tp>, - is_convertible, _Tp>, - is_constructible<_Unex, expected<_Up, _Err>&>, - is_constructible<_Unex, expected<_Up, _Err>>, - is_constructible<_Unex, const expected<_Up, _Err>&>, - is_constructible<_Unex, const expected<_Up, _Err>> + = __or_v&>, + is_constructible<_Tp, expected<_Up, _Gr>>, + is_constructible<_Tp, const expected<_Up, _Gr>&>, + is_constructible<_Tp, const expected<_Up, _Gr>>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_convertible&, _Tp>, + is_convertible, _Tp>, + is_constructible<_Unex, expected<_Up, _Gr>&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> >; - template + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // If t is cv bool, we know it can be constructed from expected, + // but we don't want to cause the expected(U&&) constructor to be used, + // so we only check the is_constructible, ...> cases. + template + static constexpr bool __cons_from_expected<_Up, _Gr, _Unex, bool> + = __or_v&>, + is_constructible<_Unex, expected<_Up, _Gr>>, + is_constructible<_Unex, const expected<_Up, _Gr>&>, + is_constructible<_Unex, const expected<_Up, _Gr>> + >; + + template constexpr static bool __explicit_conv = __or_v<__not_>, - __not_> + __not_> >; template @@ -445,8 +471,9 @@ namespace __expected template requires (!is_same_v, expected>) && (!is_same_v, in_place_t>) - && (!__expected::__is_unexpected>) && is_constructible_v<_Tp, _Up> + && (!__expected::__is_unexpected>) + && __expected::__not_constructing_bool_from_expected<_Tp, _Up> constexpr explicit(!is_convertible_v<_Up, _Tp>) expected(_Up&& __v) noexcept(is_nothrow_constructible_v<_Tp, _Up>) diff --git a/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc new file mode 100644 index 00000000000..cd029c44963 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/expected/lwg3836.cc @@ -0,0 +1,34 @@ +// { dg-do run { target c++23 } } + +#include +#include + +constexpr void +test_convert_contained_value_to_bool() +{ + struct BaseError { }; + struct DerivedError : BaseError { }; + + std::expected e = false; + + // Should use expected(const expected&) ctor, not expected(U&&): + std::expected e2 = e; + + // Contained value should be e.value() not static_cast(e): + VERIFY( e2.value() == false ); + + std::expected e3(std::unexpect); + std::expected e4 = e3; + // Should have error, not static_cast(e3): + VERIFY( ! e4.has_value() ); +} + +int main() +{ + test_convert_contained_value_to_bool(); + + static_assert([] { + test_convert_contained_value_to_bool(); + return true; + }()); +}