On Tue, Jan 13, 2026 at 10:45:03PM +0800, Jason Merrill wrote: > On 1/9/26 2:29 PM, Nathaniel Shead wrote: > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? I'm not > > 100% sure if this matches the wording in the standard, but it more > > closely matches Clang's current behaviour and seems to fix a lot of > > issues I've seen people have with GCC's current behaviour. > > > > -- >8 -- > > > > Many expressions rely on standard library names being visible when used, > > such as for structured bindings, typeid, coroutine traits, and so forth. > > When these expressions occur in templates, and the instantiations occur > > in a TU where those names are not visible, we currently error, even if > > the name was visible in the TU the template was defined. > > > > This is a poor user experience (made worse by fixit suggestions to add > > an include in the module the template was defined, which wouldn't fix > > the error anyway). Instead, I think it seems reasonable (and probably > > the intention of the wording?) that we should perform the lookup in the > > instantiation context of the lookup. > > It seems to me that the lookup should occur in the definition context of the > template, both binding the name and making the definition decl-reachable. > The full instantiation path is more than we need. >
I've had a bit of a look into this; would this mean perhaps extending 'DEPENDENT_OPERATOR_TYPE' (or something like it) to save the results of name lookup at definition time for all contexts that we currently do 'lookup_qualified_name' somewhere if processing_template_decl, and then at instantiation time supplementing the results with a second lookup and using that set? Or is there another approach that you're thinking of here? > > When using 'import std' this should fix all such errors. If using > > traditional #includes to provide the standard library this may or may > > not fix the error; in many cases we will still discard the relevant > > names (e.g. typeid in a template does not currently cause us to consider > > std::type_info to be decl-reachable). > > > This also fixes the XFAIL in adl-12_b.C as we add_candidates now > > properly considers names visible in the instantiation context of the > > comparison. > > Similarly those declarations should be decl-reachable under > https://eel.is/c++draft/module.global.frag#note-1 > Yes, we already consider the names decl-reachable (and they are part of the module interface) due to the ADL, they're just not visible from the TU we instantiate from, without either this patch or some other way to perform the lookup in the context of the definition. > > PR c++/122609 > > > > gcc/cp/ChangeLog: > > > > * name-lookup.cc (name_lookup::search_namespace_only): Find all > > names visible in the instantiation context. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/modules/adl-12_b.C: Remove XFAIL. > > * g++.dg/modules/inst-8_a.C: New test. > > * g++.dg/modules/inst-8_b.C: New test. > > * g++.dg/modules/inst-8_c.C: New test. > > > > Signed-off-by: Nathaniel Shead <[email protected]> > > --- > > gcc/cp/name-lookup.cc | 13 +++++--- > > gcc/testsuite/g++.dg/modules/adl-12_b.C | 7 ++--- > > gcc/testsuite/g++.dg/modules/inst-8_a.C | 27 ++++++++++++++++ > > gcc/testsuite/g++.dg/modules/inst-8_b.C | 42 +++++++++++++++++++++++++ > > gcc/testsuite/g++.dg/modules/inst-8_c.C | 12 +++++++ > > 5 files changed, 92 insertions(+), 9 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/modules/inst-8_a.C > > create mode 100644 gcc/testsuite/g++.dg/modules/inst-8_b.C > > create mode 100644 gcc/testsuite/g++.dg/modules/inst-8_c.C > > > > diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc > > index 5d93c13265d..549a04df57c 100644 > > --- a/gcc/cp/name-lookup.cc > > +++ b/gcc/cp/name-lookup.cc > > @@ -861,7 +861,11 @@ name_lookup::search_namespace_only (tree scope) > > /* I presume the binding list is going to be sparser than > > the import bitmap. Hence iterate over the former > > checking for bits set in the bitmap. */ > > - bitmap imports = get_import_bitmap (); > > + /* Similarly to with ADL, it seems most helpful to consider > > + any declaration visible in the instantiation context as > > + being visible for name lookup (PR c++/122609). */ > > + bitmap inst_path = NULL; > > + bitmap imports = visible_instantiation_path (&inst_path); > > binding_cluster *cluster = BINDING_VECTOR_CLUSTER_BASE (val); > > int marker = 0; > > int dup_detect = 0; > > @@ -929,11 +933,11 @@ name_lookup::search_namespace_only (tree scope) > > found:; > > /* Is it loaded? */ > > + unsigned mod = cluster->indices[jx].base; > > if (cluster->slots[jx].is_lazy ()) > > { > > gcc_assert (cluster->indices[jx].span == 1); > > - lazy_load_binding (cluster->indices[jx].base, > > - scope, name, &cluster->slots[jx]); > > + lazy_load_binding (mod, scope, name, &cluster->slots[jx]); > > } > > tree bind = cluster->slots[jx]; > > if (!bind) > > @@ -966,7 +970,8 @@ name_lookup::search_namespace_only (tree scope) > > dup_detect |= dup; > > } > > - if (bool (want & LOOK_want::ANY_REACHABLE)) > > + if (bool (want & LOOK_want::ANY_REACHABLE) > > + || (inst_path && bitmap_bit_p (inst_path, mod))) > > { > > type = STAT_TYPE (bind); > > bind = STAT_DECL (bind); > > diff --git a/gcc/testsuite/g++.dg/modules/adl-12_b.C > > b/gcc/testsuite/g++.dg/modules/adl-12_b.C > > index cde8c35b20e..abe772d49c7 100644 > > --- a/gcc/testsuite/g++.dg/modules/adl-12_b.C > > +++ b/gcc/testsuite/g++.dg/modules/adl-12_b.C > > @@ -20,10 +20,7 @@ void test(Q::X x) { > > #if __cpp_impl_three_way_comparison >= 201907L > > rewrite_ops(0); // OK > > - > > - // This should error, but lookup_qualified_name in add_candidates > > - // doesn't look in the instantiation context of this call, so > > - // we don't see the operator!= and think we can validly rewrite. > > - rewrite_ops_error(0); // { dg-error "required from here" "PR122609" { > > xfail *-*-* } } > > + rewrite_ops_error(0); // { dg-message "required from here" "" { target > > c++20 } } > > + // { dg-prune-output "no match for" } > > #endif > > } > > diff --git a/gcc/testsuite/g++.dg/modules/inst-8_a.C > > b/gcc/testsuite/g++.dg/modules/inst-8_a.C > > new file mode 100644 > > index 00000000000..1b0a47978cb > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/modules/inst-8_a.C > > @@ -0,0 +1,27 @@ > > +// PR c++/122609 > > +// { dg-do compile { target c++20 } } > > +// { dg-additional-options "-fmodules" } > > +// { dg-module-cmi std } > > + > > +module; > > +#include <coroutine> > > +#include <tuple> > > +#include <typeinfo> > > +export module std; > > + > > +// PR c++/101140 > > +export using ::operator new; > > +export using ::operator delete; > > + > > +export namespace std { > > + using std::coroutine_handle; > > + using std::coroutine_traits; > > + using std::suspend_always; > > + > > + using std::get; > > + using std::tuple; > > + using std::tuple_size; > > + using std::tuple_element; > > + > > + using std::type_info; > > +} > > diff --git a/gcc/testsuite/g++.dg/modules/inst-8_b.C > > b/gcc/testsuite/g++.dg/modules/inst-8_b.C > > new file mode 100644 > > index 00000000000..4e3b63da671 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/modules/inst-8_b.C > > @@ -0,0 +1,42 @@ > > +// PR c++/122609 > > +// { dg-do compile { target c++20 } } > > +// { dg-additional-options "-fmodules" } > > +// { dg-module-cmi M } > > + > > +export module M; > > +import std; // not exported > > + > > +export template <typename T> void structured_binding() { > > + std::tuple<T> t; > > + auto [x] = t; > > +} > > + > > +export template <typename T> void operator_new() { > > + // PR c++/101140 > > + T x; > > + new (&x) T; > > +} > > + > > +export template <typename T> void use_typeid() { > > + const auto& id = typeid(T); > > +} > > + > > +struct simple_promise; > > +struct simple_coroutine : std::coroutine_handle<simple_promise> { > > + using promise_type = ::simple_promise; > > +}; > > +struct simple_promise { > > + simple_coroutine get_return_object() { return { > > simple_coroutine::from_promise(*this) }; } > > + std::suspend_always initial_suspend() noexcept { return {}; } > > + std::suspend_always final_suspend() noexcept { return {}; } > > + void return_void() {} > > + void unhandled_exception() {} > > +}; > > +template <typename T> simple_coroutine coroutine_impl() { > > + co_return; > > +} > > +export template <typename T> void coroutine() { > > + simple_coroutine sc = coroutine_impl<T>(); > > + sc.resume(); > > + sc.destroy(); > > +} > > diff --git a/gcc/testsuite/g++.dg/modules/inst-8_c.C > > b/gcc/testsuite/g++.dg/modules/inst-8_c.C > > new file mode 100644 > > index 00000000000..460232d5a87 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/modules/inst-8_c.C > > @@ -0,0 +1,12 @@ > > +// PR c++/122609 > > +// { dg-do compile { target c++20 } } > > +// { dg-additional-options "-fmodules" } > > + > > +import M; > > + > > +int main() { > > + structured_binding<int>(); > > + operator_new<int>(); > > + use_typeid<int>(); > > + coroutine<int>(); > > +} >
