From patchwork Thu Jun 20 15:36: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: 1950253 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=NbjXQyte; 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 4W4l3g3JzPz20KL for ; Fri, 21 Jun 2024 01:37:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B356A389367C for ; Thu, 20 Jun 2024 15:37:17 +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 ESMTPS id 07AE2389000C for ; Thu, 20 Jun 2024 15:36:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 07AE2389000C 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 07AE2389000C 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=1718897813; cv=none; b=fEbzErr2uP+ps4piUtDh1KrHpyY5xiF2qx/G48SfwAO6X1FsHS9lFZp6uwR/9hTC3VymW0EazXFrn1KRzPlj68ru1uTt1VGWzyV42xOzPNBejlD2C3sn7s+1YfqPkp1CS78M3UUEwGxk5cB3+nJE3KN1BqidwGakpGkcjfJKYtw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718897813; c=relaxed/simple; bh=7jSZ98x5ANcs1RRDh1MwMOPeuniSUC5NTswv3vVcHcM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=BHysMQ2SnQ+1SjTn1rf26hWO9imHQsMcN2IkxHYsY3sMGZQxaf9PJd3jv/aHc8JeyHJLRD7ctWKbqwzkk/BJVmRKb+uTc77sFZvlpxODKV47FulVQWGBMNdSrszWyXuVXc0VFb+7+4GRpqesbQOd7rGj4/NoQbDDeSMyEWCnfbA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718897807; 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=WipWQRM5c4kihG35o3tGFKWaS50dxqQhoS3M+hRtUHI=; b=NbjXQyte+1UZVZ48BpQZ96k09fe8SD5xpGnEo/uAwX3SSJxEdPtxhaMNAZQpevchj3TyvX aX3gCq1vvWHeraWb8OcQnA2wKVjiRBNbwhoWy+vzZsWoGGY5a6u9OHH86Qukq7oaSXGEP/ x3DdCfETS7ScTDDmxPkDWRzZRo2m0pM= Received: from mx-prod-mc-01.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-110-d47HwFXuM6eDfzsM3leEvQ-1; Thu, 20 Jun 2024 11:36:44 -0400 X-MC-Unique: d47HwFXuM6eDfzsM3leEvQ-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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 97CC21956089; Thu, 20 Jun 2024 15:36:43 +0000 (UTC) Received: from localhost (unknown [10.42.28.182]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E13CD1955E83; Thu, 20 Jun 2024 15:36:42 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] libstdc++: Make std::any_cast ill-formed (LWG 3305) Date: Thu, 20 Jun 2024 16:36:32 +0100 Message-ID: <20240620153641.2024863-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=-10.8 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, RCVD_IN_SBL_CSS, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. -- >8 -- LWG 3305 was approved earlier this year in Tokyo. We need to give an error if using std::any_cast, but std::any_cast is valid (but always returns null). libstdc++-v3/ChangeLog: * include/std/any (any_cast(any*), any_cast(const any*)): Add static assertion to reject void types, as per LWG 3305. * testsuite/20_util/any/misc/lwg3305.cc: New test. --- libstdc++-v3/include/std/any | 8 ++++++++ .../testsuite/20_util/any/misc/lwg3305.cc | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any index 690ddc2aa57..e4709b1ce04 100644 --- a/libstdc++-v3/include/std/any +++ b/libstdc++-v3/include/std/any @@ -554,6 +554,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline const _ValueType* any_cast(const any* __any) noexcept { + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3305. any_cast + static_assert(!is_void_v<_ValueType>); + + // As an optimization, don't bother instantiating __any_caster for + // function types, since std::any can only hold objects. if constexpr (is_object_v<_ValueType>) if (__any) return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); @@ -563,6 +569,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline _ValueType* any_cast(any* __any) noexcept { + static_assert(!is_void_v<_ValueType>); + if constexpr (is_object_v<_ValueType>) if (__any) return static_cast<_ValueType*>(__any_caster<_ValueType>(__any)); diff --git a/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc b/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc new file mode 100644 index 00000000000..49f5d747ab3 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/misc/lwg3305.cc @@ -0,0 +1,15 @@ +// { dg-do compile { target c++17 } } + +// LWG 3305. any_cast + +#include + +void +test_lwg3305() +{ + std::any a; + (void) std::any_cast(&a); // { dg-error "here" } + const std::any a2; + (void) std::any_cast(&a2); // { dg-error "here" } +} +// { dg-error "static assertion failed" "" { target *-*-* } 0 }