https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101199
Bug ID: 101199
Summary: program changes the value of a dummy argument
Product: gcc
Version: 11.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: ygalklein at gmail dot com
Target Milestone: ---
I have written the following example code:
```fortran
module mod_original_struct
implicit none
private
public :: original_struct, extended_struct
type original_struct
private
real, PUBLIC :: var1
real :: var2, var3, var4
real, dimension(3) :: vec1, vec2
contains
private
procedure, public :: init=>initoriginal_struct
end type original_struct
type, extends(original_struct) :: extended_struct
private
type(original_struct), dimension(2), public :: origs
contains
private
procedure, public :: init=>initextended_struct, advance
end type extended_struct
contains
subroutine advance(this)
class(extended_struct), intent(inout) :: this
print*, 'head of advance, this%var1 = ', this%var1
call this%init(vec1=this%vec1(:),&
vec2=this%vec2(:),&
var1=this%var1,&
var2=this%var2,&
var3=this%var3,&
var4=this%var4,&
origs=this%origs)
end subroutine advance
subroutine initoriginal_struct(this, vec1, vec2, var1, var2, var3, var4,
origs)
class(original_struct), intent(out) :: this
real, dimension(3), intent(in) :: vec1, vec2
real, intent(in) :: var1, var2, var3, var4
type(original_struct), intent(in), dimension(2), optional :: origs
this%vec1(:) = vec1(:)
this%vec2(:) = vec2(:)
this%var1 = var1
this%var2 = var2
this%var3 = var3
this%var4 = var4
if (present(origs)) error stop "initoriginal_struct was called with
origs, this is strange"
end subroutine initoriginal_struct
subroutine initextended_struct(this, vec1, vec2, var1, var2, var3, var4,
origs)
class(extended_struct), intent(out) :: this
real, dimension(3), intent(in) :: vec1, vec2
real, intent(in) :: var1, var2, var3, var4
type(original_struct), intent(in), dimension(2), optional :: origs
print*, 'head of initextended_struct, the input argument var1 = ', var1
this%vec1(:) = vec1(:)
this%vec2(:) = vec2(:)
this%var1 = var1
this%var2 = var2
this%var3 = var3
this%var4 = var4
if (.not. present(origs)) error stop "initextended_struct was called
without origs"
this%origs(:) = origs(:)
end subroutine initextended_struct
end module mod_original_struct
program example
use mod_original_struct, only: extended_struct, original_struct
implicit none
type(original_struct), DIMENSION(2) :: origs
type(extended_struct) :: extended1
call origs(1)%init(vec1=[0.5, 0., 0.],&
vec2=[0., 0.3, 0.],&
var1=3.,&
var2=1.2,&
var3=0.4,&
var4=4.5)
call origs(2)%init(vec1=[0., 0.8, 0.],&
vec2=[0.2, 0., 0.],&
var1=4.,&
var2=1.5,&
var3=0.5,&
var4=6.5)
call extended1%init(vec1=[0., 0., 0.7],&
vec2=[0.2, 0., 0.],&
var1=10.,&
var2=6.,&
var3=3.2,&
var4=10.,&
origs=origs)
print*, 'bf advance, extended1%var1 = ', extended1%var1
call extended1%advance()
end program example
```
when compiling with gfortran 11.1, the program compiles succesfully and the
output is:
head of initextended_struct, the input argument var1 = 10.000000000000000
bf advance, extended1%var1 = 10.000000000000000
head of advance, this%var1 = 10.000000000000000
head of initextended_struct, the input argument var1 = 0.0000000000000000
One can see that the value of var1 that was sent to init of the extented struct
in the routine advance has changed its value from the line bf the call to init
to the head of init.
This is a failure of the program.
The same happens with gfortran 10.3 and also gfortran 9.3 and also gfortran 8.2
and also 7.3 and also 6.4 and also 5.4 and also 4.8.2
One should note that compiling with intel compiler 2020u4 - compiles
succesfully and the output is:
head of initextended_struct, the input argument var1 = 10.00000
bf advance, extended1%var1 = 10.00000
head of advance, this%var1 = 10.00000
head of initextended_struct, the input argument var1 = 10.00000
as one can see - using intel compiler one has no problem and the value does not
change.