Project

General

Profile

Bug #13648

[PATCH] Nested map of Enumerator::Lazy with packed values gives wrong result

Added by akihikodaki (Akihiko Odaki) almost 2 years ago. Updated almost 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0dev (2017-06-09 trunk 59052) [x86_64-linux]
[ruby-core:81638]

Description

This test case ends up with the following result.

class Step
  include Enumerable
  attr_reader :current, :args

  def initialize(enum)
    @enum = enum
    @current = nil
    @args = nil
  end

  def each(*args)
    @args = args
    @enum.each do |v|
      @current = v
      if v.is_a? Enumerable
        yield *v
      else
        yield v
      end
    end
  end
end

a = Step.new([[1, 2]])
assert_equal([[[1, 2]]], a.lazy.map {|*args| args}.map {|*args| args}.to_a)
<[[[1, 2]]]> expected but was
<[[1, 2]]>.

Here, [[[1, 2]]] is expected because:

  • An array should be created with the first map, which results in [1, 2].
  • The array should be wrapped in another array with the second map, which results in [[1, 2]].
  • The array should be wrapped in another array with to_a, which results in [[[1, 2]]].

However, it returns [[1, 2]] because:

  • An array will be created with the first map, which results in [1, 2].
  • However, the array will be internally considered as "packed" and the unpacked arguments will be passed to the second map.
  • The second map wraps them into another array, which results in [1, 2].
  • The array will be wrapped in another array with to_a, which results in [[1, 2]].

I have attached the test case and a fix. The fix marks values returned by blocks are not packed.


Files

fix.patch (2.18 KB) fix.patch A test case and fix akihikodaki (Akihiko Odaki), 06/09/2017 04:09 PM

Related issues

Has duplicate Ruby trunk - Bug #13699: Multiple maps over lazy enumerator yielding multiple values in 2.4.x causes crashClosedActions

Associated revisions

Revision 59056
Added by nobu (Nobuyoshi Nakada) almost 2 years ago

enumerator.c: fix nested maps

  • enumerator.c (lazy_map_proc, lazy_grep_iter_proc): marks values returned by blocks are not packed in the case of nested maps, so that the result will be same as non-lazy version. based on the patch by akihikodaki (Akihiko Odaki) at [ruby-core:81638], without GCC extension. [Bug#13648]

Revision f03997d3
Added by nagachika (Tomoyuki Chikanaga) almost 2 years ago

merge revision(s) 59056: [Backport #13648] [Backport #13699]

enumerator.c: fix nested maps

* enumerator.c (lazy_map_proc, lazy_grep_iter_proc): marks values
  returned by blocks are not packed in the case of nested maps, so
  that the result will be same as non-lazy version.  based on the
  patch by akihikodaki (Akihiko Odaki) at [ruby-core:81638],
  without GCC extension.  [Bug#13648]

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

Revision 59363
Added by nagachika (Tomoyuki Chikanaga) almost 2 years ago

merge revision(s) 59056: [Backport #13648] [Backport #13699]

enumerator.c: fix nested maps

* enumerator.c (lazy_map_proc, lazy_grep_iter_proc): marks values
  returned by blocks are not packed in the case of nested maps, so
  that the result will be same as non-lazy version.  based on the
  patch by akihikodaki (Akihiko Odaki) at [ruby-core:81638],
  without GCC extension.  [Bug#13648]

History

#1

Updated by akihikodaki (Akihiko Odaki) almost 2 years ago

  • ruby -v changed from ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux] to ruby 2.5.0dev (2017-06-09 trunk 59052) [x86_64-linux]

Updated by nobu (Nobuyoshi Nakada) almost 2 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED
  • Description updated (diff)

Thank you, I commit your patch without GCC extension.

#3

Updated by nobu (Nobuyoshi Nakada) almost 2 years ago

  • Status changed from Open to Closed
#4

Updated by nobu (Nobuyoshi Nakada) almost 2 years ago

  • Has duplicate Bug #13699: Multiple maps over lazy enumerator yielding multiple values in 2.4.x causes crash added

Updated by nagachika (Tomoyuki Chikanaga) almost 2 years ago

  • Backport changed from 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE

ruby_2_4 r59363 merged revision(s) 59056.

Also available in: Atom PDF