From: Marek Polacek Date: Tue, 17 Feb 2026 22:08:52 +0000 (-0500) Subject: c++/reflection: ICE with missing OVERLOAD [PR124150] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=99b63e4ac7ee06e2f5030b72dc876bf35e9842ca;p=thirdparty%2Fgcc.git c++/reflection: ICE with missing OVERLOAD [PR124150] A function template is supposed to be wrapped in an OVERLOAD. Since in certain cases like members_of it is not, splice makes sure to add the OVERLOAD if needed. But it wasn't looking into TEMPLATE_ID_EXPRs and so we ended up with a "naked" TEMPLATE_DECL and crashed. We can add the missing OVERLOAD in eval_substitute. PR c++/124150 gcc/cp/ChangeLog: * reflect.cc (eval_substitute): Add an OVERLOAD around a DECL_FUNCTION_TEMPLATE_P. gcc/testsuite/ChangeLog: * g++.dg/reflect/substitute4.C: New test. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index a80f7ca0a14..522b7c06a29 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -5394,7 +5394,11 @@ eval_substitute (location_t loc, const constexpr_ctx *ctx, ret = finish_template_variable (ret, tf_none); } else - ret = lookup_template_function (r, rvec); + { + if (DECL_FUNCTION_TEMPLATE_P (r)) + r = ovl_make (r, NULL_TREE); + ret = lookup_template_function (r, rvec); + } return get_reflection_raw (loc, ret); } diff --git a/gcc/testsuite/g++.dg/reflect/substitute4.C b/gcc/testsuite/g++.dg/reflect/substitute4.C new file mode 100644 index 00000000000..ff9cd403e4b --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/substitute4.C @@ -0,0 +1,68 @@ +// PR c++/124150 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include +using namespace std; + +template +consteval meta::info find_non_static_template_function() { + for (auto r : meta::members_of(^^T, meta::access_context::current())) { + if (meta::identifier_of(r) == "non_static_function") { + return r; + } + } + return ^^void; +} +template +consteval meta::info find_static_template_member_func() { + for (auto r : meta::members_of(^^T, meta::access_context::current())) { + if (meta::identifier_of(r) == "static_function") { + return r; + } + } + return ^^void; +} +template +consteval meta::info find_non_template_member_func() { + for (auto r : meta::members_of(^^T, meta::access_context::current())) { + if (meta::identifier_of(r) == "non_template_member_func") { + return r; + } + } + return ^^void; +} + +template +void call_member_template_functions(Ts* ptr) { + [:substitute(static_func, {^^T}):](); + ptr->[:substitute(non_static_func, {^^T}):](); + ptr->[:non_template_func:](); +} + +struct A { + template + static void static_function() { } + template + void non_static_function() { } + void non_template_member_func() { } +}; + +template +void call_non_member_template_function() { + [:meta::substitute(r, {^^T}):](); +} + +template +void non_member_template_function() { } + +int +main () +{ + A a; + call_member_template_functions(), + find_non_static_template_function(), + find_non_template_member_func()>(&a); + call_non_member_template_function(); +}