From 439fa6eb178515965dac5240b6ced65a8cbb698a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 10 Aug 2015 10:11:24 +0200 Subject: [PATCH] core.macros: Fix a for corner case In case for doesn't get a body, raise the appropriate, descriptive error instead of an IndexOutOfBounds one. Also updated the failing test case. Signed-off-by: Gergely Nagy --- hy/core/macros.hy | 2 ++ tests/compilers/test_ast.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 6a8709f..fc296a8 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -98,6 +98,8 @@ (for* [y bar] baz))" (setv body (list body)) + (if (empty? body) + (macro-error None "`for' requires a body to evaluate")) (setv lst (get body -1)) (setv belse (if (and (isinstance lst HyExpression) (= (get lst 0) "else")) [(body.pop)] diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 0fe320e..4491f83 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -484,7 +484,7 @@ def test_for_compile_error(): assert(False) try: - can_compile("(fn [] (for [x]))") + can_compile("(fn [] (for [x] x))") except HyTypeError as e: assert(e.message == "`for' requires an even number of args.") else: @@ -497,6 +497,13 @@ def test_for_compile_error(): else: assert(False) + try: + can_compile("(fn [] (for [x xx] (else 1)))") + except HyTypeError as e: + assert(e.message == "`for' requires a body to evaluate") + else: + assert(False) + def test_attribute_access(): """Ensure attribute access compiles correctly"""