GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

Fortran: Fix lost statement label in CONTAINS sections

Corrects a bug where statement labels were lost in Fortran programs with CONTAINS sections.

This commit fixes a bug in the Fortran compiler where statement labels were not correctly preserved in programs containing a CONTAINS section. The issue arose because a new namespace was created after the CONTAINS block, causing labels to be associated with the wrong scope. The fix ensures that statement labels are correctly assigned to the containing namespace and that labelled RETURN statements are properly handled.

In Details

In GFortran's parser, statement labels are associated with specific scopes. When a CONTAINS section is encountered, a new scope is implicitly created. This change in parse.cc modifies accept_statement to correctly re-establish the outer scope after processing the CONTAINS block, ensuring that any subsequent statement labels are correctly attached to the parent scope, not the inner one. It also adjusts the handling of labelled RETURN statements within this context.

For Context
CONTAINS section
A Fortran construct that separates a main program or module from its internal subprograms (functions and subroutines).
statement label
A numeric identifier assigned to a Fortran statement, used for control flow (e.g., GO TO, DO loop termination) or for referencing statements.
namespace
In programming, a declarative region that provides a scope to the identifiers (names of types, functions, variables, etc.) inside it. In this context, it refers to distinct scopes for parsing Fortran code elements.
Filed Under: fortranparserbugfix