Hi!
__builtin_constexpr_diag works fine with string literals or class arguments
like std::{,u8}string_view or string view like classes, but as the following
testcase shows doesn't work with std::string argument.
I believe the important difference is that std::string has non-trivial
destructor and so needs to be passed by invisible reference while
std::string_view doesn't.
Anyway, the following patch makes it work even with std::string.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2026-03-10 Jakub Jelinek <[email protected]>
PR c++/124404
* constexpr.cc (cxx_eval_constexpr_diag): Call convert_from_reference
on arguments.
* g++.dg/ext/constexpr-diag7.C: New test.
--- gcc/cp/constexpr.cc.jj 2026-03-05 21:43:38.305131069 +0100
+++ gcc/cp/constexpr.cc 2026-03-09 14:01:03.969046239 +0100
@@ -2350,7 +2350,7 @@ cxx_eval_constexpr_diag (const constexpr
tree args[3];
for (int i = 0; i < 3; ++i)
{
- tree arg = CALL_EXPR_ARG (t, i);
+ tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
arg = cxx_eval_constant_expression (ctx, arg,
(i == 0
|| POINTER_TYPE_P (TREE_TYPE (arg)))
--- gcc/testsuite/g++.dg/ext/constexpr-diag7.C.jj 2026-03-09
14:05:20.441687088 +0100
+++ gcc/testsuite/g++.dg/ext/constexpr-diag7.C 2026-03-09 14:05:12.930814747
+0100
@@ -0,0 +1,9 @@
+// PR c++/124404
+// { dg-do compile { target c++26 } }
+
+#include <string>
+
+consteval {
+ std::string msg = "my message";
+ __builtin_constexpr_diag (2, "", msg); // { dg-error "constexpr
message: my message" }
+}
Jakub