In C++23, since P2448, a constexpr function F that calls a non-constexpr
function N is OK as long as we don't actually call F in a constexpr
context. So instead of giving an error in maybe_save_constexpr_fundef,
we only give an error when evaluating the call. Unfortunately, as shown
in this PR, the diagnostic can be truncated:
z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because:
10 | constexpr Jam() { ft(); }
| ^~~
...because what? With this patch, we say:
z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because:
10 | constexpr Jam() { ft(); }
| ^~~
z.C:10:23: error: call to non-'constexpr' function 'int Jam::ft()'
10 | constexpr Jam() { ft(); }
| ~~^~
z.C:8:7: note: 'int Jam::ft()' declared here
8 | int ft() { return 42; }
| ^~
Like maybe_save_constexpr_fundef, explain_invalid_constexpr_fn should
also check the body of a constructor, not just the mem-initializer.
PR c++/111272
gcc/cp/ChangeLog:
* constexpr.cc (explain_invalid_constexpr_fn): Also check the body of
a constructor in C++14 and up.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-diag1.C: New test.
body = massage_constexpr_body (fun, body);
require_potential_rvalue_constant_expression (body);
if (DECL_CONSTRUCTOR_P (fun))
- cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
+ {
+ cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
+ if (cxx_dialect > cxx11)
+ {
+ /* Also check the body, not just the ctor-initializer. */
+ body = DECL_SAVED_TREE (fun);
+ require_potential_rvalue_constant_expression (body);
+ }
+ }
}
}
}
--- /dev/null
+// PR c++/111272
+// { dg-do compile { target c++14 } }
+// { dg-options "-Werror=invalid-constexpr" }
+// { dg-prune-output "some warnings being treated as errors" }
+
+struct Jam
+{
+ // constexpr // n.b.
+ int ft() { return 42; } // { dg-message "declared here" }
+
+ constexpr Jam() { ft(); } // { dg-error "call to non-.constexpr. function" }
+// { dg-message "declared here" "" { target c++20_down } .-1 }
+};
+
+constexpr bool test()
+{
+ Jam j; // { dg-error "called in a constant expression" }
+ return true;
+}
+
+static_assert(test(), ""); // { dg-error "non-constant condition" }