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") + } + } +}