llvm / llvm/llvm-project

[flang] Too-short character literal actual argument to a character array dummy is not diagnosed

Open
#224,043 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

A scalar character actual argument may be associated with a character array dummy via character storage sequence association (F'2023 15.5.2.12 p4), and flang diagnoses a too-short sequence when the actual is a designator — but a bare character *literal* that is too short is silently accepted.

```fortran
module cm
contains
subroutine takes6(c)
character(len=2), intent(in) :: c(3) ! needs a sequence of 6 characters
end subroutine
end module

subroutine t_var()
use cm
character(len=4) :: v
call takes6(v) ! diagnosed: 4 characters remaining < 6
end subroutine

subroutine t_lit()
use cm
call takes6('abcd') ! literal with 4 characters: silently accepted
end subroutine
```

Actual behavior (`flang -fc1 -fsyntax-only`, at main c44ac9eedba6):

```
error: Actual argument has fewer characters remaining in storage sequence (4) than dummy argument 'c=' (6)
call takes6(v)
```

and nothing for the literal, although the callee's element accesses beyond `c(2)` read past the end of the literal's storage in the same way.

Expected: the same diagnostic for the literal (its length is statically known).

Cause: the character storage-sequence check in `CheckCharacterActual` (`flang/lib/Semantics/check-call.cpp`) measures the remaining sequence through `evaluate::DesignatorFolder`, and a literal constant is not a designator, so `FoldDesignator` returns `std::nullopt` and the check is skipped. For a literal (or any constant of known length) the remaining sequence is simply its length, so this case could be handled directly without the folder.

(Filed with AI assistance.)

Contributor guide

Open the contributing guide

Research direction

Start in flang/lib/Semantics/check-call.cpp, specifically CheckCharacterActual and its use of evaluate::DesignatorFolder/FoldDesignator. Run flang -fc1 -fsyntax-only on the supplied reproducer and add coverage for the too-short literal case. Done means the literal receives the same fewer-characters diagnostic as the designator actual.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.