Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
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.
---
gcc/cp/reflect.cc | 14 ++++++++++----
gcc/testsuite/g++.dg/reflect/crash24.C | 23 +++++++++++++++++++++++
2 files changed, 33 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/reflect/crash24.C
diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index a7cb236f9b1..92b8e98a135 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -4108,8 +4108,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));
@@ -4133,7 +4135,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);
@@ -4160,7 +4164,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 00000000000..de62ab2865c
--- /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" }
base-commit: ded50ad7dcf6297a95dcc0a2b59ef31f25d3b68c
--
2.56.1