natalie icon indicating copy to clipboard operation
natalie copied to clipboard

Calling with_index with a block on an enumerator results in the wrong output type

Open herwinw opened this issue 8 months ago • 1 comments

Observed in #2145 where it broke our self hosted compiler. This snippet:

p [:a].map.class
p [:a].map.with_index.class
p [:a].map.with_index { |value, idx| [value, idx] }.class
p [:a].map.with_index { |value, idx| [value, idx] }.to_a.class
p [:a].map.with_index { |value, idx| [value, idx] }
p [:a].map.with_index { |value, idx| [value, idx] }.to_a

The output of MRI (I used version 3.3.3, but it should be the same in any supported version):

Enumerator
Enumerator
Array
Array
[[:a, 0]]
[[:a, 0]]

The output of the same program ran with Natalie:

Enumerator
Enumerator
Enumerator
Array
#<Enumerator:0x564692b83c60>
[:a]

The call to Enumerator#with_index is supposed to return a new Enumerator if no block is given, but should return an array if a block is given. Additionally, the output of [:a].map.with_index { |value, idx| [value, idx] }.to_a is the original array, not the mapped version.

herwinw avatar Jun 26 '24 15:06 herwinw