From 02918a8186e302bf478dfa2b5a199511d40586c0 Mon Sep 17 00:00:00 2001 From: RX14 Date: Sat, 12 Aug 2017 17:13:11 +0100 Subject: [PATCH] Remove bare array creation from multi assign --- spec/compiler/normalize/multi_assign_spec.cr | 12 ------------ spec/compiler/parser/parser_spec.cr | 4 ++-- src/compiler/crystal/semantic/literal_expander.cr | 14 ++------------ src/compiler/crystal/syntax/parser.cr | 2 +- 4 files changed, 5 insertions(+), 27 deletions(-) diff --git a/spec/compiler/normalize/multi_assign_spec.cr b/spec/compiler/normalize/multi_assign_spec.cr index 7444db607b2a..04968aa5aab3 100644 --- a/spec/compiler/normalize/multi_assign_spec.cr +++ b/spec/compiler/normalize/multi_assign_spec.cr @@ -9,10 +9,6 @@ describe "Normalize: multi assign" do assert_expand_second "d = 1\na, b, c = d", "__temp_1 = d\na = __temp_1[0]\nb = __temp_1[1]\nc = __temp_1[2]" end - it "normalizes n to 1" do - assert_expand "a = 1, 2", "a = [1, 2]" - end - it "normalizes n to n with []" do assert_expand_third "a = 1; b = 2; a[0], b[1] = 2, 3", "__temp_1 = 2\n__temp_2 = 3\na[0] = __temp_1\nb[1] = __temp_2" end @@ -21,10 +17,6 @@ describe "Normalize: multi assign" do assert_expand_third "a = 1; b = 2; a[0], b[1] = 2", "__temp_1 = 2\na[0] = __temp_1[0]\nb[1] = __temp_1[1]" end - it "normalizes n to 1 with []" do - assert_expand_second "a = 1; a[0] = 1, 2, 3", "a[0] = [1, 2, 3]" - end - it "normalizes n to n with call" do assert_expand_third "a = 1; b = 2; a.foo, b.bar = 2, 3", "__temp_1 = 2\n__temp_2 = 3\na.foo = __temp_1\nb.bar = __temp_2" end @@ -32,8 +24,4 @@ describe "Normalize: multi assign" do it "normalizes 1 to n with call" do assert_expand_third "a = 1; b = 2; a.foo, b.bar = 2", "__temp_1 = 2\na.foo = __temp_1[0]\nb.bar = __temp_1[1]" end - - it "normalizes n to 1 with call" do - assert_expand_second "a = 1; a.foo = 1, 2, 3", "a.foo = [1, 2, 3]" - end end diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index c9a27cf17f69..211c02ded775 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -122,11 +122,9 @@ describe "Parser" do it_parses "a = 1", Assign.new("a".var, 1.int32) it_parses "a = b = 2", Assign.new("a".var, Assign.new("b".var, 2.int32)) - it_parses "a = 1, 2", MultiAssign.new(["a".var] of ASTNode, [1.int32, 2.int32] of ASTNode) it_parses "a, b = 1, 2", MultiAssign.new(["a".var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode) it_parses "a, b = 1", MultiAssign.new(["a".var, "b".var] of ASTNode, [1.int32] of ASTNode) it_parses "_, _ = 1, 2", MultiAssign.new([Underscore.new, Underscore.new] of ASTNode, [1.int32, 2.int32] of ASTNode) - it_parses "a[0] = 1, 2", MultiAssign.new([Call.new("a".call, "[]", 0.int32)] of ASTNode, [1.int32, 2.int32] of ASTNode) it_parses "a[0], a[1] = 1, 2", MultiAssign.new([Call.new("a".call, "[]", 0.int32), Call.new("a".call, "[]", 1.int32)] of ASTNode, [1.int32, 2.int32] of ASTNode) it_parses "a.foo, a.bar = 1, 2", MultiAssign.new([Call.new("a".call, "foo"), Call.new("a".call, "bar")] of ASTNode, [1.int32, 2.int32] of ASTNode) it_parses "x = 0; a, b = x += 1", [Assign.new("x".var, 0.int32), MultiAssign.new(["a".var, "b".var] of ASTNode, [OpAssign.new("x".var, "+", 1.int32)] of ASTNode)] of ASTNode @@ -138,6 +136,8 @@ describe "Parser" do assert_syntax_error "1 == 2, a = 4" assert_syntax_error "x : String, a = 4" assert_syntax_error "b, 1 == 2, a = 4" + assert_syntax_error "a = 1, 2, 3", "Multiple assignment count mismatch" + assert_syntax_error "a = 1, b = 2", "Multiple assignment count mismatch" it_parses "def foo\n1\nend", Def.new("foo", body: 1.int32) it_parses "def downto(n)\n1\nend", Def.new("downto", ["n".arg], 1.int32) diff --git a/src/compiler/crystal/semantic/literal_expander.cr b/src/compiler/crystal/semantic/literal_expander.cr index bdeceb88c388..71c78c87b757 100644 --- a/src/compiler/crystal/semantic/literal_expander.cr +++ b/src/compiler/crystal/semantic/literal_expander.cr @@ -553,18 +553,6 @@ module Crystal end exps = Expressions.new(assigns) - # From: - # - # a = 1, 2, 3 - # - # To: - # - # a = [1, 2, 3] - elsif node.targets.size == 1 - target = node.targets.first - array = ArrayLiteral.new(node.values) - exps = transform_multi_assign_target(target, array) - # From: # # a, b = c, d @@ -576,6 +564,8 @@ module Crystal # a = temp1 # b = temp2 else + raise "BUG: multiple assignment count mismatch" unless node.targets.size == node.values.size + temp_vars = node.values.map { new_temp_var } assign_to_temps = [] of ASTNode diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index a1b3d9128363..8146d706221d 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -180,7 +180,7 @@ module Crystal end values.concat exps[assign_index + 1..-1] - if values.size != 1 && targets.size != 1 && targets.size != values.size + if values.size != 1 && targets.size != values.size raise "Multiple assignment count mismatch", location end