From 5b879323aa094cda4ba50a29c17f0d6de9b1817c Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Thu, 1 Dec 2016 01:49:51 -0600 Subject: [PATCH] Lvalue of setv is checked too early when using `or` Fix #1151 --- hy/compiler.py | 11 +++++---- tests/native_tests/language.hy | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 3a1fa92..34402bc 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1806,16 +1806,17 @@ class HyASTCompiler(object): ctx=ast.Load(), lineno=root_line, col_offset=root_column) + temp_variables = [name, expr_name] def make_assign(value, node=None): if node is None: line, column = root_line, root_column else: line, column = node.lineno, node.col_offset - return ast.Assign(targets=[ast.Name(id=var, - ctx=ast.Store(), - lineno=line, - col_offset=column)], + positioned_name = ast.Name(id=var, ctx=ast.Store(), + lineno=line, col_offset=column) + temp_variables.append(positioned_name) + return ast.Assign(targets=[positioned_name], value=value, lineno=line, col_offset=column) @@ -1845,7 +1846,7 @@ class HyASTCompiler(object): orelse=[])) current = current[-1].body ret = sum(root, ret) - ret += Result(expr=expr_name, temp_variables=[expr_name, name]) + ret += Result(expr=expr_name, temp_variables=temp_variables) else: ret += ast.BoolOp(op=opnode(), lineno=root_line, diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index ce0410b..7ab27ad 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -949,6 +949,28 @@ (and 0 (setv a 2)) (assert (= a 1))) +(defn test-and-#1151-do [] + (setv a (and 0 (do 2 3))) + (assert (= a 0)) + (setv a (and 1 (do 2 3))) + (assert (= a 3))) + +(defn test-and-#1151-for [] + (setv l []) + (setv x (and 0 (for [n [1 2]] (.append l n)))) + (assert (= x 0)) + (assert (= l [])) + (setv x (and 15 (for [n [1 2]] (.append l n)))) + (assert (= l [1 2]))) + +(defn test-and-#1151-del [] + (setv l ["a" "b"]) + (setv x (and 0 (del (get l 1)))) + (assert (= x 0)) + (assert (= l ["a" "b"])) + (setv x (and 15 (del (get l 1)))) + (assert (= l ["a"]))) + (defn test-or [] "NATIVE: test the or function" @@ -967,6 +989,27 @@ (or 1 (setv a 2)) (assert (= a 1))) +(defn test-or-#1151-do [] + (setv a (or 1 (do 2 3))) + (assert (= a 1)) + (setv a (or 0 (do 2 3))) + (assert (= a 3))) + +(defn test-or-#1151-for [] + (setv l []) + (setv x (or 15 (for [n [1 2]] (.append l n)))) + (assert (= x 15)) + (assert (= l [])) + (setv x (or 0 (for [n [1 2]] (.append l n)))) + (assert (= l [1 2]))) + +(defn test-or-#1151-del [] + (setv l ["a" "b"]) + (setv x (or 15 (del (get l 1)))) + (assert (= x 15)) + (assert (= l ["a" "b"])) + (setv x (or 0 (del (get l 1)))) + (assert (= l ["a"]))) (defn test-xor [] "NATIVE: test the xor macro"