See attached patch. A label on the END statement of a program unit with
a CONTAINS section caused an ICE in gfc_get_location_with_offset when
nothing branched to it. This is a mainline regression from the PR126909
fix (34762afa322), which moves such a label from the scratch namespace
that parse_contained opens to the host, but created the host label
without a source location. The same code missed the host of an unnamed
main program and of a module procedure, and could take the wrong label
number. The patch defines the label in the parent of the scratch
namespace, which is always the host, with gfc_define_st_label and the
label and location of the statement; that also reports a duplicate of
a host label.

The fix is in accept_statement in parse.cc; the rest is the two new
tests.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

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.

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.

gcc/testsuite/ChangeLog:

        * gfortran.dg/label_6.f90: New test.
        * gfortran.dg/label_7.f90: New test.
>From c52f5bf7265e58235efe431e89c63b9893d5f909 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sun, 15 Nov 2026 13:13:52 -0800
Subject: [PATCH] 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.

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.

gcc/testsuite/ChangeLog:

	* gfortran.dg/label_6.f90: New test.
	* gfortran.dg/label_7.f90: New test.
---
 gcc/fortran/parse.cc                  | 19 ++++-------
 gcc/testsuite/gfortran.dg/label_6.f90 | 46 +++++++++++++++++++++++++++
 gcc/testsuite/gfortran.dg/label_7.f90 |  9 ++++++
 3 files changed, 62 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/label_6.f90
 create mode 100644 gcc/testsuite/gfortran.dg/label_7.f90

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index d90ed7809a9..e0e15276a19 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 ();
diff --git a/gcc/testsuite/gfortran.dg/label_6.f90 b/gcc/testsuite/gfortran.dg/label_6.f90
new file mode 100644
index 00000000000..2e59ed5e38d
--- /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 00000000000..885003b10e8
--- /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" }
-- 
2.56.0

Reply via email to