ifx 2025.3 and nvfortran 26.1 also segfault: Compiler Explorer
Goes to show that Fortran programmers don’t make use of the VALUE, OPTIONAL attribute pair very often…
NAG Fortran:
> nagfor test_optional.f90 && ./a.out
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
[NAG Fortran Compiler normal termination]
present(a)= T a= 42
present(a)= F a= 100
LFortran (version 0.50 and higher) works too! Proof: Compiler Explorer
Cray Fortran v20:
$ crayftn -O0 test_optional.f90 && ./a.out
present(a)= T a= 42
Segmentation fault (core dumped)
With higher optimization levels it just skips the second bar call…
$ crayftn -O2 test_optional.f90 && ./a.out
present(a)= T a= 42
I wonder what happens if bind(C) is added; should be illegal IMO. I just gave it a try:
Compiler results
Cray Fortran:
ftn-630 ftn: ERROR BAR, File = test_optional.f90, Line = 5, Column = 19
Object "A", a dummy argument to a BIND(C) routine must not have both the value and optional attributes.
LFortran:
code generation error: asr_to_llvm: module failed verification. Error:
Store operand must be a pointer.
store i32 100, i32 %a, align 4
gfortran:
Error: Variable 'a' at (1) cannot have both the OPTIONAL and the VALUE attribute because procedure 'bar' is BIND(C)
ifx:
/app/example.f90(5): error #8238: A dummy argument with the VALUE attribute cannot have the OPTIONAL attribute in a BIND(C) procedure. [A]
subroutine bar(a) bind(c)
------------------^
flang
error: Semantic errors in /app/example.f90
/app/example.f90:6:35: error: VALUE attribute may not apply to an OPTIONAL in a BIND(C) procedure
integer, value, optional :: a
^
nvfortran
NVFORTRAN-S-1269-Dummy argument a cannot have both the VALUE and OPTIONAL attributes because procedure bar has the BIND attribute (/app/example.f90: 6)
NVFORTRAN/x86-64 Linux 26.1-0: compilation completed with severe errors
NAG Fortran:
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
Error: test_optional.f90, line 9: Argument A of BIND(C) procedure BAR has both the OPTIONAL and VALUE attributes
All compilers caught the problem, however the LFortran compiler message is not clear (reported here)