diff --git a/spec/compiler/formatter/formatter_spec.cr b/spec/compiler/formatter/formatter_spec.cr index 943dea41ed14..4d1549b45a1f 100644 --- a/spec/compiler/formatter/formatter_spec.cr +++ b/spec/compiler/formatter/formatter_spec.cr @@ -209,6 +209,8 @@ describe Crystal::Formatter do assert_format "def foo ( &@block) \n end", "def foo(&@block)\nend" assert_format "def foo ( @select) \n end", "def foo(@select)\nend" assert_format "def foo ( @@select) \n end", "def foo(@@select)\nend" + assert_format "def foo ( bar @select) \n end", "def foo(bar @select)\nend" + assert_format "def foo ( bar @@select) \n end", "def foo(bar @@select)\nend" assert_format "def foo(a, &@b)\nend" assert_format "def foo ( x = 1 ) \n end", "def foo(x = 1)\nend" assert_format "def foo ( x : Int32 ) \n end", "def foo(x : Int32)\nend" diff --git a/spec/compiler/parser/parser_spec.cr b/spec/compiler/parser/parser_spec.cr index 8da2cb980dd7..2389a72af9c1 100644 --- a/spec/compiler/parser/parser_spec.cr +++ b/spec/compiler/parser/parser_spec.cr @@ -268,6 +268,8 @@ module Crystal it_parses "def foo(#{kw} foo); end", Def.new("foo", [Arg.new("foo", external_name: kw.to_s)]) it_parses "def foo(@#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: kw.to_s)], [Assign.new("@#{kw}".instance_var, "__arg0".var)] of ASTNode) it_parses "def foo(@@#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: kw.to_s)], [Assign.new("@@#{kw}".class_var, "__arg0".var)] of ASTNode) + it_parses "def foo(x @#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: "x")], [Assign.new("@#{kw}".instance_var, "__arg0".var)] of ASTNode) + it_parses "def foo(x @@#{kw}); end", Def.new("foo", [Arg.new("__arg0", external_name: "x")], [Assign.new("@@#{kw}".class_var, "__arg0".var)] of ASTNode) assert_syntax_error "foo { |#{kw}| }", "cannot use '#{kw}' as a block parameter name", 1, 8 assert_syntax_error "foo { |(#{kw})| }", "cannot use '#{kw}' as a block parameter name", 1, 9 diff --git a/src/compiler/crystal/syntax/parser.cr b/src/compiler/crystal/syntax/parser.cr index 0b69138720e9..b011edf0630d 100644 --- a/src/compiler/crystal/syntax/parser.cr +++ b/src/compiler/crystal/syntax/parser.cr @@ -4041,8 +4041,12 @@ module Crystal # def method(select __arg0) # @select = __arg0 # end - if !external_name && invalid_internal_name?(param_name) - param_name, external_name = temp_arg_name, param_name + # + # The external name defaults to the internal one unless otherwise + # specified (i.e. `def method(foo @select)`). + if invalid_internal_name?(param_name) + external_name ||= param_name + param_name = temp_arg_name end ivar = InstanceVar.new(@token.value.to_s).at(location) @@ -4062,8 +4066,9 @@ module Crystal end # Same case as :INSTANCE_VAR for things like @select - if !external_name && invalid_internal_name?(param_name) - param_name, external_name = temp_arg_name, param_name + if invalid_internal_name?(param_name) + external_name ||= param_name + param_name = temp_arg_name end cvar = ClassVar.new(@token.value.to_s).at(location)