Skip to content

Commit

Permalink
Disallow multiple assignment for constants. Fixes #3466
Browse files Browse the repository at this point in the history
  • Loading branch information
Serdar Dogruyol authored and asterite committed Dec 20, 2016
1 parent 19abbfc commit df2890a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 0 additions & 4 deletions spec/compiler/normalize/multi_assign_spec.cr
Expand Up @@ -5,10 +5,6 @@ describe "Normalize: multi assign" do
assert_expand "a, b, c = 1, 2, 3", "__temp_1 = 1\n__temp_2 = 2\n__temp_3 = 3\na = __temp_1\nb = __temp_2\nc = __temp_3"
end

it "normalizes n to n with constants" do
assert_expand "a, B, C = 1, 2, 3", "__temp_1 = 1\na = __temp_1\nB = 2\nC = 3"
end

it "normalizes 1 to n" 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
Expand Down
3 changes: 2 additions & 1 deletion spec/compiler/parser/parser_spec.cr
Expand Up @@ -134,7 +134,6 @@ describe "Parser" do

it_parses "@a, b = 1, 2", MultiAssign.new(["@a".instance_var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "@@a, b = 1, 2", MultiAssign.new(["@@a".class_var, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)
it_parses "A, b = 1, 2", MultiAssign.new(["A".path, "b".var] of ASTNode, [1.int32, 2.int32] of ASTNode)

assert_syntax_error "1 == 2, a = 4"
assert_syntax_error "x : String, a = 4"
Expand Down Expand Up @@ -1324,6 +1323,8 @@ describe "Parser" do
"can't change the value of self"
assert_syntax_error "self += 1",
"can't change the value of self"
assert_syntax_error "FOO, BAR = 1, 2",
"Multiple assignment is not allowed for constants"
assert_syntax_error "self, x = 1, 2",
"can't change the value of self"
assert_syntax_error "x, self = 1, 2",
Expand Down
10 changes: 6 additions & 4 deletions src/compiler/crystal/syntax/parser.cr
Expand Up @@ -116,8 +116,10 @@ module Crystal

case @token.type
when :","
# Continue
unexpected_token unless last_is_target
unless last_is_target
raise "Multiple assignment is not allowed for constants" if last.is_a?(Path)
unexpected_token
end
when :NEWLINE, :";"
return last
else
Expand Down Expand Up @@ -173,7 +175,7 @@ module Crystal
targets << assign
values << assign.args.pop
else
raise "Bug: mutliassign index expression can only be Assign or Call"
raise "Bug: multiassign index expression can only be Assign or Call"
end

values.concat exps[assign_index + 1..-1]
Expand All @@ -187,7 +189,7 @@ module Crystal

def multi_assign_target?(exp)
case exp
when Underscore, Var, InstanceVar, ClassVar, Global, Path, Assign
when Underscore, Var, InstanceVar, ClassVar, Global, Assign
true
when Call
!exp.has_parentheses? && (
Expand Down

0 comments on commit df2890a

Please sign in to comment.