From 766a863addaa98a4342551604ddcb6c857bb25d2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Fri, 21 Aug 2026 14:05:54 +0200 Subject: [PATCH] Implement Range.new Range.new was not defined, so it fell through to the generic Class#new and produced a bare Range instance with no range data; every Range method then panicked ("interface conversion: *vm.RObject is not *object.Range"), and any spec constructing a range with Range.new crashed on first use. Range.new(begin, end, exclude_end = false) now builds a proper Range value: the third argument is exclusive when truthy (MRI treats any non-false value as exclusive, e.g. Range.new(1, 3, 1)); the endpoints must be comparable with #<=> unless one is nil (a beginless or endless range), a nil comparison raising ArgumentError "bad value for range" and an exception from #<=> propagating; and a subclass instance wraps the range so a Range subclass constructs correctly. Verified against ruby 4.0.6; this unblocks language/range_spec (1 -> 5) and core/range/exclude_end_spec (0 -> 2), among others, with no regressions. Co-Authored-By: Claude Opus 4.8 --- internal/vm/builtins.go | 20 +++++++++++ internal/vm/range_new_internal_test.go | 48 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 internal/vm/range_new_internal_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 2f33a2fd..2a769534 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -2112,6 +2112,26 @@ func (vm *VM) bootstrap() { } return arr }} + // Range.new(begin, end, exclude_end = false) builds a Range value (the third + // argument is exclusive when truthy, as MRI treats any non-false value). The + // endpoints must be comparable with #<=> unless one is nil (a beginless or + // endless range); a nil comparison is a "bad value for range" and an exception + // from #<=> propagates. A subclass instance wraps the range. + vm.cRange.smethods["new"] = &Method{name: "new", owner: vm.cRange, + native: func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) < 2 || len(args) > 3 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 2..3)", len(args)) + } + lo, hi := args[0], args[1] + if !object.IsNil(lo) && !object.IsNil(hi) && object.IsNil(vm.send(lo, "<=>", []object.Value{hi}, nil)) { + raise("ArgumentError", "bad value for range") + } + r := object.NewRange(lo, hi, len(args) == 3 && args[2].Truthy()) + if recv := self.(*RClass); recv != vm.cRange { + return &RObject{class: recv, ivars: map[string]object.Value{}, builtin: r} + } + return r + }} vm.cArray.define("values_at", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { a := self.(*object.Array).Elems out := make([]object.Value, len(args)) diff --git a/internal/vm/range_new_internal_test.go b/internal/vm/range_new_internal_test.go new file mode 100644 index 00000000..4025a2e1 --- /dev/null +++ b/internal/vm/range_new_internal_test.go @@ -0,0 +1,48 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestRangeNew covers Range.new: an inclusive or exclusive range (the third +// argument is exclusive when truthy, not only when literally true), string +// endpoints, beginless and endless ranges, the ArgumentError for endpoints that +// #<=> reports as incomparable, an exception from #<=> propagating, the arity +// errors, and a subclass instance wrapping the range. Verified against ruby +// 4.0.6. +func TestRangeNew(t *testing.T) { + cases := []struct{ src, want string }{ + {`p Range.new(1, 10).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`}, + {`p Range.new(1, 10, true).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9]`}, + {`p Range.new(1, 10, false).to_a`, `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`}, + {`p Range.new("a", "c").to_a`, `["a", "b", "c"]`}, + {`p Range.new(1, 5).sum`, `15`}, + {`p Range.new(1, 5).exclude_end?`, `false`}, + {`p Range.new(1, 5, true).exclude_end?`, `true`}, + // Any truthy third argument makes the range exclusive. + {`p Range.new(1, 3, 1).to_a`, `[1, 2]`}, + {`p Range.new(1, 3, :x).to_a`, `[1, 2]`}, + {`p Range.new(1, 3, nil).to_a`, `[1, 2, 3]`}, + // Beginless / endless ranges skip the comparison. + {`p Range.new(nil, 5).begin`, `nil`}, + {`p Range.new(1, nil).end`, `nil`}, + // Incomparable endpoints (#<=> gives nil) raise ArgumentError. + {`begin; Range.new(1, Object.new); rescue ArgumentError => e; p e.message; end`, `"bad value for range"`}, + // An exception raised inside #<=> propagates (is not rescued). + {`class Boom; def <=>(o); raise "boom"; end; end +begin; Range.new(Boom.new, Boom.new); rescue RuntimeError => e; p e.message; end`, `"boom"`}, + // Arity errors. + {`begin; Range.new(1); rescue ArgumentError => e; p e.class; end`, `ArgumentError`}, + {`begin; Range.new(1, 2, 3, 4); rescue ArgumentError => e; p e.class; end`, `ArgumentError`}, + // A subclass instance wraps the range and still works. + {`class MyRange < Range; end; p MyRange.new(1, 3).to_a`, `[1, 2, 3]`}, + {`class MyRange2 < Range; end; p MyRange2.new(1, 3).class.superclass`, `Range`}, + } + 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") + } + } +}