Project

General

Profile

Bug #14611

Exotic method parameters bug

Added by bogdan (Bogdan Gusiev) about 1 month ago. Updated about 1 month ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:86159]

Description

Consider the following code:

def test(_, x, *_)
  x
end

test(1,2,3) # => nil

By some reason second parameter is nil when the rest of parameters declared as not used.

Associated revisions

Revision 62832
Added by nobu (Nobuyoshi Nakada) about 1 month ago

parse.y: fix duplicate kwrest

  • parse.y (f_kwrest): add the variable name as an argument, as
    well as an internal variable.

  • parse.y (new_args_tail): now keyword rest argument variable is
    always placed between keyword arguments and block argument, so
    so just reorder required and optional keyword arguments. no
    longer kwrest duplicates.

Revision 9720136d
Added by nobu (Nobuyoshi Nakada) about 1 month ago

compile.c: arg var index

  • compile.c (iseq_set_arguments): determine argument variable indexes by the order, not by just IDs. arguments begin with _ can be duplicate, so by-ID index may result in a wrong value. [Bug #14611]

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

Revision 62833
Added by nobu (Nobuyoshi Nakada) about 1 month ago

compile.c: arg var index

  • compile.c (iseq_set_arguments): determine argument variable indexes by the order, not by just IDs. arguments begin with _ can be duplicate, so by-ID index may result in a wrong value. [Bug #14611]

History

#1 [ruby-core:86160] Updated by phluid61 (Matthew Kerwin) about 1 month ago

bogdan (Bogdan Gusiev) wrote:

By some reason second parameter is nil when the rest of parameters declared as not used.

Underscore as "not used" here is a convention of Rubocop; your code is no different from:

def test(a, x, *a)
  x
end

I would say the bug is that it didn't reject the def in the first place, because of the repeated parameter name.

#2 [ruby-core:86161] Updated by Hanmac (Hans Mackowiak) about 1 month ago

phluid61 (Matthew Kerwin) not so fast, ruby allows duplicate "_" as parameters so you can say that you don't want them and ruby should just ignore them
so you don't get a SyntaxError about duplicated argument name like your change did

That has nothing to do with rubocop

#3 [ruby-core:86165] Updated by phluid61 (Matthew Kerwin) about 1 month ago

Hanmac (Hans Mackowiak) wrote:

phluid61 (Matthew Kerwin) not so fast, ruby allows duplicate "_" as parameters so you can say that you don't want them and ruby should just ignore them
so you don't get a SyntaxError about duplicated argument name like your change did

That has nothing to do with rubocop

... really? When did that happen? I had a look in syntax.rdoc and other places but didn't see any mention of it.

(edit: turns out this is something I completely missed in the 1.9.3 era)

#4 [ruby-core:86166] Updated by shevegen (Robert A. Heiler) about 1 month ago

I did not know that either.

I guess the problem was that bogdan was using variables with
a leading '_'.

With this:

def x(a,a,b)
  puts 'HI'
end

I get in IRB:

SyntaxError ((irb):1: duplicated argument name

With that, however had:

def x(_a,_a,_b)
  puts 'hi'
end
x(1,2,3)
# => hi

I get no error and the method call works.

I have no idea if this is a bug or a feature but to me,
just from looking at the above, I would have assumed that
_a is equal to _a just as a was (considered) equal to
a above, in "def x(a,a,b)".

Perhaps it is because leading _ underscores are always
assumed to be special. But ideally this should be documented
somewhere nonetheless.

#5 Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Backport changed from 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN to 2.3: REQUIRED, 2.4: REQUIRED, 2.5: REQUIRED

#6 Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Status changed from Open to Closed

Applied in changeset trunk|r62833.


compile.c: arg var index

  • compile.c (iseq_set_arguments): determine argument variable indexes by the order, not by just IDs. arguments begin with _ can be duplicate, so by-ID index may result in a wrong value. [Bug #14611]

Also available in: Atom PDF