From patchwork Tue Apr 16 07:40:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 88535 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8E1E13858408 for ; Tue, 16 Apr 2024 07:41:14 +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 B6BBB3858D37 for ; Tue, 16 Apr 2024 07:40:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B6BBB3858D37 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 B6BBB3858D37 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=1713253246; cv=none; b=DG0OZXkCt0pxfkyy9KxRq5dnnWsChHFArV6yXkXfVoKwRHLaIqbmyEMJG3qLa5o4GCwIvovFO4eMKvxNv/pQgJKC3ESpfOJLc/h1p1ceQX6C/6GEf/uo8wwZ8lxrdf2WtoRRhbSfoLjz4rDSB4qgQd352AvDPl2yHs2bpVG6XHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713253246; c=relaxed/simple; bh=vZVVK5Br9QvYfMRyGz/mRrC1g1LAtmhfUtURLGeLINU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=F38yiGsNm1mAquqaFTJGCS2D/IYycwUYeaxRskuwHyPYzaBJrSO9zj6ZTGZ4Dvvq2UC78ac4S3tvgdLMia9KcBUf60b1uI9ExTkFC+mRGLsLN3xos5XBal94cHaV0qPRT3h1F44e/Qg8Rwvt7cBA/UyHVk5iLl2yG7Vhc0ay+Ig= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713253244; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=hpGZoet+2474VU4+OeNAFGM8FpUj+nNuiK2Y2X/ZKko=; b=MhdviBFvrX3PfwC3+OLLqUDPOgUsZwya195ctiCUeAKj7MQXBSnTRdJDxglR+epo7EDrJQ fcyBApzG8uVO6jl7AuNArbLwgx+0CvKNL97xJoYwiCFvpJGV8v6luEOu5zqko5itMWKTPq 9zWnnam7GQSz2hdjDXBA9XC04mkjXn8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-536-c373VV8uNUigWQGp2WGVSg-1; Tue, 16 Apr 2024 03:40:42 -0400 X-MC-Unique: c373VV8uNUigWQGp2WGVSg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2E84A80021A for ; Tue, 16 Apr 2024 07:40:42 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E7A25492BC7; Tue, 16 Apr 2024 07:40:41 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 43G7ea1E458963 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 16 Apr 2024 09:40:36 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 43G7easI458962; Tue, 16 Apr 2024 09:40:36 +0200 Date: Tue, 16 Apr 2024 09:40:36 +0200 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [committed] c++: Handle ARRAY_TYPE in check_bit_cast_type [PR114706] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Hi! https://eel.is/c++draft/bit.cast#3 says that std::bit_cast isn't constexpr if To, From and the types of all subobjects have certain properties which the check_bit_cast_type checks (such as it isn't a pointer, reference, union, member pointer, volatile). The function doesn't cp_walk_tree though, so I've missed one important case, for ARRAY_TYPEs we need to recurse on the element type. I think we don't need to handle VECTOR_TYPEs/COMPLEX_TYPEs, because those will not have a pointer/reference/union/member pointer in the element type and if the element type is volatile, I think the whole derived type is volatile as well. Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk as obvious. 2024-04-16 Jakub Jelinek PR c++/114706 * constexpr.cc (check_bit_cast_type): Handle ARRAY_TYPE. * g++.dg/cpp2a/bit-cast17.C: New test. Jakub --- gcc/cp/constexpr.cc.jj 2024-04-12 19:57:58.792749776 +0200 +++ gcc/cp/constexpr.cc 2024-04-15 17:24:00.181449078 +0200 @@ -4843,6 +4843,8 @@ check_bit_cast_type (const constexpr_ctx if (TREE_CODE (field) == FIELD_DECL && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type)) return true; + if (TREE_CODE (type) == ARRAY_TYPE) + return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type); return false; } --- gcc/testsuite/g++.dg/cpp2a/bit-cast17.C.jj 2024-04-15 17:13:20.192052326 +0200 +++ gcc/testsuite/g++.dg/cpp2a/bit-cast17.C 2024-04-15 17:38:04.758093926 +0200 @@ -0,0 +1,31 @@ +// PR c++/114706 +// { dg-do compile { target c++20 } } + +namespace std { +template +constexpr T +bit_cast (const F& f) noexcept +{ + return __builtin_bit_cast (T, f); +} +} +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'const A' contains a union type" "" { target *-*-* } .-3 } +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'A' contains a union type" "" { target *-*-* } .-4 } +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'const C' contains a union type" "" { target *-*-* } .-5 } +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'C' contains a union type" "" { target *-*-* } .-6 } +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'const E' contains a pointer type" "" { target *-*-* } .-7 } +// { dg-error "'__builtin_bit_cast' is not a constant expression because 'E' contains a pointer type" "" { target *-*-* } .-8 } + +union U { int a; int b; }; +struct A { U c[2]; }; +struct B { int d[2]; }; +struct C { U e[2][2]; }; +struct D { int f[2][2]; }; +struct E { int *g[3]; }; +struct F { char h[sizeof (int *) * 3]; }; +constexpr B i = std::bit_cast (A{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" } +constexpr A j = std::bit_cast (B{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" } +constexpr D k = std::bit_cast (C{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" } +constexpr C l = std::bit_cast (D{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" } +constexpr F m = std::bit_cast (E{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" } +constexpr E n = std::bit_cast (F{}); // { dg-message "in 'constexpr' expansion of 'std::bit_cast<" }