Skip to content

Commit 2d7e136

Browse files
authored
[flang] Adjust semantics of the char length of an array constructor (#97337)
An implied DO loop with no trips in an array constructor does not have a well-defined character length unless its data items have a length that is constant expression. That works, but the implementation is too broadly applied. An array constructor with an explicit type-spec always has a well-defined length.
1 parent 7f2a5df commit 2d7e136

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

flang/lib/Semantics/expression.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,8 @@ class ArrayConstructorContext {
15841584
std::optional<Expr<SubscriptInteger>> LengthIfGood() const {
15851585
if (type_) {
15861586
auto len{type_->LEN()};
1587-
if (len && IsConstantExpr(*len) && !ContainsAnyImpliedDoIndex(*len)) {
1587+
if (explicitType_ ||
1588+
(len && IsConstantExpr(*len) && !ContainsAnyImpliedDoIndex(*len))) {
15881589
return len;
15891590
}
15901591
}

flang/test/Lower/HLFIR/array-ctor-character.f90

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ subroutine test_set_length_sanitize(i, c1)
9393
call takes_char([character(len=i):: c1])
9494
end subroutine
9595
! CHECK-LABEL: func.func @_QPtest_set_length_sanitize(
96-
! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare {{.*}}Ec1
97-
! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare {{.*}}Ei
98-
! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
96+
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare {{.*}}Ec1
97+
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %arg0
98+
! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i64>
99+
! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i64>
99100
! CHECK: %[[VAL_26:.*]] = arith.constant 0 : i64
100101
! CHECK: %[[VAL_27:.*]] = arith.cmpi sgt, %[[VAL_25]], %[[VAL_26]] : i64
101102
! CHECK: %[[VAL_28:.*]] = arith.select %[[VAL_27]], %[[VAL_25]], %[[VAL_26]] : i64
102-
! CHECK: %[[VAL_29:.*]] = hlfir.set_length %[[VAL_6]]#0 len %[[VAL_28]] : (!fir.boxchar<1>, i64) -> !hlfir.expr<!fir.char<1,?>>
103+
! CHECK: %[[VAL_29:.*]] = hlfir.set_length %[[VAL_2]]#0 len %[[VAL_28]] : (!fir.boxchar<1>, i64) -> !hlfir.expr<!fir.char<1,?>>

flang/test/Semantics/array-constr-len.f90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ subroutine subr(s,n)
1010
print *, [(s(1:j),j=1,0)]
1111
print *, [(s(1:1),j=1,0)] ! ok
1212
print *, [character(2)::(s(1:n),j=1,0)] ! ok
13-
!ERROR: Array constructor implied DO loop has no iterations and indeterminate character length
1413
print *, [character(n)::(s(1:n),j=1,0)]
1514
end

0 commit comments

Comments
 (0)