https://gcc.gnu.org/g:cb828691fe692f9df002a2e3757a1aec68857e85
commit r15-7127-gcb828691fe692f9df002a2e3757a1aec68857e85 Author: Marek Polacek <pola...@redhat.com> Date: Tue Jan 21 14:48:46 2025 -0500 c++: further tweak to cxx_eval_outermost_constant_expr [PR118396] This patch adds an error in a !allow_non_constant case when the initializer/object types don't match. PR c++/118396 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_outermost_constant_expr): Add an error call when !allow_non_constant. Reviewed-by: Jason Merrill <ja...@redhat.com> Diff: --- gcc/cp/constexpr.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index 9f950ffed749..41ff78bbf2b9 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -9092,11 +9092,19 @@ cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant, return r; else if (non_constant_p && TREE_CONSTANT (r)) r = mark_non_constant (r); - else if (non_constant_p - /* Check we are not trying to return the wrong type. */ - || !same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r))) + else if (non_constant_p) return t; + /* Check we are not trying to return the wrong type. */ + if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r))) + { + /* If so, this is not a constant expression. */ + if (!allow_non_constant) + error ("%qE is not a constant expression because it initializes " + "a %qT rather than %qT", t, TREE_TYPE (t), type); + return t; + } + if (should_unshare) r = unshare_expr (r);