From patchwork Wed Feb 8 21:01:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1739688 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=XPOEFFJo; dkim-atps=neutral Received: from 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PBsrN1kd2z23kw for ; Thu, 9 Feb 2023 08:02:10 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E87E3858C30 for ; Wed, 8 Feb 2023 21:02:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E87E3858C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675890128; bh=lGiyIboqxD2tik7jYPELK7Tqkq7ogKW2K8Zf3p6i6AE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=XPOEFFJoNp8ThbuhfbYZKZpvmf8PN1AaD8osBmGZTAyuOxlKG+QoKOBcwGPmM+NLk e17MTZkkznklVHWvi0iwpCDq5XI42rg/ctorRK6vxoD+wM0N3t7krvGbqEwdlmeJoR f4wQCCvzZMMBbXdpoBj97TlZe+cL3zcg5x6Mj3t8= 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 EE09A3858D39 for ; Wed, 8 Feb 2023 21:01:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE09A3858D39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-282-dE8UwZ0gPYCZqn4XzdfN1A-1; Wed, 08 Feb 2023 16:01:46 -0500 X-MC-Unique: dE8UwZ0gPYCZqn4XzdfN1A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E4C41885625 for ; Wed, 8 Feb 2023 21:01:45 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.33.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id B9BDB40398A0; Wed, 8 Feb 2023 21:01:45 +0000 (UTC) To: Jason Merrill , GCC Patches , Patrick Palka Subject: [PATCH] c++: ICE initing lifetime-extended constexpr var [PR107079] Date: Wed, 8 Feb 2023 16:01:40 -0500 Message-Id: <20230208210140.391014-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" (This may not be a complete fix but I got stuck so I'm posting what I have, which at least fixes the ICE.) We ICE on the simple: struct X { const X* x = this; }; constexpr const X& x = X{}; where store_init_value initializes 'x' with &TARGET_EXPR }> but we must lifetime-extend via extend_ref_init_temps and we get _ZGR1x_.x = (const struct X *) & >>>;, (const struct X &) &_ZGR1x_; Since 'x' was declared constexpr, we do cxx_constant_init and we hit the preeval code added in r269003 while evaluating the INIT_EXPR: _ZGR1x_.x = (const struct X *) & >>> but we have no ctx.ctor or ctx.object here so lookup_placeholder won't find anything that could replace X and we ICE on 7861 /* A placeholder without a referent. We can get here when 7862 checking whether NSDMIs are noexcept, or in massage_init_elt; 7863 just say it's non-constant for now. */ 7864 gcc_assert (ctx->quiet); because cxx_constant_init means !ctx->quiet. It's not correct that there isn't a referent. I think the following patch is a pretty straightforward fix: pass the _ZGR var down to maybe_constant_init so that it can replace the PLACEHOLDER_EXPR with _ZGR1x_. What I wasn't able to make work is the commented assert in the test. It doesn't pass because we complain that _ZGR1x_ isn't a constexpr variable, but making it so would just result in "used in its own initializer" (which is true). I notice that while clang++ compiles the assert fine, MSVC++/icc reject it as non-constant. So maybe we don't have to/shouldn't make it work. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107079 gcc/cp/ChangeLog: * call.cc (set_up_extended_ref_temp): Pass var to maybe_constant_init. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-nsdmi2.C: New test. --- gcc/cp/call.cc | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C base-commit: 77bb54b1b07add45007c664724b726541d672ef3 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index f7c5d9da94b..a0afab9b26a 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec **cleanups, /* If the initializer is constant, put it in DECL_INITIAL so we get static initialization and use in constant expressions. */ - init = maybe_constant_init (expr); + init = maybe_constant_init (expr, var); /* As in store_init_value. */ init = cp_fully_fold (init); if (TREE_CONSTANT (init)) diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C new file mode 100644 index 00000000000..6dbb7eb739a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C @@ -0,0 +1,9 @@ +// PR c++/107079 +// { dg-do compile { target c++11 } } + +struct X { + const X* x = this; +}; +constexpr const X& x = X{}; +// TODO: Should the assert pass? +//static_assert(x.x == &x);