Re: [PATCH] c++/modules: ICE with GMF variable later defined inline [PR126783]

Patrick Palka <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <f9984a6f-28b3-f1a2-3bdd-4298cd5cab5f@idea>
On Wed, 12 Aug 2026, Patrick Palka wrote:

> Tested on x86_64-pc-linux-gnu, does this look OK for trunk/16?
> 
> -- >8 --
> 
> Here transfer_defining_module (added in r16-5213), assumes that if
> a redeclaration has DECL_LANG_SPECIFIC allocated, then so must the
> original declaration, but this isn't true for q below.  Instead we
> should call retrofit_lang_decl to allocate DECL_LANG_SPECIFIC on
> demand, as done in other parts of duplicate_decls.
> 
> 	PR c++/126783
> 
> gcc/cp/ChangeLog:
> 
> 	* module.cc (transfer_defining_module): Call retrofit_lang_decl
> 	instead of assuming the old declaration already has
> 	DECL_LANG_SPECIFIC allocated.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/gmf-6.C: New test.
> ---
>  gcc/cp/module.cc                     | 2 +-
>  gcc/testsuite/g++.dg/modules/gmf-6.C | 7 +++++++
>  2 files changed, 8 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/modules/gmf-6.C
> 
> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> index d6bd4c78d804..09c6c4467347 100644
> --- a/gcc/cp/module.cc
> +++ b/gcc/cp/module.cc
> @@ -22415,7 +22415,7 @@ transfer_defining_module (tree olddecl, tree newdecl)
>  
>    if (DECL_LANG_SPECIFIC (new_inner))
>      {
> -      gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
> +      retrofit_lang_decl (old_inner);
>        if (DECL_MODULE_PURVIEW_P (new_inner))
>  	DECL_MODULE_PURVIEW_P (old_inner) = true;
>        if (!DECL_MODULE_IMPORT_P (new_inner))

We can be lazier about allocating DECL_LANG_SPECIFIC here, and only do
it if DECL_MODULE_PURVIEW_P is set:

gcc/cp/module.cc                     | 9 ++++++---
 gcc/testsuite/g++.dg/modules/gmf-6.C | 7 +++++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index d6bd4c78d804..0a1da706f14a 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -22415,10 +22415,13 @@ transfer_defining_module (tree olddecl, tree newdecl)

   if (DECL_LANG_SPECIFIC (new_inner))
     {
-      gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
       if (DECL_MODULE_PURVIEW_P (new_inner))
-       DECL_MODULE_PURVIEW_P (old_inner) = true;
-      if (!DECL_MODULE_IMPORT_P (new_inner))
+       {
+         retrofit_lang_decl (old_inner);
+         DECL_MODULE_PURVIEW_P (old_inner) = true;
+       }
+      if (!DECL_MODULE_IMPORT_P (new_inner)
+         && DECL_LANG_SPECIFIC (old_inner))
        DECL_MODULE_IMPORT_P (old_inner) = false;
     }

But I don't think this micro-optimization is worth it, since apparently
it's quite rare that DECL_LANG_SPECIFIC isn't already allocated on the
old decl.

> diff --git a/gcc/testsuite/g++.dg/modules/gmf-6.C b/gcc/testsuite/g++.dg/modules/gmf-6.C
> new file mode 100644
> index 000000000000..201650917a32
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/gmf-6.C
> @@ -0,0 +1,7 @@
> +// PR c++/126783
> +// { dg-additional-options "-fmodules -Wno-global-module" }
> +// { dg-module-cmi M }
> +module;
> +extern int const q;
> +inline constexpr int q = 1;
> +export module M;
> -- 
> 2.55.0.559.g11c6700f10
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.