module foo implicit none contains character(len(string)) function bar(string) character(*), intent(in) :: string bar = string end function bar end module foo
and specifically how I declare the function result using the input argument to determine the length.
According to Modern Fortran Explained, this is fine, and they even give an example of this.
gfortran-9.2.0 compiles it fine.
However ifort-2021.1 doesn’t:
$ ifort -c test_type_spec.f90 test_type_spec.f90(4): error #6362: The data types of the argument(s) are invalid. [LEN] character(len(string)) function bar(string) ----------------^ test_type_spec.f90(5): error #6415: This name cannot be assigned this data type because it conflicts with prior uses of the name. [STRING] character(*), intent(in) :: string --------------------------------^ compilation aborted for test_type_spec.f90 (code 1)
I think ifort is wrong here. Is it?
Further, if I enable implicit typing by commenting out
implicit none, and try again with gfortran, I get this:
test_type_spec.f90:5:38: 5 | character(*), intent(in) :: string | 1 Error: Symbol ‘string’ at (1) already has basic type of REAL test_type_spec.f90:4:16: 4 | character(len(string)) function bar(string) | 1 Error: ‘string’ argument of ‘len’ intrinsic at (1) must be CHARACTER test_type_spec.f90:6:10: 6 | bar = string | 1 Error: Cannot convert REAL(4) to CHARACTER(1) at (1)
It seems that gfortran now implicitly declared
real because it appeared in the type specification of the function statement, but I don’t think it should do that. Should it?