When playing with gfortran-14.2.0
, I encountered an issue with -Wmaybe-uninitialized
as illustrated below.
$ uname -a && gfortran --version && gfortran -Werror -Wuninitialized -fsanitize=undefined -O2 test_uninit.f90
Linux 6.14.0-27-generic #27~24.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 17:38:49 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
GNU Fortran (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
test_uninit.f90:36:19:
36 | f = [(0.0, i=1, k)]
| ^
Error: ‘MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized [-Werror=maybe-uninitialized]
f951: all warnings being treated as errors
Here, test_uninit.f90 is the code below (see also test_compiler/test_uninit.f90 at master · zequipe/test_compiler · GitHub).
!--------------------------------------------------------------------------------------------------!
! test_uninit.f90
!
! This test illustrates a bug in gfortran 14. With
! ```
! gfortran -Werror -Wuninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! or
! ```
! gfortran -Werror -Wmaybe-uninitialized -fsanitize=undefined -O2 test_uninit.f90
! ```
! it fails with:
! ```
! test_uninit.f90:36:19:
!
! 36 | f = [(0.0, i=1, k)]
! | ^
! Error: ‘MEM <real(kind=4)[0:]> [(real(kind=4)[0:] *)_32][0]’ may be used uninitialized [-Werror=maybe-uninitialized]
! f951: all warnings being treated as errors
! ```
! Even with `Wuninitialized`, the error is still `-Werror=maybe-uninitialized`.
! The same error occurs with `-O3`, `-O4`, and `-Ofast`, but not with `-O` `-O0`, or `-O1`.
!--------------------------------------------------------------------------------------------------!
module test_mod
implicit none
private
public :: test
contains
subroutine test(k)
implicit none
integer(8), intent(in) :: k
integer(8) :: i
real :: f(k)
f = [(0.0, i=1, k)]
f = 0.0
end subroutine test
end module test_mod
program test_program
end program test_program
Any idea why this happens? Thanks.
N.B
- This example is minimal. Any change to the code or the compiler flags will make the error disappear.
- Surely,
integer(8)
is not standard-conforming. I wrote it in this way becausegfortran
is the only compiler concerned. I would not write this kind of code in production. - Even with
-Wuninitialized
, the error still says “-Werror=maybe-uninitialized”.