Index: flang/lib/Evaluate/fold-integer.cpp =================================================================== --- flang/lib/Evaluate/fold-integer.cpp +++ flang/lib/Evaluate/fold-integer.cpp @@ -677,7 +677,11 @@ return std::visit( [&](auto &kx) { if (auto len{kx.LEN()}) { - return Fold(context, ConvertToType(*std::move(len))); + if (IsScopeInvariantExpr(*len)) { + return Fold(context, ConvertToType(*std::move(len))); + } else { + return Expr{std::move(funcRef)}; + } } else { return Expr{std::move(funcRef)}; } Index: flang/test/Evaluate/rewrite01.f90 =================================================================== --- flang/test/Evaluate/rewrite01.f90 +++ flang/test/Evaluate/rewrite01.f90 @@ -76,4 +76,44 @@ !CHECK: PRINT *, 1_4 print *, lbound(returns_array_3(), dim=1) end subroutine + +!CHECK: len_test +subroutine len_test(a,b, c, d, e, n, m) + character(*), intent(in) :: a + character(*) :: b + external b + character(10), intent(in) :: c + character(10) :: d + external d + integer, intent(in) :: n, m + character(n), intent(in) :: e + + !CHECK: PRINT *, int(a%len,kind=4) + print *, len(a) + !CHECK: PRINT *, 5_4 + print *, len(a(1:5)) + !CHECK: PRINT *, len(b(a)) + print *, len(b(a)) + !CHECK: PRINT *, len(b(a)//a) + print *, len(b(a) // a) + !CHECK: PRINT *, 10_4 + print *, len(c) + !CHECK: PRINT *, len(c(int(i,kind=8):int(j,kind=8))) + print *, len(c(i:j)) + !CHECK: PRINT *, 5_4 + print *, len(c(1:5)) + !CHECK: PRINT *, 10_4 + print *, len(d(c)) + !CHECK: PRINT *, 20_4 + print *, len(d(c) // c) + !CHECK: PRINT *, 0_4 + print *, len(a(10:4)) + !CHECK: PRINT *, int(max(0_8,int(m,kind=8)-int(n,kind=8)+1_8),kind=4) + print *, len(a(n:m)) + !CHECK: PRINT *, len(b(a(int(n,kind=8):int(m,kind=8)))) + print *, len(b(a(n:m))) + !CHECK: PRINT *, int(max(0_8,max(0_8,int(n,kind=8))-4_8+1_8),kind=4) + print *, len(e(4:)) +end subroutine len_test + end module