lfortran icon indicating copy to clipboard operation
lfortran copied to clipboard

x = huge(x) disallowed in a constant expression

Open harperjf opened this issue 2 years ago • 3 comments

The declaration of x in this program appears to be standard-conforming, by F2018 10.1.12 para 2. Five compilers including g95 allowed it, but lfortran did not:

(lf2) john:~/Test/Glist$ cat bigdeclare.f90
program bigdeclare
  implicit none
  real(kind(1d0)):: x = huge(x)
  print *,x
end program bigdeclare
(lf2) john:~/Test/Glist$ lfortran bigdeclare.f90
semantic error: Variable 'x' is not declared
 --> bigdeclare.f90:3:30
  |
3 |   real(kind(1d0)):: x = huge(x)
  |                              ^ 'x' is undeclared


Note: Please report unclear or confusing messages as bugs at
https://github.com/lfortran/lfortran/issues.
(lf2) john:~/Test/Glist$ 

That paragraph in the F2018 standard says "If a constant expression includes a specification inquiry that depends on a type parameter or an array bound of an entity specified in the same specification-part, the type parameter or array bound shall be specified in a prior specification of the specification-part. The prior specification may be to the left of the specification inquiry in the same statement, but shall not be within the same entity-decl unless the specification inquiry appears within an initialization."

Here the prior specification is the first x, and huge(x) is a specification inquiry.

harperjf avatar Apr 19 '24 08:04 harperjf

Lfortran said this program had a semantic error but is it really an error in the intrinsic huge ? I get the same error if huge is replaced by epsilon or tiny; all three intrinsics require the kind but not the value of x to be known beforehand.

harperjf avatar Aug 06 '24 21:08 harperjf

I think we just didn't implement this yet. It's a little recursive, since x is in a symbol table, being initialized with an expression huge(x) that references x, but it should probably work. If not, we'll find a way to make this work.

certik avatar Aug 07 '24 05:08 certik

This now works with latest main and will be available in release0.50.0.

 cat a.f90 
program bigdeclare
  implicit none
  real(kind(1d0)):: x = huge(x)
  print *,x
end program bigdeclare
% lfortran a.f90 
1.79769313486231571e+308
% gfortran a.f90 && ./a.out
   1.7976931348623157E+308

Pranavchiku avatar Mar 20 '25 12:03 Pranavchiku