py/compile: Allow 'return' outside function in minimal builds.

A 'return' statement on module/class level is not correct Python, but
nothing terribly bad happens when it's allowed.  So remove the check unless
MICROPY_CPYTHON_COMPAT is on.

This is similar to MicroPython's treatment of 'import *' in functions
(except 'return' has unsurprising behavior if it's allowed).
This commit is contained in:
Petr Viktorin 2020-01-14 16:47:20 +01:00 committed by Damien George
parent d6a1e45caa
commit e6c9800645

View File

@ -1041,10 +1041,12 @@ STATIC void compile_break_cont_stmt(compiler_t *comp, mp_parse_node_struct_t *pn
} }
STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
#if MICROPY_CPYTHON_COMPAT
if (comp->scope_cur->kind != SCOPE_FUNCTION) { if (comp->scope_cur->kind != SCOPE_FUNCTION) {
compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function"); compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
return; return;
} }
#endif
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
// no argument to 'return', so return None // no argument to 'return', so return None
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);