Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler: give parser error when assigning a constant inside a multiassign #7468

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Compiler: give pare error when assigning a constant inside a multiassign
  • Loading branch information
asterite committed Feb 21, 2019
commit cf07e09a4072e7e6095420ed092492d1e52416cd
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