llvm / llvm/llvm-project

[Flang] Spurious error for variable length character function as actual argument

Open
#192,899 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Flang incorrectly rejects valid Fortran code when a character function is passed as an actual argument to a procedure that expects a variable-length character function, even when the length expressions are mathematically equivalent at runtime.

Expected Behavior: According to the Fortran standard the compiler should accept character functions as actual arguments when their result lengths are compatible at runtime, even if the length expressions are textually different. Runtime compatibility is what matters, not compile-time textual matching of expressions.

Actual Behavior: Flang rejects the code, incorrectly requiring that the length expressions match textually at compile-time rather than evaluating to the same value at runtime. For the reproducer, it will give the following error:
```
error: Actual argument function associated with procedure dummy argument 'fun=' is not compatible: function results have distinct types: CHARACTER(KIND=1,LEN=2_4*n) vs CHARACTER(KIND=1,LEN=4_8)
call s(ext,ext,2)
^^^
error: Actual argument function associated with procedure dummy argument 'fun_alt=' is not compatible: function results have distinct types: CHARACTER(KIND=1,LEN=n*(n+1_4)-n**2_4+n) vs CHARACTER(KIND=1,LEN=4_8)
call s(ext,ext,2)
```

Reproducer:

```fortran
program test
character(4),external :: ext
call s(ext,ext,2)
contains
subroutine s(fun,fun_alt,n)
integer n
! variable length character function
! "exact dependence" in Standard (J3/24-007, 15.3.3(1)) is a characteristic
character(2*n),external :: fun
character(n*(n+1)-n**2+n),external :: fun_alt
print *, fun()
print *, fun_alt()
end subroutine s
end program test

function ext()
character(4) ext
ext = 'okko'
end function
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.