In this is both a fortran and a libfortran problem.
Take the following code and try to run it and you will get an internal error:
function mystring ()
character(len=4) :: mystring
write (unit=mystring,fmt='(A)') 'aaaa'
end function mystring
end module mymod
write (unit=*,fmt='(A)') mystring()
end program main
If I remember correctly to fix this correctly you also need to fix PR 14943.
This is a f2003 feature, and not legal f95.
I think the error message can be improved.
Disallowing recursive I/O is not an "internal error".
What about "Calling I/O from function in I/O list not allowed" ?
Fixed in 4.1.0.