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.129.124]) by sourceware.org (Postfix) with ESMTPS id 7CB8F385AF92 for ; Fri, 14 Jul 2023 17:07:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7CB8F385AF92 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=1689354458; 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=jJM7wsKRJ34K3b0RtSfDeReK9N1xSkg+UzffGz/0gIs=; b=E0DkvaKbbkdG0CrKrqFv/+vH5on7FK0kxdl2H9dMb//4G2JWINtdpTags/o7IQEcv+3t+V gBpeYMEWZlyEq2gMWmmsCBkomhjqgGVrRgOCE7r0pDAazBBLH0Fu+ScEBLv/QLlFqw+94D 9t5HR1sy9o2wy/Uw8DnqylnZL1E6A20= 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_256_GCM_SHA384) id us-mta-583-7K-hBgmHNTuO5fm5jfAO_Q-1; Fri, 14 Jul 2023 13:07:36 -0400 X-MC-Unique: 7K-hBgmHNTuO5fm5jfAO_Q-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-403b3f31382so19124471cf.0 for ; Fri, 14 Jul 2023 10:07:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689354456; x=1691946456; 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=jJM7wsKRJ34K3b0RtSfDeReK9N1xSkg+UzffGz/0gIs=; b=DDog1JP4APka4xz9rZQhFPshWXCAwwaGh+MtIY8/9F2wZfhoDZGrL6XTI6WT3JNnmp 7s28w3XLQhe+QMkxXJGiga9SehYwDgs4CNcovgwnYeezQvE72mruaLOZxbrE6BYsJK41 lzfVVgcM9K7/ocuOlilTO0asUKALKbufwVxQk/136/uA+QJi5l92mKXGi9lr9cMdaMCx rGFjfteVpIPiq7iFKkU8uZ5M0a3DNa5aDArNh7AE3WnyyFcP5vPcyt2SZjDQ5LW8LuCz W3+eNvUIq7LNIrLDCvM2tuZe/VfoY/PCGLSBU5i6IrDg9rfh7oWBa1VQ8C4Atr9+JzoO Tnkg== X-Gm-Message-State: ABy/qLa3CxAx1mfUb8gGQ6QOLy9WDjQwNWvQYKyrSM4NC6em4bvGAKwH 5eHeUO3kv2MHR6KKK744osaxJ+OMOpp/Ju1jujU0inVASLoVsvJ4uHHMGByi+OGnexLS96SEFk4 BnFPQ/WBc1+LytiLm+w== X-Received: by 2002:ac8:5f0a:0:b0:403:2595:bae0 with SMTP id x10-20020ac85f0a000000b004032595bae0mr5447579qta.15.1689354456200; Fri, 14 Jul 2023 10:07:36 -0700 (PDT) X-Google-Smtp-Source: APBJJlG08wE10/Gsw2O3HwNvFRf5KSlM8tyyR6343TL/Hr9mhsAknnfdcbwXbMlZtML861DPuDdQhQ== X-Received: by 2002:ac8:5f0a:0:b0:403:2595:bae0 with SMTP id x10-20020ac85f0a000000b004032595bae0mr5447545qta.15.1689354455726; Fri, 14 Jul 2023 10:07:35 -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 b15-20020ac801cf000000b004037cd9bd40sm4102228qtg.15.2023.07.14.10.07.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 14 Jul 2023 10:07:33 -0700 (PDT) Message-ID: Date: Fri, 14 Jul 2023 13:07:32 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v2] c++: wrong error with static constexpr var in tmpl [PR109876] To: Marek Polacek Cc: GCC Patches References: <20230526231805.406578-1-polacek@redhat.com> <460bf6aa-a336-7ac2-0434-c8dcfd8d0b87@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.7 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,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 7/13/23 14:54, Marek Polacek wrote: > On Fri, May 26, 2023 at 09:47:10PM -0400, Jason Merrill wrote: >> On 5/26/23 19:18, Marek Polacek wrote: >>> The is_really_empty_class check is sort of non-obvious but the >>> comment should explain why I added it. >>> >>> + /* 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. */ > > Sorry it's taken so long to get back to this. > >> Interesting. Can we change that (i.e. mark it TREE_CONSTANT) rather than >> work around it here? > > I think we can. Maybe as in the below: > > -- >8 -- > 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. > > We weren't marking objects whose type is an empty class type > constant. This patch changes that so that v_d_e_p doesn't need > to check is_really_empty_class. > > Co-authored-by: Jason Merrill > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? OK, thanks. Incidentally, I prefer to put the "ok?" line above the scissors line since it isn't intended to be part of the commit message. > PR c++/109876 > > gcc/cp/ChangeLog: > > * decl.cc (cp_finish_decl): Set TREE_CONSTANT when initializing > an object of empty class type. > * 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. > * g++.dg/cpp1z/constexpr-template2.C: New test. > --- > gcc/cp/decl.cc | 13 +++++-- > gcc/cp/pt.cc | 7 ++++ > .../g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++ > .../g++.dg/cpp1z/constexpr-template1.C | 25 ++++++++++++ > .../g++.dg/cpp1z/constexpr-template2.C | 25 ++++++++++++ > 5 files changed, 105 insertions(+), 3 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C > create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C > create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > index 60f107d50c4..792ab330dd0 100644 > --- a/gcc/cp/decl.cc > +++ b/gcc/cp/decl.cc > @@ -8200,7 +8200,6 @@ void > cp_finish_decl (tree decl, tree init, bool init_const_expr_p, > tree asmspec_tree, int flags) > { > - tree type; > vec *cleanups = NULL; > const char *asmspec = NULL; > int was_readonly = 0; > @@ -8220,7 +8219,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, > /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ > gcc_assert (TREE_CODE (decl) != PARM_DECL); > > - type = TREE_TYPE (decl); > + tree type = TREE_TYPE (decl); > if (type == error_mark_node) > return; > > @@ -8410,7 +8409,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, > if (decl_maybe_constant_var_p (decl) > /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ > && !TYPE_REF_P (type)) > - TREE_CONSTANT (decl) = 1; > + TREE_CONSTANT (decl) = true; > } > /* This is handled mostly by gimplify.cc, but we have to deal with > not warning about int x = x; as it is a GCC extension to turn off > @@ -8421,6 +8420,14 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, > && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)) > suppress_warning (decl, OPT_Winit_self); > } > + else if (VAR_P (decl) > + && COMPLETE_TYPE_P (type) > + && !TYPE_REF_P (type) > + && !dependent_type_p (type) > + && is_really_empty_class (type, /*ignore_vptr*/false)) > + /* We have no initializer but there's nothing to initialize anyway. > + Treat DECL as constant due to c++/109876. */ > + TREE_CONSTANT (decl) = true; > > if (flag_openmp > && TREE_CODE (decl) == FUNCTION_DECL > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index fa15b75b9c5..255d18b9539 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -27983,6 +27983,13 @@ 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)) > + 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>(); > +} > diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C > new file mode 100644 > index 00000000000..5a9a96ba60a > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.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: 834f34947b20b18696487fc5d2bccab5ea720351