From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id BFCD93858CDA; Tue, 12 Dec 2023 00:37:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFCD93858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1702341463; bh=FmOIXUkp+rIwOrpDf+aB5ITJIBklZCaNNlT3TdiRSic=; h=From:To:Subject:Date:From; b=msgGDzTewtstyvJ0x8/Oz1iwrYXrmTF3bMKhm4KCbwm5uHWZgX+oOLsYOCjPAdFBy CmP2nOxKE6BY8btAIyObG8cbjj+AVbY4rbODvojdwPnoFniTVf4yCQ4hFs/qCN/2W8 fp3Qd3hssyIPA8Bv8supsQMHamRyzRLAAcKRUDmg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-8146] c++: wrong error with static constexpr var in tmpl [PR109876] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 7308b4d57f08c8fc9c6cb877d0cf2657b9b4805f X-Git-Newrev: 08f4496aa619f9b0e8dbb459452dd96edb870236 Message-Id: <20231212003743.BFCD93858CDA@sourceware.org> Date: Tue, 12 Dec 2023 00:37:43 +0000 (GMT) List-Id: https://gcc.gnu.org/g:08f4496aa619f9b0e8dbb459452dd96edb870236 commit r13-8146-g08f4496aa619f9b0e8dbb459452dd96edb870236 Author: Marek Polacek Date: Thu May 25 18:54:18 2023 -0400 c++: wrong error with static constexpr var in tmpl [PR109876] 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 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. (cherry picked from commit b5138df96a93d3b5070c88b8617eabd38cb24ab6) Diff: --- gcc/cp/decl.cc | 13 ++++++-- gcc/cp/pt.cc | 7 +++++ gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C | 25 +++++++++++++++ gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C | 25 +++++++++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 30d89bba9e8..bb4f2fb8208 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8216,7 +8216,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; @@ -8236,7 +8235,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; @@ -8426,7 +8425,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 @@ -8437,6 +8436,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 761df45f274..f056a4f38e6 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -27989,6 +27989,13 @@ value_dependent_expression_p (tree expression) && value_expr == error_mark_node)) 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>(); +}