From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 4E9353857811; Tue, 31 Aug 2021 16:17:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E9353857811 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-3258] c++: Improve error recovery with constexpr [PR92193] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/master X-Git-Oldrev: 18f0e57b9a2f1b108831fcfb25cbcc4e2de65e8e X-Git-Newrev: 9aeadd8c319d5d940fa4dc91a393fc2959d27719 Message-Id: <20210831161744.4E9353857811@sourceware.org> Date: Tue, 31 Aug 2021 16:17:44 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 31 Aug 2021 16:17:44 -0000 https://gcc.gnu.org/g:9aeadd8c319d5d940fa4dc91a393fc2959d27719 commit r12-3258-g9aeadd8c319d5d940fa4dc91a393fc2959d27719 Author: Jason Merrill Date: Mon Aug 30 18:42:05 2021 -0400 c++: Improve error recovery with constexpr [PR92193] The compiler tries to limit error cascades in limit_bad_template_recursion by avoiding triggering a new instantiation from one that has caused errors. We were exempting constexpr functions from this because they can be needed for constant evaluation, but as more and more functions get marked constexpr, this becomes an over-broad category. So as suggested on IRC, this patch only exempts functions that are needed for mandatory constant evaluation. As noted in the comment, this flag doesn't particularly need to use a bit in the FUNCTION_DECL, but there were still some free. PR c++/92193 gcc/cp/ChangeLog: * cp-tree.h (FNDECL_MANIFESTLY_CONST_EVALUATED): New. * constexpr.c (cxx_eval_call_expression): Set it. * pt.c (neglectable_inst_p): Check it. gcc/testsuite/ChangeLog: * g++.dg/diagnostic/static_assert4.C: New test. Diff: --- gcc/cp/cp-tree.h | 8 +++++++ gcc/cp/constexpr.c | 2 ++ gcc/cp/pt.c | 3 ++- gcc/testsuite/g++.dg/diagnostic/static_assert4.C | 30 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index ce7ca53a113..f0a7bd24df7 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -500,6 +500,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX]; FUNCTION_REF_QUALIFIED (in FUNCTION_TYPE, METHOD_TYPE) OVL_LOOKUP_P (in OVERLOAD) LOOKUP_FOUND_P (in RECORD_TYPE, UNION_TYPE, ENUMERAL_TYPE, NAMESPACE_DECL) + FNDECL_MANIFESTLY_CONST_EVALUATED (in FUNCTION_DECL) 5: IDENTIFIER_VIRTUAL_P (in IDENTIFIER_NODE) FUNCTION_RVALUE_QUALIFIED (in FUNCTION_TYPE, METHOD_TYPE) CALL_EXPR_REVERSE_ARGS (in CALL_EXPR, AGGR_INIT_EXPR) @@ -4213,6 +4214,13 @@ more_aggr_init_expr_args_p (const aggr_init_expr_arg_iterator *iter) #define FNDECL_USED_AUTO(NODE) \ TREE_LANG_FLAG_2 (FUNCTION_DECL_CHECK (NODE)) +/* True if NODE is needed for a manifestly constant-evaluated expression. + This doesn't especially need to be a flag, since currently it's only + used for error recovery; if we run out of function flags it could move + to an attribute. */ +#define FNDECL_MANIFESTLY_CONST_EVALUATED(NODE) \ + TREE_LANG_FLAG_4 (FUNCTION_DECL_CHECK (NODE)) + /* True for artificial decls added for OpenMP privatized non-static data members. */ #define DECL_OMP_PRIVATIZED_MEMBER(NODE) \ diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index e78fdf021b2..8be88dcfc24 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -2572,6 +2572,8 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, location_t save_loc = input_location; input_location = loc; ++function_depth; + if (ctx->manifestly_const_eval) + FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true; instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false); --function_depth; input_location = save_loc; diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index fcf3ac31b25..72b22d8c487 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -10873,7 +10873,8 @@ neglectable_inst_p (tree d) { return (d && DECL_P (d) && !undeduced_auto_decl (d) - && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d) + && !(TREE_CODE (d) == FUNCTION_DECL + ? FNDECL_MANIFESTLY_CONST_EVALUATED (d) : decl_maybe_constant_var_p (d))); } diff --git a/gcc/testsuite/g++.dg/diagnostic/static_assert4.C b/gcc/testsuite/g++.dg/diagnostic/static_assert4.C new file mode 100644 index 00000000000..c539016e526 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/static_assert4.C @@ -0,0 +1,30 @@ +// PR c++/92193 +// { dg-do compile { target c++11 } } + +template + struct has_foo + { static constexpr bool value = false; }; + +template +#ifndef NO_CONSTEXPR + constexpr +#endif + bool + foo(T t) noexcept(noexcept(t.foo())) + { return t.foo(); } + +template + void + maybe_foo(T t) + { + static_assert( has_foo::value, "has foo" ); // { dg-error "has foo" } + foo(t); + } + +struct X { }; + +int main() +{ + X x; + maybe_foo(x); +}