From patchwork Wed Sep 4 20:36:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1980991 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=WQjVfp41; 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 4WzZ736KbWz1yg3 for ; Thu, 5 Sep 2024 06:37:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD2D0385EC35 for ; Wed, 4 Sep 2024 20:37:33 +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 7BDA53858424 for ; Wed, 4 Sep 2024 20:37:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BDA53858424 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 7BDA53858424 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=1725482231; cv=none; b=F2KhpEvgh/GmoY8AdTnixZFNqzuzkqETFHWayHBQpBMEEkBYobIpRCfYBffCebX6wVmInNBXATr3EhPUe8Dlgx5mCh2T8LamDtCp3+sZMQZSjOuT11M2FgdVNoAXltU+uvFMQacVyHjjrsYP7SLpIfG5fFafHfB6WjkshBjVGwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725482231; c=relaxed/simple; bh=8z/+1SXmquTK3L75XivEVx2Xa1sAnArdvi1HAcjCgfQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZOvUlGiLlG3dHhx0RPDpySbQJnrO5w8urNitphH0scpqFW9vu1gY5IYHXL0hiXV51A9LanpWfuuf/sW/3PFM07N2ql3s02UufcG0qFLZ7x9acPufufdB/bt9xSeXBoTzQo5oRH6cHwmahPUR8yrj07raNRZOXD7SbcGsGx9ECeA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725482229; 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=rvaNOibR1cpU2orUOBJh5FnW/6uwLEAi9MpWp4eQP6M=; b=WQjVfp41J4do9791GaSlAdFBN7CkV7MSh08arapuqD8GXVAfKpbrx8C79x5rF1GQ709lpV HpqmcVf5b10fFn1Xmh3v56NwNR8J50mq90lvjPFO09U3k8u1CdzaVgeP0ASXlLnEX5uEty dMR2s583VVmp/vwOfAA+eEMgsi6/Go8= 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-684-cgKpZOz_PRylZG9z06Y9NQ-1; Wed, 04 Sep 2024 16:37:08 -0400 X-MC-Unique: cgKpZOz_PRylZG9z06Y9NQ-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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E0A1E1954B16; Wed, 4 Sep 2024 20:37:06 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3DF0D1956086; Wed, 4 Sep 2024 20:37:05 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed, gcc-14] libstdc++: Fix std::variant to reject array types [PR116381] Date: Wed, 4 Sep 2024 21:36:36 +0100 Message-ID: <20240904203704.2953245-1-jwakely@redhat.com> In-Reply-To: References: 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.5 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=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. Pushed to gcc-14. -- >8 -- For the backport, rejecting array types is only done in strict modes. libstdc++-v3/ChangeLog: PR libstdc++/116381 * include/std/variant (variant): Fix conditions for static_assert to match the spec. * testsuite/20_util/variant/types_neg.cc: New test. (cherry picked from commit 1e10b3b8825ee398f077500af6ae1f5db180983a) --- libstdc++-v3/include/std/variant | 11 +++++++---- .../testsuite/20_util/variant/types_neg.cc | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/variant/types_neg.cc diff --git a/libstdc++-v3/include/std/variant b/libstdc++-v3/include/std/variant index 4e56134a6f7..834bb548f13 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1374,10 +1374,13 @@ namespace __variant static_assert(sizeof...(_Types) > 0, "variant must have at least one alternative"); - static_assert(!(std::is_reference_v<_Types> || ...), - "variant must have no reference alternative"); - static_assert(!(std::is_void_v<_Types> || ...), - "variant must have no void alternative"); +#ifdef __STRICT_ANSI__ + static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...), + "variant alternatives must be non-array object types"); +#else + static_assert((std::is_object_v<_Types> && ...), + "variant alternatives must be object types"); +#endif using _Base = __detail::__variant::_Variant_base<_Types...>; diff --git a/libstdc++-v3/testsuite/20_util/variant/types_neg.cc b/libstdc++-v3/testsuite/20_util/variant/types_neg.cc new file mode 100644 index 00000000000..7d970e961c2 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/types_neg.cc @@ -0,0 +1,18 @@ +// { dg-do compile { target c++17 } } +// { dg-add-options strict_std } + +# include + +std::variant<> v0; // { dg-error "here" } +// { dg-error "must have at least one alternative" "" { target *-*-* } 0 } +std::variant v1; // { dg-error "here" } +std::variant v2; // { dg-error "here" } +std::variant v3; // { dg-error "here" } +std::variant v4; // { dg-error "here" } +std::variant v5; // { dg-error "here" } +std::variant v6; // { dg-error "here" } +// { dg-error "must be non-array object types" "" { target *-*-* } 0 } + +// All of variant's base classes are instantiated before checking any +// static_assert, so we get lots of errors before the expected errors above. +// { dg-excess-errors "" }