https://gcc.gnu.org/g:7bbecd581aa6f87c68d9d1088ee97b1d7f96005a
commit r17-6641-g7bbecd581aa6f87c68d9d1088ee97b1d7f96005a Author: Jerry DeLisle <[email protected]> Date: Sun Nov 15 13:13:52 2026 -0800 fortran: [PR128316] ICE for a labeled END statement after CONTAINS A label on the END statement of a program unit with a CONTAINS section caused an ICE when nothing branched to it, because the fix for PR126909 moved the label to the host without a source location. That code also found the host through the symbol of the enclosing state, which is null for an unnamed main program, so a GOTO to the label was rejected there; it skipped module procedures, and took the label number from the root of the label tree rather than from the END statement. Define the label in the parent of the scratch namespace that parse_contained opens, which is always the host, with gfc_define_st_label and the label and location of the statement. That also diagnoses a duplicate of a host label. The label of an END MODULE or END SUBMODULE statement after a CONTAINS section was dropped with the scratch namespace, so a duplicate of it was accepted; define it in the module. Assisted-by: Claude Opus 5.5 PR fortran/128316 gcc/fortran/ChangeLog: * parse.cc (accept_statement): For a labeled END statement after a CONTAINS section, define the label in the host namespace with its source location, for any host program unit. Likewise for END MODULE and END SUBMODULE. gcc/testsuite/ChangeLog: * gfortran.dg/label_6.f90: New test. * gfortran.dg/label_7.f90: New test. * gfortran.dg/label_8.f90: New test. Diff: --- gcc/fortran/parse.cc | 35 +++++++++++++++++--------- gcc/testsuite/gfortran.dg/label_6.f90 | 46 +++++++++++++++++++++++++++++++++++ gcc/testsuite/gfortran.dg/label_7.f90 | 9 +++++++ gcc/testsuite/gfortran.dg/label_8.f90 | 33 +++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 12 deletions(-) diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc index c8e60c9d665b..8229bbed30a1 100644 --- a/gcc/fortran/parse.cc +++ b/gcc/fortran/parse.cc @@ -3273,21 +3273,16 @@ accept_statement (gfc_statement st) case ST_END_SUBROUTINE: if (gfc_statement_label != NULL) { - /* After a contains section, a new namespace is started together with - a new state_stack. The statement label must be attached to the - previous state after finding the label in its namespace. */ - if (gfc_state_stack->head == NULL - && gfc_state_stack->previous - && gfc_state_stack->previous->sym - && gfc_state_stack->previous->sym->ns - && gfc_state_stack->previous->sym->ns->parent == NULL) + /* After a contains section, the label was defined in a scratch + namespace. Define it in the host and add the statement there. */ + if (gfc_state_stack->state == COMP_CONTAINS) { - int value = gfc_current_ns->st_labels->value; gfc_state_data *previous_state = gfc_state_stack; gfc_namespace *old_ns = gfc_current_ns; - gfc_current_ns = gfc_state_stack->previous->sym->ns; - new_st.here = gfc_get_st_label (value); - new_st.here->defined = ST_LABEL_TARGET; + gfc_current_ns = old_ns->parent; + new_st.here = gfc_get_st_label (gfc_statement_label->value); + gfc_define_st_label (new_st.here, ST_LABEL_TARGET, + &gfc_statement_label->where); new_st.op = EXEC_RETURN; gfc_state_stack = gfc_state_stack->previous; add_statement (); @@ -3308,6 +3303,22 @@ accept_statement (gfc_statement st) break; + case ST_END_MODULE: + case ST_END_SUBMODULE: + /* After a contains section, the label was defined in a scratch + namespace. Define it in the module. */ + if (gfc_statement_label != NULL + && gfc_state_stack->state == COMP_CONTAINS) + { + gfc_namespace *old_ns = gfc_current_ns; + gfc_current_ns = old_ns->parent; + gfc_define_st_label (gfc_get_st_label (gfc_statement_label->value), + ST_LABEL_BAD_TARGET, + &gfc_statement_label->where); + gfc_current_ns = old_ns; + } + break; + case ST_ENTRY: case ST_OMP_METADIRECTIVE: case ST_OMP_BEGIN_METADIRECTIVE: diff --git a/gcc/testsuite/gfortran.dg/label_6.f90 b/gcc/testsuite/gfortran.dg/label_6.f90 new file mode 100644 index 000000000000..2e59ed5e38d1 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/label_6.f90 @@ -0,0 +1,46 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! +! PR 128316 + +module m + implicit none +contains + subroutine s (i) + integer :: i + if (i > 0) goto 20 + i = -1 + contains + subroutine t + end subroutine +20 end subroutine +end module + +subroutine unref +contains + subroutine u + end subroutine +30 end subroutine + + use m + implicit none + type ty + end type + integer :: i + i = 5 + call s (i) + if (i /= 5) stop 1 + i = 0 + call s (i) + if (i /= -1) stop 2 + call unref + goto 17 + stop 3 +contains + subroutine sub + end subroutine +17 end + +! { dg-final { scan-tree-dump-times "__label_000017:" 1 "original" } } +! { dg-final { scan-tree-dump-times "__label_000020:" 1 "original" } } +! { dg-final { scan-tree-dump-times "__label_000030:" 1 "original" } } diff --git a/gcc/testsuite/gfortran.dg/label_7.f90 b/gcc/testsuite/gfortran.dg/label_7.f90 new file mode 100644 index 000000000000..885003b10e88 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/label_7.f90 @@ -0,0 +1,9 @@ +! { dg-do compile } +! +! PR 128316 + +17 continue ! { dg-error "(1)" } +contains + subroutine s + end subroutine +17 end ! { dg-error "Duplicate statement label 17" } diff --git a/gcc/testsuite/gfortran.dg/label_8.f90 b/gcc/testsuite/gfortran.dg/label_8.f90 new file mode 100644 index 000000000000..79980cd0afeb --- /dev/null +++ b/gcc/testsuite/gfortran.dg/label_8.f90 @@ -0,0 +1,33 @@ +! { dg-do compile } +! { dg-options "-Wunused-label" } +! +! PR 128316 + +module m1 +10 integer :: x ! { dg-error "(1)" } +! { dg-warning "defined but cannot be used" "" { target *-*-* } .-1 } +contains + subroutine s + end subroutine +10 end module m1 ! { dg-error "Duplicate statement label 10" } + +module m2 + interface + module subroutine s + end subroutine + end interface +end module m2 + +submodule (m2) sm +20 integer :: y ! { dg-error "(1)" } +! { dg-warning "defined but cannot be used" "" { target *-*-* } .-1 } +contains + module subroutine s + end subroutine +20 end submodule sm ! { dg-error "Duplicate statement label 20" } + +module m3 +contains + subroutine s + end subroutine +30 end module m3 ! { dg-warning "defined but cannot be used" }
