diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index c454bcbc..780d34a9 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -5078,13 +5078,9 @@ func (vm *VM) bootstrap() { vm.cInteger.define("step", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { limit, step := vm.stepBounds(args) if blk == nil { - // With a size, so that Enumerator#size can answer without walking - // the sequence. Asked to count 1.step(Float::INFINITY) it never - // would: it allocated 30.9 GB before a CI runner died under it. - return enumForSized(self, "step", func(*VM) object.Value { - return stepSize(self, limit, step, false) - }, args...) + return vm.stepEnum(self, self, limit, step, false, args...) } + vm.numericStepArgCheck(step) if object.IsNil(limit) { vm.numericStepEndless(blk, self, step) } else { @@ -8764,13 +8760,63 @@ func isZeroNumeric(v object.Value) bool { // rejectStringStep raises ArgumentError for a String step, which MRI refuses to // coerce even when it looks numeric ("2" / "0.1" / "1/3"); other types fall -// through to the normal numeric handling. +// through to the normal numeric handling. Used by the shared Range#step / #step +// walkers, whose String message is inherited by Range (Numeric#step raises its +// own, MRI-matching message earlier — see numericStepArgCheck). func rejectStringStep(step object.Value) { if _, ok := step.(*object.String); ok { raise("ArgumentError", "step can't be String") } } +// rejectUncomparableStep raises the ArgumentError MRI's Numeric#step (Integer#step +// / Float#step, but NOT Range#step) gives for a non-numeric, non-nil step that its +// internal `step <=> 0` comparison cannot handle — its message quotes the class +// ("comparison of String with 0 failed", likewise for a Symbol), even when the +// value looks numeric ("2"). A numeric or nil step passes through. +func (vm *VM) rejectUncomparableStep(step object.Value) { + if !object.IsNil(step) && !isNumericValue(step) { + raise("ArgumentError", "comparison of %s with 0 failed", vm.classOf(step).name) + } +} + +// numericStepArgCheck validates a block-driven Numeric#step: rejectUncomparableStep +// plus MRI's rule that a nil step is a TypeError ("step must be numeric") rather +// than the silent step-of-1 the no-block (#size) path tolerates. +func (vm *VM) numericStepArgCheck(step object.Value) { + if object.IsNil(step) { + raise("TypeError", "step must be numeric") + } + vm.rejectUncomparableStep(step) +} + +// arithSeq builds an Enumerator::ArithmeticSequence for recv.step(args) with the +// given defining triple and on-demand #size. It reuses the ordinary step-driven, +// size-bearing Enumerator, tagging it so #class and the sequence accessors change. +func (vm *VM) arithSeq(recv, begin, end, step object.Value, excl bool, size func(*VM) object.Value, args ...object.Value) *Enumerator { + e := enumForSized(recv, "step", size, args...) + e.isArithSeq = true + e.asBegin, e.asEnd, e.asStep, e.asExcl = begin, end, step, excl + return e +} + +// stepEnum is the no-block result of Integer#step / Float#step: an +// Enumerator::ArithmeticSequence for a numeric step (MRI 2.6+), or a plain +// size-bearing Enumerator for a non-numeric one — a String step yields an +// Enumerator whose #size raises only when asked, matching MRI. Both drive +// recv.step(args); a size is attached either way so #size answers without walking +// an unbounded sequence. +func (vm *VM) stepEnum(recv, begin, limit, step object.Value, excl bool, args ...object.Value) *Enumerator { + size := func(vm *VM) object.Value { + vm.rejectUncomparableStep(step) // a String step's #size raises, MRI-style + return stepSize(begin, limit, step, excl) + } + if isNumericValue(step) { + return vm.arithSeq(recv, begin, limit, step, excl, size, args...) + } + return enumForSized(recv, "step", size, args...) +} + // numericStep drives Range#step / Integer#step: it yields lo, lo+step, … toward // hi (inclusive unless exclusive). All-integer operands keep an integer walk // (exact); any float operand switches to an index-based float walk that avoids diff --git a/internal/vm/enumerator.go b/internal/vm/enumerator.go index fcc27e15..0ff562f3 100644 --- a/internal/vm/enumerator.go +++ b/internal/vm/enumerator.go @@ -41,6 +41,16 @@ type Enumerator struct { // isProduct marks an Enumerator::Product (built by Enumerator.product): a plain // generator Enumerator whose only distinction is the class it reports. isProduct bool + + // isArithSeq marks an Enumerator::ArithmeticSequence (built by Numeric#step + // without a block): a step-driven Enumerator that additionally answers + // #begin/#end/#step/#exclude_end?/#last from the defining triple below. asEnd is + // nil for an endless sequence (m.step with no limit). + isArithSeq bool + asBegin object.Value + asEnd object.Value + asStep object.Value + asExcl bool // isChain marks an Enumerator::Chain: #each iterates chainParts in turn, and // entered[i] records whether part i has been iterated (so #rewind can rewind, // in reverse, exactly the parts that were entered). @@ -151,6 +161,49 @@ func (vm *VM) registerEnumerator() { return vm.enumProduct(args, blk) }} + // Enumerator::ArithmeticSequence — the subclass Numeric#step (and #step on an + // Integer/Float) returns when called without a block. It is a step-driven + // Enumerator (its #each/#to_a/#first/#size all reuse the inherited step + // machinery) that additionally exposes the defining triple. + vm.cArithSeq = newClass("Enumerator::ArithmeticSequence", vm.cEnumerator) + vm.cArithSeq.consts = vm.cEnumerator.consts + vm.cEnumerator.consts["ArithmeticSequence"] = vm.cArithSeq + vm.cArithSeq.define("begin", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self.(*Enumerator).asBegin + }) + vm.cArithSeq.define("end", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self.(*Enumerator).asEnd + }) + vm.cArithSeq.define("step", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return self.(*Enumerator).asStep + }) + vm.cArithSeq.define("exclude_end?", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return object.Bool(self.(*Enumerator).asExcl) + }) + // #last materialises the (necessarily bounded) sequence; MRI refuses it for an + // endless one rather than looping for ever. + vm.cArithSeq.define("last", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + if object.IsNil(e.asEnd) { + raise("RangeError", "cannot get the last element of endless arithmetic sequence") + } + elems := vm.enumMaterialize(e) + if len(args) == 0 { + if len(elems) == 0 { + return object.NilV + } + return elems[len(elems)-1] + } + n := int(intArg(args[0])) + if n < 0 { + raise("ArgumentError", "negative array size") + } + if n > len(elems) { + n = len(elems) + } + return object.NewArrayFromSlice(append([]object.Value{}, elems[len(elems)-n:]...)) + }) + // Enumerator::Yielder — `y << v` / `y.yield(v)` feed the generator's values in. vm.cYielder = newClass("Enumerator::Yielder", vm.cObject) vm.cYielder.consts = vm.cEnumerator.consts // (scope is cosmetic; share the map) diff --git a/internal/vm/numeric_edges.go b/internal/vm/numeric_edges.go index a72b6fab..86bcfa5c 100644 --- a/internal/vm/numeric_edges.go +++ b/internal/vm/numeric_edges.go @@ -526,10 +526,9 @@ func (vm *VM) registerNumericEdges() { vm.cFloat.define("step", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { limit, step := vm.stepBounds(args) if blk == nil { - return enumForSized(self, "step", func(*VM) object.Value { - return stepSize(self, limit, step, false) - }, args...) + return vm.stepEnum(self, self, limit, step, false, args...) } + vm.numericStepArgCheck(step) if object.IsNil(limit) { vm.numericStepEndless(blk, self, step) } else { diff --git a/internal/vm/numeric_step_conformance_test.go b/internal/vm/numeric_step_conformance_test.go new file mode 100644 index 00000000..a4a75208 --- /dev/null +++ b/internal/vm/numeric_step_conformance_test.go @@ -0,0 +1,139 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// The expected strings below were each verified byte-for-byte against MRI Ruby +// 4.0.x (`ruby -e ...`), the reference this VM targets for Numeric#step / +// Integer#step / Float#step and the Enumerator::ArithmeticSequence they return +// without a block. See core/numeric/step_spec.rb in the ruby/spec corpus. + +// evalStep runs src and returns trimmed stdout, using the shared eval helper. +func evalStep(t *testing.T, src string) string { + t.Helper() + return strings.TrimRight(eval(t, src), "\n") +} + +func TestNumericStepArithmeticSequence(t *testing.T) { + cases := []struct{ name, src, want string }{ + // The no-block form is an Enumerator::ArithmeticSequence (MRI 2.6+). + {"class_int", `p 1.step(10, 2).class`, "Enumerator::ArithmeticSequence"}, + {"class_default_step", `p 1.step(10).class`, "Enumerator::ArithmeticSequence"}, + {"class_endless", `p 1.step.class`, "Enumerator::ArithmeticSequence"}, + {"class_keyword", `p 1.step(to: 10, by: 2).class`, "Enumerator::ArithmeticSequence"}, + {"class_float", `p 1.0.step(2.0, 0.5).class`, "Enumerator::ArithmeticSequence"}, + {"class_nil_limit", `p 1.step(nil, 2).class`, "Enumerator::ArithmeticSequence"}, + + // The defining triple is exposed through the sequence accessors. + {"begin", `p 1.step(10, 2).begin`, "1"}, + {"end", `p 1.step(10, 2).end`, "10"}, + {"end_endless", `p 1.step.end`, "nil"}, + {"step", `p 1.step(10, 2).step`, "2"}, + {"step_default", `p 1.step(10).step`, "1"}, + {"exclude_end", `p 1.step(10, 2).exclude_end?`, "false"}, + + // #each / #to_a / #size / #first reuse the step machinery. + {"to_a", `p 1.step(10, 2).to_a`, "[1, 3, 5, 7, 9]"}, + {"to_a_negative", `p 5.step(1, -2).to_a`, "[5, 3, 1]"}, + {"to_a_keyword", `p 1.step(to: 10, by: 2).to_a`, "[1, 3, 5, 7, 9]"}, + {"to_a_float", `p 1.0.step(3.0, 0.5).to_a`, "[1.0, 1.5, 2.0, 2.5, 3.0]"}, + {"size", `p 1.step(10, 2).size`, "5"}, + {"size_endless", `p 1.step.size`, "Infinity"}, + {"size_infinity_step", `p 1.step(10, Float::INFINITY).size`, "1"}, + {"first_no_arg", `p 1.step(10, 2).first`, "1"}, + {"first_n", `p 1.step(10, 2).first(3)`, "[1, 3, 5]"}, + {"endless_first", `p 1.step(nil, 2).first(3)`, "[1, 3, 5]"}, + + // #last materialises the bounded sequence. + {"last_no_arg", `p 1.step(10, 2).last`, "9"}, + {"last_n", `p 1.step(10, 2).last(2)`, "[7, 9]"}, + {"last_n_clamp", `p 1.step(10, 2).last(99)`, "[1, 3, 5, 7, 9]"}, + + // An empty sequence (begin already past end) is still an ArithmeticSequence. + {"empty_class", `p 1.step(0, 2).class`, "Enumerator::ArithmeticSequence"}, + {"empty_to_a", `p 1.step(0, 2).to_a`, "[]"}, + {"empty_first", `p 1.step(0, 2).first`, "nil"}, + {"empty_last", `p 1.step(0, 2).last`, "nil"}, + + // A String step is NOT an ArithmeticSequence: MRI returns a plain Enumerator + // whose #size raises only when asked. + {"string_class", `p 1.step(5, "foo").class`, "Enumerator"}, + {"string_class_float", `p 1.1.step(5.1, "foo").class`, "Enumerator"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := evalStep(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + }) + } +} + +// TestNumericStepArgErrors covers the argument-validation messages, each verified +// against MRI: a String step is an ArgumentError quoting the class, a nil step is +// a TypeError, and both the block form and the #size of the no-block form raise. +func TestNumericStepArgErrors(t *testing.T) { + rescueClassMsg := func(expr string) string { + return "begin; " + expr + "; puts 'NORAISE'; rescue Exception => e; puts e.class; puts e.message; end" + } + cases := []struct{ name, expr, want string }{ + {"int_string_numeric", `1.step(5, "1") {}`, "ArgumentError\ncomparison of String with 0 failed"}, + {"int_string_decimal", `1.step(5, "0.1") {}`, "ArgumentError\ncomparison of String with 0 failed"}, + {"int_string_ratio", `1.step(5, "1/3") {}`, "ArgumentError\ncomparison of String with 0 failed"}, + {"int_string_alpha", `1.step(5, "foo") {}`, "ArgumentError\ncomparison of String with 0 failed"}, + {"float_string", `1.1.step(5.1, "foo") {}`, "ArgumentError\ncomparison of String with 0 failed"}, + {"string_size", `1.step(5, "1").size`, "ArgumentError\ncomparison of String with 0 failed"}, + {"float_string_size", `1.1.step(5.1, "foo").size`, "ArgumentError\ncomparison of String with 0 failed"}, + {"nil_step_block", `1.step(5, nil) {}`, "TypeError\nstep must be numeric"}, + {"float_nil_step_block", `1.0.step(5.0, nil) {}`, "TypeError\nstep must be numeric"}, + {"zero_step", `1.step(5, 0) {}`, "ArgumentError\nstep can't be 0"}, + {"symbol_step_block", `1.step(5, :x) {}`, "ArgumentError\ncomparison of Symbol with 0 failed"}, + // The block-form last(n) rejects a negative count as MRI does. + {"last_negative", `1.step(10, 2).last(-1)`, "ArgumentError\nnegative array size"}, + // #last on an endless sequence refuses rather than looping for ever. + {"last_endless", `1.step.last`, "RangeError\ncannot get the last element of endless arithmetic sequence"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := evalStep(t, rescueClassMsg(tc.expr)) + if got != tc.want { + t.Errorf("expr=%q\n got=%q\nwant=%q", tc.expr, got, tc.want) + } + }) + } +} + +// TestRangeStepStringUnchanged pins the shared walkers' behaviour for a String +// step on a numeric Range, which Numeric#step must not disturb: MRI's Range#step +// keeps its own (inherited) message, so this VM keeps its long-standing one. +func TestRangeStepStringUnchanged(t *testing.T) { + src := "begin; (1..5).step(\"2\") {}; puts 'NORAISE'; rescue Exception => e; puts e.class; end" + if got := evalStep(t, src); got != "ArgumentError" { + t.Errorf("got=%q want ArgumentError", got) + } + // A numeric Range#step (no String) still drives rejectStringStep's pass branch. + if got := evalStep(t, `p (1..6).step(2).to_a`); got != "[1, 3, 5]" { + t.Errorf("got=%q want [1, 3, 5]", got) + } +} + +// TestNumericStepInfinity covers the infinite-step edge directly (no lambda +// forwarding): an infinite step yields the start value exactly once when it lies +// on the correct side of the limit, including the both-Infinity case. +func TestNumericStepInfinity(t *testing.T) { + cases := []struct{ name, src, want string }{ + {"pos_both_inf", `r=[]; Float::INFINITY.step(Float::INFINITY, Float::INFINITY){|x| r<