The semantics of many transformational intrinsic functions, especially reductions like SUM(), are determined by the static presence or absence of a DIM= argument. In the case of an actual DIM= argument that is syntactically present but could be dynamically absent at execution time (due to being OPTIONAL, POINTER, or ALLOCATABLE), f18 should emit some kind of diagnostic message. Other compilers either ignore this possibility or treat it as a hard error; neither really seems correct, so let's do something more nuanced. For cases where the dynamic absence of a value for DIM doesn't pose as much of a risk because it lowering is going to assume that it's equal to 1 anyway, emit only a portability warning. For other cases where the generated code or runtime support library will need the value of DIM= during execution, emit a warning that the use of an OPTIONAL/POINTER/ALLOCATABLE variable or component here is dicey and should be reconsidered. While here, also catch bad constant DIM= values. Differential Revision: https://reviews.llvm.org/D139155
69 lines
2.5 KiB
Fortran
69 lines
2.5 KiB
Fortran
! RUN: %python %S/test_errors.py %s %flang_fc1
|
|
! Test warnings and errors about DIM= arguments to transformational intrinsics
|
|
|
|
module m
|
|
contains
|
|
function f0a(a)
|
|
real, intent(in) :: a(:)
|
|
!ERROR: The value of DIM= (-1) may not be less than 1
|
|
f0a = sum(a,dim=-1)
|
|
end function
|
|
function f0b(a)
|
|
real, intent(in) :: a(:)
|
|
!ERROR: The value of DIM= (2) may not be greater than 1
|
|
f0b = sum(a,dim=2)
|
|
end function
|
|
function f1(a,d)
|
|
real, intent(in) :: a(:)
|
|
integer, optional, intent(in) :: d
|
|
!PORTABILITY: The actual argument for DIM= is optional, pointer, or allocatable, and it is assumed to be present and equal to 1 at execution time
|
|
f1 = sum(a,dim=d)
|
|
end function
|
|
function f2(a,d)
|
|
real, intent(in) :: a(:)
|
|
integer, pointer, intent(in) :: d
|
|
!PORTABILITY: The actual argument for DIM= is optional, pointer, or allocatable, and it is assumed to be present and equal to 1 at execution time
|
|
f2 = sum(a,dim=d)
|
|
end function
|
|
function f3(a,d)
|
|
real, intent(in) :: a(:)
|
|
integer, allocatable, intent(in) :: d
|
|
!PORTABILITY: The actual argument for DIM= is optional, pointer, or allocatable, and it is assumed to be present and equal to 1 at execution time
|
|
f3 = sum(a,dim=d)
|
|
end function
|
|
function f10a(a)
|
|
real, intent(in) :: a(:,:)
|
|
real, allocatable :: f10a(:)
|
|
!ERROR: The value of DIM= (-1) may not be less than 1
|
|
f10a = sum(a,dim=-1)
|
|
end function
|
|
function f10b(a)
|
|
real, intent(in) :: a(:,:)
|
|
real, allocatable :: f10b(:)
|
|
!ERROR: The value of DIM= (3) may not be greater than 2
|
|
f10b = sum(a,dim=3)
|
|
end function
|
|
function f11(a,d)
|
|
real, intent(in) :: a(:,:)
|
|
integer, optional, intent(in) :: d
|
|
real, allocatable :: f11(:)
|
|
!WARNING: The actual argument for DIM= is optional, pointer, or allocatable, and may not be absent during execution; parenthesize to silence this warning
|
|
f11 = sum(a,dim=d)
|
|
end function
|
|
function f12(a,d)
|
|
real, intent(in) :: a(:,:)
|
|
integer, pointer, intent(in) :: d
|
|
real, allocatable :: f12(:)
|
|
!WARNING: The actual argument for DIM= is optional, pointer, or allocatable, and may not be absent during execution; parenthesize to silence this warning
|
|
f12 = sum(a,dim=d)
|
|
end function
|
|
function f13(a,d)
|
|
real, intent(in) :: a(:,:)
|
|
integer, allocatable, intent(in) :: d
|
|
real, allocatable :: f13(:)
|
|
!WARNING: The actual argument for DIM= is optional, pointer, or allocatable, and may not be absent during execution; parenthesize to silence this warning
|
|
f13 = sum(a,dim=d)
|
|
end function
|
|
end module
|
|
|