Issue 244067
Summary [flang][semantics] Type of a conditional argument ignores all but its first consequent-arg
Labels flang
Assignees
Reporter vntkmr
    (Prepared with the help of AI.)
flang takes the type of an F2023 conditional argument from its first consequent-arg. C1538 makes the consequent-args agree only on declared type and kind type parameters, so the first one doesn't speak for the others on two points: whether the argument is polymorphic, and its character length. An intrinsic function whose result has the type of its argument passes the first consequent-arg's answer on. That gives a false error in one case and wrong values in the other.

## Reproducers

Polymorphism:

```fortran
module m
  type :: t
 integer :: i = 0
  end type
contains
  subroutine s(flag, a, b)
 logical, intent(in) :: flag
    type(t), intent(in) :: a(1)
    class(t), intent(in) :: b(1)
    select type (y => reshape((flag ? a : b), [1]))
 type is (t)
      print *, y%i
    end select
    select type (z => reshape((flag ? b : a), [1]))
    type is (t)
      print *, z%i
    end select
  end subroutine
end module
```

```
$ flang -fsyntax-only poly.f90
error: Semantic errors in poly.f90
poly.f90:10:23: error: Selector 'reshape((flag ? a : b), [1])' in SELECT TYPE statement must be polymorphic
```

Line 14 has the same consequent-args in the other order, and flang accepts it.

Character length:

```fortran
program p
 character(5) :: c5 = 'abcde'
  character(10) :: c10 = 'abcdefghij'
 logical :: flag
  flag = .false.
  print *, len(adjustl((flag ? c5 : c10)))
  print *, len(adjustl((flag ? c10 : c5)))
  print *, storage_size(adjustl((flag ? c5 : c10)))
end program
```

```
$ flang len.f90 && ./a.out
 5
 10
 40
```

The right output is 10, 5, and 80. `flang -fc1 -fdebug-unparse` shows that the three references fold to the constants `5_4`, `10_4`, and `40_4`.

flang version 24.0.0git (llvm-project `401da4f6b4dd`), the flang-trunk build on Compiler Explorer.

## Expected behavior

- A conditional argument is polymorphic if and only if one or more of its consequent-args is polymorphic (F2023 15.5.2.3 p4). The result of RESHAPE has the type of SOURCE (16.9.175 p4). So both selectors are polymorphic, as C1164 requires, and line 10 isn't an error.
- The chosen consequent-arg is the actual argument (15.5.2.3 p2), and the result of ADJUSTL has the length of STRING (16.9.8 p4). With `flag` false, the lengths are 10 and 5, and the storage size is 80 bits. The length isn't known at compile time, so these references can't fold.

## Cause

`ActualArgument::GetType()` returns the type of the first consequent-arg that isn't `.NIL.`:

https://github.com/llvm/llvm-project/blob/401da4f6b4dd85bbc30443680f477af54cffd18a/flang/lib/Evaluate/call.cpp#L48-L61

When an intrinsic's result has the same type as an argument, `IntrinsicInterface::Match` copies that type, with its polymorphism and its length, into the result type:

https://github.com/llvm/llvm-project/blob/401da4f6b4dd85bbc30443680f477af54cffd18a/flang/lib/Evaluate/intrinsics.cpp#L2577-L2594

`TypeAndShape::Characterize(const ActualArgument &)` has the same limitation ([characteristics.cpp](https://github.com/llvm/llvm-project/blob/401da4f6b4dd85bbc30443680f477af54cffd18a/flang/lib/Evaluate/characteristics.cpp#L189-L198)). It also takes the shape of the first consequent-arg, although `GetShape()` merges the shapes of all of them. #238055 worked around it in one caller, `HavePolymorphicDifferences()` ([check-_expression_.cpp](https://github.com/llvm/llvm-project/blob/401da4f6b4dd85bbc30443680f477af54cffd18a/flang/lib/Evaluate/check-_expression_.cpp#L1596-L1601)). Its other callers are the SIZEOF and STORAGE_SIZE folds, which the folding check from #227466 keeps away from a conditional argument today.

That check also stops a direct inquiry such as `len((flag ? c5 : c10))` from folding. It doesn't cover an inquiry of a function reference that contains a conditional argument, as in the second reproducer.

## Possible fix

- Make `GetType()` account for every consequent-arg. The argument is polymorphic if any consequent-arg is. It has a known character length only when all the lengths agree, which is the rule that `LEN()` of a `ConditionalExpr` already uses ([_expression_.cpp](https://github.com/llvm/llvm-project/blob/401da4f6b4dd85bbc30443680f477af54cffd18a/flang/lib/Evaluate/_expression_.cpp#L60-L69)).
- Make `Characterize(const ActualArgument &)` merge the type the same way, and use the merged shape.
- Remove the fix-up in `HavePolymorphicDifferences()`.

Not tested: consequent-args of a parameterized derived type whose length type parameters differ, which C1538 also allows.

jeanPerier asked for this in the review of #238055 ([`GetType()`](https://github.com/llvm/llvm-project/pull/238055#discussion_r4611638161), [`Characterize()`](https://github.com/llvm/llvm-project/pull/238055#discussion_r4611700418)). `CheckMove_Alloc` also reads the polymorphism of FROM= and TO= from `GetType()`; #227805 covers MOVE_ALLOC with a conditional argument.

Part of #180592.


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to