Skip to content

Commit df2890a

Browse files
sdogruyolAry Borenszweig
authored and
Ary Borenszweig
committedDec 20, 2016
Disallow multiple assignment for constants. Fixes #3466
1 parent 19abbfc commit df2890a

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed
 

‎spec/compiler/normalize/multi_assign_spec.cr

-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ describe "Normalize: multi assign" do
55
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"
66
end
77

8-
it "normalizes n to n with constants" do
9-
assert_expand "a, B, C = 1, 2, 3", "__temp_1 = 1\na = __temp_1\nB = 2\nC = 3"
10-
end
11-
128
it "normalizes 1 to n" do
139
assert_expand_second "d = 1\na, b, c = d", "__temp_1 = d\na = __temp_1[0]\nb = __temp_1[1]\nc = __temp_1[2]"
1410
end

‎spec/compiler/parser/parser_spec.cr

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ describe "Parser" do
134134

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

139138
assert_syntax_error "1 == 2, a = 4"
140139
assert_syntax_error "x : String, a = 4"
@@ -1324,6 +1323,8 @@ describe "Parser" do
13241323
"can't change the value of self"
13251324
assert_syntax_error "self += 1",
13261325
"can't change the value of self"
1326+
assert_syntax_error "FOO, BAR = 1, 2",
1327+
"Multiple assignment is not allowed for constants"
13271328
assert_syntax_error "self, x = 1, 2",
13281329
"can't change the value of self"
13291330
assert_syntax_error "x, self = 1, 2",

‎src/compiler/crystal/syntax/parser.cr

+6-4
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,10 @@ module Crystal
116116

117117
case @token.type
118118
when :","
119-
# Continue
120-
unexpected_token unless last_is_target
119+
unless last_is_target
120+
raise "Multiple assignment is not allowed for constants" if last.is_a?(Path)
121+
unexpected_token
122+
end
121123
when :NEWLINE, :";"
122124
return last
123125
else
@@ -173,7 +175,7 @@ module Crystal
173175
targets << assign
174176
values << assign.args.pop
175177
else
176-
raise "Bug: mutliassign index expression can only be Assign or Call"
178+
raise "Bug: multiassign index expression can only be Assign or Call"
177179
end
178180

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

188190
def multi_assign_target?(exp)
189191
case exp
190-
when Underscore, Var, InstanceVar, ClassVar, Global, Path, Assign
192+
when Underscore, Var, InstanceVar, ClassVar, Global, Assign
191193
true
192194
when Call
193195
!exp.has_parentheses? && (

0 commit comments

Comments
 (0)
Please sign in to comment.