Loop name problem

I suspect that this program is bad Fortran but I did not find where the standard forbids it. It uses i as both the name of a variable and the name of a DO loop. Three compilers complained and one ran it.


program label_do
  integer i
  i: do i = 1,3
     print *,i
  end do i
end program label_do
```
1 Like

Fortran has no reserved keywords but I believe your example configures a duplicate issue: keyword is already used for a variable - you can’t use it for a label.

I.e. this is valid because do was not used in that scope yet:

program label_do
  integer i
  do: do i = 1,3
     print *,i
  end do do
end program label_do

There isn’t such a restriction on construct names. Construct names are in a different ā€˜name space’ than variable names and such - just as integer statement labels are different than integers used in expressions. On my computer, lfortran gets it right and and gfortran gets it wrong. I’d suggest submitting a bug report to the gfortran folks and the others that get it wrong.

Uhm, the standard at §19.3.1 defines classes of local identifiers; and class (1) includes both named variables and named constructs (the do-loop identifier). So they appear to share the same class (1) namespace, hence duplicate keyword imho.

FYI, nagfor says:

$ nagfor do.f90 && a.out
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
Error: do.f90, line 3: Multiply defined symbol I
       detected at <end-of-statement>@I
Error: do.f90, line 5: Inappropriate use of symbol I
       detected at END DO@I
[NAG Fortran Compiler pass 1 error termination, 2 errors]

Interesting. (Though the word ā€˜do’ is not a problem. The variable ā€˜i’ in the original posting seems to be.) I missed that section of the Standard when I was searching through it.

This has been a feature of fortran from the beginning. One can have variables named do, if, write, print, call, rewind, and so on, and somehow the syntax of the language is able to keep track of the variables and the keywords. Construct names are a more recent language feature, and apparently it is not possible to avoid all conflicts with those.

Indeed. Lfortran managed to allow a construct name to be the same as the
name of a variable, but that is contrary to the standard and my bug report
to lfortran has correctly been labelled ā€œerror not reportedā€.

Thanks @Harper for the bug report. We now give an error message too (fix: throw error for do loop name already used for another variable by HarshilShah1804 Ā· Pull Request #8710 Ā· lfortran/lfortran Ā· GitHub).

It is a weird restriction - as the scope of the usage of the construct name is only within the construct itself. And the construct name is used in places where there shouldn’t be a conflict with a variable name. (E.g., end do, end if, end block, cycle, exit, etc.) Perhaps the restriction is there to prevent defining the same construct name twice - which seems reasonable, and the other cases got caught up in the restriction.

1 Like

It’s really not that weird… imho.

Even though the construct-name is only used within the limits of a construct, it’s a jump (like a GOTO), so it must both be unique to the program-unit that surrounds it, and also not be part of a specification-part.

I can imagine some confusing code could be written with assign statements and assigned goto statements that uses an integer variable with the same name as a construct name. The compiler might be able to keep things straight, but it might confuse a human reading the code.