https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94221
Bug ID: 94221
Summary: Explicit assignment in type is ignored in some cases
Product: gcc
Version: 9.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: r.dilz at tue dot nl
Target Milestone: ---
When working with types, I found some unexpected behavior.
It seems that the variables in a type do not get assigned their proper values
reliably. I suspect that the compiler does not check well enough whether a
variable is used or not and the whole assignment is then optimized away.
Below is a minimal example:
module pyimp
use iso_c_binding
type,bind(C) :: container
integer(C_INT) :: val = 3 !assignment here is not reliable in gfortran
end type container
contains
type(container) function test() bind(c,name='test')
print*,"running test"
print*,test%val
end function test
type(container) function test2() bind(c,name='test2')
print*,"running test - here test%val is not initialized"
end function test2
end module pyimp
program main
use pyimp
type(container) :: a
print*,"First: the test%var is initialized by an extra print statement:"
a = test()
print*,a%val
a = test2()
print*,"But in test2 it is not: "
print*,a%val
end program main
Compiled with fortran 4.8 and fortran 9.2.1, both give the same output:
First: the test%var is initialized by an extra print statement:
running test
3
3
running test - here test%val is not initialized
But in test2 it is not:
0
Whereas in test2 it should also output 3. Using ifort, I do get the expected
result for test2.