diff options
author | Damien George <damien.p.george@gmail.com> | 2018-10-26 16:48:07 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-10-28 00:33:08 +1100 |
commit | 9201f46cc8e92231f0f6c92e4d56befbc150f72c (patch) | |
tree | 425eeb6f9031a94acf2b79cb56a0a35a902da173 /py/compile.c | |
parent | c2074e7b66a042492604fbf9ea80b71cdf848e93 (diff) |
py/compile: Fix case of eager implicit conversion of local to nonlocal.
This ensures that implicit variables are only converted to implicit
closed-over variables (nonlocals) at the very end of the function scope.
If variables are closed-over when first used (read from, as was done prior
to this commit) then this can be incorrect because the variable may be
assigned to later on in the function which means they are just a plain
local, not closed over.
Fixes issue #4272.
Diffstat (limited to 'py/compile.c')
-rw-r--r-- | py/compile.c | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/py/compile.c b/py/compile.c index e90b366e0..e708bde8e 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1196,7 +1196,8 @@ STATIC void compile_declare_global(compiler_t *comp, mp_parse_node_t pn, qstr qs STATIC void compile_declare_nonlocal(compiler_t *comp, mp_parse_node_t pn, qstr qst, bool added, id_info_t *id_info) { if (added) { - scope_find_local_and_close_over(comp->scope_cur, id_info, qst); + id_info->kind = ID_INFO_KIND_GLOBAL_IMPLICIT; + scope_check_to_close_over(comp->scope_cur, id_info); if (id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { compile_syntax_error(comp, pn, "no binding for nonlocal found"); } @@ -3391,6 +3392,14 @@ mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_f #endif } else { compile_scope(comp, s, MP_PASS_SCOPE); + + // Check if any implicitly declared variables should be closed over + for (size_t i = 0; i < s->id_info_len; ++i) { + id_info_t *id = &s->id_info[i]; + if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { + scope_check_to_close_over(s, id); + } + } } // update maximim number of labels needed |