Intel Fortran error #8284:字符类型实参合规性及编译器问题咨询
Fortran代码标准合规性咨询
使用Intel ifort 2021.6.0+/ifx 2022.1.0+编译以下代码时,触发error #8284实参与接口不匹配错误,怀疑为编译器误报,现咨询该代码是否符合Fortran标准。
最小可复现示例(MWE)
Fortran代码
program main use iso_c_binding call foo("Fortran Arg") contains function istring_(o) result(v) character(len=*), intent(in) :: o character(len=:, kind=c_char), allocatable :: v v = trim(o)//c_null_char end function istring_ subroutine foo(fileName) interface subroutine C_API(fileName) bind(C, name="foo") use, intrinsic :: iso_c_binding character(len=1, kind=c_char), dimension(*), intent(in) :: fileName end subroutine C_API end interface character(len=*), intent(in) :: fileName call C_API(fileName=istring_(fileName)) end subroutine foo end program main
C代码
#include "stdio.h" void foo(const char* fileName) { printf("%s\n", fileName); }
错误输出
/app/example.f90(21): error #8284: If the actual argument is scalar, the dummy argument shall be scalar unless the actual argument is of type character or is an element of an array that is not assumed shape, pointer, or polymorphic. [FILENAME] call C_API(fileName=istring_(fileName)) ---------^
临时修复方案
将函数调用结果先赋值给局部变量再传递,即可正常编译,疑似编译器bug或回归问题:
--- tmp.f90 2023-01-05 10:19:30.778230819 +0000 +++ tmp2.f90 2023-01-05 10:19:36.342418329 +0000 @@ -18,6 +18,8 @@ end subroutine C_API end interface character(len=*), intent(in) :: fileName - call C_API(fileName=istring_(fileName)) + character(len=:), allocatable :: fileNameC + fileNameC = istring_(fileName) + call C_API(fileName=fileNameC) end subroutine foo end program main
补充信息
GFortran、Intel旧版本编译器及flang-trunk均可正常编译该示例代码。
结论
这段代码完全符合Fortran标准,Intel新版本编译器的报错属于误报或回归问题:
istring_返回的是可分配长度的标量C风格字符变量,传递给C绑定的假定大小字符数组哑元是标准允许的操作——标量字符的连续存储特性与C字符串的内存布局兼容,且Fortran标准明确允许标量字符作为实际参数匹配假定大小的字符数组哑元。- 其他主流编译器的正常编译结果也佐证了代码的合规性。
内容的提问来源于stack exchange,提问作者gnikit
相关产品推荐
相关产品推荐

