https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125797
Bug ID: 125797
Summary: [reflection] Faulty immediate-escalation with
std::meta::current_function, templates, and 'requires'
clause
Product: gcc
Version: 16.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: friedkeenan at protonmail dot com
Target Milestone: ---
The following is as little as I could manage to reduce the problem code to:
```
#include <meta>
#include <concepts>
template<typename F>
requires (std::invocable<F &, const int &>)
constexpr void caller(F f, const int x) {
f(x);
}
int main(int argc, char **) {
caller([](auto) {
(void) display_string_of(std::meta::current_function());
}, argc);
}
```
Godbolt link: https://godbolt.org/z/3Tfqx6o7o
The above code emits the following error:
```
<source>: In function 'int main(int, char**)':
<source>:11:11: error: call to consteval function 'caller<main(int,
char**)::<lambda(auto:6)> >(main(int, char**)::<lambda(auto:6)>(), argc)' is
not a constant expression
11 | caller([](auto) {
| ~~~~~~^~~~~~~~~~~
12 | (void) display_string_of(std::meta::current_function());
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 | }, argc);
| ~~~~~~~~
<source>:11:11: error: 'argc' is not a constant expression
<source>:7:6: note: 'constexpr void caller(F, int) [with F = main(int,
char**)::<lambda(auto:6)>]' was promoted to an immediate function because its
body contains an immediate-escalating expression 'f.main(int,
char**)::<lambda(auto:6)>(((int)x))'
7 | f(x);
| ~^~~
```
If one removes the 'requires' clause from the 'caller' function, or changes the
'auto' parameter of the lambda to a plain 'int', then the code successfully
compiles.
It also successfully compiles if one calls
'std::meta::access_context::current().scope()' instead of
'std::meta::current_function()'.