]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: improve constexpr type mismatch diagnostic
authorJason Merrill <jason@redhat.com>
Tue, 22 Jul 2025 04:12:12 +0000 (00:12 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 6 Aug 2025 13:21:01 +0000 (06:21 -0700)
This diagnostic failed to specify the actual type of the object being
accessed through a glvalue of an incompatible type, and could also use to
indicate where that object comes from.

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_indirect_ref): Improve diagnostic.

gcc/testsuite/ChangeLog:

* g++.dg/cpp26/constexpr-new3.C: Tweak diagnostic.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp26/constexpr-new3.C

index 65b91ec41529f2d3098b8b92095391bb3c2ef83a..b8ac454ff728a40da23055eb93f02469af703c46 100644 (file)
@@ -7179,10 +7179,23 @@ cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
                          (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
              /* DR 1188 says we don't have to deal with this.  */
              if (!ctx->quiet)
-               error_at (cp_expr_loc_or_input_loc (t),
-                         "accessing value of %qE through a %qT glvalue in a "
-                         "constant expression", build_fold_indirect_ref (sub),
-                         TREE_TYPE (t));
+               {
+                 auto_diagnostic_group d;
+                 error_at (cp_expr_loc_or_input_loc (t),
+                           "accessing value of %qT object through a %qT "
+                           "glvalue in a constant expression",
+                           TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
+                 tree ob = build_fold_indirect_ref (sub);
+                 if (DECL_P (ob))
+                   {
+                     if (DECL_ARTIFICIAL (ob))
+                       inform (DECL_SOURCE_LOCATION (ob),
+                               "%qT object created here", TREE_TYPE (ob));
+                     else
+                       inform (DECL_SOURCE_LOCATION (ob),
+                               "%q#D declared here", ob);
+                   }
+               }
              *non_constant_p = true;
              return t;
            }
index 6a06a6e6f32e5dc8cf4ee3d7ae4b43140067035f..74661994eb1bd7eb8141e35645ca8403888b6e7e 100644 (file)
@@ -37,7 +37,7 @@ baz ()
 {
   std::allocator<int> a;
   auto b = a.allocate (2);
-  new (b) long (42);           // { dg-error "accessing value of 'heap ' through a 'long int' glvalue in a constant expression" }
+  new (b) long (42);           // { dg-error "accessing value of 'int [2]' object through a 'long int' glvalue in a constant expression" }
   a.deallocate (b, 2);
   return true;
 }