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

[Flang] Array with allocatable attribute is dynamically created without allocate statement

Open ohno-fj opened this issue 2 years ago • 6 comments

Version of flang-new : 19.0.0(73447a3302541c343525570222b318e7f94f9402)/AArch64

Array with allocatable attribute is dynamically created without allocate statement.
This is an error checking program. The result value is true when the array is checked in the program with allocated intrinsic function.

Gfortran outputs a warning message (with -Wall option) and does not seem to dynamically create the array. (The result value of allocated intrinsic function is true.)
ifort appears to output a warning message and create the array dynamically. (The result value of allocated intrinsic function is false.)

It seems that Flang-new creates the array dynamically, but do we need to output a warning message? (The result value of allocated intrinsic function is true.)

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

fe1nalc34_2.f95:

module typ
  type xxx
     integer,allocatable :: ai(:)
  end type xxx
end module typ

module mod
  use typ
contains
  subroutine modsub(aa,item)
    type(xxx),intent(in) :: aa
    integer,intent(in) :: item
    integer :: error = 0
    select case(item)
    case(1)
       if (allocated(aa%ai )) error = error+1
       print *," +++ fe1nalc34-S-1 : NG +++ ",error
    end select
  end subroutine modsub
end module mod

program fe1nalc34
  use typ
  use mod
  integer,allocatable :: ai(:)
  type(xxx) :: aa
  integer :: res
  interface
     function funi(item) result(ai)
       integer,allocatable :: ai(:)
       integer,intent(in)  :: item
     endfunction funi
  end interface
  call modsub(xxx(funi(2)),1)
end program fe1nalc34

function funi(item) result(ai)
  integer,allocatable :: ai(:)
  integer,intent(in)  :: item
  select case(item)
  case(1)
     return
  end select
end function funi

$ flang-new fe1nalc34_2.f95; ./a.out
  +++ fe1nalc34-S-1 : NG +++  1
$
$ gfortran fe1nalc34_2.f95; ./a.out
fe1nalc34_2.f95:37:30:

   37 | function funi(item) result(ai)
      |                              1
Warning: Unused variable ‘ai’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:37:30:

   37 | function funi(item) result(ai)
      |                              1
Warning: Return value ‘ai’ of function ‘funi’ declared at (1) not set [-Wreturn-type]
fe1nalc34_2.f95:26:17:

   26 |   type(xxx) :: aa
      |                 1
Warning: Unused variable ‘aa’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:25:30:

   25 |   integer,allocatable :: ai(:)
      |                              1
Warning: Unused variable ‘ai’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:27:16:

   27 |   integer :: res
      |                1
Warning: Unused variable ‘res’ declared at (1) [-Wunused-variable]
  +++ fe1nalc34-S-1 : NG +++            0
$
$ ifort -diag-disable=10448 fe1nalc34_2.f90
fe1nalc34_22.f90(38): warning #6178: The return value of this FUNCTION has not been defined.   [AI]
function funi(item) result(ai)
---------------------------^
  +++ fe1nalc34-S-1 : NG +++            1
$

ohno-fj avatar Jul 16 '24 02:07 ohno-fj

@llvm/issue-subscribers-flang-frontend

Author: None (ohno-fj)

``` Version of flang-new : 19.0.0(73447a3302541c343525570222b318e7f94f9402)/AArch64 ```

Array with allocatable attribute is dynamically created without allocate statement.
This is an error checking program. The result value is true when the array is checked in the program with allocated intrinsic function.

Gfortran outputs a warning message (with -Wall option) and does not seem to dynamically create the array. (The result value of allocated intrinsic function is true.)
ifort appears to output a warning message and create the array dynamically. (The result value of allocated intrinsic function is false.)

It seems that Flang-new creates the array dynamically, but do we need to output a warning message? (The result value of allocated intrinsic function is true.)

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

fe1nalc34_2.f95:

module typ
  type xxx
     integer,allocatable :: ai(:)
  end type xxx
end module typ

module mod
  use typ
contains
  subroutine modsub(aa,item)
    type(xxx),intent(in) :: aa
    integer,intent(in) :: item
    integer :: error = 0
    select case(item)
    case(1)
       if (allocated(aa%ai )) error = error+1
       print *," +++ fe1nalc34-S-1 : NG +++ ",error
    end select
  end subroutine modsub
end module mod

program fe1nalc34
  use typ
  use mod
  integer,allocatable :: ai(:)
  type(xxx) :: aa
  integer :: res
  interface
     function funi(item) result(ai)
       integer,allocatable :: ai(:)
       integer,intent(in)  :: item
     endfunction funi
  end interface
  call modsub(xxx(funi(2)),1)
end program fe1nalc34

function funi(item) result(ai)
  integer,allocatable :: ai(:)
  integer,intent(in)  :: item
  select case(item)
  case(1)
     return
  end select
end function funi

$ flang-new fe1nalc34_2.f95; ./a.out
  +++ fe1nalc34-S-1 : NG +++  1
$
$ gfortran fe1nalc34_2.f95; ./a.out
fe1nalc34_2.f95:37:30:

   37 | function funi(item) result(ai)
      |                              1
Warning: Unused variable ‘ai’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:37:30:

   37 | function funi(item) result(ai)
      |                              1
Warning: Return value ‘ai’ of function ‘funi’ declared at (1) not set [-Wreturn-type]
fe1nalc34_2.f95:26:17:

   26 |   type(xxx) :: aa
      |                 1
Warning: Unused variable ‘aa’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:25:30:

   25 |   integer,allocatable :: ai(:)
      |                              1
Warning: Unused variable ‘ai’ declared at (1) [-Wunused-variable]
fe1nalc34_2.f95:27:16:

   27 |   integer :: res
      |                1
Warning: Unused variable ‘res’ declared at (1) [-Wunused-variable]
  +++ fe1nalc34-S-1 : NG +++            0
$
$ ifort -diag-disable=10448 fe1nalc34_2.f90
fe1nalc34_22.f90(38): warning #<!-- -->6178: The return value of this FUNCTION has not been defined.   [AI]
function funi(item) result(ai)
---------------------------^
  +++ fe1nalc34-S-1 : NG +++            1
$

llvmbot avatar Jul 16 '24 02:07 llvmbot

Reduced example:

program p
  type t
    integer, allocatable :: c(:)
  end type
  call s(t(f()))
 contains
  function f()
    integer, allocatable :: f(:)
  end
  subroutine s(x)
    type(t), intent(in) :: x
    print *, allocated(x%c)
  end
end

The program is incorrect because the result value of the function is never defined.

klausler avatar Jul 16 '24 15:07 klausler

Note that it is incorrect to not allocate the allocatable result before returning it because on the caller side, the the function reference is not an allocatable. See Fortran 2023 8.5.3 note: "Only variables and components can have the ALLOCATABLE attribute. The result of referencing a function whose result variable has the ALLOCATABLE attribute is a value that does not itself have the ALLOCATABLE attribute."

jeanPerier avatar Jul 16 '24 16:07 jeanPerier

Note that it is incorrect to not allocate the allocatable result before returning it because on the caller side, the the function reference is not an allocatable. See Fortran 2023 8.5.3 note: "Only variables and components can have the ALLOCATABLE attribute. The result of referencing a function whose result variable has the ALLOCATABLE attribute is a value that does not itself have the ALLOCATABLE attribute."

It's not really relevant to this erroneous program, though; even if the function's result had been allocated, it would still be in error, since its value would remain undefined.

klausler avatar Jul 16 '24 16:07 klausler

Relevant PR: https://github.com/llvm/llvm-project/pull/99533

ceseo avatar Jul 21 '24 23:07 ceseo

This issue is fixed:

./test.f90:37:10: warning: Function result is never defined
  function funi(item) result(ai)
           ^^^^

ceseo avatar Aug 01 '24 17:08 ceseo