From a074ae297d5dbd69e03c6c30f9cb7720685fdb62 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Thu, 21 Jul 2022 12:59:44 -0400 Subject: [PATCH] c++: non-dependent call to consteval operator [PR105912] Here we're crashing when substituting a non-dependent call to a consteval operator, whose CALL_EXPR_OPERATOR_SYNTAX flag we try to propagate to the result, but the result isn't a CALL_EXPR since the selected function is consteval. This patch fixes this by checking the result of extract_call_expr accordingly. (Note that we can't check DECL_IMMEDIATE_FUNCTION_P here because we don't know which function was selected by overload resolution from here.) PR c++/105912 gcc/cp/ChangeLog: * call.cc (extract_call_expr): Return a NULL_TREE on failure instead of asserting. * pt.cc (tsubst_copy_and_build) : Guard against NULL_TREE extract_call_expr result. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/consteval31.C: New test. (cherry picked from commit f70c18524221dcefa6cd26cee7b55503181bd912) --- gcc/cp/call.cc | 7 ++++--- gcc/cp/pt.cc | 12 ++++++------ gcc/testsuite/g++.dg/cpp2a/consteval31.C | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval31.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index dafcdb0..67c30f3 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -7116,9 +7116,10 @@ extract_call_expr (tree call) default:; } - gcc_assert (TREE_CODE (call) == CALL_EXPR - || TREE_CODE (call) == AGGR_INIT_EXPR - || call == error_mark_node); + if (TREE_CODE (call) != CALL_EXPR + && TREE_CODE (call) != AGGR_INIT_EXPR + && call != error_mark_node) + return NULL_TREE; return call; } diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index cf4ae77..80fadea 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21069,12 +21069,12 @@ tsubst_copy_and_build (tree t, bool ord = CALL_EXPR_ORDERED_ARGS (t); bool rev = CALL_EXPR_REVERSE_ARGS (t); if (op || ord || rev) - { - function = extract_call_expr (ret); - CALL_EXPR_OPERATOR_SYNTAX (function) = op; - CALL_EXPR_ORDERED_ARGS (function) = ord; - CALL_EXPR_REVERSE_ARGS (function) = rev; - } + if (tree call = extract_call_expr (ret)) + { + CALL_EXPR_OPERATOR_SYNTAX (call) = op; + CALL_EXPR_ORDERED_ARGS (call) = ord; + CALL_EXPR_REVERSE_ARGS (call) = rev; + } } RETURN (ret); diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval31.C b/gcc/testsuite/g++.dg/cpp2a/consteval31.C new file mode 100644 index 0000000..85a4d17 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval31.C @@ -0,0 +1,26 @@ +// PR c++/105912 +// { dg-do compile { target c++20 } } + +struct A { + consteval A operator+() { + return {}; + } +}; + +consteval A operator~(A) { + return {}; +} + +consteval A operator+(A, A) { + return {}; +} + +template +void f() { + A a; + ~a; + a + a; + +a; +} + +template void f(); -- 2.7.4