diff --git a/flang/lib/Evaluate/variable.cpp b/flang/lib/Evaluate/variable.cpp --- a/flang/lib/Evaluate/variable.cpp +++ b/flang/lib/Evaluate/variable.cpp @@ -258,7 +258,20 @@ } } if (auto dyType{DynamicType::From(ultimate)}) { - if (auto len{dyType->GetCharLength()}) { + auto len{dyType->GetCharLength()}; + if (!len && ultimate.attrs().test(semantics::Attr::PARAMETER)) { + // Its initializer determines the length of an implied-length named + // constant. + if (const auto *object{ + ultimate.detailsIf()}) { + if (object->init()) { + if (auto dyType2{DynamicType::From(*object->init())}) { + len = dyType2->GetCharLength(); + } + } + } + } + if (len) { if (auto constLen{ToInt64(*len)}) { return Expr{std::max(*constLen, 0)}; } else if (ultimate.owner().IsDerivedType() || diff --git a/flang/test/Evaluate/fold-substr.f90 b/flang/test/Evaluate/fold-substr.f90 --- a/flang/test/Evaluate/fold-substr.f90 +++ b/flang/test/Evaluate/fold-substr.f90 @@ -14,4 +14,9 @@ logical, parameter :: test_05b = len(ca(:)(2:4)) == 3 logical, parameter :: test_06a = ca(1)(1:2)//ca(2)(2:3)//ca(3)(3:4) == "abfgkl" logical, parameter :: test_06b = len(ca(1)(1:2)//ca(2)(2:3)//ca(3)(3:4)) == 6 + logical, parameter :: test_07a = ca(1)(:2) == "ab" + logical, parameter :: test_07b = ca(1)(3:) == "cd" + logical, parameter :: test_07c = ca(1)(:-1) == "" + logical, parameter :: test_07d = ca(1)(5:) == "" + logical, parameter :: test_07e = ca(1)(:) == "abcd" end module diff --git a/flang/test/Lower/character-local-variables.f90 b/flang/test/Lower/character-local-variables.f90 --- a/flang/test/Lower/character-local-variables.f90 +++ b/flang/test/Lower/character-local-variables.f90 @@ -116,9 +116,8 @@ subroutine assumed_length_param(n) character(*), parameter :: c(1)=(/"abcd"/) integer :: n - ! CHECK: %[[c4:.*]] = arith.constant 4 : index - ! CHECK: %[[len:.*]] = fir.convert %[[c4]] : (index) -> i64 - ! CHECK: fir.store %[[len]] to %[[tmp:.*]] : !fir.ref + ! CHECK: %[[c4:.*]] = arith.constant 4 : i64 + ! CHECK: fir.store %[[c4]] to %[[tmp:.*]] : !fir.ref ! CHECK: fir.call @_QPtake_int(%[[tmp]]) {{.*}}: (!fir.ref) -> () call take_int(len(c(n), kind=8)) end