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

            Bug ID: 125984
           Summary: GNAT 16.1.0 generates incorrect code at -O1 for nested
                    procedure accessing enclosing-scope variables
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: ada
          Assignee: unassigned at gcc dot gnu.org
          Reporter: [email protected]
                CC: dkm at gcc dot gnu.org
  Target Milestone: ---

Created attachment 64850
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64850&action=edit
Main Program and Project File

GNAT 16.1.0 generates incorrect code at -O1 for nested procedure
accessing enclosing-scope variables

Environment:

GNAT 16.1.0 gcc.exe (Rev5, Built by MSYS2 project) 16.1.0 GPRBUILD
2026.0.0 (20260430)

Platform: Windows x86_64 MSYS2 UCRT64

Build command:

gprbuild -PTest.gpr

Observed result:

R:-4.90816800672406E+00 Test Failed

Expected result:

R: 2.45189473169632E+03 Test Ok

Observations:

-   Compiling with -O0 produces the expected result.
-   Declaring R as Volatile produces the expected result.
-   GNAT Pro 23.0 produces the expected result.
-   The attached source file is a reduced reproducer derived from a
    larger astronomical computation.
-   The problem appears related to optimization of a nested procedure
    that accesses variables declared in an enclosing scope.
-   Rewriting the nested procedure to pass all required values
    explicitly as parameters avoids the problem.
-   The reproducer fails consistently and deterministically when
    compiled with the supplied project file.

Reply via email to