On Sat, Nov 01, 2025 at 09:13:06PM +0300, Jason Merrill wrote:
> On 11/1/25 3:37 AM, Nathaniel Shead wrote:
> > On Thu, Oct 30, 2025 at 07:20:22PM +0200, Jason Merrill wrote:
> > > On 10/28/25 12:06 AM, Nathaniel Shead wrote:
> > > > On Mon, Oct 27, 2025 at 05:04:18PM +0200, Jason Merrill wrote:
> > > > > On 10/26/25 2:40 PM, Nathaniel Shead wrote:
> > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk/15?
> > > > > > 
> > > > > > -- >8 --
> > > > > > 
> > > > > > The linker error in the PR is caused because when a static is 
> > > > > > defined
> > > > > > out of the class body, it doesn't yet have a definition and so
> > > > > > read_var_def (which would otherwise have noted it) never gets 
> > > > > > called.
> > > > > 
> > > > > I don't object to this change, but there's certainly a definition in
> > > > > inst6_a, why wouldn't we call read_var_def?  I imagine it's because 
> > > > > we never
> > > > > write_var_def for some reason?
> > > > > 
> > > > 
> > > > Right.  We've instantiated Type<int> so a declaration exists for
> > > > Type<int>::var, but because of this hunk in instantiate_class_template
> > > > no definition has been instantiated yet, so DECL_INITIAL is null, and so
> > > > has_definition returns false and we never call write_var_def.
> > > 
> > > Makes sense, but how is that different from a variable template
> > > specialization that we've instantiated the declaration of, but not the
> > > definition, e.g.
> > > 
> > > template <class T> T v = T();
> > > decltype(v<int>) v2 = 0;
> > > 
> > > ?
> > 
> > In 'instantiate_decl' we have the following hunk
> > 
> >        set_instantiating_module (d);
> >        if (variable_template_p (gen_tmpl))
> >     note_vague_linkage_variable (d);
> >        instantiate_body (td, args, d, false);
> 
> Yes, but in my example instantiate_decl is never called.
> 

Sorry I wasn't clear: it's called by 'mark_used' in the importing module
at the point we actually need a definition to be available.

For 'v' in your example this would cause it to be added to
pending_statics to ensure it's emitted during c_parse_final_cleanups,
but 'arr' in my doesn't get added so we get the linker errors as no
definition is ever emitted.

> > A variable template will then always have 'note_vague_linkage_variable'
> > called once its body needs to be instantiated, but 'arr' in my example
> > does not because PRIMARY_TEMPLATE_P doesn't hold.  In a non-modules
> > scenario this is fine because we'll have already done this in
> > finish_static_data_member_decl, but not in this case.
> > 
> > > >                       if (VAR_P (r))
> > > >                         {
> > > >                           /* In [temp.inst]:
> > > > 
> > > >                              [t]he initialization (and any associated
> > > >                              side-effects) of a static data member does
> > > >                              not occur unless the static data member is
> > > >                              itself used in a way that requires the
> > > >                              definition of the static data member to
> > > >                              exist.
> > > > 
> > > >                              Therefore, we do not substitute into the
> > > >                              initialized for the static data member 
> > > > here.  */
> > > >                           finish_static_data_member_decl
> > > >                             (r,
> > > >                              /*init=*/NULL_TREE,
> > > >                              /*init_const_expr_p=*/false,
> > > >                              /*asmspec_tree=*/NULL_TREE,
> > > >                              /*flags=*/0);
> > > >                           /* Instantiate members marked with attribute 
> > > > used. */
> > > >                           if (r != error_mark_node && DECL_PRESERVE_P 
> > > > (r))
> > > >                             used.safe_push (r);
> > > >                         }
> > > > 
> > > > Nathaniel
> > > > 
> > > > > > This instead moves the responsibility for noting class-scope 
> > > > > > variables
> > > > > > to read_class_def.
> > > > > > 
> > > > > >     PR c++/122421
> > > > > > 
> > > > > > gcc/cp/ChangeLog:
> > > > > > 
> > > > > >     * module.cc (trees_in::read_var_def): Don't handle class-scope
> > > > > >     variables anymore.
> > > > > >     (trees_in::read_class_def): Handle them here instead.
> > > > > > 
> > > > > > gcc/testsuite/ChangeLog:
> > > > > > 
> > > > > >     * g++.dg/modules/inst-6_a.C: New test.
> > > > > >     * g++.dg/modules/inst-6_b.C: New test.
> > > > > > 
> > > > > > Signed-off-by: Nathaniel Shead <[email protected]>
> > > > > > ---
> > > > > >     gcc/cp/module.cc                        | 15 +++++++++------
> > > > > >     gcc/testsuite/g++.dg/modules/inst-6_a.C | 14 ++++++++++++++
> > > > > >     gcc/testsuite/g++.dg/modules/inst-6_b.C | 12 ++++++++++++
> > > > > >     3 files changed, 35 insertions(+), 6 deletions(-)
> > > > > >     create mode 100644 gcc/testsuite/g++.dg/modules/inst-6_a.C
> > > > > >     create mode 100644 gcc/testsuite/g++.dg/modules/inst-6_b.C
> > > > > > 
> > > > > > diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> > > > > > index ed0d69cead4..048aec598dd 100644
> > > > > > --- a/gcc/cp/module.cc
> > > > > > +++ b/gcc/cp/module.cc
> > > > > > @@ -13059,12 +13059,11 @@ trees_in::read_var_def (tree decl, tree 
> > > > > > maybe_template)
> > > > > >               if (DECL_EXPLICIT_INSTANTIATION (decl)
> > > > > >                   && !DECL_EXTERNAL (decl))
> > > > > >                 setup_explicit_instantiation_definition_linkage 
> > > > > > (decl);
> > > > > > -     if (DECL_IMPLICIT_INSTANTIATION (decl)
> > > > > > -         || (DECL_EXPLICIT_INSTANTIATION (decl)
> > > > > > -             && !DECL_EXTERNAL (decl))
> > > > > > -         || (DECL_CLASS_SCOPE_P (decl)
> > > > > > -             && !DECL_VTABLE_OR_VTT_P (decl)
> > > > > > -             && !DECL_TEMPLATE_INFO (decl)))
> > > > > > +     /* Class static data members are handled in read_class_def.  
> > > > > > */
> > > > > > +     if (!DECL_CLASS_SCOPE_P (decl)
> > > > > > +         && (DECL_IMPLICIT_INSTANTIATION (decl)
> > > > > > +             || (DECL_EXPLICIT_INSTANTIATION (decl)
> > > > > > +                 && !DECL_EXTERNAL (decl))))
> > > > > >                 note_vague_linkage_variable (decl);
> > > > > >             }
> > > > > >           if (!dyn_init)
> > > > > > @@ -13478,6 +13477,10 @@ trees_in::read_class_def (tree defn, tree 
> > > > > > maybe_template)
> > > > > >                             DECL_ACCESS (d) = tree_cons (type, 
> > > > > > access, list);
> > > > > >                         }
> > > > > >                     }
> > > > > > +
> > > > > > +         if (TREE_CODE (decl) == VAR_DECL
> > > > > > +             && TREE_CODE (maybe_template) != TEMPLATE_DECL)
> > > > > > +           note_vague_linkage_variable (decl);
> > > > > >                 }
> > > > > >             }
> > > > > > diff --git a/gcc/testsuite/g++.dg/modules/inst-6_a.C 
> > > > > > b/gcc/testsuite/g++.dg/modules/inst-6_a.C
> > > > > > new file mode 100644
> > > > > > index 00000000000..7f35cc161bf
> > > > > > --- /dev/null
> > > > > > +++ b/gcc/testsuite/g++.dg/modules/inst-6_a.C
> > > > > > @@ -0,0 +1,14 @@
> > > > > > +// PR c++/122421
> > > > > > +// { dg-additional-options "-fmodules" }
> > > > > > +// { dg-module-cmi M }
> > > > > > +
> > > > > > +export module M;
> > > > > > +
> > > > > > +export template <typename T> struct Type {
> > > > > > +  static const int arr[3];
> > > > > > +};
> > > > > > +
> > > > > > +extern template const int Type<double>::arr[3];
> > > > > > +template <typename T> const int Type<T>::arr[] = { 42, 43, 44 };
> > > > > > +
> > > > > > +export Type<int> ti;
> > > > > > diff --git a/gcc/testsuite/g++.dg/modules/inst-6_b.C 
> > > > > > b/gcc/testsuite/g++.dg/modules/inst-6_b.C
> > > > > > new file mode 100644
> > > > > > index 00000000000..5a8092ccb14
> > > > > > --- /dev/null
> > > > > > +++ b/gcc/testsuite/g++.dg/modules/inst-6_b.C
> > > > > > @@ -0,0 +1,12 @@
> > > > > > +// PR c++/122421
> > > > > > +// { dg-additional-options "-fmodules" }
> > > > > > +
> > > > > > +import M;
> > > > > > +
> > > > > > +int main() {
> > > > > > +  const int& a = Type<int>::arr[0];
> > > > > > +  const int& b = Type<double>::arr[0];
> > > > > > +}
> > > > > > +
> > > > > > +// { dg-final { scan-assembler {_ZNW1M4TypeIiE3arrE:} } }
> > > > > > +// { dg-final { scan-assembler-not {_ZNW1M4TypeIdE3arrE:} } }
> > > > > 
> > > > 
> > > 
> > 
> 

Reply via email to