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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id B29B9385828E for ; Thu, 9 Feb 2023 23:41:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B29B9385828E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675986080; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RwcsFcmbRpkNPOe609fYokSvmXMVIZwemtWplBLJMzs=; b=ETcPKo6pQcHBal6cP8ogbh0OezLDYg/sNWezRolKY6II7cHmx3OVsU+dgPRj/K8qxTDPpe M+SRiPawWJrRr57suafQ4HfkUuefedwCiYSJmir88L+5MgPHPuYMcOjqpgWxXb5nGngpwE idOXhp7BTKfgTiA0B3u+qQDgqE9xTVk= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-656-ghdGn4Z6MTSAiYbl2VTLIw-1; Thu, 09 Feb 2023 18:41:19 -0500 X-MC-Unique: ghdGn4Z6MTSAiYbl2VTLIw-1 Received: by mail-qt1-f198.google.com with SMTP id z12-20020ac8710c000000b003b829a0eda2so2078712qto.21 for ; Thu, 09 Feb 2023 15:41:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RwcsFcmbRpkNPOe609fYokSvmXMVIZwemtWplBLJMzs=; b=0yA3qKge2xrgwTrsBg3M1BRSL9XhjLr10adFpwEZJMsYceUqQQKSx/pxaH/+cLfh68 zd+f1AVYhkh4Zrp/+1BtY93qPOW5ejpQZsDnSwg1ugwo2j64Yp8zD/48Jtf9gMDE11c6 eS37LeVZbv3ZyMfcTwpgORKiGxD1x2NvRVQQZDfcywU8zNks+8gMr+qEyhg4v+6h0Bkf dLN3HJpDn9G0b/WQ2NjrTIcjfPRmF8Tw/+MLfbpA18dZqaietsztkwkBowdxrsLbtyeS uDLeuq0pntG1K+H+6XEvmdHzrz1e1tF9NwooT6TLP/relAP9yFBF9Zu6RryQyUNS7w49 Rxzg== X-Gm-Message-State: AO0yUKUZqUCooXi/HVPIV00GBzW3vak1ZEqAzEt2s122++YzslwVCgRv e4u9Gbg2fuaos8OJqsWFoPgNUadFzy5gn1mWIeb7eFTCKBwdbOo98lXABI/aiNXV1R658aJP2hU E5fBBCEdjk0uqU9UVPw== X-Received: by 2002:a05:6214:496:b0:535:5492:b427 with SMTP id pt22-20020a056214049600b005355492b427mr22621944qvb.28.1675986078808; Thu, 09 Feb 2023 15:41:18 -0800 (PST) X-Google-Smtp-Source: AK7set8gXCqnK5g1OHzsQFL28exhWpAnGkv1f7al5PjZugCog7GCzTa0ebGDmnJd5UAjRJSxWapojQ== X-Received: by 2002:a05:6214:496:b0:535:5492:b427 with SMTP id pt22-20020a056214049600b005355492b427mr22621913qvb.28.1675986078418; Thu, 09 Feb 2023 15:41:18 -0800 (PST) Received: from [172.20.25.72] ([12.198.239.140]) by smtp.gmail.com with ESMTPSA id u188-20020a3792c5000000b0072862fcbbdcsm2391250qkd.42.2023.02.09.15.41.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Feb 2023 15:41:17 -0800 (PST) Message-ID: <88f81320-69dc-09f9-a9ef-2a2b5bea2dad@redhat.com> Date: Thu, 9 Feb 2023 15:41:16 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v2] c++: ICE initing lifetime-extended constexpr var [PR107079] To: Marek Polacek Cc: GCC Patches , Patrick Palka References: <20230208210140.391014-1-polacek@redhat.com> <7830a96e-7415-ee72-bbf7-007e5c04b9f1@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,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 List-Id: On 2/8/23 18:15, Marek Polacek wrote: > On Wed, Feb 08, 2023 at 04:00:25PM -0800, Jason Merrill wrote: >> On 2/8/23 13:01, Marek Polacek wrote: >>> (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, >> >> That sounds like we aren't (correctly) implementing >> >> https://eel.is/c++draft/expr.const#4.7 > > Ah yes, this is DR2126 = c++/101588. I wonder if the fix will include > checking startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR") > to see if its lifetime is extended. > >>> but making it so would just result in "used in its own >>> initializer" (which is true). >> >> True, but not in the sense it means; its initializer doesn't depend on its >> (uninitialized) value. > > ...because we're only interested in its address. > >>> 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); >> >> We should also pass true for manifestly_const_eval as in store_init_value. > > Done. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK. > -- >8 -- > 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_. > > The commented assert in the test doesn't pass: we complain that _ZGR1x_ > isn't a constexpr variable because we don't implement DR2126 (PR101588). > > 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 > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index f7c5d9da94b..a349d8e79db 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, /*manifestly_const_eval=*/true); > /* 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..d711b8051c0 > --- /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: The assert should pass once we implement DR2126 (c++/101588). > +//static_assert(x.x == &x); > > base-commit: f6fc79d0c90ff3a924d272eff74b32656bdf5481