Open 315234 opened 8 years ago
Just happened upon this and have been thinking about it a bit. There are kind of two issues. (1) Inference and codegen cause all the bindings they examine to get resolved. That's not ideal, but is a practical necessity. (2) This case, where the same block of code reads and writes a new global variable. It's not a problem inside functions, because the function would have to contain a global
declaration, which takes effect as soon as the method is defined (even before inference). But at the top level there need not be a global
declaration, and if there were we couldn't run it in advance since top-level code might conditionally avoid executing the declaration. So we end up in this bad case where inference causes diag
to be resolved, but completely ignores the assignment until run time.
We could tell inference and codegen not to resolve bindings in top-level code, but that could lead to really bad and unpredictable performance. Or a hybrid approach could be avoiding binding resolution just for names that are assigned somewhere (those being the "less predictable" bindings).
Sorry the title is so vague but I can't get my head around what is happening here. Minimal code which reproduces this bug:
Output:
Removing the
if
orfor
blocks results in this bug not triggering, so it looks like this has something to do with soft vs hard scope maybe. Rebinding an existing variable name is also required.