[flang] Statement function dummy argument check misses names only referenced in the execution part
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## [flang] Statement function dummy argument check misses names only referenced in the execution part
### Summary
Per F2023 19.4 p2, a statement function dummy argument name may only be the
same as an accessible name if that name is a scalar variable. `AnalyzeStmtFunctionStmt`
in `flang/lib/Semantics/resolve-names.cpp` checks this, but it only looks up
each dummy name with `FindInScope(currScope(), ...)` — i.e., names already
declared/referenced in the specification part. A global external procedure
that first becomes referenced later in the *execution* part of the same
scoping unit is not seen yet, so no error is produced, even though it should
arguably conflict per 19.4 p2 / 19.5.1.4 p2 item (11).
### Reproducer
```fortran
real function gfun(x)
real :: x
gfun = x
end function
subroutine late_reference_gap
sf(gfun) = gfun + 1.0 ! not diagnosed (gap)
print *, sf(1.0), gfun(2.0)
end subroutine
```
Flang accepts this without complaint. This test demonstrating the gap should
be added at the end of stmt-func04.f90.
### Why it happens
`AnalyzeStmtFunctionStmt` is invoked from `FinishSpecificationPart`, which
runs after the specification part's declarations have been walked but
before the execution part is ever visited (`ResolveExecutionParts` runs
later, per program-unit node, in `ResolveNamesVisitor::ResolveExecutionParts`).
So `currScope()` cannot yet contain a symbol for a name whose only
appearance in the scoping unit is a call in the execution part.
### Where a fix should probably go
Not in `AnalyzeStmtFunctionStmt`/`FinishSpecificationPart` — at that point in
the pass the execution part hasn't been resolved yet, so there's nothing more
to find there. The dummy-name accessibility check would need to be deferred
until after `ResolveExecutionParts`/`FinishExecutionParts` has run for the
scoping unit (e.g., performed as a post-pass over each subprogram once its
whole body, not just its specification part, is resolved), so that any name
referenced anywhere in the scoping unit is visible before the check runs.
Assisted-by: AI
NOTE: Above analysis on proposed fix is AI generated. It should be analyzed
before implementing.
Contributor guide
Research direction
Start in flang/lib/Semantics/resolve-names.cpp by tracing AnalyzeStmtFunctionStmt, FinishSpecificationPart, ResolveExecutionParts, and FinishExecutionParts. Add the reproducer described in test stmt-func04.f90, then determine how the check can see names referenced in the execution part. Done means the test diagnoses the late global procedure reference without regressing existing statement-function cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fortran
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100