llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] Incorrect execution result of assigning the value of an expression when the length of the variable is greater than the length of the expression

Open ohno-fj opened this issue 1 year ago • 3 comments

Version of flang : 21.0.0(ebba554a3211b0b98d3ae33ba70f9d6ceaab6ad4)/AArch64

According to Fortran standard 2023 : 10.2.1.3 Interpretation of intrinsic assignments:
In the attached program, if the length of variable (asc) is greater than the length of expression (["BD","CC"]), the value of the expression is written to fill the blanks on the right to be the same length as the variable.

However, unlimited polymorphic array (asc) is not appended with a blank. And ordinary arrays (ch1) is appended with a blank.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

FT_SelectType_24.f90:

program main
  type ty
     class(*),allocatable :: cptr(:)
  end type ty
  type(ty) :: obj1
  character(len=3)::kk ="AAA"
  character(len=3), allocatable :: ch1(:)
  allocate(ch1(3))
  allocate(obj1%cptr(2),source=kk)
  select type(asc=>obj1%cptr)
  type is(character(len=*))
     ch1 = ["BD","CC"]
       write(6,*) "len(ch1(1)) = ", len(ch1(1)), " ch1(1) = ", ch1(1)
       write(6,*) "len(ch1(2)) = ", len(ch1(2)), " ch1(2) = ", ch1(2)
     asc = ["BD","CC"]
       write(6,*) "len(asc(1)) = ", len(asc(1)), " asc(1) = ", asc(1)
       write(6,*) "len(asc(2)) = ", len(asc(2)), " asc(2) = ", asc(2)
  class default
     write(6,*) "error2"
  end select
  print*,"PASS"
end program main
$ flang FT_SelectType_24.f90; ./a.out
 len(ch1(1)) =  3  ch1(1) = BD 
 len(ch1(2)) =  3  ch1(2) = CC 
 len(asc(1)) =  3  asc(1) = BDC
 len(asc(2)) =  3  asc(2) = C
^@
 PASS
$
$ gfortran FT_SelectType_24.f90; ./a.out
 len(ch1(1)) =            3  ch1(1) = BD 
 len(ch1(2)) =            3  ch1(2) = CC 
 len(asc(1)) =            3  asc(1) = BD 
 len(asc(2)) =            3  asc(2) = CC 
 PASS
$
$ ifx FT_SelectType_24.f90; ./a.out
 len(ch1(1)) =            3  ch1(1) = BD 
 len(ch1(2)) =            3  ch1(2) = CC 
 len(asc(1)) =            3  asc(1) = BD 
 len(asc(2)) =            3  asc(2) = CC 
 PASS
$

ohno-fj avatar Apr 21 '25 01:04 ohno-fj

Lowering should generate a call to AssignExplicitLengthCharacter when the LHS of the assignment is allocatable but not deferred-length, as is the case here.

klausler avatar Apr 21 '25 16:04 klausler

@ohno-fj this seems fixed. Please check and close if it's the case.

ceseo avatar Jul 02 '25 14:07 ceseo

Thanks for your comment, @ceseo . I confirmed that this issue is corrected. I close this.

$ flang --version flang version 21.0.0git (https://github.com/llvm/llvm-project.git b822a32659c3fd5afd75c8f028aaa142fbd68e20) Target: aarch64-unknown-linux-gnu Thread model: posix InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20250630-b822a32659c3/bin Build config: +assertions $ $ flang FT_SelectType_24.f90; ./a.out len(ch1(1)) = 3 ch1(1) = BD len(ch1(2)) = 3 ch1(2) = CC len(asc(1)) = 3 asc(1) = BD len(asc(2)) = 3 asc(2) = CC PASS $

ohno-fj avatar Jul 03 '25 02:07 ohno-fj