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

--- Comment #9 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:4a99443c5dd9a235022652ba0fb143c6370ea99d

commit r15-4861-g4a99443c5dd9a235022652ba0fb143c6370ea99d
Author: Nathaniel Shead <nathanielosh...@gmail.com>
Date:   Thu Oct 31 20:54:29 2024 +1100

    c++/modules: Fix recursive dependencies [PR116317]

    In cases like the linked PR we sometimes get mutually recursive
    dependencies that both rely on the other to have been streamed as part
    of their merge key information.  In the linked PR, this causes an ICE.

    The root cause is that 'sort_cluster' is not correctly ordering the
    dependencies; both the element_t specialisation and the
    reverse_adaptor::first function decl depend on each other, but by
    streaming element_t first it ends up trying to stream itself recursively
    as part of calculating its own merge key, which apart from the checking
    ICE will also cause issues on stream-in, as the merge key will not
    properly stream.

    There is a comment already in 'sort_cluster' describing this issue, but
    it says:

         Finding the single cluster entry dep is very tricky and
         expensive.  Let's just not do that.  It's harmless in this case
         anyway.

    However in this case it was not harmless: it's just somewhat luck that
    the sorting happened to work for the existing cases in the testsuite.

    This patch solves the issue by noting any declarations that rely on deps
    first seen within their own merge key.  This declaration gets marked as
    an "entry" dep; any of these deps that end up recursively referring back
    to that entry dep as part of their own merge key do not.

    Then within sort_cluster we can ensure that the entry dep is written to
    be streamed first of its cluster; this will ensure that any other deps
    are just emitted as back-references, and the mergeable dep itself will
    structurally decompose.

            PR c++/116317

    gcc/cp/ChangeLog:

            * module.cc
            (depset::DB_MAYBE_RECURSIVE_BIT): New flag.
            (depset::DB_ENTRY_BIT): New flag.
            (depset::is_maybe_recursive): New accessor.
            (depset::is_entry): New accessor.
            (depset::hash::writing_merge_key): New field.
            (trees_out::decl_value): Inform dep_hash while we're writing the
            merge key information for a decl.
            (depset::hash::add_dependency): Find recursive deps and mark the
            entry point.
            (sort_cluster): Ensure that the entry dep is streamed first.

    gcc/testsuite/ChangeLog:

            * g++.dg/modules/late-ret-4_a.H: New test.
            * g++.dg/modules/late-ret-4_b.C: New test.

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

Reply via email to