Deferred statements should capture state of variables
zhiayang opened this issue · 0 comments
Basically
func thing()
{
var x = 1;
defer printf("%d\n", x);
x = 2;
// deferred statement runs here, outputs 1
}
The defer statement should capture the state of variables etc. at the point of declaration. This ought to include all global state, as well as local state (stack variables). Might be non-trivial, especially to capture program state as a whole. If x
above was a global variable instead, it would be a lot harder to capture.
If data/control flow analysis gets sufficiently implemented, it could be possible to trace the mutability of any given global variable (ie. is it ever assigned to or modified between the declaration of the defer
and the execution of it), and only capture non-const variables that have the possibility of changing. Not sure if worth it or not. This would also need to consider volatile
variables, and always include them.
Finally, it will definitely be non-trivial to ensure that global state across modules is captured as well. This might not even be possible without going through a number of hoops in the code generation stage (more passes anyone?)
There is of course the option of simply not capturing global state at all. It makes our lives easier, but will be less predictable for programmers, since the global state might have changed between the time the defer
statement is declared and when it is executed. Would a warning suffice if the user uses a global variable in a deferred statement?