When streaming in the artificial VAR_DECL synthesized for a class NTTP argument, we end up crashing from complete_vars because the call to maybe_register_incomplete_var from add_module_namespace_decl for this VAR_DECL pushes an unexpected NULL_TREE type onto the incomplete_vars vector.
This patch fixes this by checking for NULL_TREE before pushing onto the vector. This avoids the crash, but I noticed we still appear to mishandle these artificial VAR_DECLs across translation units: the lookup from get_template_parm_object for an existing VAR_DECL for the given class NTTP argument fails to find the streamed-in VAR_DECL from the other translation unit, so we end up creating a second VAR_DECL, but that causes specialization equivalency issues in the XFAIL'd part of the below test. I'm afraid I don't understand why the lookup fails here despite having done add_module_namespace_decl during stream-in, but fixing the ICE seems like a safe and useful step towards enabling class NTTP arguments used in modules. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/100616 gcc/cp/ChangeLog: * decl.cc (maybe_register_incomplete_var): Check result of outermost_open_class. gcc/testsuite/ChangeLog: * g++.dg/modules/pr100616_a.C: New test. * g++.dg/modules/pr100616_b.C: New test. --- gcc/cp/decl.cc | 8 +++++--- gcc/testsuite/g++.dg/modules/pr100616_a.C | 8 ++++++++ gcc/testsuite/g++.dg/modules/pr100616_b.C | 10 ++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/pr100616_a.C create mode 100644 gcc/testsuite/g++.dg/modules/pr100616_b.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 80467c19254..722b64793ed 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -18235,9 +18235,11 @@ maybe_register_incomplete_var (tree var) { /* When the outermost open class is complete we can resolve any pointers-to-members. */ - tree context = outermost_open_class (); - incomplete_var iv = {var, context}; - vec_safe_push (incomplete_vars, iv); + if (tree context = outermost_open_class ()) + { + incomplete_var iv = {var, context}; + vec_safe_push (incomplete_vars, iv); + } } } } diff --git a/gcc/testsuite/g++.dg/modules/pr100616_a.C b/gcc/testsuite/g++.dg/modules/pr100616_a.C new file mode 100644 index 00000000000..788af2eb533 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr100616_a.C @@ -0,0 +1,8 @@ +// PR c++/100616 +// { dg-additional-options "-std=c++20 -fmodules-ts" } +// { dg-module-cmi pr100616 } +export module pr100616; + +template<auto> struct C { }; +struct A { }; +C<A{}> c1; diff --git a/gcc/testsuite/g++.dg/modules/pr100616_b.C b/gcc/testsuite/g++.dg/modules/pr100616_b.C new file mode 100644 index 00000000000..8037ceda3ed --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/pr100616_b.C @@ -0,0 +1,10 @@ +// PR c++/100616 +// { dg-additional-options "-std=c++20 -fmodules-ts" } +module pr100616; + +C<A{}> c2; + +// FIXME: We don't reuse the artificial VAR_DECL for the class NTTP argument A{} +// from the other translation unit, which causes these types to be different. +using ty_a = decltype(c1); +using ty_a = decltype(c2); // { dg-bogus "conflicting" "" { xfail *-*-* } } -- 2.38.0.rc0.52.gdda7228a83