]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/reflection: wrong error with const meta::info parm [PR123614]
authorMarek Polacek <polacek@redhat.com>
Sun, 1 Feb 2026 20:20:55 +0000 (15:20 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 2 Feb 2026 14:28:13 +0000 (09:28 -0500)
Here we issue a bogus "not a constant expression" error ultimately
because get_info returns NULL_TREE for the const parameter in Name.
The cxx_eval_constant_expression in get_info produces a NOP_EXPR:

  (info) reflect_expr <int>

which is not REFLECT_EXPR_P.  This isn't caught by the
REFLECTION_TYPE_P && REFLECT_EXPR_P check in _eval_constant_expression
because OP is a NOP_EXPR.  The NOP_EXPR comes from adjust_temp_type.

I suppose I could just add STRIP_NOPS to get_info.  Or I could follow
c++/65695 / r6-41-gfb899e32c16088 and adjust cp_fold_convert to fold
away the conversion.

PR c++/123614

gcc/cp/ChangeLog:

* cvt.cc (cp_fold_convert): Avoid wrapping a REFLECT_EXPR in NOP_EXPR.

gcc/testsuite/ChangeLog:

* g++.dg/reflect/parm5.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cvt.cc
gcc/testsuite/g++.dg/reflect/parm5.C [new file with mode: 0644]

index a984611ea8e2ac061b7c0369dc7af3eb87e51641..fe9b9dc6dd18e56da44b44c42a3fe30ab9b874c1 100644 (file)
@@ -622,11 +622,12 @@ cp_fold_convert (tree type, tree expr)
   tree conv;
   if (TREE_TYPE (expr) == type)
     conv = expr;
-  else if (TREE_CODE (expr) == PTRMEM_CST
-          && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
-                          PTRMEM_CST_CLASS (expr)))
+  else if ((TREE_CODE (expr) == PTRMEM_CST
+           && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
+                           PTRMEM_CST_CLASS (expr)))
+           || (REFLECT_EXPR_P (expr) && REFLECTION_TYPE_P (type)))
     {
-      /* Avoid wrapping a PTRMEM_CST in NOP_EXPR.  */
+      /* Avoid wrapping a PTRMEM_CST/REFLECT_EXPR in NOP_EXPR.  */
       conv = copy_node (expr);
       TREE_TYPE (conv) = type;
     }
diff --git a/gcc/testsuite/g++.dg/reflect/parm5.C b/gcc/testsuite/g++.dg/reflect/parm5.C
new file mode 100644 (file)
index 0000000..0034b84
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/123614
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+
+template<class, class> struct same_type;
+template<class T> struct same_type<T, T> {};
+
+consteval auto Name(const std::meta::info meta){
+    same_type<decltype(meta), const std::meta::info>();
+    return std::meta::display_string_of(meta);
+}
+
+int main() {
+  auto sv = std::define_static_string(Name(^^int));
+}