diff options
author | Jason Merrill <jason@redhat.com> | 2025-07-22 00:12:12 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2025-08-06 06:21:01 -0700 |
commit | ce7a22bd26c39137658e973797a419d27d767c2c (patch) | |
tree | 2304ca9b9adf230fdd1aabd847289e912fed486c | |
parent | 79e4f386ad836b7f9e1bdaf6aded4585035432ef (diff) | |
download | gcc-ce7a22bd26c39137658e973797a419d27d767c2c.zip gcc-ce7a22bd26c39137658e973797a419d27d767c2c.tar.gz gcc-ce7a22bd26c39137658e973797a419d27d767c2c.tar.bz2 |
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.
-rw-r--r-- | gcc/cp/constexpr.cc | 21 | ||||
-rw-r--r-- | 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 65b91ec..b8ac454 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 6a06a6e..7466199 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; } |