On 7/3/26 3:10 AM, Jakub Jelinek wrote:
On Thu, Jul 02, 2026 at 01:33:15PM -0400, Jason Merrill wrote:
2026-07-02 Jakub Jelinek <[email protected]>
PR c++/126036
* reflect.cc (get_range_elts): Avoid calling finish_compound_literal
when processing_template_decl, instead set *non_constant_p and
return NULL_TREE.
This change is OK.
(check_metafn_return_type): Likewise.
It seems a bit odd to treat this case as a wrong return type; let's either
check METAFN_KIND_RET_VECTOR_INFO in process_metafunction or expand the
comment to justify doing it here.
So like this instead?
OK.
2026-07-03 Jakub Jelinek <[email protected]>
PR c++/126036
* reflect.cc (get_range_elts): Avoid calling finish_compound_literal
when processing_template_decl, instead set *non_constant_p and
return NULL_TREE.
(process_metafunction): Likewise.
* g++.dg/reflect/pr126036.C: New test.
--- gcc/cp/reflect.cc.jj 2026-06-30 09:20:44.103805523 +0200
+++ gcc/cp/reflect.cc 2026-07-02 23:14:03.192962977 +0200
@@ -698,6 +698,12 @@ get_range_elts (location_t loc, const co
}
if (kind == REFLECT_CONSTANT_ARRAY && sz == 0)
{
+ /* Don't call finish_compound_literal in a template. */
+ if (processing_template_decl)
+ {
+ *non_constant_p = true;
+ return NULL_TREE;
+ }
/* Return std::array <valuet, 0> {}. */
tree args = make_tree_vec (2);
TREE_VEC_ELT (args, 0) = valuet;
@@ -7887,6 +7893,14 @@ process_metafunction (const constexpr_ct
if (check_metafn_return_type (loc, METAFN_KIND_RET (minfo), rettype,
non_constant_p))
return NULL_TREE;
+ /* Don't call get_vector_of_info_elts (for any metafn which returns
+ vector<info>) in a template. */
+ if (processing_template_decl
+ && METAFN_KIND_RET (minfo) == METAFN_KIND_RET_VECTOR_INFO)
+ {
+ *non_constant_p = true;
+ return NULL_TREE;
+ }
for (int argno = 0; argno < 3; ++argno)
switch (METAFN_KIND_ARG (minfo, argno))
{
--- gcc/testsuite/g++.dg/reflect/pr126036.C.jj 2026-06-30 15:41:35.653488520
+0200
+++ gcc/testsuite/g++.dg/reflect/pr126036.C 2026-06-30 15:41:15.899727864
+0200
@@ -0,0 +1,21 @@
+// PR c++/126036
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+
+namespace A { int a; };
+
+template <int N>
+void
+foo ()
+{
+ constexpr auto m = std::meta::members_of (^^A,
std::meta::access_context::unchecked ());
+ // { dg-error "is not a constant expression because it refers to a result of 'operator
new'" "" { target *-*-* } .-1 }
+}
+
+void
+bar ()
+{
+ foo <42> ();
+}
Jakub