https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109345
Bug ID: 109345
Summary: class(*) variable that is a string array is not
handled correctly
Product: gcc
Version: 12.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: lchilutti at gmail dot com
Target Milestone: ---
program test
implicit none
character(len=255) :: str_array(2) !< Array of strings
str_array(1) = ""
str_array(2) = ""
str_array(1) = "2880:2880,1:2160::1:1,1:2160"
str_array(2) = "1:1440,2160:2160::2880:1441,2160:2160"
call foo(str_array) !< class(*)
call foo2(str_array) !< char type
call foo3(str_array) !< class(*) with str copy
contains
subroutine foo ( var) !< Uses class(*)
class(*), intent(in) :: var(:)
integer :: i
select type (var)
type is (character(len=*))
print *, "Using class(*)"
print *, "The size of var:", size(var)
do i = 1, size(var)
print *, len_trim(var(i)), "data:", trim(var(i))
enddo
end select
end subroutine
subroutine foo2 (var) !< Uses char type
character(len=*), intent(in) :: var(:)
integer :: i
print *, "Not using class(*)"
print *, "The size of var:", size(var)
do i = 1, size(var)
print *, len_trim(var(i)), "data:", trim(var(i))
enddo
end subroutine
subroutine foo3 (var) !< Uses class(*) with a workaround
class(*), intent(in) :: var(:)
integer :: i
character(len=:), dimension(:), allocatable :: str
select type (var)
type is (character(len=*))
str = var !< if you copy the class(*) var to an allocatable string it works
print *, "Using class(*), with string copy workaround"
print *, "The size of str:", size(str)
do i = 1, size(str)
print *, len_trim(str(i)), "data:", trim(str(i))
enddo
end select
end subroutine
end program
If I compile the above program with gfortran (gcc version 12.2.0), I get the
following output:
Using class(*)
The size of var: 2
28 data:2880:2880,1:2160::1:1,1:2160
255 data:880:2880,1:2160::1:1,1:2160
1
Not using class(*)
The size of var: 2
28 data:2880:2880,1:2160::1:1,1:2160
37 data:1:1440,2160:2160::2880:1441,2160:2160
Using class(*), with string copy workaround
The size of str: 2
28 data:2880:2880,1:2160::1:1,1:2160
37 data:1:1440,2160:2160::2880:1441,2160:2160
You can see that when we give an array of strings as an argument to a function
using class(*) and select type, the string returned within the select type
function is incorrect. We found a workaround in which we can copy the class(*)
input variable to an allocatable string within the select type statement, but
we should not have to do this.