Hi!

The following testcase ICEs, because potential_constant_expression_1 handles
some forms of CAST_EXPR, but cxx_eval_constant_expression doesn't.
Normally, if we have e.g. a non-dependent compound literal in a template,
finish_compound_literal will create a CAST_EXPR, but
fold_non_dependent_expr_template -> instantiate_non_dependent_expr_internal
will fold that away, so constexpr.cc evaluation doesn't see it.
reflect.cc calls finish_compound_literal in 3 spots, one is to create
std::array <type, 0> {}, another one is when creating the
std::meta::exception object to throw and the last one when creating
std::vector <std::meta::info> object to return from various metafns.
If that is done with processing_template_decl, finish_compound_literal
will again return a CAST_EXPR, but unlike the usual case it is during
constant evaluation and so instantiate_non_dependent_expr_internal
will not be invoked on it to clean that up.

Now, in the get_meta_exception_object case, we already have
  /* Don't throw in a template.  */
  if (processing_template_decl)
    {
      *non_constant_p = true;
      return NULL_TREE;
    }
This patch uses the same thing (i.e. avoid folding even non-dependent
metafn calls that would need finish_compound_literal during
processing_template_decl) to fix this.
I think in both cases it is fine to defer the constant evaluation,
the return type from those metafns is not dependent (std::meta::info
in the reflect_constant_array case, std::vector <std::meta::info>
otherwise).

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.
        (check_metafn_return_type): 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-06-30 15:29:36.321236081 +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;
@@ -7834,6 +7840,12 @@ check_metafn_return_type (location_t loc
     case METAFN_KIND_RET_VECTOR_INFO:
       if (!CLASS_TYPE_P (type))
        expected_str = "std::vector<std::meta::info>";
+      /* Don't call get_vector_of_info_elts in a template.  */
+      else if (processing_template_decl)
+       {
+         *non_constant_p = true;
+         return true;
+       }
       break;
     case METAFN_KIND_RET_ACCESS_CONTEXT:
       if (!is_std_meta_class (type, "access_context"))
--- 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

Reply via email to