| Issue |
241418
|
| Summary |
[flang] Crash in HollerithToBOZ passing a default-kind character constant to a CHARACTER(KIND=4,LEN=*) dummy
|
| Labels |
crash,
flang,
flang:semantics
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
flang aborts during semantic analysis when a default-kind character constant (a character literal, a named constant, or a Hollerith literal) is passed through an explicit interface to a scalar `CHARACTER(KIND=4)` dummy argument whose length is not a constant.
```fortran
interface
subroutine s(c)
character(kind=4,len=*) :: c
end
end interface
call s('abcd')
end
```
```console
$ flang -fsyntax-only t.f90
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
...
#8 abort
#9 (flang+0x...)
#10 Fortran::evaluate::HollerithToBOZ(Fortran::evaluate::FoldingContext&, Fortran::evaluate::Expr<Fortran::evaluate::SomeType> const&, Fortran::evaluate::DynamicType const&)
#11 Fortran::semantics::CheckExplicitDataArg(...)
#12 Fortran::semantics::CheckExplicitInterfaceArg(...)
#13 Fortran::semantics::CheckExplicitInterface(...)
#14 Fortran::semantics::CheckArguments(...)
...
flang: error: unable to execute command: Aborted
flang: error: flang frontend command failed with exit code -2 (use -v to see invocation)
```
Reproduced on x86_64 Linux with flang 24.0.0git built from main at 76f452e9ad4198aa0c1fcf1bc8ac2898e4089f1a (the backtrace above) and at 45403d3979a550bf7770b3fcec3c94e627ca3b96 (in that build `HollerithToBOZ` is inlined, so the first named frame is `CheckExplicitInterfaceArg`).
The same crash happens with:
- a Hollerith actual, `call s(4habcd)`;
- a named constant actual, `character(*), parameter :: k = 'abcd'` and `call s(k)`;
- a `character(kind=2,len=*)` dummy;
- a dummy with a non-constant explicit length, `character(kind=4,len=n) :: c`;
- a function reference instead of a `CALL`.
It does not happen with a character variable actual or with a constant-length `character(kind=4,len=4)` dummy. Both give the expected error, e.g. `Actual argument type 'CHARACTER(KIND=1,LEN=4_8)' is not compatible with dummy argument type 'CHARACTER(KIND=4,LEN=*)'`. A call through an implicit interface compiles.
**Expected:** for the character literal and the named constant, the same incompatibility error that a character variable actual gets (gfortran 13: `Type mismatch in argument 'c' at (1); passed CHARACTER(4) to CHARACTER(*,4)`). For the Hollerith actual, gfortran 13 accepts the call with a "Legacy Extension: Hollerith constant" warning; flang should at least diagnose it instead of crashing.
**Likely cause:** the mismatched actual falls into the "pass Hollerith literal to scalar as if it had been BOZ" extension in `CheckExplicitDataArg` (`flang/lib/Semantics/check-call.cpp`), which calls `HollerithToBOZ` (`flang/lib/Evaluate/tools.cpp`). That function evaluates `ToInt64(type.MeasureSizeInBytes(context, false)).value()`, and for a CHARACTER type without a constant length the size is not a constant, so `.value()` is taken on an empty optional. Returning `std::nullopt` from `HollerithToBOZ` when the size is not a known constant would let the call reach the normal type-mismatch error, as it already does for the constant-length dummy.
Noticed while reviewing 241165, which touches the same function; the crash is independent of that change.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs