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

--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Uecker <[email protected]>:

https://gcc.gnu.org/g:40ff519a133b643c9ba0aa01289b5ffb6c4bd42b

commit r17-6668-g40ff519a133b643c9ba0aa01289b5ffb6c4bd42b
Author: Martin Uecker <[email protected]>
Date:   Mon Nov 16 23:42:15 2026 +0100

    c: mark nested functions as requiring a static chain earlier [PR128760]

    When determining whether we can statically initialize a variable
    using a pointer to a nested function, it is checked that DECL_STATIC_CHAIN
    is not set.  But DECL_STATIC_CHAIN is set only after the function
    body was parsed, which can be too late.  The logic to deactivate the
    trampoline also had a flaw (mentioned in PR128518).  Until the function
    is completely parsed, we need to conservatively assume that it might
    require a static chain. Also forward declarations already need to be
    taken into account.

    To fix both issues, we set DECL_STATIC_CHAIN already when we have seen
    the declaration and then clear it at the end of the function when
    C_DECL_NONLOCAL_CONTEXT was not set.  The checks for whether a chain
    is needed are changed to use DECL_STATIC_CHAIN.

    Hence, until the body is processed, DECL_STATIC_CHAIN indicates that
    the function might need a static chain and C_FUNC_NONLOCAL_CONTEXT
    that it is known for certain that it will.  Afterwards both are
    the same until lower_nested_functions in tree-nested.cc recomputes
    DECL_STATIC_CHAIN in a later pass.

            PR c/128760
            PR c/128518

    gcc/c/ChangeLog:
            * c-decl.cc (start_decl, start_function): Set DECL_STATIC_CHAIN
            for nested functions.
            (finish_finish): Clear DECL_STATIC_CHAIN if not needed.
            * c-parser.cc (c_parser_declaration_or_fndef): Do not set
            DECL_STATIC_CHAIN.
            * c-typeck.cc (function_to_pointer_conversion,mark_decl_used):
            Use DECL_STATIC_CHAIN instead of C_FUNC_NONLOCAL_CONTEXT.
            (merge_decls): Add assertion.

    gcc/testsuite/ChangeLog:
            * gcc.dg/pr128760.c: New test.
            * gcc.dg/pr128518.c: Add dg-effective-target.
            * gcc.dg/pr128518-2.c: New test.
            * gcc.dg/Wreturn-nested-4.c: New test.

Reply via email to