https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120868
Bug ID: 120868
Summary: "unexpected AST of kind switch_expr" in constexpr
template function
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: sdowney at gmail dot com
Target Milestone: ---
Calling the google test macro EXPECT_EQ in a constexpr template function
results in:
<source>:5:16: sorry, unimplemented: unexpected AST of kind switch_expr
<source>:5:16: internal compiler error: in potential_constant_expression_1, at
cp/constexpr.cc:11183
0x2844425 diagnostic_context::diagnostic_impl(rich_location*,
diagnostic_metadata const*, diagnostic_option_id, char const*, __va_list_tag
(*) [1], diagnostic_t)
???:0
0x2866fc6 internal_error(char const*, ...)
???:0
0xaede72 fancy_abort(char const*, int, char const*)
???:0
0xb6afc5 require_potential_rvalue_constant_expression(tree_node*)
???:0
0xb6b1a5 explain_invalid_constexpr_fn(tree_node*)
???:0
0xb66b53 cxx_constant_value(tree_node*, tree_node*, int)
???:0
0xda189d finish_static_assert(tree_node*, tree_node*, unsigned long, bool,
bool)
???:0
0xd166e3 c_parse_file()
???:0
0xe7e469 c_common_parse_file()
???:0
https://compiler-explorer.com/z/nE3x3dzE5 (macros expanded)
Removing the switch reveals a different constexpr error.
Reproduced with trunk as well as with GCC-15 built locally.
```c++
#include <gtest/gtest.h>
#include <vector>
template <typename U>
constexpr auto constexpr_iterator_test(U opt) -> bool {
using iterator = typename std::remove_reference_t<decltype(opt)>::iterator;
switch (0)
case 0:
default:
if (const ::testing ::AssertionResult gtest_ar =
(::testing ::internal ::EqHelper ::Compare(
"opt.begin()", "iterator()", opt.begin(), iterator())))
;
else
::testing ::internal ::AssertHelper(
::testing ::TestPartResult ::kNonFatalFailure,
"/home/sdowney/src/Optional26/constexpr-20/tests/beman/"
"optional/optional_range_support.t.cpp",
444, gtest_ar.failure_message()) = ::testing ::Message();
return true;
};
int main() {
static_assert(constexpr_iterator_test(std::vector<int>{}));
EXPECT_TRUE(constexpr_iterator_test(std::vector<int>{}));
}
```