Fix AST generation of a naked return

This commit is contained in:
Simon Gomizelj 2018-02-08 15:08:09 -05:00
parent 7b87d42221
commit 316220b742
3 changed files with 21 additions and 3 deletions

View File

@ -1,5 +1,12 @@
.. default-role:: code
Unreleased
==============================
Bug Fixes
------------------------------
* Fix `(return)` so it works correctly to exit a Python 2 generator
0.14.0
==============================

View File

@ -2040,8 +2040,10 @@ class HyASTCompiler(object):
@checkargs(max=1)
def compile_return(self, expr):
ret = Result()
if len(expr) > 1:
ret += self.compile(expr[1])
if len(expr) == 1:
return asty.Return(expr, value=None)
ret += self.compile(expr[1])
return ret + asty.Return(expr, value=ret.force_expr)
@builds("defclass")

View File

@ -8,7 +8,7 @@ from __future__ import unicode_literals
from hy import HyString
from hy.models import HyObject
from hy.compiler import hy_compile
from hy.importer import import_buffer_to_hst
from hy.importer import hy_eval, import_buffer_to_hst
from hy.errors import HyCompileError, HyTypeError
from hy.lex.exceptions import LexException
from hy._compat import PY3
@ -30,6 +30,10 @@ def can_compile(expr):
return hy_compile(import_buffer_to_hst(expr), "__main__")
def can_eval(expr):
return hy_eval(import_buffer_to_hst(expr))
def cant_compile(expr):
try:
hy_compile(import_buffer_to_hst(expr), "__main__")
@ -664,3 +668,8 @@ def test_ast_good_yield_from():
def test_ast_bad_yield_from():
"Make sure AST can't compile invalid yield-from"
cant_compile("(yield-from)")
def test_eval_generator_with_return():
"""Ensure generators with a return statement works."""
can_eval("(fn [] (yield 1) (yield 2) (return))")