From 27dea136414c9021219de9a94c6e12a0e86a6d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Thu, 8 Jul 2021 14:05:15 +0200 Subject: [PATCH] Fix parser identifies call with named args as var (#10842) --- spec/compiler/parser/parser_spec.cr | 3 +++ src/compiler/crystal/syntax/parser.cr | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index b2e97a8e9f64..013020da646f 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -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)] diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index 32e0a6a65cd2..5aa31479bf1c 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -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 @@ -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) @@ -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