From patchwork Wed Aug 21 08:53:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1974772 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=IwoBkjnF; 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 4WpgCn0Cdtz1ydn for ; Wed, 21 Aug 2024 18:55:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C4599385EC33 for ; Wed, 21 Aug 2024 08:55:46 +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 9118E385841C for ; Wed, 21 Aug 2024 08:55:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9118E385841C 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 9118E385841C 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=1724230513; cv=none; b=CSwUuUW5dswp9BRbmZ7AshI2co/pUiB8OEkHcP7KLkNNOowRFmoC3O+WijW/T+omWmtZ3W61r4K+bqz+aYXNKeDLTSQTvynDUk80BFPeQqPsDNFxPvN2rJ5x/Ejo9DzqaGQhWKcUrYRgZCkUSNuHcxpfJbqO9JtcWk/VdAGa8fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1724230513; c=relaxed/simple; bh=xExK1FQnHtPuE10Tcna7dbnJMrZiOWw6ACxyjuOwIzo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=g4f7CsxwdN7IjpvZQjWik0dhhBaX9h2e/5831ILR7rCzxchsdd7akMewiHeSd+ZNq26R6zdY3fBIROjEKg5Wnoa6x+x688XS4wxR7gbZYu3t5cIZvuP/qEDVNiCj8jSOW2ibd5trmzGRRWOf2ZR1iDn/5DgIoxp5rIwENolM8wc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724230511; 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=kSZKiP4/0UpPFel8zEj7A8IpuI6JymjDotuJQUjXroA=; b=IwoBkjnF+Mmc7eB7ouivdypgrV12K3h0meHlNypmSE++0YI3b+WlTHbZawIUoxh59mglS2 q/r/GazO+8Jo23RMWMUFo7ebMnFroZSHEafiJNOxg+65GUkway/e4isAs5y6ZTr2NELAUL yIuspsPjc1crc9fKa0uQP/m+159LHnA= 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-596-I_pZEG47NF69PFbZ0QIP0A-1; Wed, 21 Aug 2024 04:55:10 -0400 X-MC-Unique: I_pZEG47NF69PFbZ0QIP0A-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 A9CB7195421C; Wed, 21 Aug 2024 08:55:07 +0000 (UTC) Received: from localhost (unknown [10.42.28.148]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A96631955DD6; Wed, 21 Aug 2024 08:55:06 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::variant to reject array types [PR116381] Date: Wed, 21 Aug 2024 09:53:49 +0100 Message-ID: <20240821085505.351397-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.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 trunk. Probably worth backporting too. It could potentially cause new errors for people using arrays in std::variant, but that's forbidden by the standard. -- >8 -- 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. --- libstdc++-v3/include/std/variant | 6 ++---- .../testsuite/20_util/variant/types_neg.cc | 17 +++++++++++++++++ 2 files changed, 19 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 12108d07f0b..5fb7770d889 100644 --- a/libstdc++-v3/include/std/variant +++ b/libstdc++-v3/include/std/variant @@ -1457,10 +1457,8 @@ namespace __detail::__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"); + static_assert(((std::is_object_v<_Types> && !is_array_v<_Types>) && ...), + "variant alternatives must be non-array object types"); 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..5cd3d02154b --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/variant/types_neg.cc @@ -0,0 +1,17 @@ +// { dg-do compile { target c++17 } } + +# 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 "" }