Project

General

Profile

Actions

Bug #14611

closed

Exotic method parameters bug

Added by bogdan (Bogdan Gusiev) about 6 years ago. Updated about 6 years ago.

Status:
Closed
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.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #15457: Weird behaviour when having multiple arguments of same name and splat operatorClosedActions

Updated by phluid61 (Matthew Kerwin) about 6 years 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.

Updated by Hanmac (Hans Mackowiak) about 6 years 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

Updated by phluid61 (Matthew Kerwin) about 6 years 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)

Updated by shevegen (Robert A. Heiler) about 6 years 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.

Actions #5

Updated by nobu (Nobuyoshi Nakada) about 6 years ago

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

Updated by nobu (Nobuyoshi Nakada) about 6 years 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.
    [ruby-core:86159] [Bug #14611]
Actions #7

Updated by mame (Yusuke Endoh) over 5 years ago

  • Related to Bug #15457: Weird behaviour when having multiple arguments of same name and splat operator added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0