Skip to content

Commit 5a57a8b

Browse files
committed
compile: make SyntaxError on return outside function
1 parent 19f32cb commit 5a57a8b

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

compile/compile.go

+3
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ func (c *compiler) Stmt(stmt ast.Stmt) {
967967
c.class(stmt, node)
968968
case *ast.Return:
969969
// Value Expr
970+
if c.SymTable.Type != symtable.FunctionBlock {
971+
panic(py.ExceptionNewf(py.SyntaxError, "'return' outside function"))
972+
}
970973
if node.Value != nil {
971974
c.Expr(node.Value)
972975
} else {

compile/compile_data_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -2254,6 +2254,7 @@ var compileTestData = []struct {
22542254
Firstlineno: 1,
22552255
Lnotab: "",
22562256
}, nil, ""},
2257+
{"return", "exec", nil, py.SyntaxError, "'return' outside function"},
22572258
{"def fn(): pass", "exec", &py.Code{
22582259
Argcount: 0,
22592260
Kwonlyargcount: 0,

compile/make_compile_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def nested(d):
176176
('''f(a, b, *args)''', "eval"),
177177
('''f(a, b, *args, d=e, **kwargs)''', "eval"),
178178
('''f(a, d=e, **kwargs)''', "eval"),
179+
# return
180+
('''return''', "exec", SyntaxError),
179181
# def
180182
('''def fn(): pass''', "exec"),
181183
('''def fn(a): pass''', "exec"),

0 commit comments

Comments
 (0)