]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: further tweak to cxx_eval_outermost_constant_expr [PR118396]
authorMarek Polacek <polacek@redhat.com>
Tue, 21 Jan 2025 19:48:46 +0000 (14:48 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 22 Jan 2025 15:26:46 +0000 (10:26 -0500)
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 <jason@redhat.com>
gcc/cp/constexpr.cc

index 9f950ffed749223ee9a0f52e078f3bf54ae38c93..41ff78bbf2b9b399a31b7bebe4a8be021a7242e1 100644 (file)
@@ -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);