Skip to content

Commit

Permalink
[flang] Adjust semantics of the char length of an array constructor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
klausler committed Jul 1, 2024
1 parent 259ce11 commit 1e19ecf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,8 @@ class ArrayConstructorContext {
std::optional<Expr<SubscriptInteger>> LengthIfGood() const {
if (type_) {
auto len{type_->LEN()};
if (len && IsConstantExpr(*len) && !ContainsAnyImpliedDoIndex(*len)) {
if (explicitType_ ||
(len && IsConstantExpr(*len) && !ContainsAnyImpliedDoIndex(*len))) {
return len;
}
}
Expand Down
9 changes: 5 additions & 4 deletions flang/test/Lower/HLFIR/array-ctor-character.f90
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ subroutine test_set_length_sanitize(i, c1)
call takes_char([character(len=i):: c1])
end subroutine
! CHECK-LABEL: func.func @_QPtest_set_length_sanitize(
! CHECK: %[[VAL_6:.*]]:2 = hlfir.declare {{.*}}Ec1
! CHECK: %[[VAL_9:.*]]:2 = hlfir.declare {{.*}}Ei
! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_9]]#0 : !fir.ref<i64>
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare {{.*}}Ec1
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %arg0
! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i64>
! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<i64>
! CHECK: %[[VAL_26:.*]] = arith.constant 0 : i64
! CHECK: %[[VAL_27:.*]] = arith.cmpi sgt, %[[VAL_25]], %[[VAL_26]] : i64
! CHECK: %[[VAL_28:.*]] = arith.select %[[VAL_27]], %[[VAL_25]], %[[VAL_26]] : i64
! CHECK: %[[VAL_29:.*]] = hlfir.set_length %[[VAL_6]]#0 len %[[VAL_28]] : (!fir.boxchar<1>, i64) -> !hlfir.expr<!fir.char<1,?>>
! CHECK: %[[VAL_29:.*]] = hlfir.set_length %[[VAL_2]]#0 len %[[VAL_28]] : (!fir.boxchar<1>, i64) -> !hlfir.expr<!fir.char<1,?>>
1 change: 0 additions & 1 deletion flang/test/Semantics/array-constr-len.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ subroutine subr(s,n)
print *, [(s(1:j),j=1,0)]
print *, [(s(1:1),j=1,0)] ! ok
print *, [character(2)::(s(1:n),j=1,0)] ! ok
!ERROR: Array constructor implied DO loop has no iterations and indeterminate character length
print *, [character(n)::(s(1:n),j=1,0)]
end

0 comments on commit 1e19ecf

Please sign in to comment.