From 55f0d1c9ea1266914e8f5c98372e0818b2391995 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 25 Aug 2026 17:03:41 +0200 Subject: [PATCH] Range#each: iterate an endless range lazily instead of materialising it Range#each built the whole element list with rangeElems, which raised "can't iterate from " for an endless range (1.., "a"..) because such a range has no finite element list. It now iterates an endless range lazily, the way Range#step already does: a String begin walks by #succ, an Integer or Bignum begin counts up with exact big.Int arithmetic (so an endless Bignum range does not degrade to Float and stall), and the block breaks out of the otherwise infinite walk. A begin with no successor (e.g. a Float) still raises a TypeError. Because every Enumerable method routes through #each, this also makes each_with_index, select, map, lazy, first(n), etc. work on an endless range. Bounded ranges keep their existing materialising path. Verified against ruby 4.0.6. Full package coverage stays at 100.0%. Zero- regression rubyspec sweep; core/range/each_spec 5 -> 7. (It also unblocks the endless-source example of Enumerator.product once that lands.) Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 18 ++++++++ .../vm/range_each_endless_internal_test.go | 41 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 internal/vm/range_each_endless_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 58c5e6c4..e1e3704f 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -4363,6 +4363,24 @@ func (vm *VM) bootstrap() { return enumFor(self, "each") } r := self.(*object.Range) + // An endless range cannot be materialised: iterate it lazily (the block + // breaks out). A String walks by #succ and an Integer/Bignum counts up; + // any other begin (e.g. a Float) has no successor and can't be iterated. + if object.IsNil(r.Hi) { + if loS, ok := r.Lo.(*object.String); ok { + for cur := loS.Str(); ; { + vm.callBlock(blk, []object.Value{object.NewString(cur)}) + cur = succString(cur) + } + } + switch r.Lo.(type) { + case object.Integer, *object.Bignum: + for n := new(big.Int).Set(bigVal(r.Lo)); ; n = new(big.Int).Add(n, big.NewInt(1)) { + vm.callBlock(blk, []object.Value{object.NormInt(n)}) + } + } + raise("TypeError", "can't iterate from %s", r.Lo.Inspect()) + } for _, e := range rangeElems(r) { vm.callBlock(blk, []object.Value{e}) } diff --git a/internal/vm/range_each_endless_internal_test.go b/internal/vm/range_each_endless_internal_test.go new file mode 100644 index 00000000..a479505a --- /dev/null +++ b/internal/vm/range_each_endless_internal_test.go @@ -0,0 +1,41 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestRangeEachEndless covers Range#each over an endless range, which cannot be +// materialised and so is iterated lazily: an Integer or Bignum begin counts up +// and a String begin walks by #succ, in both cases until the block breaks; a +// begin with no successor (e.g. a Float) raises a TypeError. Enumerable methods +// layered on #each (each_with_index, select, …) inherit the behaviour. Bounded +// ranges are unaffected. Verified against ruby 4.0.6. +func TestRangeEachEndless(t *testing.T) { + cases := []struct{ src, want string }{ + // Endless Integer range counts up until the block breaks. + {`a = []; (1..).each { |x| break if x > 4; a << x }; p a`, `[1, 2, 3, 4]`}, + {`a = []; (10..).each { |x| break if x > 12; a << x }; p a`, `[10, 11, 12]`}, + // Endless String range walks by #succ. + {`a = []; ("a"..).each { |x| break if x > "c"; a << x }; p a`, `["a", "b", "c"]`}, + // A Bignum begin counts up too. + {`a = []; (2**64..).each { |x| break if x > 2**64 + 2; a << x }; p a`, `[18446744073709551616, 18446744073709551617, 18446744073709551618]`}, + // Enumerable methods built on #each work on an endless range. + {`a = []; (1..).each_with_index { |x, i| break if i > 2; a << x }; p a`, `[1, 2, 3]`}, + {`p (1..).lazy.select(&:even?).first(3)`, `[2, 4, 6]`}, + {`p (1..).first(4)`, `[1, 2, 3, 4]`}, + // A begin with no successor cannot be iterated. + {`begin; (1.0..).each { |x| break }; rescue TypeError => e; p e.class; end`, `TypeError`}, + // Bounded ranges are unchanged and #each returns the range. + {`a = []; (1..3).each { |x| a << x }; p a`, `[1, 2, 3]`}, + {`p (1..3).each { |x| }.class`, `Range`}, + // No block yields an Enumerator. + {`p (1..).each.class`, `Enumerator`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}