https://gcc.gnu.org/g:99e133ae0b93ca33cae497cf3aeeb19567dc910b
commit r17-6499-g99e133ae0b93ca33cae497cf3aeeb19567dc910b Author: Marek Polacek <[email protected]> Date: Wed Nov 18 14:01:37 2026 -0500 c++/reflection: fixes for eval_reflect_* [PR128855] Some of the eval_reflect_* functions return error_mark_node without setting *non_constant_p. As a result, a variable's initializer ends up holding a value containing error_mark_node, and a later display_string_of on it crashes in dump_data_member_spec. I noticed that these errors can be emitted multiple times, so guard them with cxx_constexpr_quiet_p. Also fix printing the wrong type in reflect_{object,function}. PR c++/128855 gcc/cp/ChangeLog: * reflect.cc (eval_reflect_constant): Set *non_constant_p after giving an error. Guard the error_at call with cxx_constexpr_quiet_p. (eval_reflect_object): Likewise. Print type rather than TREE_TYPE (type) in the error. (eval_reflect_function): Likewise. gcc/testsuite/ChangeLog: * g++.dg/reflect/crash24.C: New test. Reviewed-by: Jason Merrill <[email protected]> Diff: --- gcc/cp/reflect.cc | 14 ++++++++++---- gcc/testsuite/g++.dg/reflect/crash24.C | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 60f1d0b182da..a76d3c4f974e 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -4109,8 +4109,10 @@ eval_reflect_constant (location_t loc, const constexpr_ctx *ctx, tree type, || CP_TYPE_CONST_P (type) || TYPE_REF_P (type)) { - error_at (loc, "%qT must be a cv-unqualified structural type that is " - "not a reference type", type); + if (!cxx_constexpr_quiet_p (ctx)) + error_at (loc, "%qT must be a cv-unqualified structural type that is " + "not a reference type", type); + *non_constant_p = true; return error_mark_node; } expr = convert_reflect_constant_arg (type, convert_from_reference (expr)); @@ -4134,7 +4136,9 @@ eval_reflect_object (location_t loc, const constexpr_ctx *ctx, tree type, if (eval_is_object_type (loc, ctx, type, non_constant_p, fun) != boolean_true_node) { - error_at (loc, "%qT must be an object type", TREE_TYPE (type)); + if (!cxx_constexpr_quiet_p (ctx)) + error_at (loc, "%qT must be an object type", type); + *non_constant_p = true; return error_mark_node; } type = cp_build_reference_type (type, /*rval=*/false); @@ -4161,7 +4165,9 @@ eval_reflect_function (location_t loc, const constexpr_ctx *ctx, tree type, { if (eval_is_function_type (type) != boolean_true_node) { - error_at (loc, "%qT must be a function type", TREE_TYPE (type)); + if (!cxx_constexpr_quiet_p (ctx)) + error_at (loc, "%qT must be a function type", type); + *non_constant_p = true; return error_mark_node; } type = cp_build_reference_type (type, /*rval=*/false); diff --git a/gcc/testsuite/g++.dg/reflect/crash24.C b/gcc/testsuite/g++.dg/reflect/crash24.C new file mode 100644 index 000000000000..de62ab2865c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/crash24.C @@ -0,0 +1,23 @@ +// PR c++/128855 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include <meta> + +struct NS { + constexpr NS () {} +private: + int x = 0; +}; +constexpr NS ns; + +constexpr auto spec + = std::meta::data_member_spec (^^int, { .name = "m", // { dg-error "must be a cv-unqualified structural type" } + .annotations = { ^^ns } }); + +constexpr auto s = std::meta::display_string_of (spec); + +struct A; +consteval { std::meta::define_aggregate (^^A, { spec }); } + +constexpr NS x = std::meta::extract<NS> (^^ns); // { dg-error "must be a cv-unqualified structural type" }
