https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121702

--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Nathaniel Shead <nsh...@gcc.gnu.org>:

https://gcc.gnu.org/g:014873fdf4bb2e00d704f182aa78b3022019ef48

commit r16-3613-g014873fdf4bb2e00d704f182aa78b3022019ef48
Author: Nathaniel Shead <nathanielosh...@gmail.com>
Date:   Fri Sep 5 13:29:12 2025 +1000

    c++/modules: Fix exported using-directive of imported namespace [PR121702]

    Currently we represent exported using-directives as a list of indices
    into the namespace array that we stream.  However this list of
    namespaces doesn't include any namespaces that we don't expose in this
    module's purview, and so we ICE.

    This patch reworks the handling to instead use the existing depset
    tracking for namespaces directly.  This means that we don't need to
    build up a second lookup map when streaming, and we can reuse the logic
    in {read,write}_namespace.  We do need to make sure that we create a
    depset for namespaces only referenced by a using-directive, though.

    I don't expect to be exporting large numbers of using-directives from a
    namespace, so for simplicity we stream the names as {parent, target}
    pairs.

    This also adjusts read handling so that we load the using-directives for
    any import (including indirect) if it's in the import list for the
    current TU.  Otherwise we run into issues if the using-directive is in
    a namespace that is otherwise never referenced in the 'export import'ing
    module, because we never walk this namespace and so never know that we
    need to emit it.  To do this the patch ensures that we calculate the
    import list before read_language is called.

    As a drive-by fix, I noticed that with modules 'add_using_namespace'
    will add duplicate using-directives because we compare usings against
    the target namespace, but we then push a wrapping USING_DECL instead.
    This reworks so that the contents of the structure is equivalent between
    modules and non-modules code.

            PR c++/121702

    gcc/cp/ChangeLog:

            * module.cc (enum module_state_counts): New counter.
            (depset::hash::add_namespace_entities): Seed using-directive
            targets for later streaming.
            (module_state::write_namespaces): Don't handle using-directives
            here.
            (module_state::read_namespaces): Likewise.
            (module_state::write_using_directives): New function.
            (module_state::read_using_directives): New function.
            (module_state::write_counts): Log using-directives.
            (module_state::read_counts): Likewise.
            (module_state::write_begin): Stream using-directives.
            (module_state::read_language): Read using-directives if
            directly importing.
            (module_state::direct_import): Update current TU import list
            before calling read_language.
            * name-lookup.cc (add_using_namespace): Fix lookup of previous
            using-directives.
            * parser.cc (cp_parser_import_declaration): Don't set
            MK_EXPORTING when performing import_module.

    gcc/testsuite/ChangeLog:

            * g++.dg/modules/namespace-10_c.C: Add check for log dump.
            * g++.dg/modules/namespace-13_a.C: New test.
            * g++.dg/modules/namespace-13_b.C: New test.
            * g++.dg/modules/namespace-13_c.C: New test.

    Signed-off-by: Nathaniel Shead <nathanielosh...@gmail.com>

Reply via email to