Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

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                         | 21 +++++++++++++++++----
 gcc/testsuite/g++.dg/cpp26/constexpr-new3.C |  2 +-
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 65b91ec4152..b8ac454ff72 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -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;
            }
diff --git a/gcc/testsuite/g++.dg/cpp26/constexpr-new3.C 
b/gcc/testsuite/g++.dg/cpp26/constexpr-new3.C
index 6a06a6e6f32..74661994eb1 100644
--- a/gcc/testsuite/g++.dg/cpp26/constexpr-new3.C
+++ b/gcc/testsuite/g++.dg/cpp26/constexpr-new3.C
@@ -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;
 }

base-commit: 79e4f386ad836b7f9e1bdaf6aded4585035432ef
-- 
2.50.1

Reply via email to