unset traces on local vars write in caller context
proc constant {name value} {
upvar 1 $name c
set c $value
trace variable c wu [list reset $value]
}
proc reset {v a i o} {
uplevel 1 [list constant $a $v]
}
proc demo {} {
puts [info exists pi]
constant pi 3.14
puts [info exists pi]
}
puts [info exists pi]
demo
puts [info exists pi]
Output is:
0
0
1
1
Output should be:
0
0
1
0
The unset trace that re-creates the variable
is doing so in the caller context of [demo].
This is wrong.
b98ee56376[7f02ff1efa] Fix scope for var unset traces when procs return. *** POTENTIAL INCOMPATIBILITY *** check-in2016-09-08 02:38:39b25357cf3aAttempt to fix [7f02ff1efa]. Make trace-18.1 fail. Suspect test is an experiment that preserves the bug. check-in2016-09-07 18:31:58
The same flaw is present on core-8-0-3-pr which suggests it is in all of recorded Tcl history.
I have a binary of tclsh 7.5p1 which exhibits the same flaw. This raises the concern that this has been wrong for so long, there may well be scripts relying on the bug. Very old, unmaintained scripts. Suggest we fix only in 8.7+, at least at first.
See branch bug-7f02ff1efa. Test trace-18.1 fails. Suspect the test is wrong.
Fixed and tested in 8.7.
Note that the documentation currently states: "If an unset occurs because of a procedure return, then the trace will be invoked in the variable context of the procedure being returned to:" So although the exhibited behaviour may not be desirable, it is what is documented.
So it is. That documentation is also as old as recorded Tcl history. The design seems unusably broken if you ask me. It forces you to write a handler script that functions correctly in 2 completely different contexts. I'll revert the change from the trunk (8.7). For now, I plan to leave it in novem. At least this wart should be removed in Tcl 9, IMHO, if not a more complete reworking of traces altogether. Experience shows me that a programmatic unset of a variable is meaningfully different from the destruction of a variable (say, because context or interp are going away), and the trace mechanism ought to be able to react to that difference.
Personally, I would not have any heartburn if it was fixed for 8.7. Difficult for me to imagine any code that relied on this behaviour. But then again my imagination doesn't go too far.
I'd like to totally remove traces on local variables from novem. The semantics are really expensive to implement and deeply confusing. However, I'd need to replace with an ability to get callbacks when a frame terminates (that is useful) and having the ability to designate variables as being unable to change after the initial setup is entirely reasonable too.
FWIW, tclquadcode is never going to be exactly reproducing of the trace-level view. The most we're likely to promise is that if a global variable would have been read from or written to at least once on some path between two suspension points (e.g., entries and calls to return or yield), a trace will be fired. That's really quite different to getting the semantics right, and gives us a lot more freedom to optimise.
unset traces on local variables are a nice means for firing handlers when local scopes are left. we are using this in some places for the automatic cleanup of local objects, when a proc scope is left. Maybe this behavior could be moved with some tweaking into command leave traces. However, the trace handler has no access to the local vars of the left proc, so one has to record the information about the objects which should be deleted somewhere else. E.g. defining on-shot handlers for every proc invocation seems expensive, global vars are not pretty, ...
test trace-18.5 is failing again on novem. Time to consider how we handle this issue again.