Skip to content

Commit

Permalink
Merge pull request #7468 from asterite/bug/parse-constant-in-multiassign
Browse files Browse the repository at this point in the history
Compiler: give parser error when assigning a constant inside a multiassign
  • Loading branch information
asterite authored Feb 21, 2019
2 parents 2ca522f + cf07e09 commit 2ae0c29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ module Crystal
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)

assert_syntax_error "a, B = 1, 2", "can't assign to constant in multiple assignment"

assert_syntax_error "1 == 2, a = 4"
assert_syntax_error "x : String, a = 4"
assert_syntax_error "b, 1 == 2, a = 4"
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ module Crystal
unexpected_token
end

targets = exps[0...assign_index].map { |exp| to_lhs(exp) }
targets = exps[0...assign_index].map { |exp| multiassign_left_hand(exp) }

assign = exps[assign_index]
values = [] of ASTNode

case assign
when Assign
targets << to_lhs(assign.target)
targets << multiassign_left_hand(assign.target)
values << assign.value
when Call
assign.name = assign.name.byte_slice(0, assign.name.bytesize - 1)
Expand Down Expand Up @@ -216,9 +216,9 @@ module Crystal
end
end

def to_lhs(exp)
if exp.is_a?(Path) && inside_def?
raise "dynamic constant assignment. Constants can only be declared at the top level or inside other types."
def multiassign_left_hand(exp)
if exp.is_a?(Path)
raise "can't assign to constant in multiple assignment", exp.location.not_nil!
end

if exp.is_a?(Call) && !exp.obj && exp.args.empty?
Expand Down

0 comments on commit 2ae0c29

Please sign in to comment.