https://gcc.gnu.org/g:8b3e2e68208730dc95a7803a21a545e93c735d5b

commit r17-1627-g8b3e2e68208730dc95a7803a21a545e93c735d5b
Author: Marek Polacek <[email protected]>
Date:   Thu Jun 11 12:18:36 2026 -0400

    c++/reflection: tweak size_of diagnostic [PR125492]
    
    Here we say "reflection with incomplete type" when size_of
    got a reflection of a function.  Let's clarify that diagnostic.
    
            PR c++/125492
    
    gcc/cp/ChangeLog:
    
            * reflect.cc (eval_size_of): Add a separate throw_exception for
            function types.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/reflect/size_of2.C: New test.
    
    Reviewed-by: Patrick Palka <[email protected]>

Diff:
---
 gcc/cp/reflect.cc                       | 14 +++++++++-----
 gcc/testsuite/g++.dg/reflect/size_of2.C | 12 ++++++++++++
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index 2c2a59722227..c773c518120a 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -3324,11 +3324,15 @@ eval_size_of (location_t loc, const constexpr_ctx *ctx, 
tree r,
   else
     type = type_of (r, kind);
   tree ret;
-  if (!complete_type_or_maybe_complain (type, NULL_TREE, tf_none)
-      /* No special casing of references needed, c_sizeof_or_alignof_type
-        returns the same size for POINTER_TYPE and REFERENCE_TYPE.  */
-      || ((ret = c_sizeof_or_alignof_type (loc, type, true, false, 0))
-         == error_mark_node))
+  if (FUNC_OR_METHOD_TYPE_P (type))
+    return throw_exception (loc, ctx,
+                           "reflection of function type in size_of",
+                           fun, non_constant_p, jump_target);
+  else if (!complete_type_or_maybe_complain (type, NULL_TREE, tf_none)
+          /* No special casing of references needed, c_sizeof_or_alignof_type
+             returns the same size for POINTER_TYPE and REFERENCE_TYPE.  */
+          || ((ret = c_sizeof_or_alignof_type (loc, type, true, false, 0))
+              == error_mark_node))
     return throw_exception (loc, ctx,
                            "reflection with incomplete type in size_of",
                            fun, non_constant_p, jump_target);
diff --git a/gcc/testsuite/g++.dg/reflect/size_of2.C 
b/gcc/testsuite/g++.dg/reflect/size_of2.C
new file mode 100644
index 000000000000..d53a4d00b26d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/size_of2.C
@@ -0,0 +1,12 @@
+// PR c++/125492
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+
+struct S {
+  void memfn ();
+};
+
+constexpr auto a = std::meta::size_of(^^void()); // { dg-error "uncaught 
exception of type 'std::meta::exception'; 'what\\\(\\\)': 'reflection of 
function type in size_of'" }
+constexpr auto b = std::meta::size_of(type_of(^^S::memfn)); // { dg-error 
"uncaught exception of type 'std::meta::exception'; 'what\\\(\\\)': 'reflection 
of function type in size_of'" }

Reply via email to