From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id D27D83857C6F for ; Wed, 30 Dec 2020 09:13:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D27D83857C6F Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-541-zhZgT6_wPbymc3yNGMtOKQ-1; Wed, 30 Dec 2020 04:13:48 -0500 X-MC-Unique: zhZgT6_wPbymc3yNGMtOKQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7893210066FE for ; Wed, 30 Dec 2020 09:13:47 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-11.ams2.redhat.com [10.36.112.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0EB0477BE2; Wed, 30 Dec 2020 09:13:46 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 0BU9DibW1973751 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 30 Dec 2020 10:13:44 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 0BU9DhKu1973750; Wed, 30 Dec 2020 10:13:43 +0100 Date: Wed, 30 Dec 2020 10:13:43 +0100 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Fix ICE with __builtin_bit_cast [PR98469] Message-ID: <20201230091343.GU3788@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-6.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Dec 2020 09:13:52 -0000 Hi! On the following testcase we ICE during constexpr evaluation (for warnings), because the IL has ADDR_EXPR of BIT_CAST_EXPR and ADDR_EXPR case asserts the result is not a CONSTRUCTOR. I've tried to force a temporary for those in call.c next to: if (convs->need_temporary_p || TREE_CODE (expr) == CONSTRUCTOR || TREE_CODE (expr) == VA_ARG_EXPR) but that resulted in a lot of ICEs, so this patch just punts on lval evaluation of BIT_CAST_EXPR instead, normally __builtin_bit_cast is called from std::bit_cast which is constexpr and therefore the BIT_CAST_EXPR isn't evaluated there during parsing or tsubst and when evaluating the call to std::bit_cast the NRV optimized return is assigned to some temporary or variable and so BIT_CAST_EXPR is not evaluated as lval. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-12-30 Jakub Jelinek PR c++/98469 * constexpr.c (cxx_eval_constant_expression) : Punt if lval is true. * g++.dg/cpp2a/bit-cast8.C: New test. * g++.dg/cpp2a/bit-cast9.C: New test. --- gcc/cp/constexpr.c.jj 2020-12-23 22:44:05.398093175 +0100 +++ gcc/cp/constexpr.c 2020-12-29 10:32:44.865030881 +0100 @@ -6900,6 +6900,15 @@ cxx_eval_constant_expression (const cons return t; case BIT_CAST_EXPR: + if (lval) + { + if (!ctx->quiet) + error_at (EXPR_LOCATION (t), + "address of a call to %qs is not a constant expression", + "__builtin_bit_cast"); + *non_constant_p = true; + return t; + } r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); break; --- gcc/testsuite/g++.dg/cpp2a/bit-cast8.C.jj 2020-12-29 10:35:31.547140723 +0100 +++ gcc/testsuite/g++.dg/cpp2a/bit-cast8.C 2020-12-29 10:34:26.431879120 +0100 @@ -0,0 +1,11 @@ +// PR c++/98469 +// { dg-do compile { target c++20 } } +// { dg-options "-Wall" } + +struct S { int s; }; + +S +foo () +{ + return __builtin_bit_cast (S, 0); +} --- gcc/testsuite/g++.dg/cpp2a/bit-cast9.C.jj 2020-12-29 10:35:35.018101365 +0100 +++ gcc/testsuite/g++.dg/cpp2a/bit-cast9.C 2020-12-29 10:35:05.905431494 +0100 @@ -0,0 +1,15 @@ +// PR c++/98469 +// { dg-do compile { target c++20 } } +// { dg-options "-Wall" } + +template +constexpr T +bit_cast (const F &f) noexcept +{ + return __builtin_bit_cast (T, f); +} +struct S { int s; }; +constexpr int foo (const S &x) { return x.s; } +constexpr int bar () { return foo (bit_cast (0)); } +constexpr int x = bar (); +static_assert (!x); Jakub