Skip to content

Commit

Permalink
Disallow '!' or '?' at the end of the LHS in an assignment
Browse files Browse the repository at this point in the history
Issue: #6685
This affects mainly variable defined in top level, instance and class variable raise already an exception.
  • Loading branch information
Maroo-b committed Mar 24, 2019
1 parent 3286e11 commit ce8e1f8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 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 "b? = 1", "unexpected token: ="
assert_syntax_error "b! = 1", "unexpected token: ="
assert_syntax_error "a, B = 1, 2", "can't assign to constant in multiple assignment"

assert_syntax_error "1 == 2, a = 4"
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Crystal::Command
end

private def execute(output_filename, run_args, compiler)
time? = @time && !@progress_tracker.stats?
time = @time && !@progress_tracker.stats?
status, elapsed_time = @progress_tracker.stage("Execute") do
begin
elapsed = Time.measure do
Expand All @@ -221,7 +221,7 @@ class Crystal::Command
end
end

if time?
if time
puts "Execute: #{elapsed_time}"
end

Expand Down
6 changes: 5 additions & 1 deletion src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ module Crystal
break
when :"="
slash_is_regex!

if atomic.is_a?(Call) && atomic.name == "[]"
next_token_skip_space_or_newline

Expand All @@ -333,6 +332,11 @@ module Crystal
raise "can't change the value of self", location
end

if atomic.is_a?(Call) && (atomic.name.ends_with?('?') ||
atomic.name.ends_with?('!'))
raise "unexpected token: =", location
end

atomic = Var.new(atomic.name).at(atomic) if atomic.is_a?(Call)

next_token_skip_space_or_newline
Expand Down

0 comments on commit ce8e1f8

Please sign in to comment.