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

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

https://gcc.gnu.org/g:c567a7760cf3f7821a376b0ee8192ce9d9e5ed42

commit r17-1042-gc567a7760cf3f7821a376b0ee8192ce9d9e5ed42
Author: Jerry DeLisle <[email protected]>
Date:   Tue May 26 19:21:52 2026 -0700

    fortran: fix ICE with procedure pointer declared in BLOCK

    Procedure pointer declared inside a BLOCK construct in a program that has
    contained procedures caused an ICE in convert_nonlocal_reference_op
    (tree-nested.cc) because get_proc_pointer_decl set the proc pointer's
    DECL_CONTEXT to NULL instead of the enclosing program function decl.

    The root cause: the condition to call gfc_add_decl_to_function vs
    gfc_add_decl_to_parent_function checked whether proc_name->backend_decl
    matched current_function_decl.  For a BLOCK namespace the proc_name has
    FL_LABEL flavor and its backend_decl is never set, so the condition failed
    and gfc_add_decl_to_parent_function was called.  That function sets
    DECL_CONTEXT to DECL_CONTEXT(current_function_decl), which is NULL for a
    top-level program.  The tree-nested pass then found no nesting level
    matching target_context = NULL and crashed in the internal_error call
    dereferencing the NULL target_context.

    Fix: add the missing BLOCK namespace check (FL_LABEL flavor) so that
    procedure pointers in BLOCK constructs are treated like regular variables
    and added to the enclosing function via gfc_add_decl_to_function.

    Assisted by: Claude Sonnet 4.6

            PR fortran/105582

    gcc/fortran/ChangeLog:

            * trans-decl.cc (get_proc_pointer_decl): Add FL_LABEL check to
            route BLOCK-construct procedure pointers to
gfc_add_decl_to_function
            rather than gfc_add_decl_to_parent_function.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/block_proc_ptr_1.f90: New test.

Reply via email to