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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy