fortran - 如何强制 fortran 编译器在子程序违反 "intent(in)"的情况下生成错误,而忽略意图

标签 fortran

这个问题与我之前的问题有关:How to force compiler to interpret omitted intent as intent(inout) .将遗漏的意图解释为意图(inout)似乎是不可能的,因此违反意图(in)的问题仍然存在。

同样的例子:

module test
  implicit none
  contains

  subroutine fun1(x)
    real(8), intent(in)::x
    call fun2(x)               
  end subroutine

  subroutine fun2(x)
   real(8) :: x
   x = 10
  end subroutine
end module

gfortran 和 ifort 可以编译此代码而不会出现任何错误/警告。所以我的问题是:

当意图(in)变量传递给具有省略意图(但声明接口(interface))的子例程时,如何强制fortran编译器生成错误?

最佳答案

正如 IanH 所说,您需要一个可以为您处理的处理器(即编译器)。例如,如果你给它正确的标志,NAG 编译器就会这样做(免责声明 - 我为 NAG 工作)。我稍微修改了您的代码以使其可移植,并添加了一个驱动程序来显示这一点:

$ cat t.f90 
module test
  implicit none

  Integer, Parameter :: wp = Selected_real_kind( 12, 70 )

  contains

  subroutine fun1(x)
    real(wp), intent(in)::x
    call fun2(x)               
  end subroutine

  subroutine fun2(x)
   real(wp) :: x
   x = 10
  end subroutine
end module

Program test_test

  Use test

  Implicit None

  Real( wp ) :: x

  x = 5.0_wp

  Call fun1( x ) 

End Program test_test
$ nagfor t.f90
NAG Fortran Compiler Release 5.3.1 pre-release(904)
[NAG Fortran Compiler normal termination]
$ ./a.out
$ nagfor -C=all -C=undefined t.f90 
NAG Fortran Compiler Release 5.3.1 pre-release(904)
[NAG Fortran Compiler normal termination]
$ ./a.out
Runtime Error: t.f90, line 15: Dummy argument X is associated with an expression - cannot assign
Program terminated by fatal error
Aborted (core dumped)
$ 

所以搜索标志,可能会有一些帮助 - 如果不是向提供编译器的人提示!

关于fortran - 如何强制 fortran 编译器在子程序违反 "intent(in)"的情况下生成错误,而忽略意图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781506/

相关文章:

fortran - gfortran 错误 : zgesvd in lapack

segmentation-fault - Gfortran : Segmentation fault with long character

fortran - Netcdf 和 Fortran 结构

loops - 如何将循环转换为向量赋值符号

numpy - Fortran 相当于 numpy.where() 函数?

windows - 立即在控制台屏幕上的同一行上打印

fortran - gfortran -std=f2008 如果调用 fseek 存在则编译标志错误

module - 如何修复 CMake Fortran 模块依赖性?

fortran - 将数组长度传递给函数

c++ - Fortran Do 循环的上限