https://gcc.gnu.org/g:ce7a22bd26c39137658e973797a419d27d767c2c
commit r16-3045-gce7a22bd26c39137658e973797a419d27d767c2c Author: Jason Merrill <ja...@redhat.com> Date: Tue Jul 22 00:12:12 2025 -0400 c++: improve constexpr type mismatch diagnostic 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. Diff: --- 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 65b91ec41529..b8ac454ff728 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 6a06a6e6f32e..74661994eb1b 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; }