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 2CCFC3858401 for ; Sat, 27 May 2023 01:47:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CCFC3858401 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=1685152034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JNXurKuIbz+wLhJ02A0UVl9xc4SyNTpefXfWWCCbjTc=; b=fuhboO4bYAdVMyIFiv0E9yzkKQlHEFcxeNyVYjCEGjYFdD32uVczSsYSTMmsQ3hogKC+8J OehrMtxkI4clrfvDysSgdFS2IsTRYnWPZtvsBOZlJv3DUwvEctEHyEFphst1m97woJlhjW PP9dPM7wy1ZLEnv8YM0iN5xw+aAsMD8= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-28-Y_r_3cTlMNiKoLnmfqfeFg-1; Fri, 26 May 2023 21:47:13 -0400 X-MC-Unique: Y_r_3cTlMNiKoLnmfqfeFg-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-62609400a3bso18855546d6.2 for ; Fri, 26 May 2023 18:47:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685152033; x=1687744033; h=content-transfer-encoding:in-reply-to:from:references: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=JNXurKuIbz+wLhJ02A0UVl9xc4SyNTpefXfWWCCbjTc=; b=Z4ohvUF/0fFjkEtQ5BiAf15PINTENBsXYM3SM9bZp+iMPGqdK2dNqRsSw73vuF9zGc HB2+EtELLTl4a407x1ibW7uILvt9kqkt24/nZ+3ZG0OfVOoW9gc+BBGUV0yB5PHcVxBh IXhONq/pBM/YGwi5C3fxYkihF3orWKLoRxQDOrdRwrglXIdF5sCqZe5rjHhHPFhInQUm avLuSQpPqiG3jbGxET7v0+4awvFsOaiIAC7Y/3s1cFKk61fFZeShcofZapYoVTTBfj+H 3BcY8zlgSqP6GWw79oRt3PYp6z0faV6nIpYmm+B2Cvh5Gx+SqDA49rogXRwGA6Xv2I6H 00/A== X-Gm-Message-State: AC+VfDzvDJxB6EDLkW0tA7DFzsh4HgoU04IZfEEeucl2Oh/brWsQxbb4 UuSsOtNFHZlrRR4D0LXZRGXXwxnPHmN+/e2rq4xVMfbCA6R+kixUryOVHp1be22uTQTI8PYz0W+ w8lYswGleaTBqkj4AUQ== X-Received: by 2002:a05:6214:300e:b0:61b:201b:1700 with SMTP id ke14-20020a056214300e00b0061b201b1700mr3796155qvb.38.1685152032750; Fri, 26 May 2023 18:47:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5F3P3LjjgagvPkekCeE7ilsJ/CCvoZmY6HATyA6fhE3AXyYGHJK+hgDxCBaJ9OCPSb4+ArZQ== X-Received: by 2002:a05:6214:300e:b0:61b:201b:1700 with SMTP id ke14-20020a056214300e00b0061b201b1700mr3796147qvb.38.1685152032476; Fri, 26 May 2023 18:47:12 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id mz17-20020a0562142d1100b0061b7f6b5e15sm1598175qvb.140.2023.05.26.18.47.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 26 May 2023 18:47:11 -0700 (PDT) Message-ID: <460bf6aa-a336-7ac2-0434-c8dcfd8d0b87@redhat.com> Date: Fri, 26 May 2023 21:47:10 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH] c++: wrong error with static constexpr var in tmpl [PR109876] To: Marek Polacek , GCC Patches References: <20230526231805.406578-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20230526231805.406578-1-polacek@redhat.com> 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.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 5/26/23 19:18, Marek Polacek wrote: > Since r8-509, we'll no longer create a static temporary var for > the initializer '{ 1, 2 }' for num in the attached test because > the code in finish_compound_literal is now guarded by > '&& fcl_context == fcl_c99' but it's fcl_functional here. This > causes us to reject num as non-constant when evaluating it in > a template. > > Jason's idea was to treat num as value-dependent even though it > actually isn't. This patch implements that suggestion. > > The is_really_empty_class check is sort of non-obvious but the > comment should explain why I added it. > > Co-authored-by: Jason Merrill > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/109876 > > gcc/cp/ChangeLog: > > * pt.cc (value_dependent_expression_p) : Treat a > constexpr-declared non-constant variable as value-dependent. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/constexpr-template12.C: New test. > * g++.dg/cpp1z/constexpr-template1.C: New test. > --- > gcc/cp/pt.cc | 12 ++++++ > .../g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++ > .../g++.dg/cpp1z/constexpr-template1.C | 25 ++++++++++++ > 3 files changed, 75 insertions(+) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C > create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 7fb3e75bceb..38fd8070705 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -27969,6 +27969,18 @@ value_dependent_expression_p (tree expression) > else if (TYPE_REF_P (TREE_TYPE (expression))) > /* FIXME cp_finish_decl doesn't fold reference initializers. */ > return true; > + /* We have a constexpr variable and we're processing a template. When > + there's lifetime extension involved (for which finish_compound_literal > + used to create a temporary), we'll not be able to evaluate the > + variable until instantiating, so pretend it's value-dependent. */ > + else if (DECL_DECLARED_CONSTEXPR_P (expression) > + && !TREE_CONSTANT (expression) > + /* When there's nothing to initialize, we'll never mark the > + VAR_DECL TREE_CONSTANT, therefore it would remain > + value-dependent and we wouldn't instantiate. */ Interesting. Can we change that (i.e. mark it TREE_CONSTANT) rather than work around it here? > + && !is_really_empty_class (TREE_TYPE (expression), > + /*ignore_vptr*/false)) > + return true; > return false; > > case DYNAMIC_CAST_EXPR: > diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C > new file mode 100644 > index 00000000000..a9e065320c8 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C > @@ -0,0 +1,38 @@ > +// PR c++/109876 > +// { dg-do compile { target c++11 } } > + > +using size_t = decltype(sizeof 0); > + > +namespace std { > +template struct initializer_list { > + const int *_M_array; > + size_t _M_len; > + constexpr size_t size() const { return _M_len; } > +}; > +} // namespace std > + > +constexpr std::initializer_list gnum{2}; > + > +template struct Array {}; > +template void g() > +{ > + static constexpr std::initializer_list num{2}; > + static_assert(num.size(), ""); > + Array ctx; > + > + constexpr Array<1> num1{}; > +} > + > +template > +struct Foo > +{ > + static constexpr std::initializer_list num = { 1, 2 }; > + static_assert(num.size(), ""); > + Array ctx; > +}; > + > +void > +f (Foo<5>) > +{ > + g<0>(); > +} > diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C > new file mode 100644 > index 00000000000..58be046fd36 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C > @@ -0,0 +1,25 @@ > +// PR c++/109876 > +// { dg-do compile { target c++17 } } > + > +struct Foo {}; > +template struct X {}; > + > +void g() > +{ > + static constexpr Foo foo; > + X x; > +} > + > +template > +void f() > +{ > + static constexpr Foo foo; > + X x; > +} > + > +void > +h () > +{ > + f<0>(); > + f<1>(); > +} > > base-commit: 8d6bd830f5f9c939e8565c0341a0c6c588834484