On Tue, Jan 21, 2025 at 11:00:13AM -0500, Jason Merrill wrote: > On 1/21/25 9:54 AM, Jason Merrill wrote: > > On 1/20/25 5:58 PM, Marek Polacek wrote: > > > @@ -9087,7 +9092,9 @@ 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) > > > + 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))) > > > return t; > > Actually, I guess we also want to give an error if !allow_non_constant.
Like so? (It would not have triggered before pre-r15-7103 because there we're called from maybe_constant_init, thus allow_non_constant=true.) Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- 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. --- 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 9f950ffed74..85d469b055e 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 " + "the wrong object", t); + return t; + } + if (should_unshare) r = unshare_expr (r); base-commit: 16d778239397b2f70a1e0680c0b82ae6ee98fe9e -- 2.48.1