空数组的绑定检查——各种编译器的行为



更新20210914:Absoft支持证实,下面描述的af95/af90的行为是意外的,实际上是一个错误。Absoft的开发人员将努力解决这个问题。其他编译器在这方面的操作是正确的。感谢@Vladimir F的回答、评论和建议。


我的印象是Fortran对大小为0的数组很酷。然而,在使用Absoft Pro 21.0时,我遇到了一个涉及此类数组的(奇怪)错误。相反,gfortranifortnagforpgfortransunf95g95都对同一段代码感到满意。

下面是一个最小的工作示例。

! testempty.f90
!!!!!! A module that offends AF90/AF95 !!!!!!!!!!!!!!!!!!!!!!!!
module empty_mod
implicit none
private
public :: foo
contains
subroutine foo(n)
implicit none
integer, intent(in) :: n
integer :: a(0)
integer :: b(n - 1)
call bar(a)  ! AF90/AF95 is happy with this line.
call bar(b)  ! AF90/AF95 is angry with this line.
end subroutine foo
subroutine bar(x)
implicit none
integer, intent(out) :: x(:)
x = 1  ! BAR(B) annoys AF90/AF95 regardless of this line.
end subroutine bar
end module empty_mod
!!!!!! Module ends !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!! Main program !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
program testempty
use empty_mod, only : foo
implicit none
call foo(2)  ! AF90/AF95 is happy with this line.
call foo(1)  ! AF90/AF95 is angry with this line.
write (*, *) 'Succeed!'  ! Declare victory when arriving here.
end program testempty
!!!!!! Main program ends !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

将这段代码命名为testempty.f90。然后运行

$ af95 -no-pie -et -Rb -g -O0 -o atest testempty.f90
$ ./atest

这就是我的机器上发生的事情(Ubuntu 20.04,linux 5.4.0-77-通用,x86_64):

./atest
? FORTRAN Runtime Error:
? Subscript 1 is out of range for dimension 1 for array 
? B with bounds 1:
? File testempty.f90; Line 19
? atest, run-time exception on Mon Sep 13 14:08:41 2021
? Program counter: 000000001004324B
? Signal SIGABRT, Abort
? Traceback follows
OBJECT             PC                ROUTINE                LINE  SOURCE
libpthread.so.0    000000001004324B  raise                   N/A  N/A
atest              00000000004141F3  __abs_f90rerr           N/A  N/A
atest              000000000041CA81  _BOUNDS_ERROR           N/A  N/A
atest              00000000004097B4  __FOO.in.EMPTY_MO       N/A  N/A
atest              000000000040993A  MAIN__                   40  testempty.f90
atest              000000000042A209  main                    N/A  N/A
libc.so.6          000000000FD0C0B3  __libc_start_main       N/A  N/A
atest              000000000040956E  _start                  N/A  N/A

所以af95call bar(b)惹恼了。使用af90,结果相同。

我使用gfortranifortnagforpgfortransunf95g95测试了相同的代码。尽管我明确地强加了绑定检查,但他们都对代码非常满意。以下是测试的Makefile

# This Makefile tests the following compilers on empty arrays.
#
# af95: Absoft 64-bit Pro Fortran 21.0.0
# gfortran: GNU Fortran (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
# ifort: ifort (IFORT) 2021.2.0 20210228
# nagfor: NAG Fortran Compiler Release 7.0(Yurakucho) Build 7036
# pgfortran: pgfortran (aka nvfortran) 21.3-0 LLVM 64-bit x86-64
# sunf95: Oracle Developer Studio 12.6
# g95: G95 (GCC 4.0.3 (g95 0.94!) Jan 17 2013)
#
# Tested on Ubuntu 20.04 with Linux 5.4.0-77-generic x86_64
.PHONY: test clean
test:
make -s gtest
make -s itest
make -s ntest
make -s ptest
make -s stest
make -s 9test
make -s atest
gtest: FC = gfortran -Wall -Wextra -fcheck=all
itest: FC = ifort -warn all -check all
ntest: FC = nagfor -C
ptest: FC = pgfortran -C -Mbounds
stest: FC = sunf95 -w3 -xcheck=%all -C
9test: FC = g95 -Wall -Wextra -fbounds-check
atest: FC = af95 -no-pie -et -Rb
%test: testempty.f90
$(FC) -g -O0 -o $@ $<
./$@
clean:
rm -f *.o *.mod *.dbg *test

问题:

  1. af95/af90标准的行为符合吗
  2. 我的代码中是否包含任何违反Fortran标准的内容
  3. 一般来说,在Fortran代码中包含空数组是否被认为是危险的?有时它们是不可避免的,因为数据大小在运行时之前往往是不可确定的

By";"标准";,我指的是2003年、2008年和2018年。

非常感谢您的评论或批评。

(同样的问题也出现在Fortran话语中,我希望它不会违反这里的规则。)

这个程序对我来说还可以。零大小的数组在Fortran中是完全可能的,尽管我承认我通常没有自动数组,但这只是巧合。

我认为这是Absoft编译器或其数组边界检查器中的编译器错误。

最新更新