From 03aafad65719d147a39cbc948020aa03b5daa94d Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 24 Jul 2018 08:59:52 -0700 Subject: [PATCH] Make empty expressions illegal at the top level --- NEWS.rst | 7 +++++++ hy/compiler.py | 5 +++-- tests/compilers/test_ast.py | 6 ++++++ tests/native_tests/language.hy | 5 ----- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 3b57b8a..2b92b99 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -1,5 +1,12 @@ .. default-role:: code +Unreleased +============================== + +Removals +------------------------------ +* Empty expressions (`()`) are no longer legal at the top level. + 0.15.0 ============================== diff --git a/hy/compiler.py b/hy/compiler.py index 8fcb854..f92fe4d 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1546,8 +1546,9 @@ class HyASTCompiler(object): # Go through compile again if the type changed. return self.compile(expression) - if expression == []: - return self.compile_atom(HyList().replace(expression)) + if not expression: + raise HyTypeError( + expression, "empty expressions are not allowed at top level") fn = expression[0] func = None diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 5fbc727..a5f42af 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -67,6 +67,12 @@ def test_ast_bad_type(): pass +def test_empty_expr(): + "Empty expressions should be illegal at the top level." + cant_compile("(print ())") + can_compile("(print '())") + + def test_ast_bad_if(): "Make sure AST can't compile invalid if*" cant_compile("(if*)") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 37f4d4d..59141b5 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -1401,11 +1401,6 @@ (assert (= y [5]))) -(defn test-empty-list [] - "Evaluate an empty list to a []" - (assert (= () []))) - - (defn test-string [] (assert (string? (string "a"))) (assert (string? (string 1)))