Issue 233278
Summary [flang] named constant can be referenced in declarations before its PARAMETER statement
Labels flang
Assignees
Reporter ivan-pi
    F2023 (J3/24-007, 8.5.13 p2) says "A named constant shall not be referenced unless it has been defined previously; it may be defined previously in the same statement". This is NOT a named constraint. 

In the following program, the variable `n` is used as an implicitly typed local variable that is neither a dummy argument, nor in a COMMON block:

```fortran
! param_before_def.f
      subroutine s(a)
      double precision a(n)
 double precision w(n)
      integer n
      parameter (n=50)
      w(1) = 1.0d0
      a(1) = w(1) + size(w)
      print *, size(a), size(w)
 end
      program param_before_def
      double precision x(100) ! bigger array
      call s(x)
      end
```

Flang accepts it without diagnostic, and the the prinout shows that `n` takes the value 50:

```
$ flang -pedantic param_before_def.f && ./a.out
 50 50
```

gfortran, ifx and nvfortran all reject this program: https://godbolt.org/z/zT78cevbT

The flang [extensions](https://flang.llvm.org/docs/Extensions.html#extensions-deletions-and-legacy-features-supported-by-default) do document that, 

> * A named constant defined by a PARAMETER statement is permitted to appear before its explicit type declaration in a scope with IMPLICIT NONE(TYPE); it acquires the type it would have had under implicit typing rules (F2023 8.7), and a later explicit declaration must specify that same type (F2023 8.6.11 p2).

This is the following case:

```fortran
! example.f90
implicit none (type)
parameter (n = 5)     ! defined first ...
integer :: n          ! ... typed afterwards
end 
```

and here flang correctly warns that, 

```
/app/example.f90:3:12: warning: 'n' was used without (or before) being explicitly typed [-Wforward-ref-implicit-none]
  parameter (n = 5)     ! defined first ...
 ^
```

The Extensions document does not mention the case above where the reference would _precede_ the PARAMETER statement itself. The expected behavior would be to either forbid such usage or at least issue a portability warning under `-pedantic`. 

Found using Claude Opus. The full Claude report: [flang-01-named-constant-referenced-before-parameter-statement.md](https://github.com/user-attachments/files/33635052/flang-01-named-constant-referenced-before-parameter-statement.md)


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

Reply via email to