diff options
author | Jason Merrill <jason@redhat.com> | 2025-02-15 10:48:17 +0100 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2025-02-15 13:27:17 +0100 |
commit | de66529f2e7bb24fb2b61b82e6a953f3f6c12902 (patch) | |
tree | 9a9da9f53e5616af68bed38408020f6452fab10d /gcc | |
parent | bf84e5e64662f8f0fdebfc0212e32bfca678f9eb (diff) | |
download | gcc-de66529f2e7bb24fb2b61b82e6a953f3f6c12902.zip gcc-de66529f2e7bb24fb2b61b82e6a953f3f6c12902.tar.gz gcc-de66529f2e7bb24fb2b61b82e6a953f3f6c12902.tar.bz2 |
c++: NRVO, constexpr, lambda [PR118053]
Here during constant evaluation we encounter a VAR_DECL with DECL_VALUE_EXPR
of the RESULT_DECL, where the latter has been adjusted for
pass-by-invisible-reference. We already had the code to deal with this, we
just need to use it in the non-capture case of DECL_VALUE_EXPR as well.
PR c++/118053
gcc/cp/ChangeLog:
* constexpr.cc (cxx_eval_constant_expression): Generalize
DECL_VALUE_EXPR invisiref handling.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-lambda1.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/constexpr.cc | 19 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C | 20 |
2 files changed, 30 insertions, 9 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 299b134..59dd066 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -7683,18 +7683,19 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t, definition, so don't try to look at the closure. But if the captured variable is constant, try to evaluate it directly. */ r = DECL_CAPTURED_VARIABLE (t); - tree type = TREE_TYPE (t); - if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) - { - /* Adjust r to match the reference-ness of t. */ - if (TYPE_REF_P (type)) - r = build_address (r); - else - r = convert_from_reference (r); - } } else r = DECL_VALUE_EXPR (t); + + tree type = TREE_TYPE (t); + if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r))) + { + /* Adjust r to match the reference-ness of t. */ + if (TYPE_REF_P (type)) + r = build_address (r); + else + r = convert_from_reference (r); + } return cxx_eval_constant_expression (ctx, r, lval, non_constant_p, overflow_p); } diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C new file mode 100644 index 0000000..68152e2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-lambda1.C @@ -0,0 +1,20 @@ +// PR c++/118053 +// { dg-do compile { target c++14 } } + +template <typename _Tp> struct vector { + _Tp * _M_finish; + vector(_Tp); + vector(const vector &); + constexpr auto back() { return *_M_finish; } +}; +template <typename Funct> void +run(Funct funct) { funct(1); } + +vector<int> +runner() try { + vector<int> vec{1}; + run([&](auto) { vec.back(); }); + return vec; +} catch (...) { + return 1; +} |