Project

General

Profile

Bug #12073

local variable interferes with keyword arguments

Added by josh.cheek (Josh Cheek) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin13]
[ruby-core:73816]

Description

This code causes a syntax error:

def a(b:)
  b
end
a = 1
a b: 9

# !> program.rb:5: syntax error, unexpected ':', expecting end-of-input
# !> a b: (9)
# !>     ^

However, if we remove the local assignment it works

def a(b:)
  b
end
a b: 9 # => 9

Or if we use parentheses it works

def a(b:)
  b
end
a = 1
a(b: 9) # => 9

Or if we use non-keyword arguments it works

def a(b)
  b
end
a = 1
a 9 # => 9

Associated revisions

Revision b118f5d8
Added by nagachika (Tomoyuki Chikanaga) over 3 years ago

merge revision(s) 53834,53835,53836: [Backport #12073]

    parse.y: simplify local ID condition

    * parse.y (tokenize_ident, parse_ident): ident in tokenize_ident()

can be a local id only when called from parse_ident(), but never
from parse_gvar() and parse_atmark().
* parse.y (parse_ident): allow keyword arguments just after a
method where the same name local variable is defined.
[ruby-core:73816] [Bug#12073]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54618 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 54618
Added by nagachika (Tomoyuki Chikanaga) over 3 years ago

merge revision(s) 53834,53835,53836: [Backport #12073]

parse.y: simplify local ID condition

* parse.y (tokenize_ident, parse_ident): ident in tokenize_ident()

can be a local id only when called from parse_ident(), but never
from parse_gvar() and parse_atmark().
* parse.y (parse_ident): allow keyword arguments just after a
method where the same name local variable is defined.
[ruby-core:73816] [Bug#12073]

Revision 3df0e54b
Added by usa (Usaku NAKAMURA) over 3 years ago

merge revision(s) 53834,53835,53836: [Backport #12073]

    parse.y: simplify local ID condition

    * parse.y (tokenize_ident, parse_ident): ident in tokenize_ident()

can be a local id only when called from parse_ident(), but never
from parse_gvar() and parse_atmark().
* parse.y (parse_ident): allow keyword arguments just after a
method where the same name local variable is defined.
[ruby-core:73816] [Bug#12073]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@54676 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 54676
Added by usa (Usaku NAKAMURA) over 3 years ago

merge revision(s) 53834,53835,53836: [Backport #12073]

parse.y: simplify local ID condition

* parse.y (tokenize_ident, parse_ident): ident in tokenize_ident()

can be a local id only when called from parse_ident(), but never
from parse_gvar() and parse_atmark().
* parse.y (parse_ident): allow keyword arguments just after a
method where the same name local variable is defined.
[ruby-core:73816] [Bug#12073]

History

Updated by seanlinsley (Sean Linsley) over 3 years ago

This is also an issue with array destructuring:

def a(*b)
  b
end

a = [1,2,3]

a a
 => [[1, 2, 3]] 

a *a
TypeError: no implicit conversion of Array into Integer

a(*a)
 => [1, 2, 3]

This is on Ruby 2.2.3.

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

  • Description updated (diff)
  • Status changed from Open to Closed
  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED

Sean Linsley wrote:

This is also an issue with array destructuring:

You are warned.

$ ruby -w -e 'def a(*b); b; end' -e 'a=[1,2,3]' -e 'a *a'
-e:3: warning: `*' after local variable or literal is interpreted as binary operator
-e:3: warning: even though it seems like argument prefix
-e:3: warning: possibly useless use of * in void context
-e:3:in `*': no implicit conversion of Array into Integer (TypeError)
    from -e:3:in `<main>'

Updated by nagachika (Tomoyuki Chikanaga) over 3 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE

ruby_2_3 r54618 merged revision(s) 53834,53835,53836.

Updated by usa (Usaku NAKAMURA) over 3 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE to 2.1: REQUIRED, 2.2: DONE, 2.3: DONE

ruby_2_2 r54676 merged revision(s) 53834,53835,53836.

Also available in: Atom PDF