diff mbox series

c++/modules: Propagate purview/import for templates in duplicate_decls [PR116803]

Message ID 66f67763.170a0220.8386b.3627@mx.google.com
State New
Headers show
Series c++/modules: Propagate purview/import for templates in duplicate_decls [PR116803] | expand

Commit Message

Nathaniel Shead Sept. 27, 2024, 9:14 a.m. UTC
Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

-- >8 --

We need to ensure that for a declaration in the module purview, that the
resulting declaration has PURVIEW_P set and IMPORT_P cleared so that we
understand it might be something requiring exporting.  This is normally
handled for a declaration by set_instantiating_module, but when this
declaration is a redeclaration duplicate_decls needs to propagate this
to olddecl.

This patch only changes the logic for template declarations, because in
the non-template case the whole contents of olddecl's DECL_LANG_SPECIFIC
is replaced with newdecl's (which includes these flags), so there's
nothing to do.

	PR c++/116803

gcc/cp/ChangeLog:

	* decl.cc (duplicate_decls): Propagate DECL_MODULE_PURVIEW_P and
	DECL_MODULE_IMPORT_P for template redeclarations.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/merge-18_a.H: New test.
	* g++.dg/modules/merge-18_b.H: New test.
	* g++.dg/modules/merge-18_c.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/decl.cc                            | 10 ++++++++++
 gcc/testsuite/g++.dg/modules/merge-18_a.H |  8 ++++++++
 gcc/testsuite/g++.dg/modules/merge-18_b.H | 13 +++++++++++++
 gcc/testsuite/g++.dg/modules/merge-18_c.C | 10 ++++++++++
 4 files changed, 41 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_a.H
 create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_b.H
 create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_c.C

Comments

Jason Merrill Sept. 27, 2024, 6:17 p.m. UTC | #1
On 9/27/24 5:14 AM, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

OK.

> -- >8 --
> 
> We need to ensure that for a declaration in the module purview, that the
> resulting declaration has PURVIEW_P set and IMPORT_P cleared so that we
> understand it might be something requiring exporting.  This is normally
> handled for a declaration by set_instantiating_module, but when this
> declaration is a redeclaration duplicate_decls needs to propagate this
> to olddecl.
> 
> This patch only changes the logic for template declarations, because in
> the non-template case the whole contents of olddecl's DECL_LANG_SPECIFIC
> is replaced with newdecl's (which includes these flags), so there's
> nothing to do.
> 
> 	PR c++/116803
> 
> gcc/cp/ChangeLog:
> 
> 	* decl.cc (duplicate_decls): Propagate DECL_MODULE_PURVIEW_P and
> 	DECL_MODULE_IMPORT_P for template redeclarations.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/merge-18_a.H: New test.
> 	* g++.dg/modules/merge-18_b.H: New test.
> 	* g++.dg/modules/merge-18_c.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/decl.cc                            | 10 ++++++++++
>   gcc/testsuite/g++.dg/modules/merge-18_a.H |  8 ++++++++
>   gcc/testsuite/g++.dg/modules/merge-18_b.H | 13 +++++++++++++
>   gcc/testsuite/g++.dg/modules/merge-18_c.C | 10 ++++++++++
>   4 files changed, 41 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_a.H
>   create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_b.H
>   create mode 100644 gcc/testsuite/g++.dg/modules/merge-18_c.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 5ddb7eafa50..a81a7dd2e9e 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -2528,6 +2528,16 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
>   	    }
>   	}
>   
> +      /* Propagate purviewness and importingness as with
> +	 set_instantiating_module.  */
> +      if (modules_p ())
> +	{
> +	  if (DECL_MODULE_PURVIEW_P (new_result))
> +	    DECL_MODULE_PURVIEW_P (old_result) = true;
> +	  if (!DECL_MODULE_IMPORT_P (new_result))
> +	    DECL_MODULE_IMPORT_P (old_result) = false;
> +	}
> +
>         /* If the new declaration is a definition, update the file and
>   	 line information on the declaration, and also make
>   	 the old declaration the same definition.  */
> diff --git a/gcc/testsuite/g++.dg/modules/merge-18_a.H b/gcc/testsuite/g++.dg/modules/merge-18_a.H
> new file mode 100644
> index 00000000000..8d86ad980ba
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/merge-18_a.H
> @@ -0,0 +1,8 @@
> +// PR c++/116803
> +// { dg-additional-options "-fmodule-header" }
> +// { dg-module-cmi {} }
> +
> +namespace ns {
> +  template <typename T> void foo();
> +  template <typename T> extern const int bar;
> +}
> diff --git a/gcc/testsuite/g++.dg/modules/merge-18_b.H b/gcc/testsuite/g++.dg/modules/merge-18_b.H
> new file mode 100644
> index 00000000000..2a762e2ac49
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/merge-18_b.H
> @@ -0,0 +1,13 @@
> +// PR c++/116803
> +// { dg-additional-options "-fmodule-header -fdump-lang-module" }
> +// { dg-module-cmi {} }
> +
> +import "merge-18_a.H";
> +
> +namespace ns {
> +  template <typename T> void foo() {}
> +  template <typename T> const int bar = 123;
> +}
> +
> +// { dg-final { scan-lang-dump {Writing definition '::ns::template foo'} module } }
> +// { dg-final { scan-lang-dump {Writing definition '::ns::template bar'} module } }
> diff --git a/gcc/testsuite/g++.dg/modules/merge-18_c.C b/gcc/testsuite/g++.dg/modules/merge-18_c.C
> new file mode 100644
> index 00000000000..b90d85f7502
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/merge-18_c.C
> @@ -0,0 +1,10 @@
> +// PR c++/116803
> +// { dg-module-do link }
> +// { dg-additional-options "-fmodules-ts" }
> +
> +import "merge-18_b.H";
> +
> +int main() {
> +  ns::foo<int>();
> +  static_assert(ns::bar<int> == 123);
> +}
diff mbox series

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 5ddb7eafa50..a81a7dd2e9e 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -2528,6 +2528,16 @@  duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
 	    }
 	}
 
+      /* Propagate purviewness and importingness as with
+	 set_instantiating_module.  */
+      if (modules_p ())
+	{
+	  if (DECL_MODULE_PURVIEW_P (new_result))
+	    DECL_MODULE_PURVIEW_P (old_result) = true;
+	  if (!DECL_MODULE_IMPORT_P (new_result))
+	    DECL_MODULE_IMPORT_P (old_result) = false;
+	}
+
       /* If the new declaration is a definition, update the file and
 	 line information on the declaration, and also make
 	 the old declaration the same definition.  */
diff --git a/gcc/testsuite/g++.dg/modules/merge-18_a.H b/gcc/testsuite/g++.dg/modules/merge-18_a.H
new file mode 100644
index 00000000000..8d86ad980ba
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/merge-18_a.H
@@ -0,0 +1,8 @@ 
+// PR c++/116803
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+namespace ns {
+  template <typename T> void foo();
+  template <typename T> extern const int bar;
+}
diff --git a/gcc/testsuite/g++.dg/modules/merge-18_b.H b/gcc/testsuite/g++.dg/modules/merge-18_b.H
new file mode 100644
index 00000000000..2a762e2ac49
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/merge-18_b.H
@@ -0,0 +1,13 @@ 
+// PR c++/116803
+// { dg-additional-options "-fmodule-header -fdump-lang-module" }
+// { dg-module-cmi {} }
+
+import "merge-18_a.H";
+
+namespace ns {
+  template <typename T> void foo() {}
+  template <typename T> const int bar = 123;
+}
+
+// { dg-final { scan-lang-dump {Writing definition '::ns::template foo'} module } }
+// { dg-final { scan-lang-dump {Writing definition '::ns::template bar'} module } }
diff --git a/gcc/testsuite/g++.dg/modules/merge-18_c.C b/gcc/testsuite/g++.dg/modules/merge-18_c.C
new file mode 100644
index 00000000000..b90d85f7502
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/merge-18_c.C
@@ -0,0 +1,10 @@ 
+// PR c++/116803
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import "merge-18_b.H";
+
+int main() {
+  ns::foo<int>();
+  static_assert(ns::bar<int> == 123);
+}