for loop with body "puts $i" does not compile correctly
Closed / Duplicate217782ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 25 years ago
Descriptionplain2000-10-26 00:43:34
OriginalBugID: 727 Bug
Version: 8.0.3
SubmitDate: '1998-10-13'
LastModified: '1999-08-13'
Severity: SER
Status: Closed
Submitter: admin
ChangedBy: hobbs
RelatedBugIDs: 1669
OS: All
OSVersion: NA
Machine: NA
FixedDate: '2000-10-25'
FixedInVersion: NA
ClosedDate: '1999-08-13'
"Jeffrey P. Talberg" wrote:
>
> I was trying to create a simple tcl example that I can use for teaching
> a course illustrating the difference between "" and {} grouping by using
> the for command .
[snip]
> #-----------------------------------------
> # This is the one I had a question about
> #-----------------------------------------
> for {set i 0} {$i < 10} {incr i} "puts $i"
>
> Why does the last version still print the numbers 0-9? I expected it to
> print 0 10 times. I thought the statement "puts $i" would become "puts
> 0" when the for command underwent grouping and substitution prior to
> command dispatch.
You are correct, that's what it should do.
>
> I was intrigued by these results, can anyone explain why tcl behaves
> this way?
That sure looks like a bug to me. Probably our friend the byte compiler
doing some "optimizations" for us.
This has prompted me, finally, to reinstall tcl 7.6. It seems like there
are lots of little bugaboos popping up that seem to be related to the
byte compiler. Sure enough, running this example with 7.6 gives the
expected results:
% for {set i 0} {$i < 10} {incr i} "puts $i"
can't read "i": no such variable
% set i 0
0
% for {set i 0} {$i < 10} {incr i} "puts $i"
0
0
0
...
FWIW, I can duplicate the behavior you report with both 8.0.3 and 8.1a2
and protclsh. Interestingly, not only does the loop perform wrong, but
the interpreter doesn't seem to care that $i isn't defined during the
parsing phase. It looks like the byte compiler is just assuming the body
shouldn't go through substitution regardless of the quoting (shame on
it!).
As proof, modifying the code to be this:
for {set i 0} {$i < 10} {incr i} [list puts $i]
... gives the same (broken) results. Interestingly, this only seems to
happen if the variable in the print statement is the same as the loop
variable. For example, if I change the command to be "puts $z" I get the
expected error message "can't read "z": no such variable".
This still exists in 8.2b3. See related 1669 for detailed evaluation.
-- 08/13/1999 hobbs
welch Closed ticket [217782ffff].
[History diff]2000-10-26 07:49:17
msoferplain2001-08-22 22:18:34
Logged In: YES user_id=148712 This behaviour is generalized in bug report #219166
Log in (anonymous is fine) to add a comment.