Skip to content

Commit 62a3a28

Browse files
committed
py: Set compiler scope before folding constants so error messages work.
Addresses issue adafruit#1140.
1 parent d010602 commit 62a3a28

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

py/compile.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,15 +3605,16 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
36053605
comp->is_repl = is_repl;
36063606
comp->compile_error = MP_OBJ_NULL;
36073607

3608-
// optimise constants
3608+
// create the module scope
3609+
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3610+
3611+
// optimise constants (scope must be set for error messages to work)
3612+
comp->scope_cur = module_scope;
36093613
mp_map_t consts;
36103614
mp_map_init(&consts, 0);
3611-
pn = fold_constants(comp, pn, &consts);
3615+
module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
36123616
mp_map_deinit(&consts);
36133617

3614-
// set the outer scope
3615-
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
3616-
36173618
// compile pass 1
36183619
comp->emit = emit_pass1_new();
36193620
comp->emit_method_table = &emit_pass1_method_table;
@@ -3764,7 +3765,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
37643765
#endif // !MICROPY_EMIT_CPYTHON
37653766

37663767
// free the parse tree
3767-
mp_parse_node_free(pn);
3768+
mp_parse_node_free(module_scope->pn);
37683769

37693770
// free the scopes
37703771
mp_raw_code_t *outer_raw_code = module_scope->raw_code;

0 commit comments

Comments
 (0)