This wrong code bug is fixed by the attached patch.

I plan commit as simple and obvious.

It's not even a oneliner

Regression tested on X86_64.

Regards,

Jerry

---

    fortran: [PR82065] ISO_FORTRAN_ENV array constants used in a procedure

    Array-valued named constants from ISO_FORTRAN_ENV are forced TREE_STATIC
    so they can be inlined, but the static initializer was only emitted when
    the symbol's namespace belonged to the main program or a module.  Used
    inside any procedure the array was emitted uninitialized, yielding zeros.

    Assisted-by: Claude Opus 5

            PR fortran/82065

    gcc/fortran/ChangeLog:

            * trans-decl.cc (gfc_get_symbol_decl): Emit the static
            initializer for intrinsic array parameters regardless of the
            enclosing namespace.

    gcc/testsuite/ChangeLog:

            * gfortran.dg/iso_fortran_env_10.f90: New test.
commit 835d6510371cbe512256856a8b3c02564b88ed10
Author: Jerry DeLisle <[email protected]>
Date:   Sun Aug 16 11:57:30 2026 -0700

    fortran: [PR82065] ISO_FORTRAN_ENV array constants used in a procedure
    
    Array-valued named constants from ISO_FORTRAN_ENV are forced TREE_STATIC
    so they can be inlined, but the static initializer was only emitted when
    the symbol's namespace belonged to the main program or a module.  Used
    inside any procedure the array was emitted uninitialized, yielding zeros.
    
    Assisted-by: Claude Opus 5
    
            PR fortran/82065
    
    gcc/fortran/ChangeLog:
    
            * trans-decl.cc (gfc_get_symbol_decl): Emit the static
            initializer for intrinsic array parameters regardless of the
            enclosing namespace.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/iso_fortran_env_10.f90: New test.

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 1d85e5b94cd..7f46900244a 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -2117,7 +2117,8 @@ gfc_get_symbol_decl (gfc_symbol * sym)
       && !(sym->attr.use_assoc && !intrinsic_array_parameter)
       && (sym->attr.save || sym->ns->proc_name->attr.is_main_program
 	  || !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl))
-	  || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE)
+	  || sym->attr.data || sym->ns->proc_name->attr.flavor == FL_MODULE
+	  || intrinsic_array_parameter)
       && (flag_coarray != GFC_FCOARRAY_LIB
 	  || !sym->attr.codimension || sym->attr.allocatable)
       && !(IS_PDT (sym) || IS_CLASS_PDT (sym)))
diff --git a/gcc/testsuite/gfortran.dg/iso_fortran_env_10.f90 b/gcc/testsuite/gfortran.dg/iso_fortran_env_10.f90
new file mode 100644
index 00000000000..0b06943d3e9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/iso_fortran_env_10.f90
@@ -0,0 +1,50 @@
+! { dg-do run }
+!
+! PR fortran/82065
+!
+! Array-valued named constants from ISO_FORTRAN_ENV were emitted without
+! a static initializer when used inside a procedure.
+
+module m
+contains
+  subroutine mod_proc
+    use iso_fortran_env, only : integer_kinds
+    if (size (integer_kinds) < 1) stop 1
+    if (integer_kinds(1) < 1) stop 2
+  end subroutine
+end module
+
+program main
+  use m
+  use iso_fortran_env
+  implicit none
+
+  call check (integer_kinds)
+  call testsub
+  call testsub2
+  call mod_proc
+
+contains
+
+  subroutine check (k)
+    integer, intent(in) :: k(:)
+    if (size (k) < 1) stop 3
+    if (any (k < 1)) stop 4
+  end subroutine
+
+  subroutine testsub
+    if (size (integer_kinds) < 1) stop 5
+    if (any (integer_kinds < 1)) stop 6
+    call check (integer_kinds)
+  end subroutine
+
+  subroutine testsub2
+    use iso_fortran_env, only : x => integer_kinds, y => real_kinds, &
+                                z => logical_kinds, c => character_kinds
+    if (any (x < 1)) stop 7
+    if (any (y < 1)) stop 8
+    if (any (z < 1)) stop 9
+    if (any (c < 1)) stop 10
+  end subroutine
+
+end program main

Reply via email to