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 244723858C60 for ; Thu, 9 Feb 2023 02:15:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 244723858C60 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=1675908936; 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: in-reply-to:in-reply-to:references:references; bh=NsgHUJMVrJImz97PQ2m1+VAyhXThLRBDxj2tgViBJD0=; b=i67KRFlgwdtim3pNaOpzoLSDSsg/I8uBiLxmfaOSAmAZ10cOxUwaZUzofuHeeLWkt5zte5 S3IFfpDOy57g8DEgn/ilJbQxE0TgkxDl30AkKK+8HcB6BaKWgqWqao9Q0XCQy+NbAU2cXB QRYV4EXBRLZmDTNvs8EpXYvQnw6xH/4= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-90-zF3l0lfcMKOMN616HNH6rw-1; Wed, 08 Feb 2023 21:15:35 -0500 X-MC-Unique: zF3l0lfcMKOMN616HNH6rw-1 Received: by mail-qk1-f198.google.com with SMTP id g6-20020ae9e106000000b00720f9e6e3e2so374317qkm.13 for ; Wed, 08 Feb 2023 18:15:35 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NsgHUJMVrJImz97PQ2m1+VAyhXThLRBDxj2tgViBJD0=; b=AKr+2/HPYVj7e/BSupPTElzOkNVZKoEXBNb9Pf2GVc6NOmVrhageglsu1P0Kij+rqN AY/E6/YTFrSR4ptsDT+/JFr07kDr2sFVxve4v+2EUOeB4z5lH7UQZbYXbtZOzAG0X/uk GxA7gvzos1ZK/Kai7XWGWvjX4GJ0TCnGyi0bGz5+L3eJTBFQ/wp9qEiujXbovqdPJT6w zFldnh4czUmkME1EnqoSwSIIALbmLMw50IQ060U8Mxpai5xbjUvKT4bRiZEdaxgqh4bp 3gltIk0R1IxlyGqlAW8d0pW86oLaCbTyMpNWLmF45CTsU+x/43FKUDA87KcwJUQMB41C eksA== X-Gm-Message-State: AO0yUKUqIa1GvsN9Kqy5GvW1/PyfeRCiMco41r+XsI1ewkQmJNjhz+yD B8cYTFewLhly35QVyHvYNcQUuiL7sqhK8QAu/O0ezt6uQPSfxWFMoxBLBTbI0pHDDiJk+cbroRN WF1K2Bjqtol2gv6ARtQ== X-Received: by 2002:ad4:4eef:0:b0:538:9623:f077 with SMTP id dv15-20020ad44eef000000b005389623f077mr18954777qvb.12.1675908934964; Wed, 08 Feb 2023 18:15:34 -0800 (PST) X-Google-Smtp-Source: AK7set96iCGv73bI5KPeJ1c4PIfPOJhlONvY0myuZW7zgcake/+7MoxAIXsp44Su0KVb277SG7X5Ag== X-Received: by 2002:ad4:4eef:0:b0:538:9623:f077 with SMTP id dv15-20020ad44eef000000b005389623f077mr18954756qvb.12.1675908934727; Wed, 08 Feb 2023 18:15:34 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id e2-20020a37b502000000b00720f61af739sm366005qkf.115.2023.02.08.18.15.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Feb 2023 18:15:34 -0800 (PST) Date: Wed, 8 Feb 2023 21:15:32 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches , Patrick Palka Subject: [PATCH v2] c++: ICE initing lifetime-extended constexpr var [PR107079] Message-ID: References: <20230208210140.391014-1-polacek@redhat.com> <7830a96e-7415-ee72-bbf7-007e5c04b9f1@redhat.com> MIME-Version: 1.0 In-Reply-To: <7830a96e-7415-ee72-bbf7-007e5c04b9f1@redhat.com> User-Agent: Mutt/2.2.9 (2022-11-12) 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=-12.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,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 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? -- >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 -- 2.39.1