空数组的边界检查——各种编译器的行为

Nun*_*uno 2 fortran gfortran intel-fortran pgi-visual-fortran nag-fortran

更新20210914:Absoft 支持确认下面描述的af95/行为是无意的,并且确实是一个错误。af90Absoft 开发人员将努力解决这个问题。其他编译器在这方面的行为是正确的。感谢@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 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Run Code Online (Sandbox Code Playgroud)

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

$ af95 -no-pie -et -Rb -g -O0 -o atest testempty.f90
$ ./atest
Run Code Online (Sandbox Code Playgroud)

这是我的机器上发生的情况(Ubuntu 20.04,linux 5.4.0-77-generic,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
Run Code Online (Sandbox Code Playgroud)

于是就af95被气恼了call bar(b)。与af90,结果是一样的。

gfortran我使用、ifortnagforpgfortransunf95和测试了相同的代码g95。尽管我明确地施加了边界检查,但他们所有人都对代码非常满意。以下是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
Run Code Online (Sandbox Code Playgroud)

问题:

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

我所说的“标准”是指 2003 年、2008 年和 2018 年。

非常感谢您的任何意见或批评。

(同样的问题在Fortran Discourse上也提出了,希望不要违反这里的规则。)

Vla*_*r F 5

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

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

  • 我没有进入1.,2。3. 点。零大小的数组很好而且不危险。 (2认同)