| Issue |
242873
|
| Summary |
[flang] INQUIRE(IOLENGTH=): the variable is never treated as defined or checked for definability, and the output items are treated as value references
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
flang version 24.0.0git (https://github.com/llvm/llvm-project fe077b6e4150de8d5712e7a6a6507bdd397a7877)
With `-pedantic`, this program gets two usage warnings, and neither is correct:
```fortran
program iolength_open_recl
implicit none
integer :: a(4), recl
inquire(iolength=recl) a
open(20, form='unformatted', access='direct', recl=recl, status='scratch')
close(20)
end program
```
```console
$ flang -fsyntax-only -pedantic t.f90
t.f90:3:14: warning: Value of uninitialized local variable 'a' is used but never defined [-Wused-undefined-variable]
t.f90:3:20: warning: Value of local variable 'recl' is never used [-Wunused-variable]
```
The usual idiom for sizing a direct-access record gets the same treatment:
```fortran
subroutine iolength_derived_record
implicit none
type t
real :: x(3)
end type
type(t) :: rec
integer :: n
inquire(iolength=n) rec
print *, n
end subroutine
```
```console
warning: Value of uninitialized local variable 'rec' is used but never defined [-Wused-undefined-variable]
warning: Value of uninitialized local variable 'n' is used but never defined [-Wused-undefined-variable]
```
There are two problems:
1. **The IOLENGTH= variable is never marked as defined.** A specifier in an INQUIRE statement other than FILE=, ID= and UNIT= is a variable definition context (F2023 19.6.7 p1 (7)), and executing the INQUIRE defines it (19.6.5 p1 (10)). `IoChecker` runs `CheckForDefinableVariable` on the INQUIRE specifier variables (NUMBER=, SIZE=, ...), but nothing in it handles `InquireStmt::Iolength`. So the variable is never recorded as defined, and its definability is never checked either. flang accepts this program without a diagnostic, although it violates C846:
```fortran
subroutine iolength_intent_in(n)
implicit none
integer, intent(in) :: n
inquire(iolength=n) 1.0
end subroutine
```
The same dummy argument as `inquire(unit=6, number=n)` is rejected ("NUMBER variable 'n' is not definable"). gfortran rejects both.
2. **The output items are counted as references to their values.** F2023 12.10.3 p1 defines the IOLENGTH= result from the types, type parameters and extents of the output list. The values of the items are not needed. `ExprChecker::Pre(const parser::Expr &)` records every symbol in every _expression_ as a value use. Inquiry intrinsics are already handled this way: in `size(a)`, `a` is not counted as a use. An output item of INQUIRE(IOLENGTH=) should be treated the same way, while a variable in a subscript or a bound of the item is still a use (in `inquire(iolength=n) a(1:m)`, the warning about an undefined `m` is correct).
A related gap is in the same function: `IoChecker::Leave(const parser::InquireStmt &)` calls `CheckForPureSubprogram()` only for the inquire-spec-list form. C15107 prohibits any *inquire-stmt* in a pure subprogram, so this is accepted without an error (gfortran rejects it):
```fortran
pure subroutine iolength_in_pure(n)
implicit none
integer, intent(out) :: n
inquire(iolength=n) 1.0
end subroutine
```
A possible fix:
- add `IoChecker::Enter(const parser::InquireStmt::Iolength &)` that calls `CheckForDefinableVariable(..., "IOLENGTH")`. This produces the C846/C859 errors and, through `WhyNotDefinable`, records the definition;
- in `ExprChecker`, analyze the IOLENGTH output items with `isDefinition=true` semantics (the same treatment as an inquiry intrinsic's first argument), so that subscripts and function arguments are still counted as uses;
- move `CheckForPureSubprogram()` out of the inquire-spec-list branch.
(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