c++/modules: Fix missed unwrapping of STAT_HACK in ADL [PR121893]

Message ID aMQZj-gp2h_kFXJE@Thaum.localdomain
State Committed
Commit 85409b9d6a29392d33f3fbac40c641eeda22062a
Headers
Series c++/modules: Fix missed unwrapping of STAT_HACK in ADL [PR121893] |

Commit Message

Nathaniel Shead Sept. 12, 2025, 1:01 p.m. UTC
  Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

-- >8 --

My r16-3559-gc2e567a6edb563 reworked ADL for modules, including a change
to allow seeing module-linkage declarations if they only exist on the
instantiation path.  This caused a crash however as I neglected to
unwrap the stat hack wrapper when we were happy to see all declarations,
allowing search_adl to add non-functions to the overload set.

	PR c++/121893

gcc/cp/ChangeLog:

	* name-lookup.cc (name_lookup::adl_namespace_fns): Unwrap the
	STAT_HACK also when on_inst_path.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/adl-10_a.C: New test.
	* g++.dg/modules/adl-10_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/name-lookup.cc                   |  4 +++-
 gcc/testsuite/g++.dg/modules/adl-10_a.C | 23 +++++++++++++++++++++++
 gcc/testsuite/g++.dg/modules/adl-10_b.C |  7 +++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/adl-10_a.C
 create mode 100644 gcc/testsuite/g++.dg/modules/adl-10_b.C
  

Comments

Jason Merrill Sept. 12, 2025, 1:04 p.m. UTC | #1
On 9/12/25 3:01 PM, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

OK (obvious).

> -- >8 --
> 
> My r16-3559-gc2e567a6edb563 reworked ADL for modules, including a change
> to allow seeing module-linkage declarations if they only exist on the
> instantiation path.  This caused a crash however as I neglected to
> unwrap the stat hack wrapper when we were happy to see all declarations,
> allowing search_adl to add non-functions to the overload set.
> 
> 	PR c++/121893
> 
> gcc/cp/ChangeLog:
> 
> 	* name-lookup.cc (name_lookup::adl_namespace_fns): Unwrap the
> 	STAT_HACK also when on_inst_path.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/adl-10_a.C: New test.
> 	* g++.dg/modules/adl-10_b.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/name-lookup.cc                   |  4 +++-
>   gcc/testsuite/g++.dg/modules/adl-10_a.C | 23 +++++++++++++++++++++++
>   gcc/testsuite/g++.dg/modules/adl-10_b.C |  7 +++++++
>   3 files changed, 33 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/modules/adl-10_a.C
>   create mode 100644 gcc/testsuite/g++.dg/modules/adl-10_b.C
> 
> diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
> index 43b95bffecf..32af7a6aed9 100644
> --- a/gcc/cp/name-lookup.cc
> +++ b/gcc/cp/name-lookup.cc
> @@ -1306,7 +1306,9 @@ name_lookup::adl_namespace_fns (tree scope, bitmap imports,
>   		    /* For lookups on the instantiation path we can see any
>   		       module-linkage declaration; otherwise we should only
>   		       see exported decls.  */
> -		    if (!on_inst_path)
> +		    if (on_inst_path)
> +		      bind = STAT_DECL (bind);
> +		    else
>   		      bind = STAT_VISIBLE (bind);
>   		  }
>   
> diff --git a/gcc/testsuite/g++.dg/modules/adl-10_a.C b/gcc/testsuite/g++.dg/modules/adl-10_a.C
> new file mode 100644
> index 00000000000..eef4ef0f724
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/adl-10_a.C
> @@ -0,0 +1,23 @@
> +// PR c++/121893
> +// { dg-additional-options "-fmodules -Wno-global-module" }
> +// { dg-module-cmi M }
> +
> +module;
> +namespace ns {
> +  struct S {};
> +
> +  struct F {
> +    template <typename T> void operator()(T) {}
> +  };
> +  inline constexpr F foo{};
> +}
> +
> +export module M;
> +
> +namespace ns {
> +  export using ns::S;
> +  export using ns::foo;
> +}
> +
> +template <typename T> void foo(T) {}
> +export template <typename T> void go(T t) { foo(t); }
> diff --git a/gcc/testsuite/g++.dg/modules/adl-10_b.C b/gcc/testsuite/g++.dg/modules/adl-10_b.C
> new file mode 100644
> index 00000000000..9be64a96f4e
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/adl-10_b.C
> @@ -0,0 +1,7 @@
> +// PR c++/121893
> +// { dg-additional-options "-fmodules" }
> +
> +import M;
> +int main() {
> +  ::go(ns::S{});
> +}
  

Patch

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 43b95bffecf..32af7a6aed9 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -1306,7 +1306,9 @@  name_lookup::adl_namespace_fns (tree scope, bitmap imports,
 		    /* For lookups on the instantiation path we can see any
 		       module-linkage declaration; otherwise we should only
 		       see exported decls.  */
-		    if (!on_inst_path)
+		    if (on_inst_path)
+		      bind = STAT_DECL (bind);
+		    else
 		      bind = STAT_VISIBLE (bind);
 		  }
 
diff --git a/gcc/testsuite/g++.dg/modules/adl-10_a.C b/gcc/testsuite/g++.dg/modules/adl-10_a.C
new file mode 100644
index 00000000000..eef4ef0f724
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/adl-10_a.C
@@ -0,0 +1,23 @@ 
+// PR c++/121893
+// { dg-additional-options "-fmodules -Wno-global-module" }
+// { dg-module-cmi M }
+
+module;
+namespace ns {
+  struct S {};
+
+  struct F {
+    template <typename T> void operator()(T) {}
+  };
+  inline constexpr F foo{};
+}
+
+export module M;
+
+namespace ns {
+  export using ns::S;
+  export using ns::foo;
+}
+
+template <typename T> void foo(T) {}
+export template <typename T> void go(T t) { foo(t); }
diff --git a/gcc/testsuite/g++.dg/modules/adl-10_b.C b/gcc/testsuite/g++.dg/modules/adl-10_b.C
new file mode 100644
index 00000000000..9be64a96f4e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/adl-10_b.C
@@ -0,0 +1,7 @@ 
+// PR c++/121893
+// { dg-additional-options "-fmodules" }
+
+import M;
+int main() {
+  ::go(ns::S{});
+}