From: Patrick Palka Date: Sat, 25 Jul 2026 20:12:19 +0000 (-0400) Subject: c++: function NTTP argument in pack considered unused [PR126280] X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d47f48643b3db4e207b0cc3382f614aa76c9c2c5;p=thirdparty%2Fgcc.git c++: function NTTP argument in pack considered unused [PR126280] This is just the pack version of PR c++/105848, in which our non-dependent call pruning may cause us to not mark an otherwise unused function pointer template argument within a pack as used. PR c++/126280 gcc/cp/ChangeLog: * pt.cc (mark_template_arguments_used): Split out loop into ... (mark_template_arguments_used_1): ... here. Recurse into argument packs. gcc/testsuite/ChangeLog: * g++.dg/template/fn-ptr6.C: New test. Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b18fa5c02cf..6a081b838e0 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -224,6 +224,7 @@ static bool uses_outer_template_parms (tree); static tree alias_ctad_tweaks (tree, tree); static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t); static tree deduction_guides_for (tree, bool&, tsubst_flags_t); +static void mark_template_arguments_used_1 (tree); /* Make the current scope suitable for access checking when we are processing T. T can be FUNCTION_DECL for instantiated function @@ -23739,6 +23740,14 @@ mark_template_arguments_used (tree tmpl, tree args) /* We already marked outer arguments when specializing the context. */ args = INNERMOST_TEMPLATE_ARGS (args); + mark_template_arguments_used_1 (args); +} + +/* Main recursive part of the above. */ + +static void +mark_template_arguments_used_1 (tree args) +{ for (tree arg : tree_vec_range (args)) { /* A (pointer/reference to) function or variable NTTP argument. */ @@ -23779,6 +23788,8 @@ mark_template_arguments_used (tree tmpl, tree args) cp_walk_tree_without_duplicates (&DECL_INITIAL (arg), mark_used_r, nullptr); } + else if (TREE_CODE (arg) == NONTYPE_ARGUMENT_PACK) + mark_template_arguments_used_1 (ARGUMENT_PACK_ARGS (arg)); } } diff --git a/gcc/testsuite/g++.dg/template/fn-ptr6.C b/gcc/testsuite/g++.dg/template/fn-ptr6.C new file mode 100644 index 00000000000..fa111c6d942 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/fn-ptr6.C @@ -0,0 +1,26 @@ +// PR c++/126280 +// A version of fn-ptr3a.C where the template parameter is a pack. +// { dg-do compile { target c++11 } } + +template +void f(T) { T::fail; } // { dg-error "fail" } + +template +struct A { + // P not called +}; + +template +void wrap() { + // P not called +} + +template +void g() { + A a; // { dg-message "required from" } + wrap(); // { dg-message "required from" } +} + +int main() { + g<0>(); +}