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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id C6533385782A for ; Mon, 4 Jan 2021 20:44:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C6533385782A Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-332-rNKhAXF3OJyDz8nrbHqLwQ-1; Mon, 04 Jan 2021 15:44:50 -0500 X-MC-Unique: rNKhAXF3OJyDz8nrbHqLwQ-1 Received: by mail-qv1-f72.google.com with SMTP id k16so10004471qve.19 for ; Mon, 04 Jan 2021 12:44:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=nuIt4/nArnd+kgQDroUhHiA6jK3yrkstJfph41GaAQE=; b=dPKzhkuKXdSAG4eq6/w7NizFKt+s/ke22zFn4hgN8qY4B+QFpAuOpvs3Iq066bFhSu H+cY9Hx86ytGofQK3yP+yEhC1gXHWMYqyDmPNEg+VTDXg/dPGzr9uvVBXqDZxQw9AsAN S5zXy4qJrHBAVlBEB52PnXjVdh9Ey8XPDIHSb0lQXPOcKsKM0JRAGPbUJz3GTqZhsTJV 0RsEWkoF0VG808bT1nIXW8ClmMDkXYGjqEp+aMNQvpmSaESUD0mC20h4l2DcJ6BYnOlk rJaswQ0MhlIqfGgxPhr5RKhHg7X2+x8xDRXO0IyeMqP5A1uZqU849H7SSVU5LKbCD32j nYsA== X-Gm-Message-State: AOAM531XjJfTFUSImTnXX6zOthRwm4b012j+EyVCV9pWfQiy2O6vexcT utMuTUjtrcZF5Xi9lo85l+tu+x24hNWosxf4EsRYmd237m5AMrxxRtQwPZA9vDHs35kI58KJIVC GYeCyaOe6BBolYG7yjdNjezClhxiZxx37tGty19QO0R/N54OhLC/40uNEBXUGVJw4pg== X-Received: by 2002:ac8:4f45:: with SMTP id i5mr37833741qtw.349.1609793089232; Mon, 04 Jan 2021 12:44:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJycgxzj94KqkoC1QYIEFYzv1H04RXzhVWr68DXz2ybK4a2e3DK0VV+Z4Rpv7JO+oGQYPClSKg== X-Received: by 2002:ac8:4f45:: with SMTP id i5mr37833717qtw.349.1609793088800; Mon, 04 Jan 2021 12:44:48 -0800 (PST) Received: from [192.168.1.148] (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id d3sm23694486qka.36.2021.01.04.12.44.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Jan 2021 12:44:48 -0800 (PST) Subject: Re: [PATCH] c++: Fix ICE with __builtin_bit_cast [PR98469] To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: <20201230091343.GU3788@tucnak> From: Jason Merrill Message-ID: Date: Mon, 4 Jan 2021 15:44:46 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.4.3 MIME-Version: 1.0 In-Reply-To: <20201230091343.GU3788@tucnak> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, 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: Mon, 04 Jan 2021 20:44:53 -0000 On 12/30/20 4:13 AM, Jakub Jelinek wrote: > 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. This change is OK, but part of the problem is that we're trying to do overload resolution for an S copy/move constructor, which we shouldn't be because bit_cast is a prvalue, so in C++17 and up we should use it to directly initialize the target without any implied constructor call. It seems we're mishandling this because the code in build_special_member_call specifically looks for TARGET_EXPR or CONSTRUCTOR, and BIT_CAST_EXPR is neither of those. Wrapping a BIT_CAST_EXPR of aggregate type in a TARGET_EXPR would address this, and any other places that expect a class prvalue to come in the form of a TARGET_EXPR. > 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 >