Skip to content

Commit

Permalink
Fix parser identifies call with named args as var (#10842)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Jul 8, 2021
1 parent 9020ed8 commit 27dea13
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ module Crystal
it_parses "foo = 1; foo(-1)", [Assign.new("foo".var, 1.int32), Call.new(nil, "foo", -1.int32)]
it_parses "foo = 1; b = 2; foo -b", [Assign.new("foo".var, 1.int32), Assign.new("b".var, 2.int32), Call.new("foo".var, "-", "b".var)]
it_parses "foo = 1; b = 2; foo +b", [Assign.new("foo".var, 1.int32), Assign.new("b".var, 2.int32), Call.new("foo".var, "+", "b".var)]
it_parses "foo = 1; foo a: 1", Expressions.new([Assign.new("foo".var, 1.int32), Call.new(nil, "foo", named_args: [NamedArgument.new("a", 1.int32)])] of ASTNode)
it_parses "foo = 1; foo {}", Expressions.new([Assign.new("foo".var, 1.int32), Call.new(nil, "foo", block: Block.new)] of ASTNode)
it_parses "foo = 1; foo &x", Expressions.new([Assign.new("foo".var, 1.int32), Call.new(nil, "foo", block_arg: "x".call)] of ASTNode)
it_parses "def foo(x)\n x\nend; foo = 1; b = 2; foo -b", [Def.new("foo", ["x".arg], "x".var), Assign.new("foo".var, 1.int32), Assign.new("b".var, 2.int32), Call.new("foo".var, "-", "b".var)]
it_parses "def foo(x)\n x\nend; foo = 1; b = 2; foo +b", [Def.new("foo", ["x".arg], "x".var), Assign.new("foo".var, 1.int32), Assign.new("b".var, 2.int32), Call.new("foo".var, "+", "b".var)]

Expand Down
7 changes: 4 additions & 3 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4147,6 +4147,7 @@ module Crystal
block_arg = call_args.block_arg
named_args = call_args.named_args
has_parentheses = call_args.has_parentheses
force_call ||= has_parentheses || (args.try(&.empty?) == false) || (named_args.try(&.empty?) == false)
else
has_parentheses = false
end
Expand Down Expand Up @@ -4189,8 +4190,8 @@ module Crystal
call
else
if args
maybe_var = !force_call && is_var && !has_parentheses
if maybe_var && args.size == 0
maybe_var = !force_call && is_var
if maybe_var
Var.new(name)
else
call = Call.new(nil, name, args, nil, nil, named_args, global)
Expand All @@ -4209,7 +4210,7 @@ module Crystal
end
Var.new(name)
else
if !force_call && !named_args && !global && !has_parentheses && @assigned_vars.includes?(name)
if !force_call && !named_args && !global && @assigned_vars.includes?(name)
raise "can't use variable name '#{name}' inside assignment to variable '#{name}'", location
end

Expand Down

0 comments on commit 27dea13

Please sign in to comment.