https://gcc.gnu.org/g:cf588f1a8e7406ced5b08f32f9d23f015a240a31
commit r16-1350-gcf588f1a8e7406ced5b08f32f9d23f015a240a31 Author: Iain Sandoe <i...@sandoe.co.uk> Date: Mon Jun 2 09:42:23 2025 +0100 c++: Fix template class lookup [PR120495, PR115605]. In the reported issues, using lookup_template_class () directly on (for example) the coroutine_handle identifier fails because a class-local TYPE_DECL is found. This is because, in the existing code, lookup is called with default parameters which means that class contexts are examined first. Fix this, when a context is provided by the caller, by doing lookup in namespace provided. PR c++/120495 PR c++/115605 gcc/cp/ChangeLog: * pt.cc (lookup_template_class): Honour provided namespace contexts when looking up class templates. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr120495.C: New test. * g++.dg/pr115605.C: New test. Signed-off-by: Iain Sandoe <i...@sandoe.co.uk> Diff: --- gcc/cp/pt.cc | 30 ++++++++++------ gcc/testsuite/g++.dg/coroutines/pr120495.C | 55 ++++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/pr115605.C | 10 ++++++ 3 files changed, 84 insertions(+), 11 deletions(-) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b5c877a385f5..752b0ea8544c 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10052,15 +10052,20 @@ tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl) D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. + If D1 is an identifier and CONTEXT is non-NULL, then the lookup is + carried out in CONTEXT. Currently, only namespaces are supported for + CONTEXT. + + If D1 is an identifier and CONTEXT is NULL, the lookup is performed + in the innermost non-namespace binding. + + Otherwise CONTEXT is ignored and no lookup is carried out. + IN_DECL, if non-NULL, is the template declaration we are trying to instantiate. Issue error and warning messages under control of COMPLAIN. - If the template class is really a local class in a template - function, then the FUNCTION_CONTEXT is the function in which it is - being instantiated. - ??? Note that this function is currently called *twice* for each template-id: the first time from the parser, while creating the incomplete type (finish_template_type), and the second type during the @@ -10079,20 +10084,23 @@ lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, spec_entry **slot; spec_entry *entry; - if (identifier_p (d1)) + if (identifier_p (d1) && context) + { + gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL); + push_decl_namespace (context); + templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL); + pop_decl_namespace (); + } + else if (identifier_p (d1)) { tree value = innermost_non_namespace_value (d1); if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) templ = value; else - { - if (context) - push_decl_namespace (context); + { templ = lookup_name (d1); templ = maybe_get_template_decl_from_type_decl (templ); - if (context) - pop_decl_namespace (); - } + } } else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) { diff --git a/gcc/testsuite/g++.dg/coroutines/pr120495.C b/gcc/testsuite/g++.dg/coroutines/pr120495.C new file mode 100644 index 000000000000..f59c34a86765 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/pr120495.C @@ -0,0 +1,55 @@ +// { dg-additional-options "-fsyntax-only" } + +#include <coroutine> +#include <exception> + +struct fire_and_forget { +}; + +template <typename... Args> +struct std::coroutine_traits<fire_and_forget, Args...> +{ + struct promise_type + { + fire_and_forget get_return_object() const noexcept + { + return{}; + } + + void return_void() const noexcept + { + } + + suspend_never initial_suspend() const noexcept + { + return{}; + } + + suspend_never final_suspend() const noexcept + { + return{}; + } + + void unhandled_exception() const noexcept + { + std::terminate(); + } + }; +}; + +struct foo +{ + fire_and_forget bar() + { + co_await std::suspend_always{ }; + } + +private: + // The line below triggered the error. + using coroutine_handle = std::coroutine_handle<>; +}; + +int main() +{ + foo{}.bar(); +} diff --git a/gcc/testsuite/g++.dg/pr115605.C b/gcc/testsuite/g++.dg/pr115605.C new file mode 100644 index 000000000000..9e342555c894 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr115605.C @@ -0,0 +1,10 @@ +// { dg-do compile { target c++20 } } + +#include <array> + +int foo() { + int const tuple_size = 5; + std::array<int, 3> array {1, 2, 3}; + auto [a, b, c] = array; + return c; +}