Project

General

Profile

Actions

Bug #21256

closed

`it` value is wrapped in an array when that doesn't happen with numbered parameter

Bug #21256: `it` value is wrapped in an array when that doesn't happen with numbered parameter
1

Added by Earlopain (Earlopain _) about 1 year ago. Updated 8 months ago.

Status:
Closed
Assignee:
Target version:
-
ruby -v:
ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-linux]
[ruby-core:121592]

Description

I have the following script:

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "rspec-core", "3.13.3"
  gem "rspec-expectations", "3.13.3"
end

require "rspec/autorun"
require "rspec/expectations"

RSpec::Matchers.define :example do
  chain(:foo) { pp _1 }
  chain(:bar) { pp it }
end

RSpec.describe "demo" do
  it do
    example.foo(1)
    example.bar(2)
  end
end

It calls two methods were the only difference is that one accesses the argument with _1, the other with it. It prints the following:

1
[2]

So, it for some reason wraps it in an array. I appologise for the reproduction script, it uses rspec and the amount of metaprogramming it uses makes it impossible for me to reduce this further.

When running with parser=parse.y, the expected output is returned (just 2, no array). I also tested against current ruby-dev and it also happens there.

Updated by mame (Yusuke Endoh) about 1 year ago 2Actions #1 [ruby-core:121594]

  • Status changed from Open to Assigned
  • Assignee set to prism

Here is a repro without rspec.

class C
  define_method(:foo) do
    p it
  end
end

class D < C
  def foo(*args)
    super(*args)
  end
end

D.new.foo(1)
$ ./local/bin/ruby --parser=parse.y t.rb
1

$ ./local/bin/ruby --parser=prism t.rb
[1]

Updated by Earlopain (Earlopain _) about 1 year ago Actions #2 [ruby-core:121643]

Thanks for that info, it allowed me to make a potential fix: https://github.com/ruby/ruby/pull/13108

Updated by Earlopain (Earlopain _) 10 months ago Actions #3

  • Status changed from Assigned to Closed

Applied in changeset git|265059603c3aa6a13f90096c71b32046a17938f3.


[Bug #21256] Fix it parameter when splatting and define_method is used

It was failing to set the leads, like numblocks do, causing the result to be wrapped in an array

Updated by mame (Yusuke Endoh) 10 months ago Actions #4 [ruby-core:122811]

  • Status changed from Closed to Open

That fix caused an ASAN error. I will revert it for now. See https://github.com/ruby/ruby/pull/13947

Updated by mame (Yusuke Endoh) 10 months ago Actions #5

  • Status changed from Open to Closed

Applied in changeset git|dc8ae249517e017e52a4b34a14ddd594a706a9d5.


Revert "[Bug #21256] Fix it parameter when splatting and define_method is…"

This reverts commit 265059603c3aa6a13f90096c71b32046a17938f3.

Updated by mame (Yusuke Endoh) 10 months ago Actions #6

  • Status changed from Closed to Open

Updated by Earlopain (Earlopain _) 10 months ago 1Actions #7 [ruby-core:122813]

Sorry about that. It looks like removing the first handling of PM_IT_PARAMETERS_NODE you wrote about in https://github.com/ruby/ruby/pull/13947 runs into https://bugs.ruby-lang.org/issues/20965 / https://github.com/ruby/ruby/commit/667a0f9f928be843a0810f2c61b633be1f8cd46a

For example the change

  1) Failure:
TestProc#test_parameters_lambda [/home/mame/work/ruby/test/ruby/test_proc.rb:1445]:
<[[:req]]> expected but was
<[[:req, :it]]>.

is consistent with how numbered parameters are handled:

proc { _1 }.parameters(lambda: true)
=> [[:req, :_1]]

But yeah, looks like it is not so trivial. I will leave the fix up to someone else more experienced.

Updated by kddnewton (Kevin Newton) 8 months ago Actions #8

  • Status changed from Open to Closed

Applied in changeset git|4f4b4e3b37c884093a165801b5028c14a7de1604.


Fill in lead num for blocks with it

Fixes [Bug #21256]

Co-Authored-By: Earlopain

Actions

Also available in: PDF Atom